diff --git a/source/glest_game/menu/main_menu.cpp b/source/glest_game/menu/main_menu.cpp index 1560cdfff..a27768b94 100644 --- a/source/glest_game/menu/main_menu.cpp +++ b/source/glest_game/menu/main_menu.cpp @@ -160,9 +160,13 @@ void MainMenu::keyPress(char c){ state->keyPress(c); } -void MainMenu::setState(MenuState *state){ - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(oldstate != NULL && oldstate != state) { +void MainMenu::setState(MenuState *newstate) { + printf("In [%s::%s Line: %d] oldstate [%p] newstate [%p] this->state [%p]\n",__FILE__,__FUNCTION__,__LINE__,oldstate,newstate,this->state); + + //delete this->state; + //this->state = newstate; + + if(oldstate != NULL && oldstate != newstate) { delete oldstate; //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -180,9 +184,10 @@ void MainMenu::setState(MenuState *state){ //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - this->state= state; + this->state= newstate; + GraphicComponent::resetFade(); - menuBackground.setTargetCamera(state->getCamera()); + menuBackground.setTargetCamera(newstate->getCamera()); } bool MainMenu::isInSpecialKeyCaptureEvent() { diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 33d1def93..c84102005 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -518,23 +518,34 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } -MenuStateCustomGame::~MenuStateCustomGame() { +void MenuStateCustomGame::cleanup() { + //printf("IN MenuStateCustomGame cleanup - A\n"); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); if(publishToMasterserverThread != NULL) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(publishToMasterserverThread != NULL && - publishToMasterserverThread->canShutdown(true) == true && - publishToMasterserverThread->shutdownAndWait() == true) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - delete publishToMasterserverThread; - } - publishToMasterserverThread = NULL; + //printf("IN MenuStateCustomGame cleanup - B\n"); + needToBroadcastServerSettings = false; needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); + publishToMasterserverThread->setThreadOwnerValid(false); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(publishToMasterserverThread->canShutdown(true) == true && + publishToMasterserverThread->shutdownAndWait() == true) { + //printf("IN MenuStateCustomGame cleanup - C\n"); + delete publishToMasterserverThread; + } + + //printf("IN MenuStateCustomGame cleanup - D\n"); + publishToMasterserverThread = NULL; } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + //printf("IN MenuStateCustomGame cleanup - E\n"); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); cleanupFactionTexture(); cleanupMapPreviewTexture(); @@ -542,28 +553,42 @@ MenuStateCustomGame::~MenuStateCustomGame() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } +MenuStateCustomGame::~MenuStateCustomGame() { + //printf("IN MenuStateCustomGame Destructor - A\n"); + + cleanup(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); +} + void MenuStateCustomGame::returnToParentMenu() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); needToBroadcastServerSettings = false; needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); bool returnToMasterServerMenu = parentMenuIsMs; +/* if(publishToMasterserverThread != NULL && publishToMasterserverThread->canShutdown() == true && publishToMasterserverThread->shutdownAndWait() == true) { + publishToMasterserverThread->setThreadOwnerValid(false); delete publishToMasterserverThread; publishToMasterserverThread = NULL; } +*/ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); if(returnToMasterServerMenu) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + cleanup(); mainMenu->setState(new MenuStateMasterserver(program, mainMenu)); } else { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + cleanup(); mainMenu->setState(new MenuStateNewGame(program, mainMenu)); } @@ -596,17 +621,18 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL)); needToBroadcastServerSettings = false; needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); safeMutex.ReleaseLock(); +/* if(publishToMasterserverThread != NULL && publishToMasterserverThread->canShutdown() == true && publishToMasterserverThread->shutdownAndWait() == true) { + publishToMasterserverThread->setThreadOwnerValid(false); delete publishToMasterserverThread; publishToMasterserverThread = NULL; } - - //publishToMasterserverThreadInDeletion = false; - //safeMutexPtr.ReleaseLock(); +*/ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -1091,20 +1117,24 @@ void MenuStateCustomGame::PlayNow() { } needToBroadcastServerSettings = false; needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); safeMutex.ReleaseLock(); +/* if(publishToMasterserverThread != NULL && publishToMasterserverThread->canShutdown() == true && publishToMasterserverThread->shutdownAndWait() == true) { + publishToMasterserverThread->setThreadOwnerValid(false); delete publishToMasterserverThread; publishToMasterserverThread = NULL; } - +*/ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); assert(program != NULL); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + cleanup(); program->setState(new Game(program, &gameSettings)); return; } @@ -1795,10 +1825,15 @@ void MenuStateCustomGame::publishToMasterserver() void MenuStateCustomGame::simpleTask(BaseThread *callingThread) { try { - if(callingThread->getQuitStatus() == true) { + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - A\n"); + + MutexSafeWrapper safeMutexThreadOwner(callingThread->getMutexThreadOwnerValid()); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { return; } + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - B\n"); + MutexSafeWrapper safeMutex(callingThread->getMutexThreadObjectAccessor()); bool republish = (needToRepublishToMasterserver == true && publishToServerInfo.size() != 0); needToRepublishToMasterserver = false; @@ -1807,19 +1842,22 @@ void MenuStateCustomGame::simpleTask(BaseThread *callingThread) { bool broadCastSettings = needToBroadcastServerSettings; needToBroadcastServerSettings = false; bool hasClientConnection = false; + if(broadCastSettings == true) { - ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); - hasClientConnection = serverInterface->hasClientConnection(); + ServerInterface *serverInterface = NetworkManager::getInstance().getServerInterface(false); + if(serverInterface != NULL) { + hasClientConnection = serverInterface->hasClientConnection(); + } } - bool needPing = (difftime(time(NULL),lastNetworkPing) >= GameConstants::networkPingInterval); - safeMutex.ReleaseLock(true); + bool needPing = (difftime(time(NULL),lastNetworkPing) >= GameConstants::networkPingInterval); if(callingThread->getQuitStatus() == true) { return; } + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - C\n"); + if(republish == true) { - safeMutex.Lock(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); string request = Config::getInstance().getString("Masterserver") + "addServerInfo.php?"; @@ -1837,66 +1875,85 @@ void MenuStateCustomGame::simpleTask(BaseThread *callingThread) { //printf("the request is:\n%s\n",request.c_str()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] the request is:\n%s\n",__FILE__,__FUNCTION__,__LINE__,request.c_str()); safeMutex.ReleaseLock(true); + safeMutexThreadOwner.ReleaseLock(); std::string serverInfo = SystemFlags::getHTTP(request,handle); SystemFlags::cleanupHTTP(&handle); - if(callingThread->getQuitStatus() == true) { + MutexSafeWrapper safeMutexThreadOwner2(callingThread->getMutexThreadOwnerValid()); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner2.isValidMutex() == false) { return; } - safeMutex.Lock(); + //printf("the result is:\n'%s'\n",serverInfo.c_str()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] the result is:\n'%s'\n",__FILE__,__FUNCTION__,__LINE__,serverInfo.c_str()); // uncomment to enable router setup check of this server if(EndsWith(serverInfo, "OK") == false) { + if(callingThread->getQuitStatus() == true) { + return; + } + showMasterserverError=true; masterServererErrorToShow = (serverInfo != "" ? serverInfo : "No Reply"); } - safeMutex.ReleaseLock(true); + } + else { + safeMutexThreadOwner.ReleaseLock(); } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(callingThread->getQuitStatus() == true) { - return; - } + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - D\n"); if(broadCastSettings == true) { - safeMutex.Lock(); + MutexSafeWrapper safeMutexThreadOwner2(callingThread->getMutexThreadOwnerValid()); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner2.isValidMutex() == false) { + return; + } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); GameSettings gameSettings; loadGameSettings(&gameSettings); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - serverInterface->setGameSettings(&gameSettings,false); - if(hasClientConnection == true) { + if(callingThread->getQuitStatus() == true) { + return; + } + ServerInterface *serverInterface= NetworkManager::getInstance().getServerInterface(false); + if(serverInterface != NULL) { + serverInterface->setGameSettings(&gameSettings,false); + } + + if(hasClientConnection == true && serverInterface != NULL) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - serverInterface->broadcastGameSetup(&gameSettings); } - safeMutex.ReleaseLock(true); } - if(callingThread->getQuitStatus() == true) { - return; - } + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - E\n"); - safeMutex.Lock(); if(needPing == true) { + MutexSafeWrapper safeMutexThreadOwner2(callingThread->getMutexThreadOwnerValid()); + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner2.isValidMutex() == false) { + return; + } + lastNetworkPing = time(NULL); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] Sending nmtPing to clients\n",__FILE__,__FUNCTION__,__LINE__); - ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); - NetworkMessagePing msg(GameConstants::networkPingInterval,time(NULL)); - serverInterface->broadcastPing(&msg); + ServerInterface *serverInterface= NetworkManager::getInstance().getServerInterface(false); + if(serverInterface != NULL) { + NetworkMessagePing msg(GameConstants::networkPingInterval,time(NULL)); + serverInterface->broadcastPing(&msg); + } } safeMutex.ReleaseLock(); + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - F\n"); } catch(const std::exception &ex) { char szBuf[4096]=""; diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index 103a12010..6b41f8e41 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -145,7 +145,7 @@ private: public: MenuStateCustomGame(Program *program, MainMenu *mainMenu ,bool openNetworkSlots= false, bool parentMenuIsMasterserver=false, bool autostart=false); - ~MenuStateCustomGame(); + virtual ~MenuStateCustomGame(); void mouseClick(int x, int y, MouseButton mouseButton); void mouseMove(int x, int y, const MouseState *mouseState); @@ -191,6 +191,7 @@ private: void PlayNow(); void SetActivePlayerNameEditor(); + void cleanup(); }; }}//end namespace diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 2a5aad7a1..94acb419b 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -300,13 +300,15 @@ void MenuStateMasterserver::cleanup() { MutexSafeWrapper safeMutex((updateFromMasterserverThread != NULL ? updateFromMasterserverThread->getMutexThreadObjectAccessor() : NULL)); needUpdateFromServer = false; + safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(updateFromMasterserverThread != NULL && - updateFromMasterserverThread->canShutdown(true) == true && - updateFromMasterserverThread->shutdownAndWait() == true) { - delete updateFromMasterserverThread; + updateFromMasterserverThread->canShutdown(true) == true) { + if(updateFromMasterserverThread->shutdownAndWait() == true) { + delete updateFromMasterserverThread; + } } updateFromMasterserverThread = NULL; diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 80b9fa2c6..15eb0e282 100755 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -45,7 +45,7 @@ const int ClientInterface::messageWaitTimeout= 10000; //10 seconds const int ClientInterface::waitSleepTime= 10; const int ClientInterface::maxNetworkCommandListSendTimeWait = 4; -ClientInterface::ClientInterface() { +ClientInterface::ClientInterface() : GameNetworkInterface() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] constructor for %p\n",__FILE__,__FUNCTION__,__LINE__,this); clientSocket= NULL; diff --git a/source/glest_game/network/network_manager.cpp b/source/glest_game/network/network_manager.cpp index 9da7dbe65..eb6d7b9ff 100644 --- a/source/glest_game/network/network_manager.cpp +++ b/source/glest_game/network/network_manager.cpp @@ -27,83 +27,108 @@ NetworkManager &NetworkManager::getInstance(){ } NetworkManager::NetworkManager() { - SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); gameNetworkInterface= NULL; networkRole= nrIdle; - SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); } void NetworkManager::init(NetworkRole networkRole) { - SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface); assert(gameNetworkInterface==NULL); this->networkRole = networkRole; - if(networkRole==nrServer){ + if(networkRole == nrServer) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface); gameNetworkInterface = new ServerInterface(); } else { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface); gameNetworkInterface = new ClientInterface(); } - SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + //printf("==========] CREATING gameNetworkInterface [%p]\n",gameNetworkInterface); + + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); } void NetworkManager::end() { - SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); + + //printf("==========] DELETING gameNetworkInterface [%p]\n",gameNetworkInterface); delete gameNetworkInterface; gameNetworkInterface= NULL; networkRole= nrIdle; - SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); } -void NetworkManager::update(){ - if(gameNetworkInterface!=NULL){ +void NetworkManager::update() { + if(gameNetworkInterface!=NULL) { gameNetworkInterface->update(); } } -bool NetworkManager::isNetworkGame(){ +bool NetworkManager::isNetworkGame() { return networkRole==nrClient || getServerInterface()->getConnectedSlotCount()>0; } -GameNetworkInterface* NetworkManager::getGameNetworkInterface(){ - assert(gameNetworkInterface!=NULL); - if(gameNetworkInterface==NULL) { - throw runtime_error("gameNetworkInterface==NULL"); - } +GameNetworkInterface* NetworkManager::getGameNetworkInterface(bool throwErrorOnNull) { + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); + //printf("==========] GET gameNetworkInterface [%p]\n",gameNetworkInterface); + + if(throwErrorOnNull) { + assert(gameNetworkInterface!=NULL); + + if(gameNetworkInterface==NULL) { + throw runtime_error("gameNetworkInterface==NULL"); + } + } + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); return gameNetworkInterface; } -ServerInterface* NetworkManager::getServerInterface(){ - assert(gameNetworkInterface!=NULL); - if(gameNetworkInterface==NULL) { - throw runtime_error("gameNetworkInterface==NULL"); - } +ServerInterface* NetworkManager::getServerInterface(bool throwErrorOnNull) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); + //printf("==========] GET gameNetworkInterface (server) [%p]\n",gameNetworkInterface); - assert(networkRole==nrServer); - if(networkRole!=nrServer) { - throw runtime_error("networkRole!=nrServer"); - } + if(throwErrorOnNull) { + assert(gameNetworkInterface!=NULL); + if(gameNetworkInterface==NULL) { + throw runtime_error("gameNetworkInterface==NULL"); + } + assert(networkRole==nrServer); + if(networkRole!=nrServer) { + throw runtime_error("networkRole!=nrServer"); + } + } + + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); return dynamic_cast(gameNetworkInterface); } -ClientInterface* NetworkManager::getClientInterface(){ - assert(gameNetworkInterface!=NULL); - if(gameNetworkInterface==NULL) { - throw runtime_error("gameNetworkInterface==NULL"); - } +ClientInterface* NetworkManager::getClientInterface(bool throwErrorOnNull) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); + //printf("==========] GET gameNetworkInterface (client) [%p]\n",gameNetworkInterface); - assert(networkRole==nrClient); - if(networkRole!=nrClient) { - throw runtime_error("networkRole!=nrClient"); - } + if(throwErrorOnNull) { + assert(gameNetworkInterface!=NULL); + if(gameNetworkInterface==NULL) { + throw runtime_error("gameNetworkInterface==NULL"); + } + + assert(networkRole==nrClient); + if(networkRole!=nrClient) { + throw runtime_error("networkRole!=nrClient"); + } + } + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); return dynamic_cast(gameNetworkInterface); } diff --git a/source/glest_game/network/network_manager.h b/source/glest_game/network/network_manager.h index 1f2317097..2ab5d4146 100644 --- a/source/glest_game/network/network_manager.h +++ b/source/glest_game/network/network_manager.h @@ -3,9 +3,9 @@ // // Copyright (C) 2001-2008 Martio Figueroa // -// You can redistribute this code and/or modify it under -// the terms of the GNU General Public License as published -// by the Free Software Foundation; either version 2 of the +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version // ============================================================== @@ -42,9 +42,9 @@ public: void update(); bool isNetworkGame(); - GameNetworkInterface* getGameNetworkInterface(); - ServerInterface* getServerInterface(); - ClientInterface* getClientInterface(); + GameNetworkInterface* getGameNetworkInterface(bool throwErrorOnNull=true); + ServerInterface* getServerInterface(bool throwErrorOnNull=true); + ClientInterface* getClientInterface(bool throwErrorOnNull=true); NetworkRole getNetworkRole() { return networkRole; } }; diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 82436f498..6cb9a55f4 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -60,7 +60,8 @@ double LAG_CHECK_GRACE_PERIOD = 15; // badly and we want to give time for them to catch up double MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE = 1; -ServerInterface::ServerInterface() { +ServerInterface::ServerInterface() : GameNetworkInterface() { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); gameHasBeenInitiated = false; gameSettingsUpdateCount = 0; currentFrameCount = 0; @@ -82,10 +83,13 @@ ServerInterface::ServerInterface() { slots[i]= NULL; switchSetupRequests[i]= NULL; } + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); serverSocket.setBlock(false); - //serverSocket.bind(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str())); + + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); serverSocket.setBindPort(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str())); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(Config::getInstance().getBool("EnableFTPXfer","false") == true) { std::pair mapsPath; vector pathList = Config::getInstance().getPathListForType(ptMaps); @@ -105,11 +109,13 @@ ServerInterface::ServerInterface() { } } + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); int portNumber = Config::getInstance().getInt("FTPServerPort",intToStr(ServerSocket::getFTPServerPort()).c_str()); ServerSocket::setFTPServerPort(portNumber); ftpServer = new FTPServerThread(mapsPath,tilesetsPath,portNumber); ftpServer->start(); } + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } ServerInterface::~ServerInterface() { diff --git a/source/shared_lib/include/platform/common/base_thread.h b/source/shared_lib/include/platform/common/base_thread.h index 1579b623e..e580ba128 100644 --- a/source/shared_lib/include/platform/common/base_thread.h +++ b/source/shared_lib/include/platform/common/base_thread.h @@ -35,6 +35,9 @@ protected: Mutex mutexThreadObjectAccessor; + bool threadOwnerValid; + Mutex mutexThreadOwnerValid; + Mutex mutexExecutingTask; bool executingTask; @@ -74,6 +77,11 @@ public: void setExecutingTask(bool value); bool getExecutingTask(); + + void setThreadOwnerValid(bool value); + bool getThreadOwnerValid(); + Mutex * getMutexThreadOwnerValid(); + Mutex * getMutexThreadObjectAccessor() { return &mutexThreadObjectAccessor; } }; diff --git a/source/shared_lib/include/platform/sdl/thread.h b/source/shared_lib/include/platform/sdl/thread.h index 353424089..47644fa21 100644 --- a/source/shared_lib/include/platform/sdl/thread.h +++ b/source/shared_lib/include/platform/sdl/thread.h @@ -91,6 +91,9 @@ public: this->ownerId = ownerId; Lock(); } + bool isValidMutex() const { + return(this->mutex != NULL); + } void Lock() { if(this->mutex != NULL) { diff --git a/source/shared_lib/sources/feathery_ftp/ftpCmds.c b/source/shared_lib/sources/feathery_ftp/ftpCmds.c index c361e69b9..7fb212d2e 100755 --- a/source/shared_lib/sources/feathery_ftp/ftpCmds.c +++ b/source/shared_lib/sources/feathery_ftp/ftpCmds.c @@ -674,14 +674,13 @@ LOCAL int ftpCmdPasv(int sessionId, const char* args, int len) if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, client IP = %u, remote IP = %u, port = %d, ftpAddUPNPPortForward = %p, ftpRemoveUPNPPortForward = %p\n", sessionId, ftpGetSession(sessionId)->remoteIp, ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp), port,ftpAddUPNPPortForward,ftpRemoveUPNPPortForward); - if(ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp) != 0) + if(ftpAddUPNPPortForward != NULL && ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp) != 0) { ftpGetSession(sessionId)->remoteFTPServerPassivePort = port; - if(ftpAddUPNPPortForward) { + if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, adding UPNP port forward\n", sessionId); - ftpAddUPNPPortForward(port, port); - } + ftpAddUPNPPortForward(port, port); remoteFTPServerIp = ftpFindExternalFTPServerIp(ftpGetSession(sessionId)->remoteIp); diff --git a/source/shared_lib/sources/feathery_ftp/ftpRuntime.c b/source/shared_lib/sources/feathery_ftp/ftpRuntime.c index c2a7eba47..e84e4acce 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpRuntime.c +++ b/source/shared_lib/sources/feathery_ftp/ftpRuntime.c @@ -205,8 +205,8 @@ int ftpShutdown(void) if(ftpGetSession(n)->open) { ftpUntrackSocket(ftpGetSession(n)->ctrlSocket); - ftpCloseSession(n); - } + } + ftpCloseSession(n); } ftpArchCleanup(); diff --git a/source/shared_lib/sources/feathery_ftp/ftpSession.c b/source/shared_lib/sources/feathery_ftp/ftpSession.c index 2c5c0e8e8..9ebc80c2b 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpSession.c +++ b/source/shared_lib/sources/feathery_ftp/ftpSession.c @@ -129,11 +129,15 @@ if(VERBOSE_MODE_ENABLED) printf("In ftpCloseSession sessionId = %d, remote IP = if(VERBOSE_MODE_ENABLED) printf("In ftpCmdPasv sessionId = %d, removing UPNP port forward [%d]\n", id,sessions[id].remoteFTPServerPassivePort); ftpRemoveUPNPPortForward(sessions[id].remoteFTPServerPassivePort, sessions[id].remoteFTPServerPassivePort); + sessions[id].remoteFTPServerPassivePort = 0; } } - ftpCloseSocket(sessions[id].ctrlSocket); - ftpCloseTransmission(id); - + if(sessions[id].open) { + ftpCloseSocket(sessions[id].ctrlSocket); + ftpCloseTransmission(id); + } + sessions[id].remoteIp = 0; + sessions[id].ctrlSocket = 0; sessions[id].open = FALSE; if(VERBOSE_MODE_ENABLED) printf("Session %d closed\n", id); diff --git a/source/shared_lib/sources/platform/common/base_thread.cpp b/source/shared_lib/sources/platform/common/base_thread.cpp index ea6fcb9ff..4e6226740 100644 --- a/source/shared_lib/sources/platform/common/base_thread.cpp +++ b/source/shared_lib/sources/platform/common/base_thread.cpp @@ -29,6 +29,7 @@ BaseThread::BaseThread() : Thread() { setHasBeginExecution(false); setExecutingTask(false); setDeleteSelfOnExecutionDone(false); + setThreadOwnerValid(true); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -39,6 +40,28 @@ BaseThread::~BaseThread() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); } +Mutex * BaseThread::getMutexThreadOwnerValid() { + if(getThreadOwnerValid() == true) { + return &mutexThreadOwnerValid; + } + return NULL; +} + +void BaseThread::setThreadOwnerValid(bool value) { + MutexSafeWrapper safeMutex(&mutexThreadOwnerValid); + threadOwnerValid = value; + safeMutex.ReleaseLock(); +} + +bool BaseThread::getThreadOwnerValid() { + bool ret = false; + MutexSafeWrapper safeMutex(&mutexThreadOwnerValid); + ret = threadOwnerValid; + safeMutex.ReleaseLock(); + + return ret; +} + void BaseThread::signalQuit() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str()); diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index 6b23494ef..6445e255b 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -89,7 +89,7 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter bool SimpleTaskThread::canShutdown(bool deleteSelfIfShutdownDelayed) { bool ret = (getExecutingTask() == false); - if(deleteSelfIfShutdownDelayed == true) { + if(ret == false && deleteSelfIfShutdownDelayed == true) { setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); signalQuit(); } diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 683fcc72f..48b0614a5 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -753,6 +753,8 @@ Socket::Socket(PLATFORM_SOCKET sock) { } Socket::Socket() { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + this->pingThread = NULL; this->connectedIpAddress = "";