Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Core/GameEngine/Include/GameNetwork/NetPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ class NetPacket;
typedef std::list<NetPacket *> NetPacketList;
typedef std::list<NetPacket *>::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();
Expand All @@ -62,7 +61,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
26 changes: 7 additions & 19 deletions Core/GameEngine/Source/GameNetwork/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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;
}
Expand Down Expand Up @@ -270,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());

Expand Down Expand Up @@ -315,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;
Expand Down
6 changes: 3 additions & 3 deletions Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -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);
Expand All @@ -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);
Expand Down
52 changes: 23 additions & 29 deletions Core/GameEngine/Source/GameNetwork/NetPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,27 @@ 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.
NetCommandMsg *msg = ref->getCommand();

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.
Expand All @@ -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) {
Expand Down
Loading