From 82c9002d0af8b493454f68a231c04f6fcca37a29 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 25 Nov 2011 20:03:07 +0000 Subject: [PATCH] - attempt to further stabilize cpu usage and network reliability --- .../glest_game/network/client_interface.cpp | 6 +- source/glest_game/network/connection_slot.cpp | 83 ++++++++++++------- source/glest_game/network/connection_slot.h | 9 +- .../glest_game/network/network_interface.cpp | 19 +++-- source/glest_game/network/network_interface.h | 2 +- .../glest_game/network/server_interface.cpp | 2 +- .../include/platform/posix/socket.h | 4 + .../sources/platform/posix/socket.cpp | 42 ++++++++++ 8 files changed, 118 insertions(+), 49 deletions(-) diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 9f75a9d0a..31d25a66a 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -175,7 +175,7 @@ std::string ClientInterface::getServerIpAddress() { } void ClientInterface::updateLobby() { - NetworkMessageType networkMessageType = getNextMessageType(); + NetworkMessageType networkMessageType = getNextMessageType(0); switch(networkMessageType) { case nmtInvalid: @@ -725,7 +725,7 @@ void ClientInterface::waitUntilReady(Checksum* checksum) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); return; } - NetworkMessageType networkMessageType = getNextMessageType(); + NetworkMessageType networkMessageType = getNextMessageType(0); // consume old messages from the lobby bool discarded = shouldDiscardNetworkMessage(networkMessageType); @@ -1038,7 +1038,7 @@ NetworkMessageType ClientInterface::waitForMessage() NetworkMessageType msg = nmtInvalid; //uint64 waitLoopCount = 0; while(msg == nmtInvalid) { - msg = getNextMessageType(); + msg = getNextMessageType(200); if(msg == nmtInvalid) { if(chrono.getMillis() % 250 == 0 && isConnected() == false) { if(quit == false) { diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index 751c965e2..dcc869f3c 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -215,13 +215,15 @@ void ConnectionSlotThread::execute() { safeMutex.ReleaseLock(); } */ - if(this->slot != NULL) { + bool slotconnected = false; + //if(this->slot != NULL) { + { ConnectionSlotEvent event; event.triggerId = this->slotIndex; event.socketTriggered = true; ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); - this->slot->updateSlot(&event); + slotconnected = this->slot->updateSlot(&event); } if(getQuitStatus() == true) { @@ -229,9 +231,14 @@ void ConnectionSlotThread::execute() { break; } - if(chrono.getMillis() % 300 == 0) { - sleep(1); + if(slotconnected == false) { + if(chrono.getMillis() % 5 == 0) { + sleep(25); + } } + //else { + // this->slot->hasDataToReadWithWait(250); + //} } if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -306,7 +313,7 @@ ConnectionSlot::~ConnectionSlot() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__); } -void ConnectionSlot::updateSlot(ConnectionSlotEvent *event) { +bool ConnectionSlot::updateSlot(ConnectionSlotEvent *event) { Chrono chrono; if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); @@ -326,7 +333,7 @@ void ConnectionSlot::updateSlot(ConnectionSlotEvent *event) { //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] MUTEX LOCK held for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); - this->update(checkForNewClients,event->triggerId); + bool slotconnected = this->update(checkForNewClients,event->triggerId); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took %lld msecs\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); @@ -342,11 +349,14 @@ void ConnectionSlot::updateSlot(ConnectionSlotEvent *event) { //} if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took %lld msecs\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + + return slotconnected; } -void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { +bool ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { //Chrono chrono; //chrono.start(); + pair socketInfo; try { clearThreadErrorList(); @@ -356,7 +366,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); - pair socketInfo = this->getSocketInfo(); + socketInfo = this->getSocketInfo(); if(socketInfo.second == NULL) { if(networkGameDataSynchCheckOkMap) networkGameDataSynchCheckOkMap = false; if(networkGameDataSynchCheckOkTile) networkGameDataSynchCheckOkTile = false; @@ -487,10 +497,10 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { //this->clearChatInfo(); bool gotTextMsg = true; - for(;this->hasDataToRead() == true && gotTextMsg == true;) { + for(;this->hasDataToReadWithWait(250) == true && gotTextMsg == true;) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] polling for networkMessageType...\n",__FILE__,__FUNCTION__,__LINE__); - NetworkMessageType networkMessageType= getNextMessageType(); + NetworkMessageType networkMessageType= getNextMessageType(200); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] networkMessageType = %d\n",__FILE__,__FUNCTION__,__LINE__,networkMessageType); @@ -518,7 +528,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } //} } @@ -539,14 +549,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -575,14 +585,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -610,7 +620,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); close(); - return; + return false; } else { //check consistency @@ -648,7 +658,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); close(); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); - return; + return false; } } @@ -669,7 +679,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -686,7 +696,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); close(); - return; + return false; } NetworkMessageLaunch networkMessageLaunch; @@ -722,14 +732,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -847,14 +857,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -876,14 +886,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -909,14 +919,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } break; @@ -955,14 +965,14 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } break; @@ -985,7 +995,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { //sendTextMessage(sErr,-1); //DisplayErrorMessage(sErr); threadErrorList.push_back(sErr); - return; + return socketInfo.first; } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got invalid message type before intro, disconnecting socket.\n",__FILE__,__FUNCTION__,__LINE__); @@ -993,7 +1003,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - return; + return false; } } } @@ -1025,6 +1035,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); } + return socketInfo.first; //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); } @@ -1197,4 +1208,14 @@ bool ConnectionSlot::hasDataToRead() { return result; } +bool ConnectionSlot::hasDataToReadWithWait(int waitMilliseconds) { + bool result = false; + MutexSafeWrapper safeMutexSlot(&mutexSocket,CODE_AT_LINE); + + if(socket != NULL && socket->hasDataToRead(waitMilliseconds) == true) { + result = true; + } + return result; +} + }}//end namespace diff --git a/source/glest_game/network/connection_slot.h b/source/glest_game/network/connection_slot.h index 4b6e3ce28..5cc2a83bb 100644 --- a/source/glest_game/network/connection_slot.h +++ b/source/glest_game/network/connection_slot.h @@ -136,7 +136,7 @@ public: ConnectionSlot(ServerInterface* serverInterface, int playerIndex); ~ConnectionSlot(); - void update(bool checkForNewClients,int lockedSlotIndex); + bool update(bool checkForNewClients,int lockedSlotIndex); void setPlayerIndex(int value) { playerIndex = value; } int getPlayerIndex() const {return playerIndex;} @@ -191,11 +191,14 @@ public: time_t getConnectedTime() const { return connectedTime; } int getSessionKey() const { return sessionKey; } - void updateSlot(ConnectionSlotEvent *event); + bool updateSlot(ConnectionSlotEvent *event); virtual bool isConnected(); PLATFORM_SOCKET getSocketId(); + bool hasDataToRead(); + bool hasDataToReadWithWait(int waitMilliseconds); + protected: Mutex * getServerSynchAccessor(); @@ -205,8 +208,6 @@ protected: void setSocket(Socket *newSocket); void deleteSocket(); virtual void update() {} - - bool hasDataToRead(); }; }}//end namespace diff --git a/source/glest_game/network/network_interface.cpp b/source/glest_game/network/network_interface.cpp index 44bbadb43..2b5d6e9fe 100644 --- a/source/glest_game/network/network_interface.cpp +++ b/source/glest_game/network/network_interface.cpp @@ -44,25 +44,26 @@ void NetworkInterface::sendMessage(const NetworkMessage* networkMessage){ networkMessage->send(socket); } -NetworkMessageType NetworkInterface::getNextMessageType() +NetworkMessageType NetworkInterface::getNextMessageType(int waitMilliseconds) { Socket* socket= getSocket(false); int8 messageType= nmtInvalid; if(socket != NULL && - socket->hasDataToRead() == true) { + socket->hasDataToReadWithWait(waitMilliseconds) == true) { + //peek message type - int dataSize = socket->getDataToRead(); - if(dataSize >= sizeof(messageType)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,dataSize); + //int dataSize = socket->getDataToRead(); + //if(dataSize >= sizeof(messageType)) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,dataSize); int iPeek = socket->peek(&messageType, sizeof(messageType)); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",__FILE__,__FUNCTION__,__LINE__,iPeek,messageType,sizeof(messageType)); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,messageType,sizeof(messageType),dataSize); - } + //} + //else { + // if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,messageType,sizeof(messageType),dataSize); + //} //sanity check new message type if(messageType < 0 || messageType >= nmtCount) { diff --git a/source/glest_game/network/network_interface.h b/source/glest_game/network/network_interface.h index 6096d81e3..1b09893d4 100644 --- a/source/glest_game/network/network_interface.h +++ b/source/glest_game/network/network_interface.h @@ -121,7 +121,7 @@ public: string getHostName() const {return Socket::getHostName();} virtual void sendMessage(const NetworkMessage* networkMessage); - NetworkMessageType getNextMessageType(); + NetworkMessageType getNextMessageType(int waitMilliseconds=100); bool receiveMessage(NetworkMessage* networkMessage); virtual bool isConnected(); diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index b687b278e..4c743f13b 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -1343,7 +1343,7 @@ void ServerInterface::waitUntilReady(Checksum *checksum) { ConnectionSlot* connectionSlot= slots[i]; if(connectionSlot != NULL && connectionSlot->isConnected() == true) { if(connectionSlot->isReady() == false) { - NetworkMessageType networkMessageType= connectionSlot->getNextMessageType(); + NetworkMessageType networkMessageType= connectionSlot->getNextMessageType(0); // consume old messages from the lobby bool discarded = shouldDiscardNetworkMessage(networkMessageType,connectionSlot); diff --git a/source/shared_lib/include/platform/posix/socket.h b/source/shared_lib/include/platform/posix/socket.h index 7dce84e4a..3a6f58c92 100644 --- a/source/shared_lib/include/platform/posix/socket.h +++ b/source/shared_lib/include/platform/posix/socket.h @@ -139,6 +139,10 @@ public: static bool hasDataToRead(std::map &socketTriggeredList); static bool hasDataToRead(PLATFORM_SOCKET socket); bool hasDataToRead(); + + static bool hasDataToReadWithWait(PLATFORM_SOCKET socket,int waitMilliseconds); + bool hasDataToReadWithWait(int waitMilliseconds); + virtual void disconnectSocket(); PLATFORM_SOCKET getSocketId() const { return sock; } diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 68c5cbf1c..d365ff3f5 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -1020,6 +1020,48 @@ bool Socket::hasDataToRead(PLATFORM_SOCKET socket) return bResult; } +bool Socket::hasDataToReadWithWait(int waitMilliseconds) +{ + return Socket::hasDataToReadWithWait(sock,waitMilliseconds) ; +} + +bool Socket::hasDataToReadWithWait(PLATFORM_SOCKET socket,int waitMilliseconds) +{ + bool bResult = false; + + Chrono chono; + chono.start(); + if(Socket::isSocketValid(&socket) == true) + { + fd_set rfds; + struct timeval tv; + + /* Watch stdin (fd 0) to see when it has input. */ + FD_ZERO(&rfds); + FD_SET(socket, &rfds); + + /* Wait up to 0 seconds. */ + tv.tv_sec = 0; + tv.tv_usec = 1000 * waitMilliseconds; + + int retval = 0; + { + //MutexSafeWrapper safeMutex(&dataSynchAccessor); + retval = select((int)socket + 1, &rfds, NULL, NULL, &tv); + } + if(retval) + { + if (FD_ISSET(socket, &rfds)) + { + bResult = true; + } + } + } + + //printf("hasdata waited [%d] milliseconds [%d], bResult = %d\n",chono.getMillis(),waitMilliseconds,bResult); + return bResult; +} + int Socket::getDataToRead(bool wantImmediateReply) { unsigned long size = 0;