summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sessionManager.cpp')
-rw-r--r--src/sessionManager.cpp25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index 20f6820..c62402c 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -2,12 +2,13 @@
#include "log.hpp"
#include "session.hpp"
#include <algorithm>
+#include <cstring>
#include <unistd.h>
#include <utility>
void CSessionManager::shutdownSessions() {
for (const auto &[thread, session] : m_vSessions)
- session->onShutdown();
+ session->onKick("Server shutting down");
}
CSessionManager::CSessionManager() {
@@ -57,13 +58,33 @@ 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) {
+const CSession *CSessionManager::getByName(const std::string &name) const {
+ auto it = std::ranges::find_if(m_vSessions, [&name](const auto &s) { return s.second->getName() == name; });
+ return it != m_vSessions.end() ? it->second.get() : nullptr;
+}
+
+const CSession *CSessionManager::getByIp(const char m_ip[INET_ADDRSTRLEN]) const {
+ auto it = std::ranges::find_if(m_vSessions, [&m_ip](const auto &s) { log(LOG, s.second->m_ip); return strcmp(s.second->m_ip, m_ip) == 0; });
+ return it != m_vSessions.end() ? it->second.get() : nullptr;
+}
+
+void CSessionManager::kick(const CSession *session, const std::string &reason) const {
+ std::ranges::for_each(m_vSessions, [session, reason](const auto &s) {
+ if (s.second.get() == session)
+ s.second->onKick(reason, true);
+ });
+}
+
+void CSessionManager::removeSession(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool &kill) {
auto it = std::ranges::find_if(m_vSessions, [session](const auto &s) { return s.second.get() == session->second.get(); });
if (it != m_vSessions.end()) {
+ const auto native_handle = it->first.native_handle();
if (it->first.joinable())
it->first.detach(); // .detach the thread since it's removing itself
it->second.reset();
m_vSessions.erase(it);
+ if (kill && native_handle != 0)
+ pthread_cancel(native_handle);
}
}