From f9516210a6877be61137812dda5d1ebb79210d88 Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Mon, 13 Jul 2026 16:21:46 +0200 Subject: [PATCH 1/4] Removed unused static (packet and command count) variables. --- Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index 9ea8927d696..2f9e248e9c0 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -459,9 +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; for (Int i = 0; i < MAX_MESSAGES; ++i) { @@ -489,10 +486,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 +510,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 720a3e70487958da4dcb7791a2aab3277cf91c9b Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Mon, 13 Jul 2026 16:22:58 +0200 Subject: [PATCH 2/4] 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 2f9e248e9c0..fb8d10d8ef8 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -473,19 +473,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->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(); } // Delete this packet since we won't be needing it anymore. @@ -501,15 +500,13 @@ void ConnectionManager::doRelay() { } NetCommandList *cmdList = m_netCommandWrapperList->getReadyCommands(); - NetCommandRef *cmd = cmdList->getFirstMessage(); - while (cmd != nullptr) { + for (NetCommandRef* cmd = cmdList->getFirstMessage(); cmd; cmd = cmd->getNext()) { if (CommandRequiresAck(cmd->getCommand())) { ackCommand(cmd, m_localSlot); } if (!processNetCommand(cmd)) { sendRemoteCommand(cmd); } - cmd = cmd->getNext(); } // Delete this packet since we won't be needing it anymore. From 404327d0fa19fedc4728f6c6135e044cc897704b Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Mon, 13 Jul 2026 17:02:00 +0200 Subject: [PATCH 3/4] Improved loop logic performance. --- .../Source/GameNetwork/ConnectionManager.cpp | 4 +- Core/GameEngine/Source/GameNetwork/LANAPI.cpp | 7 +- Core/GameEngine/Source/GameNetwork/NAT.cpp | 4 +- .../Source/GameNetwork/Transport.cpp | 71 ++++++++++--------- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index fb8d10d8ef8..b123da9cdd9 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -461,7 +461,7 @@ void ConnectionManager::destroyGameMessages() { void ConnectionManager::doRelay() { NetPacket *packet = nullptr; - 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) { // This transport buffer has yet to be processed. @@ -496,6 +496,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..8f888ddf182 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,43 +333,47 @@ Bool Transport::doRecv() m_incomingPackets[m_statisticsSlot]++; m_incomingBytes[m_statisticsSlot] += len; - for (int i=0; im_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; } } - else - { + + continue; + } #endif - if (m_inBuffer[i].length == 0) - { - // Empty slot; use it - m_inBuffer[i].length = incomingMessage.length; - m_inBuffer[i].addr = ntohl(from.sin_addr.S_un.S_addr); - m_inBuffer[i].port = ntohs(from.sin_port); - memcpy(&m_inBuffer[i], buf, len); - break; - } -#if defined(RTS_DEBUG) + + for (; bufferIndex < ARRAY_SIZE(m_inBuffer); ++bufferIndex) + { + if (m_inBuffer[bufferIndex].length == 0) + { + // Empty slot; use it + m_inBuffer[bufferIndex].length = incomingMessage.length; + m_inBuffer[bufferIndex].addr = ntohl(from.sin_addr.S_un.S_addr); + m_inBuffer[bufferIndex].port = ntohs(from.sin_port); + memcpy(&m_inBuffer[bufferIndex], buf, len); + ++bufferIndex; + break; } -#endif } - //DEBUG_ASSERTCRASH(i MAX_PACKET_SIZE) { DEBUG_LOG(("Transport::queueSend - Invalid Packet size")); return false; } - for (i=0; i Date: Mon, 13 Jul 2026 17:03:17 +0200 Subject: [PATCH 4/4] Used '>' and '<=' instead of '==' and '!='. --- .../Source/GameNetwork/ConnectionManager.cpp | 2 +- Core/GameEngine/Source/GameNetwork/Transport.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp index b123da9cdd9..affe2449585 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -462,7 +462,7 @@ void ConnectionManager::doRelay() { NetPacket *packet = nullptr; for (size_t i = 0; i < ARRAY_SIZE(m_transport->m_inBuffer); ++i) { - if (m_transport->m_inBuffer[i].length != 0) { + 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. diff --git a/Core/GameEngine/Source/GameNetwork/Transport.cpp b/Core/GameEngine/Source/GameNetwork/Transport.cpp index 8f888ddf182..cbdbcb88222 100644 --- a/Core/GameEngine/Source/GameNetwork/Transport.cpp +++ b/Core/GameEngine/Source/GameNetwork/Transport.cpp @@ -213,7 +213,7 @@ Bool Transport::doSend() { // Send all messages for (size_t i = 0; i < ARRAY_SIZE(m_outBuffer); ++i) { - if (m_outBuffer[i].length != 0) + if (m_outBuffer[i].length > 0) { int bytesSent = 0; // TheSuperHackers @info The handling of data sizing of the payload within a UDP packet is confusing due to the current networking implementation @@ -252,11 +252,11 @@ Bool Transport::doSend() { for (size_t i = 0; i < ARRAY_SIZE(m_delayedInBuffer); ++i) { - if (m_delayedInBuffer[i].message.length != 0 && m_delayedInBuffer[i].deliveryTime <= now) + if (m_delayedInBuffer[i].message.length > 0 && m_delayedInBuffer[i].deliveryTime <= now) { for (; bufferIndex < ARRAY_SIZE(m_inBuffer); ++bufferIndex) { - if (m_inBuffer[bufferIndex].length == 0) + if (m_inBuffer[bufferIndex].length <= 0) { // Empty slot; use it memcpy(&m_inBuffer[bufferIndex], &m_delayedInBuffer[i].message, sizeof(TransportMessage)); @@ -341,7 +341,7 @@ Bool Transport::doRecv() { for (; bufferIndex < ARRAY_SIZE(m_delayedInBuffer); ++bufferIndex) { - if (m_delayedInBuffer[bufferIndex].message.length == 0) + if (m_delayedInBuffer[bufferIndex].message.length <= 0) { // Empty slot; use it m_delayedInBuffer[bufferIndex].deliveryTime = @@ -363,7 +363,7 @@ Bool Transport::doRecv() for (; bufferIndex < ARRAY_SIZE(m_inBuffer); ++bufferIndex) { - if (m_inBuffer[bufferIndex].length == 0) + if (m_inBuffer[bufferIndex].length <= 0) { // Empty slot; use it m_inBuffer[bufferIndex].length = incomingMessage.length; @@ -396,7 +396,7 @@ Bool Transport::queueSend(UnsignedInt addr, UnsignedShort port, const UnsignedBy for (size_t i = 0; i < ARRAY_SIZE(m_outBuffer); ++i) { - if (m_outBuffer[i].length == 0) + if (m_outBuffer[i].length <= 0) { // Insert data here m_outBuffer[i].length = len;