summaryrefslogtreecommitdiff
path: root/src/sessionManager.cpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-20 21:06:46 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-21 11:18:14 +0800
commitbc23856add348060b600fd0f9cbccfd79b744af5 (patch)
tree64a3d5ebe3b5bf21fcba1e18b1685e2fa852ab56 /src/sessionManager.cpp
parente23bd1552448f444ca0f97fcfd5c68016b2e7eb6 (diff)
perf: directly kick instead of calling function in the session(killing it's self?)
Diffstat (limited to 'src/sessionManager.cpp')
-rw-r--r--src/sessionManager.cpp22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index 435c6dc..f019cff 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -7,8 +7,8 @@
#include <utility>
void CSessionManager::shutdownSessions() {
- for (const auto &[thread, session] : m_vSessions)
- session->onKick("Server shutting down");
+ for (auto &session : m_vSessions)
+ kick(&session, false, "Server shutting down");
}
CSessionManager::CSessionManager() {
@@ -58,24 +58,26 @@ bool CSessionManager::nameExists(const std::string &name) {
return std::ranges::any_of(m_vSessions, [&name](const auto &s) { return s.second->getName() == name; });
}
-const CSession *CSessionManager::getByName(const std::string &name) 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 {
+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::kick(CSession *session, const bool &kill, const std::string &reason) {
+ for (auto &_session : m_vSessions)
+ if (_session.second.get() == session)
+ kick(&_session, kill, reason);
}
-void CSessionManager::removeSession(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool &kill) {
+void CSessionManager::kick(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool &kill, const std::string &reason) {
+ if (!reason.empty())
+ session->second->write(reason);
+
auto it = std::ranges::find_if(m_vSessions, [session](const auto &s) { return s.second.get() == session->second.get(); });
if (it != m_vSessions.end()) {