summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
blob: a249f61c841fe1dfcd0cda61f5ff00d9048af0db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "sessionManager.hpp"
#include "session.hpp"
#include "server.hpp"
#include <print>

CSessionManager::CSessionManager() = default;

CSessionManager::~CSessionManager() {
  for (auto &[thread, session] : m_vSessions) {
    thread.join();
  }
}

void CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) {
  std::shared_ptr session = std::make_shared<CSession>(std::move(sockfd));
  m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session));
}

void CSessionManager::enterLoop() {
  while (true) {
    newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket().take(), nullptr, nullptr) /* awaits new client/session */});
  }
}

void CSessionManager::broadcast(const std::string &msg) const {
  for (const auto &[thread, session] : m_vSessions) {
    session->write(msg);
  }
}