From 0d627cde91b49e20cdc100861be28f4decfc75e7 Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Wed, 3 Jun 2026 00:00:14 +0200 Subject: [PATCH 1/2] Refactored 'ConstructBigCommandPacketList'. --- .../Include/GameNetwork/NetPacket.h | 2 +- .../Source/GameNetwork/Connection.cpp | 20 ++----- .../Source/GameNetwork/NetPacket.cpp | 52 ++++++++----------- 3 files changed, 28 insertions(+), 46 deletions(-) diff --git a/Core/GameEngine/Include/GameNetwork/NetPacket.h b/Core/GameEngine/Include/GameNetwork/NetPacket.h index cdd498b1ad8..c4f2c99dcb6 100644 --- a/Core/GameEngine/Include/GameNetwork/NetPacket.h +++ b/Core/GameEngine/Include/GameNetwork/NetPacket.h @@ -62,7 +62,7 @@ class NetPacket : public MemoryPoolObject NetCommandList *getCommandList(); static NetCommandRef *ConstructNetCommandMsgFromRawData(const UnsignedByte *data, UnsignedInt dataLength); - static NetPacketList ConstructBigCommandPacketList(NetCommandRef *ref); + static NetCommandList *ConstructBigCommandList(NetCommandRef *ref); UnsignedByte *getData(); Int getLength(); diff --git a/Core/GameEngine/Source/GameNetwork/Connection.cpp b/Core/GameEngine/Source/GameNetwork/Connection.cpp index c1f84e88881..8a482c87b54 100644 --- a/Core/GameEngine/Source/GameNetwork/Connection.cpp +++ b/Core/GameEngine/Source/GameNetwork/Connection.cpp @@ -158,26 +158,14 @@ void Connection::sendNetCommandMsg(NetCommandMsg *msg, UnsignedByte relay) { if (!msgFits) { NetCommandRef *origref = NEW_NETCOMMANDREF(msg); origref->setRelay(relay); - // the message doesn't fit in a single packet, need to split it up. - NetPacketList packetList = NetPacket::ConstructBigCommandPacketList(origref); - NetPacketListIter tempPacketPtr = packetList.begin(); - - while (tempPacketPtr != packetList.end()) { - NetPacket *tempPacket = (*tempPacketPtr); - NetCommandList *list = tempPacket->getCommandList(); - NetCommandRef *ref1 = list->getFirstMessage(); - while (ref1 != nullptr) { - NetCommandRef *ref2 = m_netCommandList->addMessage(ref1->getCommand()); + // the message doesn't fit in a single packet, need to split it up. + if (NetCommandList* list = NetPacket::ConstructBigCommandList(origref)) { + for (NetCommandRef* ref1 = list->getFirstMessage(); ref1 != nullptr; ref1 = ref1->getNext()) { + NetCommandRef* ref2 = m_netCommandList->addMessage(ref1->getCommand()); ref2->setRelay(relay); - - ref1 = ref1->getNext(); } - deleteInstance(tempPacket); - tempPacket = nullptr; - ++tempPacketPtr; - deleteInstance(list); list = nullptr; } diff --git a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp index c04446d7e51..1b24f9ae673 100644 --- a/Core/GameEngine/Source/GameNetwork/NetPacket.cpp +++ b/Core/GameEngine/Source/GameNetwork/NetPacket.cpp @@ -71,7 +71,8 @@ NetCommandRef *NetPacket::ConstructNetCommandMsgFromRawData(const UnsignedByte * return ref; } -NetPacketList NetPacket::ConstructBigCommandPacketList(NetCommandRef *ref) { +NetCommandList *NetPacket::ConstructBigCommandList(NetCommandRef *ref) +{ // if we don't have a unique command ID, then the wrapped command cannot // be identified. Therefore don't allow commands without a unique ID to // be wrapped. @@ -79,19 +80,18 @@ NetPacketList NetPacket::ConstructBigCommandPacketList(NetCommandRef *ref) { if (!DoesCommandRequireACommandID(msg->getNetCommandType())) { DEBUG_CRASH(("Trying to wrap a command that doesn't have a unique command ID")); - return NetPacketList(); + return nullptr; } UnsignedInt bufferSize = GetBufferSizeNeededForCommand(msg); // need to implement. I have a drinking problem. - UnsignedByte *bigPacketData = nullptr; - - NetPacketList packetList; + UnsignedByte* bigPacketData = NEW UnsignedByte[bufferSize]; // create the buffer for the huge message and fill the buffer with that message. - UnsignedInt bigPacketCurrentOffset = 0; - bigPacketData = NEW UnsignedByte[bufferSize]; ref->getCommand()->copyBytesForNetPacket(bigPacketData, *ref); + NetCommandList* commandList = newInstance(NetCommandList); + commandList->init(); + // create the wrapper command message we'll be using. NetWrapperCommandMsg *wrapperMsg = newInstance(NetWrapperCommandMsg); // get the amount of space needed for the wrapper message, not including the wrapped command data. @@ -102,55 +102,49 @@ NetPacketList NetPacket::ConstructBigCommandPacketList(NetCommandRef *ref) { if ((bufferSize % commandSizePerPacket) > 0) { ++numChunks; } + UnsignedInt currentChunk = 0; + UnsignedInt bigPacketCurrentOffset = 0; - // create the packets and the wrapper messages. while (currentChunk < numChunks) { - NetPacket *packet = newInstance(NetPacket); - - UnsignedInt dataSizeThisPacket = commandSizePerPacket; - if ((bufferSize - bigPacketCurrentOffset) < dataSizeThisPacket) { - dataSizeThisPacket = bufferSize - bigPacketCurrentOffset; + UnsignedInt dataSizeThisChunk = commandSizePerPacket; + if ((bufferSize - bigPacketCurrentOffset) < dataSizeThisChunk) { + dataSizeThisChunk = bufferSize - bigPacketCurrentOffset; } - NetCommandDataChunk bigPacket(dataSizeThisPacket); - memcpy(bigPacket.data(), bigPacketData + bigPacketCurrentOffset, bigPacket.size()); + + NetCommandDataChunk chunkData(dataSizeThisChunk); + memcpy(chunkData.data(), bigPacketData + bigPacketCurrentOffset, chunkData.size()); if (DoesCommandRequireACommandID(wrapperMsg->getNetCommandType())) { wrapperMsg->setID(GenerateNextCommandID()); } + wrapperMsg->setPlayerID(msg->getPlayerID()); wrapperMsg->setExecutionFrame(msg->getExecutionFrame()); wrapperMsg->setChunkNumber(currentChunk); wrapperMsg->setNumChunks(numChunks); wrapperMsg->setDataOffset(bigPacketCurrentOffset); - wrapperMsg->setData(bigPacket); + wrapperMsg->setData(chunkData); wrapperMsg->setTotalDataLength(bufferSize); wrapperMsg->setWrappedCommandID(msg->getID()); - bigPacketCurrentOffset += dataSizeThisPacket; + bigPacketCurrentOffset += dataSizeThisChunk; - NetCommandRef *ref = NEW_NETCOMMANDREF(wrapperMsg); - ref->setRelay(ref->getRelay()); - - if (packet->addCommand(ref) == FALSE) { - DEBUG_LOG_LEVEL(DEBUG_LEVEL_NET, ("NetPacket::BeginBigCommandPacketList - failed to add a wrapper command to the packet")); // I still have a drinking problem. - } - - packetList.push_back(packet); - - deleteInstance(ref); - ref = nullptr; + NetCommandRef* chunkRef = NEW_NETCOMMANDREF(wrapperMsg); + chunkRef->setRelay(ref->getRelay()); + commandList->addMessage(chunkRef); ++currentChunk; } + wrapperMsg->detach(); wrapperMsg = nullptr; delete[] bigPacketData; bigPacketData = nullptr; - return packetList; + return commandList; } UnsignedInt NetPacket::GetBufferSizeNeededForCommand(NetCommandMsg *msg) { From b83c38ec25c7e795609a221b076d1cfe4ca524f6 Mon Sep 17 00:00:00 2001 From: Caball009 <82909616+Caball009@users.noreply.github.com> Date: Wed, 3 Jun 2026 00:01:42 +0200 Subject: [PATCH 2/2] Removed memory pool for 'NetPacket'. --- Core/GameEngine/Include/GameNetwork/NetPacket.h | 5 ++--- .../Source/Common/System/GameMemoryInitPools_Generals.inl | 1 - .../Source/Common/System/GameMemoryInitPools_GeneralsMD.inl | 1 - Core/GameEngine/Source/GameNetwork/Connection.cpp | 6 +++--- Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp | 6 +++--- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Core/GameEngine/Include/GameNetwork/NetPacket.h b/Core/GameEngine/Include/GameNetwork/NetPacket.h index c4f2c99dcb6..1e0c66ab4c5 100644 --- a/Core/GameEngine/Include/GameNetwork/NetPacket.h +++ b/Core/GameEngine/Include/GameNetwork/NetPacket.h @@ -45,13 +45,12 @@ class NetPacket; typedef std::list NetPacketList; typedef std::list::iterator NetPacketListIter; -class NetPacket : public MemoryPoolObject +class NetPacket { - MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetPacket, "NetPacket") public: NetPacket(); NetPacket(TransportMessage *msg); - //virtual ~NetPacket(); + ~NetPacket(); void init(); void reset(); diff --git a/Core/GameEngine/Source/Common/System/GameMemoryInitPools_Generals.inl b/Core/GameEngine/Source/Common/System/GameMemoryInitPools_Generals.inl index d8177c459d4..36080f71985 100644 --- a/Core/GameEngine/Source/Common/System/GameMemoryInitPools_Generals.inl +++ b/Core/GameEngine/Source/Common/System/GameMemoryInitPools_Generals.inl @@ -522,7 +522,6 @@ static PoolSizeRec PoolSizes[] = { "Campaign", 32, 32 }, { "Mission", 32, 32 }, { "ModalWindow", 32, 32 }, - { "NetPacket", 32, 32 }, { "AISideInfo", 32, 32 }, { "AISideBuildList", 32, 32 }, { "MetaMapRec", 256, 32 }, diff --git a/Core/GameEngine/Source/Common/System/GameMemoryInitPools_GeneralsMD.inl b/Core/GameEngine/Source/Common/System/GameMemoryInitPools_GeneralsMD.inl index f9f5b5a0a18..f33873f3fa1 100644 --- a/Core/GameEngine/Source/Common/System/GameMemoryInitPools_GeneralsMD.inl +++ b/Core/GameEngine/Source/Common/System/GameMemoryInitPools_GeneralsMD.inl @@ -518,7 +518,6 @@ static PoolSizeRec PoolSizes[] = { "Campaign", 32, 32 }, { "Mission", 88, 32 }, { "ModalWindow", 32, 32 }, - { "NetPacket", 32, 32 }, { "AISideInfo", 32, 32 }, { "AISideBuildList", 32, 32 }, { "MetaMapRec", 256, 32 }, diff --git a/Core/GameEngine/Source/GameNetwork/Connection.cpp b/Core/GameEngine/Source/GameNetwork/Connection.cpp index 8a482c87b54..518eba520ed 100644 --- a/Core/GameEngine/Source/GameNetwork/Connection.cpp +++ b/Core/GameEngine/Source/GameNetwork/Connection.cpp @@ -135,7 +135,7 @@ void Connection::sendNetCommandMsg(NetCommandMsg *msg, UnsignedByte relay) { // 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); + packet = new(NetPacket); } @@ -258,7 +258,7 @@ UnsignedInt Connection::doSend() { NetCommandRef *msg = m_netCommandList->getFirstMessage(); while ((msg != nullptr) && couldQueue) { - NetPacket *packet = newInstance(NetPacket); + NetPacket *packet = new(NetPacket); packet->init(); packet->setAddress(m_user->GetIPAddr(), m_user->GetPort()); @@ -303,7 +303,7 @@ UnsignedInt Connection::doSend() { m_lastTimeSent = curtime; } - deleteInstance(packet); // delete the packet now that we're done with it. + delete 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 9ea8927d696..102eb71550c 100644 --- a/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp +++ b/Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp @@ -469,7 +469,7 @@ void ConnectionManager::doRelay() { // 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 = new(NetPacket)(&(m_transport->m_inBuffer[i])); //DEBUG_LOG(("ConnectionManager::doRelay() - got a packet with %d commands", packet->getNumCommands())); //LOGBUFFER( packet->getData(), packet->getLength() ); @@ -495,7 +495,7 @@ void ConnectionManager::doRelay() { ++numPackets; // Delete this packet since we won't be needing it anymore. - deleteInstance(packet); + delete packet; packet = nullptr; deleteInstance(cmdList); @@ -522,7 +522,7 @@ void ConnectionManager::doRelay() { ++numPackets; // Delete this packet since we won't be needing it anymore. - deleteInstance(packet); + delete packet; packet = nullptr; deleteInstance(cmdList);