summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sessionManager.cpp')
-rw-r--r--src/sessionManager.cpp29
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) {