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
14 changes: 4 additions & 10 deletions Core/GameEngine/Include/GameNetwork/NetPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,12 @@ be specialized code.
#include "Common/MessageStream.h"
#include "Common/GameMemory.h"

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(const TransportMessage& msg);
~NetPacket();

void init();
void reset();
Expand All @@ -62,7 +56,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
59 changes: 17 additions & 42 deletions Core/GameEngine/Source/GameNetwork/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,59 +131,37 @@ 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) {
NetPacket packet;

// 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
// one wrapper command at a time.
packet->reset();

NetCommandRef *tempref = NEW_NETCOMMANDREF(msg);
if (packet.addCommand(tempref)) {
deleteInstance(tempref);
tempref = nullptr;
} else {
tempref->setRelay(relay);

Bool msgFits = packet->addCommand(tempref);
deleteInstance(tempref); // delete the temporary reference.
tempref = nullptr;

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());
if (NetCommandList* list = NetPacket::ConstructBigCommandList(tempref)) {
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;
}

deleteInstance(origref);
origref = nullptr;
deleteInstance(tempref);
tempref = nullptr;

return;
}
Expand Down Expand Up @@ -270,9 +248,8 @@ UnsignedInt Connection::doSend() {
NetCommandRef *msg = m_netCommandList->getFirstMessage();

while ((msg != nullptr) && couldQueue) {
NetPacket *packet = newInstance(NetPacket);
packet->init();
packet->setAddress(m_user->GetIPAddr(), m_user->GetPort());
NetPacket packet;
packet.setAddress(m_user->GetIPAddr(), m_user->GetPort());

Bool notDone = TRUE;

Expand All @@ -283,7 +260,7 @@ UnsignedInt Connection::doSend() {
time_t timeLastSent = msg->getTimeLastSent();

if (((curtime - timeLastSent) > m_retryTime) || (timeLastSent == -1)) {
notDone = packet->addCommand(msg);
notDone = packet.addCommand(msg);
if (notDone) {
// the msg command was added to the packet.
if (CommandRequiresAck(msg->getCommand())) {
Expand All @@ -308,14 +285,12 @@ UnsignedInt Connection::doSend() {
++numpackets;

/// @todo Make the act of giving the transport object a packet to send more efficient. Make the transport take a NetPacket object rather than the raw data, thus avoiding an extra memcpy.
if (packet->getNumCommands() > 0) {
if (packet.getNumCommands() > 0) {
// If the packet actually has any information to give, give it to the transport object
// for transmission.
couldQueue = m_transport->queueSend(packet->getAddr(), packet->getPort(), packet->getData(), packet->getLength());
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;
Expand Down
18 changes: 4 additions & 14 deletions Core/GameEngine/Source/GameNetwork/ConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,20 +462,18 @@ void ConnectionManager::doRelay() {
static Int numPackets = 0;
static Int numCommands = 0;

NetPacket *packet = nullptr;

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]));
NetPacket packet(m_transport->m_inBuffer[i]);

//DEBUG_LOG(("ConnectionManager::doRelay() - got a packet with %d commands", packet->getNumCommands()));
//LOGBUFFER( packet->getData(), packet->getLength() );
//DEBUG_LOG(("ConnectionManager::doRelay() - got a packet with %d commands", packet.getNumCommands()));
//LOGBUFFER( packet.getData(), packet.getLength() );

// Get the command list from the packet.
NetCommandList *cmdList = packet->getCommandList();
NetCommandList *cmdList = packet.getCommandList();
NetCommandRef *cmd = cmdList->getFirstMessage();

// Iterate through the commands in this packet and send them to the proper connections.
Expand All @@ -494,10 +492,6 @@ void ConnectionManager::doRelay() {
}
++numPackets;

// Delete this packet since we won't be needing it anymore.
deleteInstance(packet);
packet = nullptr;

deleteInstance(cmdList);
cmdList = nullptr;

Expand All @@ -521,10 +515,6 @@ void ConnectionManager::doRelay() {
}
++numPackets;

// Delete this packet since we won't be needing it anymore.
deleteInstance(packet);
packet = nullptr;

deleteInstance(cmdList);
cmdList = nullptr;
}
Expand Down
63 changes: 29 additions & 34 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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this call necessary?


// 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;

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.
}
bigPacketCurrentOffset += dataSizeThisChunk;

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;
Comment thread
Caball009 marked this conversation as resolved.
}

UnsignedInt NetPacket::GetBufferSizeNeededForCommand(NetCommandMsg *msg) {
Expand All @@ -173,13 +167,14 @@ 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);

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;
}

/**
Expand Down
Loading