From 6ff51685a82045f5a52627bdb13257b3616ae5ad Mon Sep 17 00:00:00 2001 From: Gent Semaj Date: Thu, 2 Jan 2025 17:39:16 -0500 Subject: [PATCH] Move event buffers to Monitor --- src/Chat.cpp | 20 +++++++++----------- src/Chat.hpp | 2 -- src/Email.cpp | 5 ++--- src/Email.hpp | 2 -- src/servers/Monitor.cpp | 18 ++++++++++++------ src/servers/Monitor.hpp | 4 ++++ 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/Chat.cpp b/src/Chat.cpp index eea03d1..480b88c 100644 --- a/src/Chat.cpp +++ b/src/Chat.cpp @@ -1,6 +1,7 @@ #include "Chat.hpp" #include "servers/CNShardServer.hpp" +#include "servers/Monitor.hpp" #include "Player.hpp" #include "PlayerManager.hpp" @@ -8,9 +9,6 @@ #include -std::vector Chat::dump; -std::vector Chat::bcasts; - using namespace Chat; static void chatHandler(CNSocket* sock, CNPacketData* data) { @@ -29,7 +27,7 @@ static void chatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[FreeChat] " + PlayerManager::getPlayerName(plr, true) + ": " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); // send to client INITSTRUCT(sP_FE2CL_REP_SEND_FREECHAT_MESSAGE_SUCC, resp); @@ -52,7 +50,7 @@ static void menuChatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[MenuChat] " + PlayerManager::getPlayerName(plr, true) + ": " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); // send to client INITSTRUCT(sP_FE2CL_REP_SEND_MENUCHAT_MESSAGE_SUCC, resp); @@ -126,7 +124,7 @@ static void announcementHandler(CNSocket* sock, CNPacketData* data) { + std::to_string(announcement->iDuringTime) + " " + PlayerManager::getPlayerName(plr, false) + ": " + AUTOU16TOU8(msg.szAnnounceMsg); std::cout << "Broadcast " << logLine << std::endl; - bcasts.push_back(logLine); + Monitor::bcasts.push_back(logLine); } // Buddy freechatting @@ -159,7 +157,7 @@ static void buddyChatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[BuddyChat] " + PlayerManager::getPlayerName(plr) + " (to " + PlayerManager::getPlayerName(otherPlr) + "): " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); U8toU16(fullChat, (char16_t*)&resp.szFreeChat, sizeof(resp.szFreeChat)); @@ -189,7 +187,7 @@ static void buddyMenuChatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[BuddyMenuChat] " + PlayerManager::getPlayerName(plr) + " (to " + PlayerManager::getPlayerName(otherPlr) + "): " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); U8toU16(fullChat, (char16_t*)&resp.szFreeChat, sizeof(resp.szFreeChat)); @@ -222,7 +220,7 @@ static void tradeChatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[TradeChat] " + PlayerManager::getPlayerName(plr) + " (to " + PlayerManager::getPlayerName(otherPlr) + "): " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); resp.iEmoteCode = pacdat->iEmoteCode; sock->sendPacket(resp, P_FE2CL_REP_PC_TRADE_EMOTES_CHAT); @@ -245,7 +243,7 @@ static void groupChatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[GroupChat] " + PlayerManager::getPlayerName(plr, true) + ": " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); // send to client INITSTRUCT(sP_FE2CL_REP_SEND_ALL_GROUP_FREECHAT_MESSAGE_SUCC, resp); @@ -268,7 +266,7 @@ static void groupMenuChatHandler(CNSocket* sock, CNPacketData* data) { std::string logLine = "[GroupMenuChat] " + PlayerManager::getPlayerName(plr, true) + ": " + fullChat; std::cout << logLine << std::endl; - dump.push_back(logLine); + Monitor::chats.push_back(logLine); // send to client INITSTRUCT(sP_FE2CL_REP_SEND_ALL_GROUP_MENUCHAT_MESSAGE_SUCC, resp); diff --git a/src/Chat.hpp b/src/Chat.hpp index 0dff714..2d52544 100644 --- a/src/Chat.hpp +++ b/src/Chat.hpp @@ -8,8 +8,6 @@ #include namespace Chat { - extern std::vector dump; - extern std::vector bcasts; void init(); void sendServerMessage(CNSocket* sock, std::string msg); // uses MOTD diff --git a/src/Email.cpp b/src/Email.cpp index d3133b6..a7fffad 100644 --- a/src/Email.cpp +++ b/src/Email.cpp @@ -3,6 +3,7 @@ #include "core/Core.hpp" #include "db/Database.hpp" #include "servers/CNShardServer.hpp" +#include "servers/Monitor.hpp" #include "PlayerManager.hpp" #include "Items.hpp" @@ -10,8 +11,6 @@ using namespace Email; -std::vector Email::dump; - // New email notification static void emailUpdateCheck(CNSocket* sock, CNPacketData* data) { INITSTRUCT(sP_FE2CL_REP_PC_NEW_EMAIL, resp); @@ -324,7 +323,7 @@ static void emailSend(CNSocket* sock, CNPacketData* data) { std::string logEmail = "[Email] " + PlayerManager::getPlayerName(plr, true) + " (to " + PlayerManager::getPlayerName(&otherPlr, true) + "): <" + email.SubjectLine + ">\n" + email.MsgBody; std::cout << logEmail << std::endl; - dump.push_back(logEmail); + Monitor::emails.push_back(logEmail); // notification to recipient if online CNSocket* recipient = PlayerManager::getSockFromID(pkt->iTo_PCUID); diff --git a/src/Email.hpp b/src/Email.hpp index 55e4302..c1a9906 100644 --- a/src/Email.hpp +++ b/src/Email.hpp @@ -4,7 +4,5 @@ #include namespace Email { - extern std::vector dump; - void init(); } diff --git a/src/servers/Monitor.cpp b/src/servers/Monitor.cpp index 092ffc5..231b0c5 100644 --- a/src/servers/Monitor.cpp +++ b/src/servers/Monitor.cpp @@ -14,6 +14,12 @@ static std::mutex sockLock; // guards socket list static std::list sockets; static sockaddr_in address; +std::vector Monitor::chats; +std::vector Monitor::bcasts; +std::vector Monitor::emails; + +using namespace Monitor; + static bool transmit(std::list::iterator& it, char *buff, int len) { int n = 0; int sock = *it; @@ -99,7 +105,7 @@ outer: } // chat - for (auto& str : Chat::dump) { + for (auto& str : chats) { n = std::snprintf(buff, sizeof(buff), "chat %s\n", str.c_str()); if (!transmit(it, buff, n)) @@ -107,7 +113,7 @@ outer: } // announcements - for (auto& str : Chat::bcasts) { + for (auto& str : bcasts) { n = std::snprintf(buff, sizeof(buff), "bcast %s\n", str.c_str()); if (!transmit(it, buff, n)) @@ -115,7 +121,7 @@ outer: } // emails - for (auto& str : Email::dump) { + for (auto& str : emails) { n = process_email(buff, str); if (!transmit(it, buff, n)) @@ -131,9 +137,9 @@ outer: it++; } - Chat::dump.clear(); - Chat::bcasts.clear(); - Email::dump.clear(); + chats.clear(); + bcasts.clear(); + emails.clear(); } bool Monitor::acceptConnection(SOCKET fd, uint16_t revents) { diff --git a/src/servers/Monitor.hpp b/src/servers/Monitor.hpp index 3edcf73..e905700 100644 --- a/src/servers/Monitor.hpp +++ b/src/servers/Monitor.hpp @@ -3,6 +3,10 @@ #include "core/Core.hpp" namespace Monitor { + extern std::vector chats; + extern std::vector bcasts; + extern std::vector emails; + SOCKET init(); bool acceptConnection(SOCKET, uint16_t); };