From 7a56113d69c5a56ea333f87c5d67cd5a371aa090 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Fri, 7 Mar 2025 18:02:11 +0800 Subject: fix: session not disconnected properly perf: use server sockfd directly instead of a dupe of it --- src/sessionManager.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/sessionManager.cpp') 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 +#include +#include +#include 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(std::move(sockfd)); - m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session)); +std::pair> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor 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().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> *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); + } } -- cgit v1.2.3