diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-05-21 18:01:42 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-05-21 22:49:45 +0800 |
| commit | 815c3955dbd9ff7e1a427cf4bc66da38b581b178 (patch) | |
| tree | 37b7b20175aeadf789e990fac7bd364792dd7e49 /src | |
| parent | a7cd7d8eb37c50554ae7c75f2ba94db74d293a1b (diff) | |
feat: join and leave message
internal: refactored messaging management, use instnace pointer for
verification of session
Diffstat (limited to 'src')
| -rw-r--r-- | src/IOManager.cpp | 14 | ||||
| -rw-r--r-- | src/IOManager.hpp | 9 | ||||
| -rw-r--r-- | src/chatManager.cpp | 9 | ||||
| -rw-r--r-- | src/inputManager.cpp | 4 | ||||
| -rw-r--r-- | src/session.cpp | 6 | ||||
| -rw-r--r-- | src/sessionManager.cpp | 21 | ||||
| -rw-r--r-- | src/sessionManager.hpp | 5 |
7 files changed, 50 insertions, 18 deletions
diff --git a/src/IOManager.cpp b/src/IOManager.cpp index c5363af..eb648e5 100644 --- a/src/IOManager.cpp +++ b/src/IOManager.cpp @@ -16,7 +16,19 @@ const std::vector<CIOManager::SIO> &CIOManager::getIO() const { } void CIOManager::addLog(const SLog &log) { - m_vIO.push_back({.msg = std::nullopt, .log = log}); + m_vIO.push_back({.log = log}); + if (g_pInputManager) + g_pInputManager->updateIO(); +} + +void CIOManager::addMessage(const CChatManager::SMessage &msg) { + m_vIO.push_back({.msg = msg}); + if (g_pInputManager) + g_pInputManager->updateIO(); +} + +void CIOManager::addCustom(const std::pair<std::string, std::string> &custom) { + m_vIO.push_back({.custom = custom}); if (g_pInputManager) g_pInputManager->updateIO(); } diff --git a/src/IOManager.hpp b/src/IOManager.hpp index ba741ff..cc21a90 100644 --- a/src/IOManager.hpp +++ b/src/IOManager.hpp @@ -14,13 +14,16 @@ class CIOManager { std::string log; }; struct SIO { - // cannot have both at once - std::optional<CChatManager::SMessage> msg; - std::optional<SLog> log; + // once a time + std::optional<CChatManager::SMessage> msg = std::nullopt; + std::optional<SLog> log = std::nullopt; + std::optional<std::pair<std::string, std::string>> custom = std::nullopt; }; const std::vector<SIO> &getIO() const; void addLog(const SLog &log); + void addMessage(const CChatManager::SMessage &msg); + void addCustom(const std::pair<std::string, std::string> &custom); private: std::vector<SIO> m_vIO; diff --git a/src/chatManager.cpp b/src/chatManager.cpp index 07dfce4..6e8424b 100644 --- a/src/chatManager.cpp +++ b/src/chatManager.cpp @@ -14,15 +14,12 @@ CChatManager::~CChatManager() { void CChatManager::newMessage(const SMessage &msg) { log(LOG, "{}: {}", msg.username, msg.msg); - const auto session = g_pSessionManager->getByName(msg.username); + const auto session = msg.sender ? g_pSessionManager->getByPtr(*msg.sender) : g_pSessionManager->getByName(msg.username); if (session && session->isMuted()) { session->write("You are muted"); return; } - g_pSessionManager->broadcastChat(msg); - - g_pIOManager->m_vIO.push_back({.msg = msg, .log = std::nullopt}); - g_pInputManager->updateIO(); + g_pSessionManager->broadcast(msg); } std::string CChatManager::fmtBroadcastMessage(const SMessage &msg) { @@ -32,7 +29,7 @@ std::string CChatManager::fmtBroadcastMessage(const SMessage &msg) { std::vector<CChatManager::SMessage> CChatManager::getChat() const { std::vector<SMessage> chat; - for (const auto &[msg, log] : g_pIOManager->getIO()) + for (const auto &[msg, _, _] : g_pIOManager->getIO()) if (msg) chat.push_back(*msg); return chat; diff --git a/src/inputManager.cpp b/src/inputManager.cpp index cc3880b..d392081 100644 --- a/src/inputManager.cpp +++ b/src/inputManager.cpp @@ -25,7 +25,7 @@ CInputManager::CInputManager() })) , logComponent(ftxui::Renderer([] { std::vector<ftxui::Element> messages; - for (const auto &[chat, log] : g_pIOManager->getIO()) { + for (const auto &[chat, log, custom] : g_pIOManager->getIO()) { if (chat && !chat->msg.empty()) { std::string username = chat->username; if (username.back() == '\n') @@ -33,6 +33,8 @@ CInputManager::CInputManager() messages.push_back(ftxui::window(ftxui::text(username), ftxui::paragraph(chat->msg))); } else if (log && !log->log.empty()) messages.push_back(ftxui::window(ftxui::text(NFormatter::fmt(log->type, "")), ftxui::paragraph(log->log))); + else if (custom) + messages.push_back(ftxui::window(ftxui::text(custom->first), ftxui::paragraph(custom->second))); } return ftxui::vbox(messages); diff --git a/src/session.cpp b/src/session.cpp index 2814c41..07b89f3 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -1,6 +1,7 @@ #include "session.hpp" #include "chatManager.hpp" #include "commandHandler.hpp" +#include "format.hpp" #include "log.hpp" #include "server.hpp" #include "sessionManager.hpp" @@ -21,6 +22,7 @@ CSession::CSession() { m_ip.resize(strlen(m_ip.c_str())); m_port = ntohs(m_addr.sin_port); m_isAdmin = std::ranges::any_of(m_adminIps, [this](const char *ip) { return m_ip == ip; }); + log(LOG, "Client {} connected on port {}", m_ip, m_port); log(TRACE, "session: initialized"); @@ -194,6 +196,8 @@ bool CSession::registerSession() { log(LOG, "Client {} registered as: {}", m_ip, m_name); + g_pSessionManager->broadcast(NFormatter::fmt(NONEWLINE, "{} has joined the chat", m_name), (uintptr_t)this); + setDeaf(false); return true; } @@ -249,7 +253,7 @@ bool CSession::write(const std::string &msg) { } bool CSession::writeChat(const CChatManager::SMessage &msg) { - if ((uintptr_t)this != *msg.sender) { + if (msg.sender ? (uintptr_t)this != *msg.sender : msg.msg != m_name) { if (msg.admin && !m_isAdmin) return false; 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 <algorithm> -#include <cstring> #include <ranges> #include <unistd.h> #include <utility> +#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<uintptr_t> 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<std::shared_ptr<CSession>> CSessionManager::getSessions() const { return m_vSessions | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to<std::vector>(); } @@ -97,6 +108,8 @@ void CSessionManager::kick(CSession *session, const bool kill, const std::string void CSessionManager::kick(std::pair<std::jthread, std::shared_ptr<CSession>> *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); diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp index 8e02a7d..6e946d6 100644 --- a/src/sessionManager.hpp +++ b/src/sessionManager.hpp @@ -15,7 +15,7 @@ class CSessionManager { std::pair<std::jthread, std::shared_ptr<CSession>> *newSession(); void run(); - void broadcast(const std::string &msg) const; + void broadcast(const std::string &msg, std::optional<uintptr_t> self = std::nullopt) const; // second param only specified when we want to exclude the sender 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); @@ -25,10 +25,11 @@ 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: - void broadcastChat(const CChatManager::SMessage &msg) const; + void broadcast(const CChatManager::SMessage &msg) const; std::vector<std::pair<std::jthread, std::shared_ptr<CSession>>> m_vSessions; |
