From 1e0a5dc30431dbd9e756f75e909479a792e712a0 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 7 Dec 2010 06:12:51 +0000 Subject: [PATCH] - better stability of menu navigation and threads --- .../menu/menu_state_custom_game.cpp | 127 +++++++++++------- .../sources/platform/common/base_thread.cpp | 2 +- 2 files changed, 77 insertions(+), 52 deletions(-) diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index e5a01b8f7..773fed214 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -60,7 +60,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b currentTechName_factionPreview=""; currentFactionName_factionPreview=""; mapPreviewTexture=NULL; - + publishToMasterserverThread = NULL; Lang &lang= Lang::getInstance(); @@ -69,7 +69,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b defaultPlayerName = config.getString("NetPlayerName",Socket::getHostName().c_str()); enableFactionTexturePreview = config.getBool("FactionPreview","true"); enableMapPreview = config.getBool("MapPreview","true"); - + showFullConsole=false; mainMessageBox.registerGraphicComponent(containerName,"mainMessageBox"); @@ -133,7 +133,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b int networkHeadPos=700; int networkPos=networkHeadPos-labelOffset; int xoffset=10; - + //map listBox // put them all in a set, to weed out duplicates (gbm & mgm with same name) // will also ensure they are alphabetically listed (rather than how the OS provides them) @@ -144,17 +144,17 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b findAll(config.getPathListForType(ptMaps), "*.mgm", results, true, false); copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); results.clear(); - + if (allMaps.empty()) { throw runtime_error("No maps were found!"); } copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); mapFiles = results; - + copy(mapFiles.begin(), mapFiles.end(), std::back_inserter(playerSortedMaps[0])); copy(playerSortedMaps[0].begin(), playerSortedMaps[0].end(), std::back_inserter(formattedPlayerSortedMaps[0])); std::for_each(formattedPlayerSortedMaps[0].begin(), formattedPlayerSortedMaps[0].end(), FormatString()); - + for ( int i = 0 ; i(cfgMainKeys,cfgUserKeys)); - + console.addLine(lang.get("To switch off music press")+" - \""+configKeys.getCharKey("ToggleMusic")+"\""); //chatManager.init(&console, world.getThisTeamIndex()); @@ -520,24 +520,31 @@ MenuStateCustomGame::~MenuStateCustomGame() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); if(publishToMasterserverThreadInDeletion == false) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); MutexSafeWrapper safeMutexPtr(&publishToMasterserverThreadPtrChangeAccessor); + 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__); needToBroadcastServerSettings = false; needToRepublishToMasterserver = false; //BaseThread::shutdownAndWait(publishToMasterserverThread); if(publishToMasterserverThread != NULL && publishToMasterserverThread->shutdownAndWait() == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); delete publishToMasterserverThread; + publishToMasterserverThread = NULL; } - publishToMasterserverThread = NULL; + publishToMasterserverThreadInDeletion = false; safeMutexPtr.ReleaseLock(); } else { safeMutexPtr.ReleaseLock(); } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -562,8 +569,9 @@ void MenuStateCustomGame::returnToParentMenu(){ if(publishToMasterserverThread != NULL && publishToMasterserverThread->shutdownAndWait() == true) { delete publishToMasterserverThread; + publishToMasterserverThread = NULL; } - publishToMasterserverThread = NULL; + publishToMasterserverThreadInDeletion = false; safeMutexPtr.ReleaseLock(); @@ -614,8 +622,9 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ if(publishToMasterserverThread != NULL && publishToMasterserverThread->shutdownAndWait() == true) { delete publishToMasterserverThread; + publishToMasterserverThread = NULL; } - publishToMasterserverThread = NULL; + publishToMasterserverThreadInDeletion = false; safeMutexPtr.ReleaseLock(); @@ -724,7 +733,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ */ else if(listBoxTileset.mouseClick(x, y)){ MutexSafeWrapper safeMutex(&masterServerThreadAccessor); - + if(listBoxPublishServer.getSelectedItemIndex() == 0) { needToRepublishToMasterserver = true; } @@ -804,13 +813,13 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ else { for(int i=0; isetGameSettings(&gameSettings,false); @@ -1127,8 +1136,9 @@ void MenuStateCustomGame::PlayNow() { if(publishToMasterserverThread != NULL && publishToMasterserverThread->shutdownAndWait() == true) { delete publishToMasterserverThread; + publishToMasterserverThread = NULL; } - publishToMasterserverThread = NULL; + publishToMasterserverThreadInDeletion = false; safeMutexPtr.ReleaseLock(); @@ -1261,11 +1271,11 @@ void MenuStateCustomGame::render() { renderer.renderLabel(&labelPlayerNames[i]); renderer.renderListBox(&listBoxControls[i]); - + if(listBoxControls[i].getSelectedItemIndex()!=ctClosed){ if(listBoxRMultiplier[i].getEnabled() && (listBoxAdvanced.getSelectedItemIndex() == 1)){ renderer.renderListBox(&listBoxRMultiplier[i]); - } + } renderer.renderListBox(&listBoxFactions[i]); renderer.renderListBox(&listBoxTeams[i]); renderer.renderLabel(&labelNetStatus[i]); @@ -1294,13 +1304,13 @@ void MenuStateCustomGame::render() { renderer.renderLabel(&labelTeam); renderer.renderLabel(&labelMapInfo); renderer.renderLabel(&labelAdvanced); - + renderer.renderListBox(&listBoxMap); renderer.renderListBox(&listBoxTileset); renderer.renderListBox(&listBoxMapFilter); renderer.renderListBox(&listBoxTechTree); renderer.renderListBox(&listBoxAdvanced); - + if(listBoxPublishServer.getEditable()) { @@ -1339,7 +1349,7 @@ void MenuStateCustomGame::render() { renderer.renderMapPreview(&mapPreview, renderAll, 10, 350); } } - + renderer.renderChatManager(&chatManager); renderer.renderConsole(&console,showFullConsole,true); } @@ -1383,7 +1393,7 @@ void MenuStateCustomGame::update() { bool hasOneNetworkSlotOpen = false; int currentConnectionCount=0; Config &config = Config::getInstance(); - + bool masterServerErr = showMasterserverError; if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); @@ -1411,7 +1421,7 @@ void MenuStateCustomGame::update() { mainMessageBoxState=1; showMessageBox( generalErrorToShow, "Error", false); } - + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); @@ -1427,7 +1437,7 @@ void MenuStateCustomGame::update() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n",__FILE__,__FUNCTION__,__LINE__,switchSetupRequests[i]->getToFactionIndex()); //printf("switchSetupRequests[i]->getSelectedFactionName()=%s\n",switchSetupRequests[i]->getSelectedFactionName().c_str()); //printf("switchSetupRequests[i]->getToTeam()=%d\n",switchSetupRequests[i]->getToTeam()); - + if(switchSetupRequests[i]->getToFactionIndex() != -1) { int newFactionIdx = switchSetupRequests[i]->getToFactionIndex(); /* @@ -1493,7 +1503,7 @@ void MenuStateCustomGame::update() { switchSetupRequests[i]=NULL; } } - + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); @@ -1507,7 +1517,7 @@ void MenuStateCustomGame::update() { ConnectionSlot* connectionSlot= serverInterface->getSlot(i); //assert(connectionSlot!=NULL); - + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); hasOneNetworkSlotOpen=true; @@ -1667,7 +1677,7 @@ void MenuStateCustomGame::update() { serverInterface->setGameSettings(&gameSettings,false); needToSetChangedGameSettings = false; } - + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); @@ -1684,7 +1694,7 @@ void MenuStateCustomGame::update() { listBoxPublishServer.setEditable(false); //listBoxEnableServerControlledAI.setEditable(false); } - + bool republishToMaster = (difftime(time(NULL),lastMasterserverPublishing) >= 5); if(republishToMaster == true) { @@ -1707,7 +1717,7 @@ void MenuStateCustomGame::update() { } bool broadCastSettings = (difftime(time(NULL),lastSetChangedGameSettings) >= 2); - + if(broadCastSettings == true) { needToBroadcastServerSettings=true; } @@ -1815,13 +1825,13 @@ void MenuStateCustomGame::publishToMasterserver() if((connectionSlot!=NULL) && (connectionSlot->isConnected())) { slotCountConnectedPlayers++; - } + } } else if(listBoxControls[i].getSelectedItemIndex() == ctHuman) { slotCountHumans++; slotCountConnectedPlayers++; - } + } } //?status=waiting&system=linux&info=titus @@ -1877,6 +1887,11 @@ void MenuStateCustomGame::simpleTask() { bool needPing = (difftime(time(NULL),lastNetworkPing) >= GameConstants::networkPingInterval); safeMutex.ReleaseLock(); + if( publishToMasterserverThread == NULL || + publishToMasterserverThread->getQuitStatus() == true) { + return; + } + if(republish == true) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -1898,7 +1913,7 @@ void MenuStateCustomGame::simpleTask() { std::string serverInfo = SystemFlags::getHTTP(request,handle); SystemFlags::cleanupHTTP(&handle); - + //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()); @@ -1911,6 +1926,11 @@ void MenuStateCustomGame::simpleTask() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + if( publishToMasterserverThread == NULL || + publishToMasterserverThread->getQuitStatus() == true) { + return; + } + if(broadCastSettings) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -1938,6 +1958,11 @@ void MenuStateCustomGame::simpleTask() { } } + if( publishToMasterserverThread == NULL || + publishToMasterserverThread->getQuitStatus() == true) { + return; + } + if(needPing == true) { lastNetworkPing = time(NULL); @@ -2107,7 +2132,7 @@ void MenuStateCustomGame::saveGameSettingsToFile(std::string fileName) { //ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); saveGameFile << "Description=" << gameSettings.getDescription() << std::endl; - + saveGameFile << "MapFilterIndex=" << gameSettings.getMapFilterIndex() << std::endl; saveGameFile << "Map=" << gameSettings.getMap() << std::endl; saveGameFile << "Tileset=" << gameSettings.getTileset() << std::endl; @@ -2175,7 +2200,7 @@ GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string fileName) gameSettings.setDefaultVictoryConditions(properties.getBool("DefaultVictoryConditions")); gameSettings.setFogOfWar(properties.getBool("FogOfWar")); listBoxAdvanced.setSelectedItemIndex(properties.getInt("AdvancedIndex","0")); - + gameSettings.setAllowObservers(properties.getBool("AllowObservers","false")); gameSettings.setEnableObserverModeAtEndGame(properties.getBool("EnableObserverModeAtEndGame")); @@ -2583,7 +2608,7 @@ void MenuStateCustomGame::keyDown(char key) { else if(key == configKeys.getCharKey("ToggleMusic")) { Config &config = Config::getInstance(); Lang &lang= Lang::getInstance(); - + float configVolume = (config.getInt("SoundVolumeMusic") / 100.f); float currentVolume = CoreData::getInstance().getMenuMusic()->getVolume(); if(currentVolume > 0) { @@ -2669,7 +2694,7 @@ void MenuStateCustomGame::switchToNextMapGroup(const int direction){ // if there are no maps for the current selection we switch to next selection while(formattedPlayerSortedMaps[i].empty()){ i=i+direction; - if(i>GameConstants::maxPlayers){ + if(i>GameConstants::maxPlayers){ i=0; } if(i<0){ diff --git a/source/shared_lib/sources/platform/common/base_thread.cpp b/source/shared_lib/sources/platform/common/base_thread.cpp index ff4183447..9b2330910 100644 --- a/source/shared_lib/sources/platform/common/base_thread.cpp +++ b/source/shared_lib/sources/platform/common/base_thread.cpp @@ -142,7 +142,7 @@ bool BaseThread::shutdownAndWait() { int maxWaitSeconds = 7; if(pThread->canShutdown() == false) { - maxWaitSeconds = 3; + maxWaitSeconds = 2; } for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= maxWaitSeconds; ) {