diff options
Diffstat (limited to 'src/sessionManager.cpp')
| -rw-r--r-- | src/sessionManager.cpp | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index d960aeb..290b8c6 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -1,7 +1,9 @@ #include "sessionManager.hpp" #include "server.hpp" #include "session.hpp" -#include <print> +#include <algorithm> +#include <unistd.h> +#include <utility> CSessionManager::CSessionManager() = default; @@ -10,17 +12,29 @@ CSessionManager::~CSessionManager() { 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)); +std::pair<std::jthread, std::shared_ptr<CSession>> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) { + std::shared_ptr session = std::make_shared<CSession>(std::move(sockfd)); + const auto instance = &m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session)); + instance->second->setSelf(instance); + return instance; } void CSessionManager::enterLoop() { while (true) - newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket().take(), nullptr, nullptr) /* awaits new client/session */}); + newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket()->get(), nullptr, nullptr) /* awaits new client/session */}); } -void CSessionManager::broadcast(const std::string &msg) const { +void CSessionManager::broadcast(const std::string &msg, eFormatType type) const { for (const auto &[thread, session] : m_vSessions) - session->write(msg); + session->write(msg, type); +} + +void CSessionManager::removeSession(std::pair<std::jthread, std::shared_ptr<CSession>> *session) { + auto it = std::ranges::find_if(m_vSessions, [session](const auto &s) { return s.second.get() == session->second.get(); }); + + if (it != m_vSessions.end()) { + if (it->first.joinable()) + it->first.detach(); // .detach the thread since it's removing itself + m_vSessions.erase(it); + } } |
