From 203ad150efbcfb17a04600df89672e1bb4bb20fb Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 16 Mar 2013 06:55:16 +0000 Subject: [PATCH] - battle end screen render fix for tomreyn - more thread safety for threaded client sockets --- source/glest_game/main/battle_end.cpp | 11 +++- source/glest_game/main/battle_end.h | 1 + .../glest_game/network/client_interface.cpp | 58 ++++++++++++------- source/glest_game/network/client_interface.h | 18 ++---- 4 files changed, 51 insertions(+), 37 deletions(-) diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index fe2f047c..3b37a087 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -58,6 +58,7 @@ BattleEnd::BattleEnd(Program *program, const Stats *stats,ProgramState *originSt mouseY = 0; mouse2d = 0; renderToTexture = NULL; + renderToTextureCount = 0; const Metrics &metrics= Metrics::getInstance(); Lang &lang= Lang::getInstance(); @@ -420,7 +421,7 @@ void BattleEnd::render() { else { //printf("Rendering to texture!\n"); - if(menuBackgroundVideo == NULL) { + if(menuBackgroundVideo == NULL && renderToTextureCount >= 300) { renderer.beginRenderToTexture(&renderToTexture); } @@ -736,13 +737,17 @@ void BattleEnd::render() { renderer.renderMessageBox(&mainMessageBox); } - if(menuBackgroundVideo == NULL || renderToTexture == NULL) { + if(menuBackgroundVideo == NULL && renderToTexture == NULL) { renderer.renderMouse2d(mouseX, mouseY, mouse2d, 0.f); } - if(menuBackgroundVideo == NULL) { + if(menuBackgroundVideo == NULL && renderToTextureCount >= 300) { renderer.endRenderToTexture(&renderToTexture); } + + if(menuBackgroundVideo == NULL) { + renderToTextureCount++; + } } renderer.renderFPSWhenEnabled(lastFps); diff --git a/source/glest_game/main/battle_end.h b/source/glest_game/main/battle_end.h index 6b5583e7..91570855 100644 --- a/source/glest_game/main/battle_end.h +++ b/source/glest_game/main/battle_end.h @@ -44,6 +44,7 @@ private: int mouse2d; GraphicMessageBox mainMessageBox; Texture2D *renderToTexture; + uint64 renderToTextureCount; ProgramState *originState; const char *containerName; diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 01bf2ea0..b8059983 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -44,25 +44,17 @@ const bool debugClientInterfacePerf = false; // ===================================================== ClientInterfaceThread::ClientInterfaceThread(ClientInterface *client) : BaseThread() { - //this->triggerIdMutex = new Mutex(); this->clientInterface = client; - //this->masterController = NULL; } ClientInterfaceThread::~ClientInterfaceThread() { this->clientInterface = NULL; - //this->masterController = NULL; - //delete this->triggerIdMutex; - //this->triggerIdMutex = NULL; } void ClientInterfaceThread::setQuitStatus(bool value) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d value = %d\n",__FILE__,__FUNCTION__,__LINE__,value); BaseThread::setQuitStatus(value); -// if(value == true) { -// semTaskSignalled.signal(); -// } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); } @@ -80,7 +72,6 @@ bool ClientInterfaceThread::canShutdown(bool deleteSelfIfShutdownDelayed) { void ClientInterfaceThread::execute() { RunningStatusSafeWrapper runningStatus(this); try { - //setRunningStatus(true); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n",__FILE__,__FUNCTION__,__LINE__,this); @@ -92,20 +83,12 @@ void ClientInterfaceThread::execute() { clientInterface->getSocket(true)->setBlock(true); } - //unsigned int idx = 0; for(;this->clientInterface != NULL;) { if(getQuitStatus() == true) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); break; } - //semTaskSignalled.waitTillSignalled(); - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //static string masterSlaveOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - //MasterSlaveThreadControllerSafeWrapper safeMasterController(masterController,20000,masterSlaveOwnerId); - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(getQuitStatus() == true) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); break; @@ -113,8 +96,6 @@ void ClientInterfaceThread::execute() { ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); - if(debugClientInterfacePerf == true) printf("START === Client thread\n"); uint64 loopCount = 0; @@ -151,7 +132,6 @@ void ClientInterfaceThread::execute() { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n",__FILE__,__FUNCTION__,__LINE__,this); } catch(const exception &ex) { - //setRunningStatus(false); SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -184,6 +164,8 @@ ClientInterface::ClientInterface() : GameNetworkInterface() { cachedPendingCommandsIndex = 0; cachedLastPendingFrameCount = 0; + flagAccessor = new Mutex(); + this->readyForInGameJoin = false; clientSocket= NULL; sessionKey = 0; @@ -275,11 +257,32 @@ ClientInterface::~ClientInterface() { networkCommandListThreadAccessor = NULL; safeMutex.ReleaseLock(false,true); + delete flagAccessor; + flagAccessor = NULL; //printf("END === Client destructor\n"); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } +bool ClientInterface::getJoinGameInProgress() { + MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); + return joinGameInProgress; +} +bool ClientInterface::getJoinGameInProgressLaunch() { + MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); + return joinGameInProgressLaunch; +} + +bool ClientInterface::getReadyForInGameJoin() { + MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); + return readyForInGameJoin; +} + +bool ClientInterface::getResumeInGameJoin() { + MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); + return resumeInGameJoin; +} + void ClientInterface::connect(const Ip &ip, int port) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); @@ -445,8 +448,11 @@ void ClientInterface::updateLobby() { playerIndex= networkMessageIntro.getPlayerIndex(); serverName= networkMessageIntro.getName(); serverFTPPort = networkMessageIntro.getFtpPort(); + + MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); this->joinGameInProgress = networkMessageIntro.getGameInProgress(); this->joinGameInProgressLaunch = false; + safeMutexFlags.ReleaseLock(); //printf("Client got intro playerIndex = %d\n",playerIndex); @@ -832,6 +838,7 @@ void ClientInterface::updateLobby() { { NetworkMessageReady networkMessageReady; if(receiveMessage(&networkMessageReady)) { + MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); this->readyForInGameJoin = true; } @@ -1309,8 +1316,11 @@ bool ClientInterface::isMasterServerAdminOverride() { void ClientInterface::waitUntilReady(Checksum* checksum) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); bool signalServerWhenReadyToStartJoinedGame = this->readyForInGameJoin; this->readyForInGameJoin = false; + safeMutexFlags.ReleaseLock(); + Logger &logger= Logger::getInstance(); Chrono chrono; @@ -1554,7 +1564,7 @@ void ClientInterface::waitUntilReady(Checksum* checksum) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); //check checksum - if(this->joinGameInProgress == false && networkMessageReady.getChecksum() != checksum->getSum()) { + if(getJoinGameInProgress() == false && networkMessageReady.getChecksum() != checksum->getSum()) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); Lang &lang= Lang::getInstance(); @@ -1631,14 +1641,17 @@ void ClientInterface::waitUntilReady(Checksum* checksum) { return; } + MutexSafeWrapper safeMutexFlags2(flagAccessor,CODE_AT_LINE); this->joinGameInProgress = false; this->joinGameInProgressLaunch = false; //printf("Client signalServerWhenReadyToStartJoinedGame = %d\n",signalServerWhenReadyToStartJoinedGame); if(signalServerWhenReadyToStartJoinedGame == true) { this->resumeInGameJoin = true; + safeMutexFlags2.ReleaseLock(); } else { + safeMutexFlags2.ReleaseLock(); // delay the start a bit, so clients have more room to get messages // This is to ensure clients don't start ahead of the server and thus // constantly freeze because they are waiting for the server to catch up @@ -1837,6 +1850,7 @@ void ClientInterface::close(bool lockMutex) { connectedTime = 0; gotIntro = false; + MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); this->joinGameInProgress = false; this->joinGameInProgressLaunch = false; this->readyForInGameJoin = false; @@ -2044,9 +2058,11 @@ void ClientInterface::broadcastGameSetup(const GameSettings *gameSettings) { void ClientInterface::broadcastGameStart(const GameSettings *gameSettings) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); if(this->joinGameInProgress == true) { this->joinGameInProgressLaunch = true; } + safeMutexFlags.ReleaseLock(); //printf("Sending game launch joinGameInProgress: %d\n",joinGameInProgress); diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h index aa3edf9e..1ee976f8 100644 --- a/source/glest_game/network/client_interface.h +++ b/source/glest_game/network/client_interface.h @@ -34,9 +34,6 @@ class ClientInterfaceThread : public BaseThread, public SlaveThreadControllerInt protected: ClientInterface *clientInterface; - //Semaphore semTaskSignalled; - //Mutex *triggerIdMutex; - //MasterSlaveThreadController *masterController; virtual void setQuitStatus(bool value); @@ -45,7 +42,6 @@ public: virtual ~ClientInterfaceThread(); virtual void execute(); - //virtual void setMasterController(MasterSlaveThreadController *master) { masterController = master; } virtual void setMasterController(MasterSlaveThreadController *master) { } virtual void signalSlave(void *userdata) { } @@ -57,7 +53,6 @@ public: // ===================================================== class ClientInterface: public GameNetworkInterface { - //public SimpleTaskCallbackInterface { private: static const int messageWaitTimeout; static const int waitSleepTime; @@ -86,7 +81,6 @@ private: int sessionKey; int serverFTPPort; - //SimpleTaskThread *networkCommandListThread; ClientInterfaceThread *networkCommandListThread; Mutex *networkCommandListThreadAccessor; @@ -94,6 +88,7 @@ private: uint64 cachedPendingCommandsIndex; uint64 cachedLastPendingFrameCount; + Mutex *flagAccessor; bool joinGameInProgress; bool joinGameInProgressLaunch; bool readyForInGameJoin; @@ -106,15 +101,14 @@ public: virtual ~ClientInterface(); virtual Socket* getSocket(bool mutexLock=true) {return clientSocket;} - //virtual const Socket* getSocket() const {return clientSocket;} virtual void close(); - bool getJoinGameInProgress() const { return joinGameInProgress; } - bool getJoinGameInProgressLaunch() const { return joinGameInProgressLaunch; } + bool getJoinGameInProgress(); + bool getJoinGameInProgressLaunch(); - bool getReadyForInGameJoin() const { return readyForInGameJoin; } + bool getReadyForInGameJoin(); - bool getResumeInGameJoin() const { return resumeInGameJoin; } + bool getResumeInGameJoin(); void sendResumeGameMessage(); uint64 getCachedLastPendingFrameCount(); @@ -147,7 +141,6 @@ public: int getGameSettingsReceivedCount() const { return gameSettingsReceivedCount; } int getPlayerIndex() const {return playerIndex;} - //const GameSettings *getGameSettings() {return &gameSettings;} void connect(const Ip &ip, int port); void reset(); @@ -178,7 +171,6 @@ public: void broadcastGameSetup(const GameSettings *gameSettings); void broadcastGameStart(const GameSettings *gameSettings); - //virtual void simpleTask(BaseThread *callingThread); void updateNetworkFrame(); virtual void saveGame(XmlNode *rootNode) {};