diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 6e9e7c18..9a7fb0ce 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -856,6 +856,16 @@ void MenuStateCustomGame::cleanup() { delete publishToMasterserverThread; if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } + else { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s %d] Error cannot shutdown publishToMasterserverThread\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s",szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); + + publishToMasterserverThread->setOverrideShutdownTask(shutdownTaskStatic); + //publishToMasterserverThread->cleanup(); + } publishToMasterserverThread = NULL; } else { @@ -865,6 +875,16 @@ void MenuStateCustomGame::cleanup() { publishToMasterserverThread->shutdownAndWait() == true) { delete publishToMasterserverThread; } + else { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s %d] Error cannot shutdown publishToMasterserverThread\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s",szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); + + publishToMasterserverThread->setOverrideShutdownTask(shutdownTaskStatic); + //publishToMasterserverThread->cleanup(); + } } publishToMasterserverThread = NULL; @@ -897,15 +917,6 @@ void MenuStateCustomGame::returnToParentMenu() { needToRepublishToMasterserver = false; lastNetworkPing = time(NULL); ParentMenuState parentMenuState = this->parentMenuState; -/* - if(publishToMasterserverThread != NULL && - publishToMasterserverThread->canShutdown() == true && - publishToMasterserverThread->shutdownAndWait() == true) { - publishToMasterserverThread->setThreadOwnerValid(false); - delete publishToMasterserverThread; - publishToMasterserverThread = NULL; - } -*/ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); @@ -2567,10 +2578,18 @@ void MenuStateCustomGame::publishToMasterserver() { } void MenuStateCustomGame::setupTask(BaseThread *callingThread) { + MenuStateCustomGame::setupTaskStatic(callingThread); +} +void MenuStateCustomGame::shutdownTask(BaseThread *callingThread) { + MenuStateCustomGame::shutdownTaskStatic(callingThread); +} +void MenuStateCustomGame::setupTaskStatic(BaseThread *callingThread) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); CURL *handle = SystemFlags::initHTTP(); callingThread->setGenericData(handle); } -void MenuStateCustomGame::shutdownTask(BaseThread *callingThread) { +void MenuStateCustomGame::shutdownTaskStatic(BaseThread *callingThread) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); CURL *handle = callingThread->getGenericData(); SystemFlags::cleanupHTTP(&handle); } diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index 6d1b9345..dc3b8288 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -211,6 +211,8 @@ public: virtual void simpleTask(BaseThread *callingThread); virtual void setupTask(BaseThread *callingThread); virtual void shutdownTask(BaseThread *callingThread); + static void setupTaskStatic(BaseThread *callingThread); + static void shutdownTaskStatic(BaseThread *callingThread); virtual bool isInSpecialKeyCaptureEvent(); virtual bool isMasterserverMode() const; diff --git a/source/shared_lib/include/platform/common/simple_threads.h b/source/shared_lib/include/platform/common/simple_threads.h index bf268b64..1ee20490 100644 --- a/source/shared_lib/include/platform/common/simple_threads.h +++ b/source/shared_lib/include/platform/common/simple_threads.h @@ -62,7 +62,7 @@ public: // ===================================================== // class SimpleTaskThread // ===================================================== - +typedef void taskFunctionCallback(BaseThread *callingThread); // // This interface describes the methods a callback object must implement // @@ -70,8 +70,8 @@ class SimpleTaskCallbackInterface { public: virtual void simpleTask(BaseThread *callingThread) = 0; - virtual void setupTask(BaseThread *callingThread) {} - virtual void shutdownTask(BaseThread *callingThread) {} + virtual void setupTask(BaseThread *callingThread) { } + virtual void shutdownTask(BaseThread *callingThread) { } }; class SimpleTaskThread : public BaseThread @@ -89,6 +89,8 @@ protected: Mutex mutexLastExecuteTimestamp; time_t lastExecuteTimestamp; + taskFunctionCallback *overrideShutdownTask; + public: SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface, unsigned int executionCount=0, @@ -103,6 +105,10 @@ public: bool getTaskSignalled(); bool isThreadExecutionLagging(); + + void cleanup(); + + void setOverrideShutdownTask(taskFunctionCallback *ptr); }; // ===================================================== diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index 1d5f554f..5a8e3231 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -296,6 +296,8 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter this->executionCount = executionCount; this->millisecsBetweenExecutions = millisecsBetweenExecutions; this->needTaskSignal = needTaskSignal; + this->overrideShutdownTask = NULL; + setTaskSignalled(false); static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); @@ -308,10 +310,7 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter SimpleTaskThread::~SimpleTaskThread() { try { - if(this->simpleTaskInterface != NULL) { - this->simpleTaskInterface->shutdownTask(this); - this->simpleTaskInterface = NULL; - } + cleanup(); } catch(const exception &ex) { SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); @@ -322,6 +321,21 @@ SimpleTaskThread::~SimpleTaskThread() { } } +void SimpleTaskThread::cleanup() { + if(this->overrideShutdownTask != NULL) { + this->overrideShutdownTask(this); + this->overrideShutdownTask = NULL; + } + else if(this->simpleTaskInterface != NULL) { + this->simpleTaskInterface->shutdownTask(this); + this->simpleTaskInterface = NULL; + } +} + +void SimpleTaskThread::setOverrideShutdownTask(taskFunctionCallback *ptr) { + this->overrideShutdownTask = ptr; +} + bool SimpleTaskThread::isThreadExecutionLagging() { bool result = false; static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 05478fca..487c62c9 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -155,6 +155,9 @@ std::string SystemFlags::getHTTP(std::string URL,CURL *handle,int timeOut,CURLco /* get contents from the URL */ CURLcode result = curl_easy_perform(handle); + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("CURL result = %d\n",result); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("CURL errbuf [%s]\n",errbuf); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] return code [%d] [%s]\n",__FILE__,__FUNCTION__,__LINE__,result,errbuf); std::string serverResponse = (chunk.memory != NULL ? chunk.memory : "");