From a38b8c35e4c9b85e0b035c5dfa2bee26a5c36b65 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 7 Apr 2025 18:20:47 +0800 Subject: internal: use smart pointer for everything, no more naked pointer --- src/sessionManager.cpp | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'src/sessionManager.cpp') diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index b81aeaf..6d9df28 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -12,7 +12,7 @@ void CSessionManager::shutdownSessions() { return; for (auto &session : m_vSessions) - kick(&session, false, "Server shutting down"); + kick(std::unique_ptr>>(&session), false, "Server shutting down"); } CSessionManager::CSessionManager() { @@ -35,11 +35,10 @@ CSessionManager::~CSessionManager() { log(SYS, "SessionManager: bye"); } -std::pair> *CSessionManager::newSession() { +void CSessionManager::newSession() { std::shared_ptr session = std::make_shared(); - const auto instance = &m_vSessions.emplace_back(std::jthread(&CSession::run, session.get()), session); - instance->second->setSelf(instance); - return instance; + auto instance = std::unique_ptr>>(&m_vSessions.emplace_back(std::jthread(&CSession::run, session.get()), session)); + instance->second->setSelf(std::move(instance)); } void CSessionManager::run() { @@ -66,7 +65,7 @@ bool CSessionManager::nameExists(const std::string &name) { if (it != m_vSessions.end()) { if (!it->second->isValid()) { - kick(it->second.get(), true, "Connection lost"); + kick(it->second, true, "Connection lost"); return false; } return true; @@ -74,28 +73,28 @@ bool CSessionManager::nameExists(const std::string &name) { return false; } -CSession *CSessionManager::getByName(const std::string &name) const { +std::shared_ptr 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; + return it != m_vSessions.end() ? it->second : nullptr; } -CSession *CSessionManager::getByIp(const std::string &ip) const { +std::shared_ptr CSessionManager::getByIp(const std::string &ip) const { auto it = std::ranges::find_if(m_vSessions, [&ip](const auto &s) { log(LOG, s.second->m_ip); return s.second->m_ip == ip; }); - return it != m_vSessions.end() ? it->second.get() : nullptr; + return it != m_vSessions.end() ? it->second : nullptr; } std::vector> CSessionManager::getSessions() const { return m_vSessions | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to(); } -void CSessionManager::kick(CSession *session, const bool kill, const std::string &reason) { +void CSessionManager::kick(std::shared_ptr session, const bool kill, const std::string &reason) { for (auto &_session : m_vSessions) - if (_session.second.get() == session) - kick(&_session, kill, reason); + if (_session.second == session) + kick(std::unique_ptr>>(&_session), kill, reason); } -void CSessionManager::kick(std::pair> *session, const bool kill, const std::string &reason) { - auto it = std::ranges::find_if(m_vSessions, [session](const auto &s) { return s.second.get() == session->second.get(); }); +void CSessionManager::kick(std::unique_ptr>> session, const bool kill, const std::string &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()) { if (!reason.empty()) { -- cgit v1.2.3