From 131dc244e67e3b6e9304e7076c397c761860044c Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 10 Mar 2025 18:36:00 +0800 Subject: fix(registerName): no longer allow same name the previous behaviour was it wouldn't broadcast the message to the same name debug: more logging, maybe we should disable TRACE by default in the future? WIP: partial impl of signal handling(SIGINT(keyboardinterrupt) etc) --- src/sessionManager.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/sessionManager.cpp') diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index 84c3709..f4573e1 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -4,24 +4,30 @@ #include #include #include +#include "log.hpp" -CSessionManager::CSessionManager() = default; +CSessionManager::CSessionManager() { + log(LOG, "SessionManager: initialized"); +}; CSessionManager::~CSessionManager() { for (auto &[thread, session] : m_vSessions) - thread.join(); + if (thread.joinable()) + thread.join(); + + log(SYS, "SessionManager: bye"); } std::pair> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) { - std::shared_ptr session = std::make_unique(std::move(sockfd)); + std::shared_ptr session = std::make_shared(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()->get(), nullptr, nullptr) /* awaits new client/session */}); +void CSessionManager::run() { + while (true) + newSession(Hyprutils::OS::CFileDescriptor{accept(g_pServer->getSocket()->get(), nullptr, nullptr) /* awaits new client/session */}); } void CSessionManager::broadcast(const std::string &msg, eFormatType type) const { @@ -30,9 +36,16 @@ void CSessionManager::broadcast(const std::string &msg, eFormatType type) const } void CSessionManager::broadcastChat(const std::string &msg, const std::string &username) const { - for (const auto &[thread, session] : m_vSessions) - if (session->getName() != username) + for (const auto &[thread, session] : m_vSessions) { + if (!session) continue; + auto name = session->getName(); + if (name != username) session->write(msg, NONE); + } +} + +bool CSessionManager::nameExists(const std::string &name) { + return std::ranges::any_of(m_vSessions, [&name](const auto &s) { return s.second->getName() == name; }); } void CSessionManager::removeSession(std::pair> *session) { -- cgit v1.2.3