summaryrefslogtreecommitdiff
path: root/src
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
parente23bd1552448f444ca0f97fcfd5c68016b2e7eb6 (diff)
perf: directly kick instead of calling function in the session(killing it's self?)
Diffstat (limited to 'src')
-rw-r--r--src/session.cpp8
-rw-r--r--src/session.hpp1
-rw-r--r--src/sessionManager.cpp22
-rw-r--r--src/sessionManager.hpp8
4 files changed, 17 insertions, 22 deletions
diff --git a/src/session.cpp b/src/session.cpp
index 50eaff4..10c549b 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -127,7 +127,7 @@ void CSession::run() {
} else
log(LOG, "Client {} exited without even registering", m_ip);
- g_pSessionManager->removeSession(self);
+ g_pSessionManager->kick(self);
}
bool CSession::registerSession() {
@@ -205,9 +205,3 @@ const std::string &CSession::getName() const {
void CSession::setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self) {
this->self = self;
}
-
-void CSession::onKick(const std::string &reason, const bool &kill) {
- if (!reason.empty())
- write(reason);
- g_pSessionManager->removeSession(self, kill);
-}
diff --git a/src/session.hpp b/src/session.hpp
index 390cd71..b9c1cc1 100644
--- a/src/session.hpp
+++ b/src/session.hpp
@@ -64,7 +64,6 @@ class CSession {
void onRecv(const SRecvData &data);
void onSend(const std::string &msg);
#endif
- void onKick(const std::string &reason = "", const bool &kill = false);
friend class CSessionManager;
};
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()) {
diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp
index 79fa372..70faa58 100644
--- a/src/sessionManager.hpp
+++ b/src/sessionManager.hpp
@@ -15,15 +15,15 @@ class CSessionManager {
void run();
void broadcast(const std::string &msg) const;
- void kick(const CSession *session, const std::string &reason = "") const;
+ void kick(CSession *session, const bool &kill = false, const std::string &reason = "");
+ void kick(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool &kill = false, const std::string &reason = "");
void addSession(const CSession &session);
- void removeSession(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool &kill = false);
bool nameExists(const std::string &name);
void shutdownSessions();
- const CSession *getByName(const std::string &name) const;
- const CSession *getByIp(const char m_ip[INET_ADDRSTRLEN]) const;
+ CSession *getByName(const std::string &name) const;
+ CSession *getByIp(const char m_ip[INET_ADDRSTRLEN]) const;
private:
void broadcastChat(const std::string &msg, const std::string &username) const;