From 6a8a5051679930e3c0fa4b511058b7d50b81f08a Mon Sep 17 00:00:00 2001 From: Marc-Eric Martel Date: Fri, 27 Oct 2023 12:02:29 -0400 Subject: [PATCH] Yeehaw. --- SQCSim-common/netprotocol.cpp | 111 +++++++++++++++++++++++++++++++--- SQCSim-common/netprotocol.h | 2 + SQCSim-srv/server.cpp | 69 ++++++++------------- 3 files changed, 130 insertions(+), 52 deletions(-) diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 1053191..837180a 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -637,27 +637,122 @@ bool netprot::emptyPack(netprot::Packet pck) { switch (pck.type) { case PACKET_TYPE::INPUT: delete (Input*)pck.ptr; - break; + return true; case PACKET_TYPE::OUTPUT: delete (Output*)pck.ptr; - break; + return true; case PACKET_TYPE::SYNC: delete (Sync*)pck.ptr; - break; + return true; case PACKET_TYPE::CHAT: delete (Chat*)pck.ptr; - break; + return true; case PACKET_TYPE::GAMEINFO: delete (GameInfo*)pck.ptr; - break; + return true; case PACKET_TYPE::ERRLOG: delete (ErrorLog*)pck.ptr; - break; + return true; case PACKET_TYPE::LOGINF: delete (LoginInfo*)pck.ptr; - break; + return true; default: return false; } - return true; } + +template <> +void netprot::sendPack(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) { + switch (pack->type) { + case PACKET_TYPE::INPUT: + sendPack(sock, (Input*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::OUTPUT: + sendPack(sock, (Output*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::SYNC: + sendPack(sock, (Sync*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::TEAMINF: + sendPack(sock, (TeamInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::PLAYINF: + sendPack(sock, (PlayerInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::LOGINF: + sendPack(sock, (LoginInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::CHUNKMOD: + //sendPack(sock, (ChunkMod*)pack->ptr, buf, buflen); + case PACKET_TYPE::PLAYERMOD: + //sendPack(sock, (PlayerMod*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::PICKUPMOD: + //sendPack(sock, (PickupMod*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::GAMEINFO: + sendPack(sock, (GameInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::ENDINFO: + //sendPack(sock, (EndInfo*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::CHAT: + sendPack(sock, (Chat*)pack->ptr, buf, buflen); + case PACKET_TYPE::ERRLOG: + sendPack(sock, (ErrorLog*)pack->ptr, buf, buflen); + return; + case PACKET_TYPE::LAST_PACK: [[fallthrough]]; + case PACKET_TYPE::ERR: [[fallthrough]]; + default: + return; + } +} + +template <> +void netprot::sendPackTo(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { + switch (pack->type) { + case PACKET_TYPE::INPUT: + sendPackTo(sock, (Input*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::OUTPUT: + sendPackTo(sock, (Output*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::SYNC: + sendPackTo(sock, (Sync*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::TEAMINF: + sendPackTo(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::PLAYINF: + sendPackTo(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::LOGINF: + sendPackTo(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::CHUNKMOD: + //sendPackTo(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::PLAYERMOD: + //sendPackTo(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::PICKUPMOD: + //sendPackTo(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::GAMEINFO: + sendPackTo(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::ENDINFO: + //sendPackTo(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::CHAT: + sendPackTo(sock, (Chat*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::ERRLOG: + sendPackTo(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); + return; + case PACKET_TYPE::LAST_PACK: [[fallthrough]]; + case PACKET_TYPE::ERR: [[fallthrough]]; + default: + return; + } +} \ No newline at end of file diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 8cbcba5..33f233e 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -134,6 +134,8 @@ namespace netprot { bool emptyPack(Packet pck); + Packet makePack(void* ptr, PACKET_TYPE type); // Pour pouvoir faire une liste de stock à supprimer sans avoir à en faire une pour chaque type. + template void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen); template void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad); diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 9d3178b..a0c2a5e 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -196,52 +196,53 @@ void Server::Run() { if (recvfrom(m_sock_udp, m_buf.ptr, m_buf.len, 0, (sockaddr*)&sockad, &socklen) > 0) { Packet pck = getPack(&m_buf); switch (pck.type) { - case netprot::PACKET_TYPE::ERR: + using enum netprot::PACKET_TYPE; + case ERR: std::cout << "ERROR!" << std::endl; break; - case netprot::PACKET_TYPE::INPUT: + case INPUT: std::cout << "INPUT!" << std::endl; break; - case netprot::PACKET_TYPE::OUTPUT: + case OUTPUT: std::cout << "OUTPUT!" << std::endl; break; - case netprot::PACKET_TYPE::SYNC: + case SYNC: std::cout << "SYNC!" << std::endl; break; - case netprot::PACKET_TYPE::TEAMINF: + case TEAMINF: std::cout << "TEAMINF!" << std::endl; break; - case netprot::PACKET_TYPE::SELFINF: + case SELFINF: std::cout << "SELFINF!" << std::endl; break; - case netprot::PACKET_TYPE::PLAYINF: + case PLAYINF: std::cout << "PLAYINF!" << std::endl; break; - case netprot::PACKET_TYPE::LOGINF: + case LOGINF: std::cout << "LOGINF!" << std::endl; break; - case netprot::PACKET_TYPE::CHUNKMOD: + case CHUNKMOD: std::cout << "CHUNKMOD!" << std::endl; break; - case netprot::PACKET_TYPE::PLAYERMOD: + case PLAYERMOD: std::cout << "PLAYERMOD!" << std::endl; break; - case netprot::PACKET_TYPE::PICKUPMOD: + case PICKUPMOD: std::cout << "PICKUPMOD!" << std::endl; break; - case netprot::PACKET_TYPE::GAMEINFO: + case GAMEINFO: std::cout << "GAMEINFO!" << std::endl; break; - case netprot::PACKET_TYPE::ENDINFO: + case ENDINFO: std::cout << "ENDINFO!" << std::endl; break; - case netprot::PACKET_TYPE::CHAT: + case CHAT: std::cout << "CHAT!" << std::endl; break; - case netprot::PACKET_TYPE::ERRLOG: + case ERRLOG: std::cout << "ERRLOG!" << std::endl; break; - case netprot::PACKET_TYPE::LAST_PACK: + case LAST_PACK: std::cout << "wtf?!" << std::endl; break; } @@ -271,14 +272,14 @@ inline std::string Server::LogTimestamp() { void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) { switch (m_log) { - using enum LOG_DEST; // C++20! - case LOGFILE: - m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl; - break; - case CONSOLE: - default: - std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl; - break; + using enum LOG_DEST; // C++20! + case LOGFILE: + m_logfile << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl; + break; + case CONSOLE: [[fallthrough]]; // Pour dire que c'est voulu que ça traverse vers le case en dessous (C++17!) + default: + std::cout << LogTimestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl; + break; } if (is_fatal) { @@ -315,23 +316,3 @@ uint64_t Server::getUniqueId() { m_ids.pop_back(); return id; } - -// Test serialize/deserialize: -/* - netprot::LoginInfo log, log2; - std::cout << "Nom? "; - std::cin.getline(log.name, 32); // NO! STD::CIN >> VARIABLE;! EVEEEEEERRRR!!! - log.sid = 12345; - char* buf = new char[150]; - uint32_t buflen = 150; - - netprot::Serialize(&log, &buf, &buflen); - - bool is_work = netprot::Deserialize(&log2, buf, buflen); - - std::string str; - str.append(is_work ? "Y " : "N ").append(log2.name).append(": ").append(std::to_string(log2.sid)); - - Log(str, false, false); -*/ -