summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-07 18:02:11 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-20 23:44:57 +0800
commit7a56113d69c5a56ea333f87c5d67cd5a371aa090 (patch)
tree7b3873aabf9d35abeac7e4970935cb9f185ccaa7 /src/sessionManager.cpp
parent3972c5f76483bf5f64bcf19685ee391c4d6b1a9b (diff)
fix: session not disconnected properly
perf: use server sockfd directly instead of a dupe of it
Diffstat (limited to 'src/sessionManager.cpp')
-rw-r--r--src/sessionManager.cpp28
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);
+ }
}