From 3746bf4f11115267f7ebd04e9589e86c79e0644c Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 23 May 2013 07:12:29 +0000 Subject: [PATCH] bugfix to properly close socket when connected client leaves game lobby --- .../menu/menu_state_connected_game.cpp | 60 +++++++++++++++++++ .../menu/menu_state_connected_game.h | 4 ++ .../glest_game/network/client_interface.cpp | 5 +- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 30fdfb04..9b3d3257 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -57,9 +57,12 @@ struct FormatString { MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainMenu,JoinMenu joinMenuInfo, bool openNetworkSlots): MenuState(program, mainMenu, "connected-game") { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + containerName = "ClientConnectedGame"; switchSetupRequestFlagType |= ssrft_NetworkPlayerName; updateDataSynchDetailText = false; + launchingNewGame = false; needToBroadcastServerSettings=false; broadcastServerSettingsDelayTimer=0; @@ -723,7 +726,49 @@ void MenuStateConnectedGame::reloadUI() { GraphicComponent::reloadFontsForRegisterGraphicComponents(containerName); } +void MenuStateConnectedGame::disconnectFromServer() { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //printf("!!!!!! In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,PlatformExceptionHandler::getStackTrace().c_str()); + + NetworkManager &networkManager= NetworkManager::getInstance(); + ClientInterface* clientInterface= networkManager.getClientInterface(false); + if(clientInterface != NULL) { + CoreData &coreData= CoreData::getInstance(); + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + + soundRenderer.playFx(coreData.getClickSoundA()); + if(clientInterface->getSocket() != NULL) { + if(clientInterface->isConnected() == true) { + Lang &lang= Lang::getInstance(); + const vector languageList = clientInterface->getGameSettings()->getUniqueNetworkPlayerLanguages(); + for(unsigned int i = 0; i < languageList.size(); ++i) { + string sQuitText = lang.get("QuitGame",languageList[i]); + clientInterface->sendTextMessage(sQuitText,-1,false,languageList[i]); + } + sleep(1); + + } + clientInterface->close(); + } + clientInterface->reset(); + } + currentFactionName=""; + currentMap=""; + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + MenuStateConnectedGame::~MenuStateConnectedGame() { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if(launchingNewGame == false) { + disconnectFromServer(); + NetworkManager &networkManager= NetworkManager::getInstance(); + networkManager.end(); + } + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(modHttpServerThread != NULL) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -776,6 +821,8 @@ MenuStateConnectedGame::~MenuStateConnectedGame() { delete factionVideo; factionVideo = NULL; } + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } string MenuStateConnectedGame::refreshTilesetModInfo(string tilesetInfo) { @@ -1430,6 +1477,7 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ else if(buttonDisconnect.mouseClick(x,y)){ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); +/* soundRenderer.playFx(coreData.getClickSoundA()); if(clientInterface->getSocket() != NULL) { if(clientInterface->isConnected() == true) { @@ -1450,6 +1498,10 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ currentFactionName=""; currentMap=""; returnToJoinMenu(); +*/ + disconnectFromServer(); + networkManager.end(); + returnToJoinMenu(); return; } @@ -2315,10 +2367,16 @@ void MenuStateConnectedGame::returnToJoinMenu() { if(returnMenuInfo == jmSimple) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + launchingNewGame = true; + disconnectFromServer(); mainMenu->setState(new MenuStateJoinGame(program, mainMenu)); } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + launchingNewGame = true; + disconnectFromServer(); mainMenu->setState(new MenuStateMasterserver(program, mainMenu)); } } @@ -3364,6 +3422,8 @@ void MenuStateConnectedGame::update() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + launchingNewGame = true; + //disconnectFromServer(); program->setState(new Game(program, clientInterface->getGameSettings(),false)); return; diff --git a/source/glest_game/menu/menu_state_connected_game.h b/source/glest_game/menu/menu_state_connected_game.h index a2694e21..455ffd3e 100644 --- a/source/glest_game/menu/menu_state_connected_game.h +++ b/source/glest_game/menu/menu_state_connected_game.h @@ -220,6 +220,8 @@ private: time_t broadcastServerSettingsDelayTimer; int lastGameSettingsReceivedCount; + bool launchingNewGame; + public: MenuStateConnectedGame(Program *program, MainMenu *mainMenu, JoinMenu joinMenuInfo=jmSimple, bool openNetworkSlots= false); @@ -283,6 +285,8 @@ private: string refreshTechModInfo(string techInfo); string refreshMapModInfo(string mapInfo); string getMapCRC(string mapName); + + void disconnectFromServer(); }; }}//end namespace diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 36d5804a..f0aed3ac 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -243,7 +243,10 @@ ClientInterface::~ClientInterface() { if(lang.hasString("PlayerLeftGame",languageList[i]) == true) { sQuitText = lang.get("PlayerLeftGame",languageList[i]); } - sendTextMessage(sQuitText,-1,false,languageList[i]); + + if(clientSocket != NULL && clientSocket->isConnected() == true) { + sendTextMessage(sQuitText,-1,false,languageList[i]); + } } }