From ccf3ccdba9ee066a8d231f7d8fc4673d11144280 Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Tue, 28 Apr 2026 20:21:00 +0200 Subject: [PATCH 1/6] Refactored misc code. --- Core/GameEngine/Source/GameNetwork/NetCommandList.cpp | 2 +- Core/GameEngine/Source/GameNetwork/NetPacket.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/NetCommandList.cpp b/Core/GameEngine/Source/GameNetwork/NetCommandList.cpp index b6a834c987b..451205975c3 100644 --- a/Core/GameEngine/Source/GameNetwork/NetCommandList.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetCommandList.cpp @@ -317,7 +317,7 @@ NetCommandRef * NetCommandList::addMessage(NetCommandMsg *cmdMsg) { } // Make sure this command isn't already in the list. - if (isEqualCommandMsg(tempmsg->getCommand(), msg->getCommand())) { + if (isEqualCommandMsg(tempmsg->getCommand(), msg->getCommand())) { // This command is already in the list, don't duplicate it. deleteInstance(msg); diff --git a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp index 2c59457c84c..939a22330b0 100644 --- a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp @@ -722,8 +722,7 @@ NetCommandList * NetPacket::getCommandList() { break; } - case 'Z': { - + case NetPacketFieldTypes::Repeat: { ++i; // Repeat the last command, doing some funky cool byte-saving stuff if (lastCommand == nullptr) { From db729a4de677b71db861d970b1b605b43fcddf7b Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Tue, 28 Apr 2026 20:22:25 +0200 Subject: [PATCH 2/6] Removed unused static (packet and command count) variables. --- Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index 6aed5ac39dc..bf5ef83debe 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -459,8 +459,6 @@ void ConnectionManager::destroyGameMessages() { * assumption that a command will only be relayed once. */ void ConnectionManager::doRelay() { - static Int numPackets = 0; - static Int numCommands = 0; NetPacket *packet = nullptr; @@ -489,10 +487,7 @@ void ConnectionManager::doRelay() { sendRemoteCommand(cmd); } cmd = cmd->getNext(); - - ++numCommands; } - ++numPackets; // Delete this packet since we won't be needing it anymore. deleteInstance(packet); @@ -516,10 +511,7 @@ void ConnectionManager::doRelay() { sendRemoteCommand(cmd); } cmd = cmd->getNext(); - - ++numCommands; } - ++numPackets; // Delete this packet since we won't be needing it anymore. deleteInstance(packet); From 99ee4998492b278debf75e0f8d74bba56273323c Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Tue, 28 Apr 2026 20:24:55 +0200 Subject: [PATCH 3/6] Significantly reduced the number of packet (re)allocations. --- .../Include/GameNetwork/NetPacket.h | 3 ++- .../Source/GameNetwork/Connection.cpp | 19 +++++++------------ .../Source/GameNetwork/ConnectionManager.cpp | 15 ++++----------- .../Source/GameNetwork/NetPacket.cpp | 15 ++++++++++----- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/Core/GameEngine/Include/GameNetwork/NetPacket.h b/Core/GameEngine/Include/GameNetwork/NetPacket.h index bdba5780ed5..295bca4a284 100644 --- a/Core/GameEngine/Include/GameNetwork/NetPacket.h +++ b/Core/GameEngine/Include/GameNetwork/NetPacket.h @@ -50,11 +50,12 @@ class NetPacket : public MemoryPoolObject MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetPacket, "NetPacket") public: NetPacket(); - NetPacket(TransportMessage *msg); + NetPacket(const TransportMessage& msg); //virtual ~NetPacket(); void init(); void reset(); + void CopyTransportMessage(const TransportMessage& msg); void setAddress(Int addr, Int port); Bool addCommand(NetCommandRef *msg); Int getNumCommands(); diff --git a/Core/GameEngine/Source/GameNetwork/Connection.cpp b/Core/GameEngine/Source/GameNetwork/Connection.cpp index c1f84e88881..805188c7fc3 100644 --- a/Core/GameEngine/Source/GameNetwork/Connection.cpp +++ b/Core/GameEngine/Source/GameNetwork/Connection.cpp @@ -131,18 +131,13 @@ User * Connection::getUser() { * The relay mostly has to do with the packet router. */ void Connection::sendNetCommandMsg(NetCommandMsg *msg, UnsignedByte relay) { - static NetPacket *packet = nullptr; - - // this is done so we don't have to allocate and delete a packet every time we send a message. - if (packet == nullptr) { - packet = newInstance(NetPacket); - } - - if (m_isQuitting) return; if (m_netCommandList != nullptr) { + // this is done so we don't have to allocate and delete a packet every time we send a message. + static NetPacket* packet = newInstance(NetPacket); + // check to see if this command will fit in a packet. If not, we need to split it up. // we are splitting up the command here so that the retry logic will not try to // resend the ENTIRE command (i.e. multiple packets work of data) and only do the retry @@ -269,9 +264,11 @@ UnsignedInt Connection::doSend() { // iterate through all the messages and put them into a packet(s). NetCommandRef *msg = m_netCommandList->getFirstMessage(); + // this is done so we don't have to allocate and delete a packet every time we send a message. + static NetPacket* packet = newInstance(NetPacket); + while ((msg != nullptr) && couldQueue) { - NetPacket *packet = newInstance(NetPacket); - packet->init(); + packet->reset(); packet->setAddress(m_user->GetIPAddr(), m_user->GetPort()); Bool notDone = TRUE; @@ -314,8 +311,6 @@ UnsignedInt Connection::doSend() { couldQueue = m_transport->queueSend(packet->getAddr(), packet->getPort(), packet->getData(), packet->getLength()); m_lastTimeSent = curtime; } - - deleteInstance(packet); // delete the packet now that we're done with it. } return numpackets; diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index bf5ef83debe..a51b6774285 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -459,15 +459,16 @@ void ConnectionManager::destroyGameMessages() { * assumption that a command will only be relayed once. */ void ConnectionManager::doRelay() { - - NetPacket *packet = nullptr; + // this is done so we don't have to allocate and delete a packet every time we relay a message. + static NetPacket* packet = newInstance(NetPacket); for (Int i = 0; i < MAX_MESSAGES; ++i) { if (m_transport->m_inBuffer[i].length != 0) { // This transport buffer has yet to be processed. // make a NetPacket out of this data so it can be broken up into individual commands. - packet = newInstance(NetPacket)(&(m_transport->m_inBuffer[i])); + packet->reset(); + packet->CopyTransportMessage(m_transport->m_inBuffer[i]); //DEBUG_LOG(("ConnectionManager::doRelay() - got a packet with %d commands", packet->getNumCommands())); //LOGBUFFER( packet->getData(), packet->getLength() ); @@ -489,10 +490,6 @@ void ConnectionManager::doRelay() { cmd = cmd->getNext(); } - // Delete this packet since we won't be needing it anymore. - deleteInstance(packet); - packet = nullptr; - deleteInstance(cmdList); cmdList = nullptr; @@ -513,10 +510,6 @@ void ConnectionManager::doRelay() { cmd = cmd->getNext(); } - // Delete this packet since we won't be needing it anymore. - deleteInstance(packet); - packet = nullptr; - deleteInstance(cmdList); cmdList = nullptr; } diff --git a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp index 939a22330b0..c19b50b0343 100644 --- a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp @@ -293,13 +293,18 @@ NetPacket::NetPacket() { /** * Constructor given raw transport data. */ -NetPacket::NetPacket(TransportMessage *msg) { +NetPacket::NetPacket(const TransportMessage& msg) { init(); - m_packetLen = msg->length; - memcpy(m_packet, msg->data, MAX_PACKET_SIZE); + CopyTransportMessage(msg); +} + +void NetPacket::CopyTransportMessage(const TransportMessage& msg) +{ + m_packetLen = msg.length; + memcpy(m_packet, msg.data, MAX_PACKET_SIZE); m_numCommands = -1; - m_addr = msg->addr; - m_port = msg->port; + m_addr = msg.addr; + m_port = msg.port; } /** From daaf11cacfd75b346e8ead8ce68e0ae1c6f8d0bf Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Tue, 28 Apr 2026 20:26:05 +0200 Subject: [PATCH 4/6] Refactored while loops to for loops. --- Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index a51b6774285..6ff456626e5 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -475,19 +475,18 @@ void ConnectionManager::doRelay() { // Get the command list from the packet. NetCommandList *cmdList = packet->getCommandList(); - NetCommandRef *cmd = cmdList->getFirstMessage(); // Iterate through the commands in this packet and send them to the proper connections. - while (cmd != nullptr) { + for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd = cmd->getNext()) { //DEBUG_LOG(("ConnectionManager::doRelay() - Looking at a command of type %s", //GetNetCommandTypeAsString(cmd->getCommand()->getNetCommandType()))); + if (CommandRequiresAck(cmd->getCommand())) { ackCommand(cmd, m_localSlot); } if (!processNetCommand(cmd)) { sendRemoteCommand(cmd); } - cmd = cmd->getNext(); } deleteInstance(cmdList); @@ -499,15 +498,13 @@ void ConnectionManager::doRelay() { } NetCommandList *cmdList = m_netCommandWrapperList->getReadyCommands(); - NetCommandRef *cmd = cmdList->getFirstMessage(); - while (cmd != nullptr) { + for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd = cmd->getNext()) { if (CommandRequiresAck(cmd->getCommand())) { ackCommand(cmd, m_localSlot); } if (!processNetCommand(cmd)) { sendRemoteCommand(cmd); } - cmd = cmd->getNext(); } deleteInstance(cmdList); From 4fb9e1debfe2ff2c1edca6c022b13c5bfe758792 Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Tue, 28 Apr 2026 20:27:36 +0200 Subject: [PATCH 5/6] Improved loop logic performance. --- .../Source/GameNetwork/ConnectionManager.cpp | 6 +++-- Core/GameEngine/Source/GameNetwork/LANAPI.cpp | 7 ++++-- Core/GameEngine/Source/GameNetwork/NAT.cpp | 4 +++- .../Source/GameNetwork/Transport.cpp | 23 +++++++++---------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index 6ff456626e5..0555fd49637 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -462,8 +462,8 @@ void ConnectionManager::doRelay() { // this is done so we don't have to allocate and delete a packet every time we relay a message. static NetPacket* packet = newInstance(NetPacket); - for (Int i = 0; i < MAX_MESSAGES; ++i) { - if (m_transport->m_inBuffer[i].length != 0) { + for (size_t i = 0; i < ARRAY_SIZE(m_transport->m_inBuffer); ++i) { + if (m_transport->m_inBuffer[i].length > 0) { // This transport buffer has yet to be processed. // make a NetPacket out of this data so it can be broken up into individual commands. @@ -494,6 +494,8 @@ void ConnectionManager::doRelay() { // signal that this has been processed. m_transport->m_inBuffer[i].length = 0; + } else { + break; } } diff --git a/Core/GameEngine/Source/GameNetwork/LANAPI.cpp b/Core/GameEngine/Source/GameNetwork/LANAPI.cpp index 1016d26f66b..8cbfbdea6c5 100644 --- a/Core/GameEngine/Source/GameNetwork/LANAPI.cpp +++ b/Core/GameEngine/Source/GameNetwork/LANAPI.cpp @@ -341,8 +341,7 @@ void LANAPI::update() } // Handle any new messages - int i; - for (i=0; im_inBuffer) && !LANbuttonPushed; ++i) { if (m_transport->m_inBuffer[i].length > 0) { @@ -432,6 +431,10 @@ void LANAPI::update() // Mark it as read m_transport->m_inBuffer[i].length = 0; } + else + { + break; + } } if(LANbuttonPushed) return; diff --git a/Core/GameEngine/Source/GameNetwork/NAT.cpp b/Core/GameEngine/Source/GameNetwork/NAT.cpp index 159d8532cfa..d68470b0505 100644 --- a/Core/GameEngine/Source/GameNetwork/NAT.cpp +++ b/Core/GameEngine/Source/GameNetwork/NAT.cpp @@ -325,7 +325,7 @@ NATConnectionState NAT::connectionUpdate() { m_transport->update(); // check to see if we've been probed. - for (Int i = 0; i < MAX_MESSAGES; ++i) { + for (size_t i = 0; i < ARRAY_SIZE(m_transport->m_inBuffer); ++i) { if (m_transport->m_inBuffer[i].length > 0) { #ifdef DEBUG_LOGGING UnsignedInt ip = m_transport->m_inBuffer[i].addr; @@ -368,6 +368,8 @@ NATConnectionState NAT::connectionUpdate() { PRINTF_IP_AS_4_INTS(ip), m_transport->m_inBuffer[i].port)); m_transport->m_inBuffer[i].length = 0; } + } else { + break; } } diff --git a/Core/GameEngine/Source/GameNetwork/Transport.cpp b/Core/GameEngine/Source/GameNetwork/Transport.cpp index 04dc7d624f5..c6af5c1f6b8 100644 --- a/Core/GameEngine/Source/GameNetwork/Transport.cpp +++ b/Core/GameEngine/Source/GameNetwork/Transport.cpp @@ -211,8 +211,7 @@ Bool Transport::doSend() { } // Send all messages - int i; - for (i=0; iRead(buf, MAX_NETWORK_MESSAGE_LEN, &from)) > 0 ) { @@ -330,7 +330,7 @@ Bool Transport::doRecv() m_incomingPackets[m_statisticsSlot]++; m_incomingBytes[m_statisticsSlot] += len; - for (int i=0; i MAX_PACKET_SIZE) { DEBUG_LOG(("Transport::queueSend - Invalid Packet size")); return false; } - for (i=0; i Date: Tue, 28 Apr 2026 20:46:46 +0200 Subject: [PATCH 6/6] Fixed warnings / errors. --- .../Source/GameNetwork/ConnectionManager.cpp | 4 ++-- Core/GameEngine/Source/GameNetwork/Transport.cpp | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index 0555fd49637..59dc427d8e5 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -477,7 +477,7 @@ void ConnectionManager::doRelay() { NetCommandList *cmdList = packet->getCommandList(); // Iterate through the commands in this packet and send them to the proper connections. - for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd = cmd->getNext()) { + for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd->getNext()) { //DEBUG_LOG(("ConnectionManager::doRelay() - Looking at a command of type %s", //GetNetCommandTypeAsString(cmd->getCommand()->getNetCommandType()))); @@ -500,7 +500,7 @@ void ConnectionManager::doRelay() { } NetCommandList *cmdList = m_netCommandWrapperList->getReadyCommands(); - for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd = cmd->getNext()) { + for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd->getNext()) { if (CommandRequiresAck(cmd->getCommand())) { ackCommand(cmd, m_localSlot); } diff --git a/Core/GameEngine/Source/GameNetwork/Transport.cpp b/Core/GameEngine/Source/GameNetwork/Transport.cpp index c6af5c1f6b8..2c55434db4c 100644 --- a/Core/GameEngine/Source/GameNetwork/Transport.cpp +++ b/Core/GameEngine/Source/GameNetwork/Transport.cpp @@ -336,17 +336,18 @@ Bool Transport::doRecv() // Latency simulation if (m_useLatency) { - if (m_delayedInBuffer[i].message.length == 0) + if (m_delayedInBuffer[bufferIndex].message.length == 0) { // Empty slot; use it - m_delayedInBuffer[i].deliveryTime = + m_delayedInBuffer[bufferIndex].deliveryTime = now + TheGlobalData->m_latencyAverage + (Int)(TheGlobalData->m_latencyAmplitude * sin(now * TheGlobalData->m_latencyPeriod)) + GameClientRandomValue(-TheGlobalData->m_latencyNoise, TheGlobalData->m_latencyNoise); - m_delayedInBuffer[i].message.length = incomingMessage.length; - m_delayedInBuffer[i].message.addr = ntohl(from.sin_addr.S_un.S_addr); - m_delayedInBuffer[i].message.port = ntohs(from.sin_port); - memcpy(&m_delayedInBuffer[i].message, buf, len); + m_delayedInBuffer[bufferIndex].message.length = incomingMessage.length; + m_delayedInBuffer[bufferIndex].message.addr = ntohl(from.sin_addr.S_un.S_addr); + m_delayedInBuffer[bufferIndex].message.port = ntohs(from.sin_port); + memcpy(&m_delayedInBuffer[bufferIndex].message, buf, len); + ++bufferIndex; break; } }