From d580a4468a26f6b47b4dfe7c9171e680c788be41 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Thu, 13 Mar 2025 16:43:14 +0800 Subject: NFormatter: no more prefix support for socket write NFormatter: remove CHAT prefix --- src/format.hpp | 4 ---- src/session.cpp | 16 ++++++++-------- src/session.hpp | 4 ++-- src/sessionManager.cpp | 7 ++++--- src/sessionManager.hpp | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/format.hpp b/src/format.hpp index df1aa40..42a4458 100644 --- a/src/format.hpp +++ b/src/format.hpp @@ -9,7 +9,6 @@ enum eFormatType : int8_t { WARN, ERR, SYS, - CHAT, }; namespace NFormatter { @@ -28,9 +27,6 @@ inline std::string fmt(eFormatType type, std::string str) { case SYS: prefix = "[SYSTEM] "; break; - case CHAT: - prefix = "[CHAT] "; - break; case TRACE: prefix = "[DEBUG] "; break; diff --git a/src/session.cpp b/src/session.cpp index a990e08..2a25db2 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -84,7 +84,7 @@ CSession::SRecvData CSession::read() { } CSession::SRecvData CSession::read(const std::string &msg) { - write({}, "{}", msg, false); + write("{}", msg, false); m_isReading = true; return read(); } @@ -102,13 +102,13 @@ bool CSession::registerSession() { return false; if (recvData.data[0] == '\n' || recvData.data[0] == ' ') { - write("Name cannot be empty", ERR); + write("Name cannot be empty"); return registerSession(); } *std::remove(recvData.data, recvData.data + strlen(recvData.data), '\n') = '\0'; if (g_pSessionManager->nameExists(recvData.data)) { - write("Name already exists", ERR); + write("Name already exists"); return registerSession(); } @@ -130,8 +130,8 @@ bool CSession::isValid() { } template -bool CSession::write(eFormatType type, std::format_string fmt, Args &&...args) { - std::string msg = NFormatter::fmt(type, fmt, std::forward(args)...); +bool CSession::write(std::format_string fmt, Args &&...args) { + std::string msg = NFormatter::fmt(NONE, fmt, std::forward(args)...); if (m_isReading) msg.insert(0, "\n"); @@ -143,8 +143,8 @@ bool CSession::write(eFormatType type, std::format_string fmt, Args &&. return true; } -bool CSession::write(const std::string &msg, eFormatType type) { - return write(type, "{}", msg); +bool CSession::write(const std::string &msg) { + return write("{}", msg); } std::string CSession::getName() { @@ -156,6 +156,6 @@ void CSession::setSelf(std::pair> *self) } void CSession::onShutdown() { - write(SYS, "Shutting down, bye"); + write("Shutting down, bye"); g_pSessionManager->removeSession(self); } diff --git a/src/session.hpp b/src/session.hpp index 48e2dad..4f5adb2 100644 --- a/src/session.hpp +++ b/src/session.hpp @@ -23,9 +23,9 @@ class CSession { SRecvData read(); SRecvData read(const std::string &msg); - bool write(const std::string &msg, eFormatType type); + bool write(const std::string &msg); template - bool write(eFormatType type, std::format_string fmt, Args &&...args); + bool write(std::format_string fmt, Args &&...args); void run(); diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp index a7490e3..049ec2b 100644 --- a/src/sessionManager.cpp +++ b/src/sessionManager.cpp @@ -38,9 +38,9 @@ void CSessionManager::run() { newSession(); } -void CSessionManager::broadcast(eFormatType type, const std::string &msg) const { +void CSessionManager::broadcast(const std::string &msg) const { for (const auto &[thread, session] : m_vSessions) - session->write(msg, type); + session->write(msg); } void CSessionManager::broadcastChat(const std::string &msg, const std::string &username) const { @@ -49,7 +49,7 @@ void CSessionManager::broadcastChat(const std::string &msg, const std::string &u continue; auto name = session->getName(); if (name != username) - session->write(msg, NONE); + session->write(msg); } } @@ -66,4 +66,5 @@ void CSessionManager::removeSession(std::pairsecond.reset(); m_vSessions.erase(it); } + } diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp index 0f95dc9..21c47b9 100644 --- a/src/sessionManager.hpp +++ b/src/sessionManager.hpp @@ -14,7 +14,7 @@ class CSessionManager { std::pair> *newSession(); void run(); - void broadcast(eFormatType type, const std::string &msg) const; + void broadcast(const std::string &msg) const; void kick(const CSession *session) const; void addSession(const CSession &session); void removeSession(std::pair> *session); -- cgit v1.2.3