From 815c3955dbd9ff7e1a427cf4bc66da38b581b178 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Wed, 21 May 2025 18:01:42 +0800 Subject: feat: join and leave message internal: refactored messaging management, use instnace pointer for verification of session --- src/sessionManager.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/sessionManager.cpp') diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index b81aeaf..a5dc713 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -1,11 +1,12 @@ #include "sessionManager.hpp" +#include "IOManager.hpp" #include "log.hpp" #include "session.hpp" #include -#include #include #include #include +#include "chatServer.hpp" void CSessionManager::shutdownSessions() { if (m_vSessions.empty()) @@ -47,18 +48,23 @@ void CSessionManager::run() { newSession(); } -void CSessionManager::broadcast(const std::string &msg) const { +void CSessionManager::broadcast(const std::string &msg, std::optional self) const { for (const auto &[thread, session] : m_vSessions) - session->write(msg); + if (!self || (self && (uintptr_t)session.get() != *self)) + session->write(msg); + + g_pIOManager->addCustom({"", msg}); } -void CSessionManager::broadcastChat(const CChatManager::SMessage &msg) const { +void CSessionManager::broadcast(const CChatManager::SMessage &msg) const { for (const auto &[thread, session] : m_vSessions) { if (!session) continue; session->writeChat(msg); } + + g_pIOManager->addMessage(msg); } bool CSessionManager::nameExists(const std::string &name) { @@ -84,6 +90,11 @@ 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(); } @@ -97,6 +108,8 @@ void CSessionManager::kick(CSession *session, const bool kill, const std::string 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(); }); + g_pSessionManager->broadcast(NFormatter::fmt(NONEWLINE, "{} has left the chat", session->second->m_name), (uintptr_t)session->second.get()); + if (it != m_vSessions.end()) { if (!reason.empty()) { it->second->setDeaf(false); -- cgit v1.2.3