diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-03-10 18:36:00 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-03-20 23:44:58 +0800 |
| commit | 131dc244e67e3b6e9304e7076c397c761860044c (patch) | |
| tree | 56f634c1a16c4e5b6eb2224ba876ca97f68e4dee /src/sessionManager.cpp | |
| parent | 4e8644b8ab3b259a91387c93a3295092bd9757a0 (diff) | |
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)
Diffstat (limited to 'src/sessionManager.cpp')
| -rw-r--r-- | src/sessionManager.cpp | 29 |
1 files changed, 21 insertions, 8 deletions
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 <algorithm> #include <unistd.h> #include <utility> +#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<std::jthread, std::shared_ptr<CSession>> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) { - std::shared_ptr session = std::make_unique<CSession>(std::move(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()->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<std::jthread, std::shared_ptr<CSession>> *session) { |
