summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-06-11 19:55:25 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-06-11 19:55:25 +0800
commit9d1452447481d45344a1e33446ad9a418e9ea64f (patch)
tree9fdaf4301c461f23eec210663874b3cdd567779b
parente30f6eb6872d57f7da764690589c2f0c9ffdb53b (diff)
internal: fully discard the use of uintptr_t for identification use
-rw-r--r--src/chatManager.cpp2
-rw-r--r--src/chatManager.hpp10
-rw-r--r--src/session.cpp5
-rw-r--r--src/sessionManager.cpp5
-rw-r--r--src/sessionManager.hpp1
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 <string>
#include <vector>
+class CSession;
+
class CChatManager {
public:
CChatManager();
~CChatManager();
struct SMessage {
- std::string msg;
- std::string username;
- std::optional<uintptr_t> sender = std::nullopt; // cant include session to use CSession* cause circular dependency
- bool admin = false;
+ std::string msg;
+ std::string username;
+ std::optional<std::shared_ptr<CSession>> 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 <cerrno>
#include <cstring>
#include <memory>
+#include <optional>
#include <sys/socket.h>
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<std::shared_ptr<CSession>> CSessionManager::getSessions() const {
return m_vSessions | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to<std::vector>();
}
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<std::shared_ptr<CSession>> getSessions() const;
private: