From 87c1bc549167b57f1f26e8ec79d5f55935b8a4dd Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 3 Jan 2011 02:16:00 +0000 Subject: [PATCH] - bugfix for socket issue mentioned today in the forums by PT --- source/glest_game/network/network_message.cpp | 65 +++++++------------ 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index fbc83338f..e787106f2 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -326,15 +326,14 @@ bool NetworkMessageCommandList::addCommand(const NetworkCommand* networkCommand) bool NetworkMessageCommandList::receive(Socket* socket) { // _peek_ type, commandCount & frame num first. - for(int peekAttempt = 1; peekAttempt < 2000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + const double MAX_MSG_WAIT_SECONDS = 3; + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data, commandListHeaderSize) == true) { break; } - //else { - // sleep(1); // sleep 1 ms to wait for socket data - //} } if (NetworkMessage::peek(socket, &data, commandListHeaderSize) == false) { @@ -349,16 +348,13 @@ bool NetworkMessageCommandList::receive(Socket* socket) { // read header + data.commandCount commands. int totalMsgSize = commandListHeaderSize + (sizeof(NetworkCommand) * data.header.commandCount); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); // _peek_ type, commandCount & frame num first. - for(int peekAttempt = 1; peekAttempt < 2000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); - + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data, totalMsgSize) == true) { break; } - //else { - // sleep(1); // sleep 1 ms to wait for socket data - //} } @@ -583,15 +579,13 @@ bool NetworkMessageSynchNetworkGameData::receive(Socket* socket) { data.header.techCRCFileCount = 0; - for(int peekAttempt = 1; peekAttempt < 5000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); + const double MAX_MSG_WAIT_SECONDS = 10; + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data, HeaderSize) == true) { break; } - else { - sleep(1); // sleep 1 ms to wait for socket data - } } if (NetworkMessage::peek(socket, &data, HeaderSize) == false) { @@ -629,15 +623,11 @@ bool NetworkMessageSynchNetworkGameData::receive(Socket* socket) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] iPacketLoop = %d, packetIndex = %d, maxFileCountPerPacket = %d, packetFileCount = %d, packetDetail1DataSize = %d, packetDetail2DataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,iPacketLoop,packetIndex,maxFileCountPerPacket,packetFileCount,packetDetail1DataSize,packetDetail2DataSize); - for(int peekAttempt = 1; peekAttempt < 5000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); - + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data.detail.techCRCFileList[packetIndex], packetDetail1DataSize) == true) { break; } - else { - sleep(1); // sleep 1 ms to wait for socket data - } } result = NetworkMessage::receive(socket, &data.detail.techCRCFileList[packetIndex], packetDetail1DataSize); @@ -647,14 +637,11 @@ bool NetworkMessageSynchNetworkGameData::receive(Socket* socket) { //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] data.detail.techCRCFileList[i] = [%s]\n",__FILE__,__FUNCTION__,__LINE__,data.detail.techCRCFileList[i].getString().c_str()); } - for(int peekAttempt = 1; peekAttempt < 5000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data.detail.techCRCFileCRCList[packetIndex], packetDetail2DataSize) == true) { break; } - else { - sleep(1); // sleep 1 ms to wait for socket data - } } result = NetworkMessage::receive(socket, &data.detail.techCRCFileCRCList[packetIndex], packetDetail2DataSize); @@ -776,14 +763,13 @@ bool NetworkMessageSynchNetworkGameDataStatus::receive(Socket* socket) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] about to get nmtSynchNetworkGameDataStatus\n",__FILE__,__FUNCTION__,__LINE__); data.header.techCRCFileCount = 0; - for(int peekAttempt = 1; peekAttempt < 5000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); + const double MAX_MSG_WAIT_SECONDS = 3; + + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data, HeaderSize) == true) { break; } - else { - sleep(1); // sleep 1 ms to wait for socket data - } } if (NetworkMessage::peek(socket, &data, HeaderSize) == false) { @@ -814,15 +800,11 @@ bool NetworkMessageSynchNetworkGameDataStatus::receive(Socket* socket) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] iPacketLoop = %d, packetIndex = %d, packetFileCount = %d\n",__FILE__,__FUNCTION__,__LINE__,iPacketLoop,packetIndex,packetFileCount); - for(int peekAttempt = 1; peekAttempt < 5000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); - + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data.detail.techCRCFileList[packetIndex], (DetailSize1 * packetFileCount)) == true) { break; } - else { - sleep(1); // sleep 1 ms to wait for socket data - } } result = NetworkMessage::receive(socket, &data.detail.techCRCFileList[packetIndex], (DetailSize1 * packetFileCount)); @@ -832,14 +814,11 @@ bool NetworkMessageSynchNetworkGameDataStatus::receive(Socket* socket) { //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] data.detail.techCRCFileList[i] = [%s]\n",__FILE__,__FUNCTION__,__LINE__,data.detail.techCRCFileList[i].getString().c_str()); } - for(int peekAttempt = 1; peekAttempt < 5000; peekAttempt++) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] peekAttempt = %d\n",__FILE__,__FUNCTION__,__LINE__,peekAttempt); + // Wait a max of x seconds for this message + for(time_t elapsedWait = time(NULL); difftime(time(NULL),elapsedWait) <= MAX_MSG_WAIT_SECONDS;) { if (NetworkMessage::peek(socket, &data.detail.techCRCFileCRCList[packetIndex], (DetailSize2 * packetFileCount)) == true) { break; } - else { - sleep(1); // sleep 1 ms to wait for socket data - } } result = NetworkMessage::receive(socket, &data.detail.techCRCFileCRCList[packetIndex], (DetailSize2 * packetFileCount));