summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/session.cpp5
-rw-r--r--src/session.hpp1
-rw-r--r--src/sessionManager.cpp8
-rw-r--r--src/sessionManager.hpp1
4 files changed, 15 insertions, 0 deletions
diff --git a/src/session.cpp b/src/session.cpp
index f8506c2..a6875b0 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -144,3 +144,8 @@ std::string CSession::getName() {
void CSession::setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self) {
this->self = self;
}
+
+void CSession::onShutdown() {
+ write(SYS, "Shutting down, bye");
+ g_pSessionManager->removeSession(self);
+}
diff --git a/src/session.hpp b/src/session.hpp
index d32fd96..6f1382c 100644
--- a/src/session.hpp
+++ b/src/session.hpp
@@ -42,5 +42,6 @@ class CSession {
void onErrno(eEventType);
void onRecv(const SRecvData &data);
void onSend(const std::string &msg);
+ void onShutdown();
friend class CSessionManager;
};
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index f4573e1..b4e1ad5 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -6,7 +6,15 @@
#include <utility>
#include "log.hpp"
+void CSessionManager::shutdownSessions() {
+ for (const auto &[thread, session] : m_vSessions)
+ session->onShutdown();
+}
+
CSessionManager::CSessionManager() {
+ if (std::atexit([]() { g_pSessionManager->shutdownSessions(); }))
+ log(ERR, "Failed to register SessionManager atexit handler, will fallback to quick_exit after hang");
+
log(LOG, "SessionManager: initialized");
};
diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp
index 08ba541..8f2130a 100644
--- a/src/sessionManager.hpp
+++ b/src/sessionManager.hpp
@@ -20,6 +20,7 @@ class CSessionManager {
void removeSession(std::pair<std::jthread, std::shared_ptr<CSession>> *session);
bool nameExists(const std::string &name);
+ void shutdownSessions();
private:
void broadcastChat(const std::string &msg, const std::string &username) const;