From 9d1452447481d45344a1e33446ad9a418e9ea64f Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Wed, 11 Jun 2025 19:55:25 +0800 Subject: internal: fully discard the use of uintptr_t for identification use --- src/chatManager.cpp | 2 +- src/chatManager.hpp | 10 ++++++---- src/session.cpp | 5 +++-- src/sessionManager.cpp | 5 ----- src/sessionManager.hpp | 1 - 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/chatManager.cpp b/src/chatManager.cpp index 6e8424b..e5a6398 100644 --- a/src/chatManager.cpp +++ b/src/chatManager.cpp @@ -14,7 +14,7 @@ CChatManager::~CChatManager() { void CChatManager::newMessage(const SMessage &msg) { log(LOG, "{}: {}", msg.username, msg.msg); - const auto session = msg.sender ? g_pSessionManager->getByPtr(*msg.sender) : g_pSessionManager->getByName(msg.username); + const auto session = msg.sender ? msg.sender->get() : g_pSessionManager->getByName(msg.username); if (session && session->isMuted()) { session->write("You are muted"); return; diff --git a/src/chatManager.hpp b/src/chatManager.hpp index b48dcdb..227ef6c 100644 --- a/src/chatManager.hpp +++ b/src/chatManager.hpp @@ -5,15 +5,17 @@ #include #include +class CSession; + class CChatManager { public: CChatManager(); ~CChatManager(); struct SMessage { - std::string msg; - std::string username; - std::optional sender = std::nullopt; // cant include session to use CSession* cause circular dependency - bool admin = false; + std::string msg; + std::string username; + std::optional> sender = std::nullopt; + bool admin = false; }; void newMessage(const SMessage &msg); std::string fmtBroadcastMessage(const SMessage &msg); diff --git a/src/session.cpp b/src/session.cpp index 116c9bc..aa6147c 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include CSession::CSession() { @@ -90,7 +91,7 @@ void CSession::recvLoop() { if (isCommand) g_pCommandHandler->handleCommand(recvData->data, this); - g_pChatManager->newMessage({.msg = recvData->data, .username = m_name, .sender = (uintptr_t)this, .admin = isCommand}); + g_pChatManager->newMessage({.msg = recvData->data, .username = m_name, .sender = self->second, .admin = isCommand}); } } @@ -246,7 +247,7 @@ bool CSession::write(const std::string &msg) { } bool CSession::writeChat(const CChatManager::SMessage &msg) { - if (msg.sender ? (uintptr_t)this != *msg.sender : msg.username != m_name) { + if (msg.sender ? this != msg.sender->get() : msg.username != m_name) { if (msg.admin && !m_isAdmin) return false; diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index aae56f1..de55480 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -91,11 +91,6 @@ CSession *CSessionManager::getByIp(const std::string &ip) const { return it != m_vSessions.end() ? it->second.get() : nullptr; } -CSession *CSessionManager::getByPtr(const uintptr_t &p) const { - auto it = std::ranges::find_if(m_vSessions, [&p](const auto &s) { return (uintptr_t)s.second.get() == p; }); - return it != m_vSessions.end() ? it->second.get() : nullptr; -} - std::vector> CSessionManager::getSessions() const { return m_vSessions | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to(); } diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp index 3aa4b19..eb3705e 100644 --- a/src/sessionManager.hpp +++ b/src/sessionManager.hpp @@ -25,7 +25,6 @@ class CSessionManager { CSession *getByName(const std::string &name) const; CSession *getByIp(const std::string &ip) const; - CSession *getByPtr(const uintptr_t &p) const; std::vector> getSessions() const; private: -- cgit v1.2.3