From 0d7d0e139d7af84c2492f9eeca62e80294d5682f Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 9 Jul 2010 17:12:57 +0000 Subject: [PATCH] - updated disconnect checking to include a timeout check for our custom ping --- source/glest_game/game/game_constants.h | 1 + .../menu/menu_state_connected_game.cpp | 16 +++++++++++----- .../glest_game/menu/menu_state_connected_game.h | 2 +- .../glest_game/menu/menu_state_custom_game.cpp | 12 ++++++++++++ source/glest_game/menu/menu_state_custom_game.h | 2 ++ source/glest_game/network/client_interface.cpp | 3 +++ source/glest_game/network/connection_slot.cpp | 1 + source/glest_game/network/network_interface.h | 16 ++++++---------- source/glest_game/network/server_interface.cpp | 1 + source/glest_game/network/server_interface.h | 4 ++++ 10 files changed, 42 insertions(+), 16 deletions(-) diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index 1726c7dfc..b8619726d 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -38,6 +38,7 @@ public: static const int updateFps= 40; static const int cameraFps= 100; static int networkFramePeriod; + static const int networkPingInterval = 5; //static const int networkExtraLatency= 200; static const int maxClientConnectHandshakeSecs= 10; diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 956c39479..f5a92b568 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -48,7 +48,7 @@ struct FormatString { MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainMenu,JoinMenu joinMenuInfo, bool openNetworkSlots): MenuState(program, mainMenu, "connected-game") //← set on connected-game { - lastNetworkSend = time(NULL); + lastNetworkSendPing = time(NULL); returnMenuInfo=joinMenuInfo; Lang &lang= Lang::getInstance(); @@ -362,12 +362,18 @@ void MenuStateConnectedGame::update() ClientInterface* clientInterface= NetworkManager::getInstance().getClientInterface(); Lang &lang= Lang::getInstance(); - const int pingFrequency = 5; - if(difftime(time(NULL),lastNetworkSend) >= pingFrequency) { + if(difftime(time(NULL),lastNetworkSendPing) >= GameConstants::networkPingInterval) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to sendPingMessage...\n",__FILE__,__FUNCTION__,__LINE__); - lastNetworkSend = time(NULL); - clientInterface->sendPingMessage(pingFrequency, time(NULL)); + bool isFirstPing = (lastNetworkSendPing == 0); + lastNetworkSendPing = time(NULL); + clientInterface->sendPingMessage(GameConstants::networkPingInterval, time(NULL)); + + if(isFirstPing == false && clientInterface->getLastPingLag() >= (GameConstants::networkPingInterval * 2)) { + string playerNameStr = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()); + clientInterface->sendTextMessage(playerNameStr + "'s connection timed out communicating with server.",-1); + clientInterface->close(); + } } //update status label diff --git a/source/glest_game/menu/menu_state_connected_game.h b/source/glest_game/menu/menu_state_connected_game.h index 33e80d8e1..612ee03b6 100644 --- a/source/glest_game/menu/menu_state_connected_game.h +++ b/source/glest_game/menu/menu_state_connected_game.h @@ -71,7 +71,7 @@ private: string currentMap; JoinMenu returnMenuInfo; bool settingsReceivedFromServer; - time_t lastNetworkSend; + time_t lastNetworkSendPing; public: diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 81d4268f7..c5e281125 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -95,6 +95,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b masterServererErrorToShow = "---"; lastSetChangedGameSettings = 0; lastMasterserverPublishing = 0; + lastNetworkPing = 0; soundConnectionCount=0; vector teamItems, controlItems, results; @@ -1098,8 +1099,11 @@ void MenuStateCustomGame::simpleTask() { needToRepublishToMasterserver = false; std::map newPublishToServerInfo = publishToServerInfo; publishToServerInfo.clear(); + bool broadCastSettings = needToBroadcastServerSettings; needToBroadcastServerSettings=false; + + bool needPing = (difftime(time(NULL),lastNetworkPing) >= GameConstants::networkPingInterval); safeMutex.ReleaseLock(); if(republish == true) { @@ -1156,6 +1160,14 @@ void MenuStateCustomGame::simpleTask() { } } + if(needPing == true) { + lastNetworkPing = time(NULL); + + ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); + NetworkMessagePing msg(GameConstants::networkPingInterval,time(NULL)); + serverInterface->broadcastPing(&msg); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index 384c1b6bb..170b904fd 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -72,6 +72,8 @@ private: bool needToSetChangedGameSettings; time_t lastSetChangedGameSettings; time_t lastMasterserverPublishing; + time_t lastNetworkPing; + bool needToRepublishToMasterserver; bool needToBroadcastServerSettings; std::map publishToServerInfo; diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index aaa09ff90..3d896cc1d 100755 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -273,6 +273,7 @@ void ClientInterface::updateLobby() NetworkMessagePing networkMessagePing; if(receiveMessage(&networkMessagePing)) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + lastPingInfo = networkMessagePing; } } break; @@ -558,6 +559,7 @@ void ClientInterface::updateKeyframe(int frameCount) NetworkMessagePing networkMessagePing; if(receiveMessage(&networkMessagePing)) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + lastPingInfo = networkMessagePing; } } break; @@ -898,6 +900,7 @@ bool ClientInterface::shouldDiscardNetworkMessage(NetworkMessageType networkMess discard = true; NetworkMessagePing msg = NetworkMessagePing(); this->receiveMessage(&msg); + lastPingInfo = msg; } break; case nmtLaunch: diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index 28cf38c3b..02fca8bc5 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -268,6 +268,7 @@ void ConnectionSlot::update(bool checkForNewClients) { NetworkMessagePing networkMessagePing; if(receiveMessage(&networkMessagePing)) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + lastPingInfo = networkMessagePing; } } break; diff --git a/source/glest_game/network/network_interface.h b/source/glest_game/network/network_interface.h index 8b0defdb6..6f60f06a4 100644 --- a/source/glest_game/network/network_interface.h +++ b/source/glest_game/network/network_interface.h @@ -84,10 +84,8 @@ protected: bool networkGameDataSynchCheckOkTile; bool networkGameDataSynchCheckOkTech; - //string chatText; - //string chatSender; - //int chatTeamIndex; std::vector chatTextList; + NetworkMessagePing lastPingInfo; static DisplayMessageFunction pCB_DisplayMessage; void DisplayErrorMessage(string sErr, bool closeSocket=true); @@ -117,7 +115,6 @@ public: bool isConnected(); - //virtual void setGameSettings(GameSettings *serverGameSettings) { gameSettings = *serverGameSettings; } const virtual GameSettings * getGameSettings() { return &gameSettings; } static void setAllowDownloadDataSynch(bool value) { allowDownloadDataSynch = value; } @@ -133,12 +130,6 @@ public: virtual bool getNetworkGameDataSynchCheckOkMap() { return networkGameDataSynchCheckOkMap; } virtual bool getNetworkGameDataSynchCheckOkTile() { return networkGameDataSynchCheckOkTile; } virtual bool getNetworkGameDataSynchCheckOkTech() { return networkGameDataSynchCheckOkTech; } - //virtual bool getNetworkGameDataSynchCheckOkFogOfWar() { return networkGameDataSynchCheckOkFogOfWar; } - //virtual void setNetworkGameDataSynchCheckOkFogOfWar(bool value) { networkGameDataSynchCheckOkFogOfWar = value; } - - //const string getChatText() const {return chatText;} - //const string getChatSender() const {return chatSender;} - //int getChatTeamIndex() const {return chatTeamIndex;} const std::vector & getChatTextList() const { return chatTextList; } void clearChatInfo(); @@ -146,6 +137,11 @@ public: virtual bool getConnectHasHandshaked() const= 0; + NetworkMessagePing getLastPingInfo() const { return lastPingInfo; } + double getLastPingLag() const { + return difftime(time(NULL),lastPingInfo.getPingTime()); + } + std::string getIpAddress(); float getThreadedPingMS(std::string host); diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 5062c1b80..c7c0eaff4 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -690,6 +690,7 @@ bool ServerInterface::shouldDiscardNetworkMessage(NetworkMessageType networkMess discard = true; NetworkMessagePing msg = NetworkMessagePing(); connectionSlot->receiveMessage(&msg); + lastPingInfo = msg; } break; diff --git a/source/glest_game/network/server_interface.h b/source/glest_game/network/server_interface.h index 48cda03b8..2575cc4e0 100644 --- a/source/glest_game/network/server_interface.h +++ b/source/glest_game/network/server_interface.h @@ -91,6 +91,10 @@ public: bool isPortBound() const { return serverSocket.isPortBound(); } int getBindPort() const { return serverSocket.getBindPort(); } + void broadcastPing(const NetworkMessagePing* networkMessage, int excludeSlot= -1) { + this->broadcastMessage(networkMessage,excludeSlot); + } + public: Mutex * getServerSynchAccessor() { return &serverSynchAccessor; }