diff --git a/components/spit/include/SAD_Client.h b/components/spit/include/SAD_Client.h index 9bd309a..440d2e7 100644 --- a/components/spit/include/SAD_Client.h +++ b/components/spit/include/SAD_Client.h @@ -18,6 +18,9 @@ #include "SAD_DetectedServer.h" #include #include +#include "Mutex.hpp" +#include "Semaphore.hpp" +#include "Task.hpp" static const string SERVERAUTODETECT_PRECODE = "#SAD#"; @@ -25,39 +28,28 @@ static const string SERVERAUTODETECT_SEPERATOR = "#,#"; static const string SERVERAUTODETECT_TASKID = "#spit#"; static const string SERVERAUTODETECT_PORT = "15110"; - - class SAD_Client { //----- SAD Client Interface ------ private: - static recursive_mutex s_MutexClientInterface; static SAD_ClientInterface* s_SADClientInterface; - - - - - - //----- SAD_Client ----- public: private: - static mutex s_MutexSAD; - static condition_variable s_ConditionStopSAD; + static fos::Mutex s_MutexSAD; + static fos::Semaphore s_ConditionStopSAD; static bool s_StopSAD; - static thread* s_ThreadSend; - static thread* s_ThreadReceiveIP4; - static thread* s_ThreadReceiveIP6; + static fos::Task* s_ThreadSend; + static fos::Task* s_ThreadReceiveIP4; - static recursive_mutex s_MutexSocket; + static fos::Mutex s_MutexSocket; static SPITSocket* s_SPITSocket; - static void sendAutoDetectServer(); static void cycleStart(); static void cycleEnd(); + static void sendAutoDetectServer(); static void receiveAutoDetectServer_IP4(); - static void receiveAutoDetectServer_IP6(); public: static void startAutoDetectServer(SAD_ClientInterface* p_SADClientInterface, SPITSocket* p_SPITSocket); diff --git a/components/spit/include/SAD_DetectedServer.h b/components/spit/include/SAD_DetectedServer.h index 4f77f62..5cf3705 100644 --- a/components/spit/include/SAD_DetectedServer.h +++ b/components/spit/include/SAD_DetectedServer.h @@ -17,7 +17,7 @@ #include "SAD_ClientInterface.h" #include -#include +#include "Mutex.hpp" #include #include @@ -175,7 +175,7 @@ private: } private: - static recursive_mutex s_MutexDetectedServer; + static fos::Mutex s_MutexDetectedServer; static unordered_map s_DetectedServers; public: static const int PARAMINDEX_PRECODE = 0; @@ -222,7 +222,7 @@ public: static const SAD_DetectedServer& getRefDetectedServer(string p_ServerID) { SAD_DetectedServer* ptrDetectedServer = NULL; { - unique_lock o_Lock(s_MutexDetectedServer); + fos::MutexGuard o_Lock(s_MutexDetectedServer); auto i = s_DetectedServers.find(p_ServerID); if (i != s_DetectedServers.end()) { ptrDetectedServer = i->second; diff --git a/components/spit/include/SPITObject.h b/components/spit/include/SPITObject.h index 46b3564..51414e5 100644 --- a/components/spit/include/SPITObject.h +++ b/components/spit/include/SPITObject.h @@ -14,12 +14,11 @@ #ifndef SPITOBJECT_H #define SPITOBJECT_H -//#include "_SPITMACROS.h" #include "SPIT_Message.h" #include "SPITType.h" #include #include -#include +#include "Mutex.hpp" using namespace std; @@ -28,11 +27,11 @@ class SPITObjectListener; class SPITObject { private: - static recursive_mutex s_MutexSPITObject; + static fos::Mutex s_MutexSPITObject; static unordered_map s_SPITObjects; static unordered_map s_SPITObjectsRemote; public: - static recursive_mutex* getMutex() { + static fos::Mutex* getMutex() { return &s_MutexSPITObject; } static SPITObject* getSPITObject(string p_ID); diff --git a/components/spit/include/SPITPlayer.h b/components/spit/include/SPITPlayer.h index 64150dc..d15f870 100644 --- a/components/spit/include/SPITPlayer.h +++ b/components/spit/include/SPITPlayer.h @@ -119,8 +119,8 @@ public: //----- SPITPlayer ------ public: - static recursive_mutex s_Mutex; - static recursive_mutex* getMutex() { + static fos::Mutex s_Mutex; + static fos::Mutex* getMutex() { return &s_Mutex; } //list the SPITPlayers diff --git a/components/spit/include/SPITSocket.h b/components/spit/include/SPITSocket.h index 4142cb8..a9cc8b7 100644 --- a/components/spit/include/SPITSocket.h +++ b/components/spit/include/SPITSocket.h @@ -16,7 +16,7 @@ #ifndef SPITSOCKET_H #define SPITSOCKET_H #include "_SPITMACROS.h" -#include +#include "Mutex.hpp" #include #include @@ -59,7 +59,7 @@ public: }; private: - static recursive_mutex s_MutexIPAddresses; + static fos::Mutex s_MutexIPAddresses; public: static unordered_map s_IPAddresses; static void addIPAddress(string p_IPAddress, string p_SubnetMask); @@ -83,7 +83,7 @@ private: string m_BoundClientPort; //port set after bind int m_BoundClientFamily; bool m_IsBound; - recursive_mutex m_MutexSocket; + fos::Mutex m_MutexSocket; public: /** * Sets the ip address and port for server and client. diff --git a/components/spit/include/SPITType.h b/components/spit/include/SPITType.h index beb1fda..713e67d 100644 --- a/components/spit/include/SPITType.h +++ b/components/spit/include/SPITType.h @@ -18,7 +18,7 @@ #include "SPIT_Message.h" #include #include -#include +#include "Mutex.hpp" using namespace std; @@ -27,14 +27,14 @@ class SPITTypeListener; class SPITType { private: - static recursive_mutex s_MutexSPITType; + static fos::Mutex s_MutexSPITType; static unordered_map s_SPITTypes; static unordered_map s_SPITTypesRemote; public: - static recursive_mutex* getMutex() { + static fos::Mutex* getMutex() { return &s_MutexSPITType; } static SPITType* getSPITType(string p_ID); diff --git a/components/spit/include/SPIT_Client.h b/components/spit/include/SPIT_Client.h index f24e096..69a65dd 100644 --- a/components/spit/include/SPIT_Client.h +++ b/components/spit/include/SPIT_Client.h @@ -29,7 +29,6 @@ #include "SPIT_Handler.h" #include "SPITPlayer.h" -#include using namespace std; //static char nullChar[] = "\0"; @@ -81,8 +80,8 @@ private: /** SPITClientInterface, normally the user app should implement this*/ SPIT_ClientInterface* m_SPITClientInterface; SPITSocket* m_SPITSocket; - recursive_mutex m_MutexSPITSocket; - thread* m_ThreadReceive; + fos::Mutex m_MutexSPITSocket; + fos::TaskT * m_ThreadReceive; bool m_StopReceiveSPITMessages; int m_ConnectionStatus; @@ -153,7 +152,7 @@ public: private: bool startReceiveSPITMessages(); void stopReceiveSPITMessages(); - void receiveSPITMessage(); + static void receiveSPITMessage(SPIT_Client& rM); int sendSPITMessage(SPIT_Message::OPPart* p_SPITMessageOPPart); void fillHeader(SPIT_Message::Header* p_Header); diff --git a/components/spit/include/SPIT_Handler.h b/components/spit/include/SPIT_Handler.h index 8923164..43be675 100644 --- a/components/spit/include/SPIT_Handler.h +++ b/components/spit/include/SPIT_Handler.h @@ -14,9 +14,9 @@ #ifndef SPIT_HANDLER_H #define SPIT_HANDLER_H -#include -#include -#include +#include "Mutex.hpp" +#include "Semaphore.hpp" +#include "Task.hpp" #include using namespace std; @@ -32,9 +32,9 @@ public: private: static list s_Runnables; - static mutex s_MutexLock; - static condition_variable s_ConditionVariable; - static thread s_HandlerThread; + static fos::Mutex s_MutexLock; + static fos::Semaphore s_ConditionVariable; + static fos::Task* s_HandlerThread; static bool s_WakeUp; static bool s_Close; diff --git a/components/spit/include/UnixSocket.h b/components/spit/include/UnixSocket.h index 807b236..1cd2aa5 100644 --- a/components/spit/include/UnixSocket.h +++ b/components/spit/include/UnixSocket.h @@ -22,7 +22,6 @@ #include #include #include -#include #include "SPITSocket.h" diff --git a/components/spit/include/Watchdog.h b/components/spit/include/Watchdog.h index b38e56a..e354705 100644 --- a/components/spit/include/Watchdog.h +++ b/components/spit/include/Watchdog.h @@ -15,9 +15,9 @@ #define WATCHDOG_H #include "_SPITMACROS.h" -#include -#include -#include +#include "Mutex.hpp" +#include "Semaphore.hpp" +#include "Task.hpp" using namespace std; @@ -37,15 +37,15 @@ public: virtual ~Watchdog(); private: - Listener* m_Listener; + Listener* m_Listener; - mutex m_MutexLock; - condition_variable m_ConditionVariable; - thread* m_WatchdogThread; + fos::Mutex m_MutexLock; + fos::Semaphore m_ConditionVariable; + fos::TaskT* m_WatchdogThread; bool m_Answered; - void runWatchdog(); + static void runWatchdog(Watchdog& wd); public: void startWatchdog(); void answerWatchdog(); diff --git a/components/spit/include/_SPITMACROS.h b/components/spit/include/_SPITMACROS.h index ed10d27..fe10572 100644 --- a/components/spit/include/_SPITMACROS.h +++ b/components/spit/include/_SPITMACROS.h @@ -5,9 +5,6 @@ // To output debug information (printf calls in the code) // you must set or comment out this precompiler definition -#ifndef DEBUGOUTPUT -#define DEBUGOUTPUT -#endif #include using namespace std; diff --git a/components/spit/src/MyApp.cpp b/components/spit/src/MyApp.cpp index 774a13c..58b093c 100644 --- a/components/spit/src/MyApp.cpp +++ b/components/spit/src/MyApp.cpp @@ -2,11 +2,16 @@ #include "MyApp.h" #include "Task.hpp" #include "esp_log.h" +#include "SPIT_Handler.h" string MyApp::s_ClientName = "MySPITApp_Client"; //Name of this Client const char * TAG = "SPIT"; + void SpitClientApp() { + ESP_LOGI(TAG, "Starting SPIT task"); + // fos::Task spit(SPIT_Handler::run,"SpitHandl"); + auto ptrMyApp = std::make_unique(); //load a client project diff --git a/components/spit/src/SAD_Client.cpp b/components/spit/src/SAD_Client.cpp index 71c9894..5fc1929 100644 --- a/components/spit/src/SAD_Client.cpp +++ b/components/spit/src/SAD_Client.cpp @@ -17,16 +17,14 @@ //----- initialze static members ----- bool SAD_Client::s_StopSAD = true; -mutex SAD_Client::s_MutexSAD; -condition_variable SAD_Client::s_ConditionStopSAD; -thread* SAD_Client::s_ThreadSend = NULL; -thread* SAD_Client::s_ThreadReceiveIP4 = NULL; -thread* SAD_Client::s_ThreadReceiveIP6 = NULL; +fos::Mutex SAD_Client::s_MutexSAD; +fos::Semaphore SAD_Client::s_ConditionStopSAD; +fos::Task* SAD_Client::s_ThreadSend = NULL; +fos::Task* SAD_Client::s_ThreadReceiveIP4 = NULL; -recursive_mutex SAD_Client::s_MutexClientInterface; SAD_ClientInterface* SAD_Client::s_SADClientInterface = NULL; -recursive_mutex SAD_Client::s_MutexSocket; +fos::Mutex SAD_Client::s_MutexSocket; SPITSocket* SAD_Client::s_SPITSocket = NULL; @@ -35,12 +33,9 @@ void SAD_Client::startAutoDetectServer(SAD_ClientInterface* p_SADClientInterface if (p_SADClientInterface == NULL) return; if (p_SpitSocket == NULL) return; + s_SADClientInterface = p_SADClientInterface; { - unique_lock o_LockClient(s_MutexClientInterface); - s_SADClientInterface = p_SADClientInterface; - } - { - unique_lock o_UniqueLock(s_MutexSAD); + fos::MutexGuard o_UniqueLock(s_MutexSAD); if (s_StopSAD == false) return; s_StopSAD = false; SAD_DetectedServer::clearDetectedServers(); @@ -48,62 +43,38 @@ void SAD_Client::startAutoDetectServer(SAD_ClientInterface* p_SADClientInterface s_SPITSocket = p_SpitSocket; - s_ThreadSend = new thread(&SAD_Client::sendAutoDetectServer); - s_ThreadReceiveIP4 = new thread(&SAD_Client::receiveAutoDetectServer_IP4); - s_ThreadReceiveIP6 = new thread(&SAD_Client::receiveAutoDetectServer_IP6); + s_ThreadSend = new fos::Task(SAD_Client::sendAutoDetectServer, "sendDetect"); + s_ThreadReceiveIP4 = new fos::Task(SAD_Client::receiveAutoDetectServer_IP4, "getDetect"); } + void SAD_Client::stopAutoDetectServer(SAD_ClientInterface* p_SADClientInterface) { - bool boolStopDetection = false; + if (s_SADClientInterface != p_SADClientInterface) return; { - unique_lock o_LockClient(s_MutexClientInterface); - if (s_SADClientInterface != p_SADClientInterface) { - return; - } - } - - { - unique_lock o_UniqueLock(s_MutexSAD); + fos::MutexGuard o_UniqueLock(s_MutexSAD); if (s_StopSAD == true) return; s_StopSAD = true; } - s_ConditionStopSAD.notify_one(); + s_ConditionStopSAD.give(); if (s_ThreadSend != NULL) { - if (this_thread::get_id() != s_ThreadSend->get_id()) { - if (s_ThreadSend->joinable()) { - s_ThreadSend->join(); - } - } + delete s_ThreadSend; s_ThreadSend = NULL; } //wait for receiving threads if (s_ThreadReceiveIP4 != NULL) { - if (this_thread::get_id() != s_ThreadReceiveIP4->get_id()) { - if (s_ThreadReceiveIP4->joinable()) { - s_ThreadReceiveIP4->join(); - } - } + delete s_ThreadReceiveIP4; s_ThreadReceiveIP4 = NULL; } - if (s_ThreadReceiveIP6 != NULL) { - if (this_thread::get_id() != s_ThreadReceiveIP6->get_id()) { - if (s_ThreadReceiveIP6->joinable()) { - s_ThreadReceiveIP6->join(); - } - } - s_ThreadReceiveIP6 = NULL; - } - { - unique_lock o_LockSocket(s_MutexSocket); + fos::MutexGuard o_LockSocket(s_MutexSocket); if (s_SPITSocket != NULL) { delete s_SPITSocket; s_SPITSocket = NULL; } } { - unique_lock o_UniqueLock(s_MutexSAD); + fos::MutexGuard o_UniqueLock(s_MutexSAD); SAD_DetectedServer::clearDetectedServers(); } } @@ -111,8 +82,6 @@ void SAD_Client::stopAutoDetectServer(SAD_ClientInterface* p_SADClientInterface) void SAD_Client::sendAutoDetectServer() { SPITSocket::IPAddress* o_IPAddress; string stringBroadcastIP4; - string stringBroadcastIP6; - string stringBroadcastIP6_loopback; string stringDatagram; int intDatagramLength; bool boolDone; @@ -121,17 +90,13 @@ void SAD_Client::sendAutoDetectServer() { stringDatagram.append(SERVERAUTODETECT_SEPERATOR); stringDatagram.append(SERVERAUTODETECT_TASKID); intDatagramLength = stringDatagram.length(); - - stringBroadcastIP6 = "ff02::1"; - stringBroadcastIP6_loopback = "::1"; while(s_StopSAD == false) { //---- IP4 --- cycleStart(); { - unique_lock o_LockSocket(s_MutexSocket); - + fos::MutexGuard o_LockSocket(s_MutexSocket); if (s_SPITSocket == NULL) { return; } @@ -146,17 +111,6 @@ void SAD_Client::sendAutoDetectServer() { else { } } - //---- IP6 --- send to "ff02::1" and to the loopback address "::1" because MAC OSX seems to to need the loopback over IP6 - //"ff02::1" - if (s_SPITSocket->sendMessageTo(SPITSocket::FAMILY_IP6, stringBroadcastIP6, SERVERAUTODETECT_PORT, stringDatagram.data(), stringDatagram.length()) < 0) { - } - else { - } - //"::1" loopback - if (s_SPITSocket->sendMessageTo(SPITSocket::FAMILY_IP6, stringBroadcastIP6_loopback, SERVERAUTODETECT_PORT, stringDatagram.data(), stringDatagram.length()) < 0) { - } - else { - } } { @@ -165,8 +119,12 @@ void SAD_Client::sendAutoDetectServer() { } //wait for 3000 milliseconds or //s_StopAutoDetectServer == true see stopAutoDetectServer - unique_lock o_LockWait(s_MutexSAD); - boolDone = s_ConditionStopSAD.wait_for(o_LockWait, chrono::milliseconds(3000),[]{return s_StopSAD;}); + fos::MutexGuard o_LockWait(s_MutexSAD); + boolDone = s_ConditionStopSAD.take(3000); + if (not boolDone) // timeout occurred + { + s_StopSAD = true; + } if (s_StopSAD == true) { return; } @@ -190,11 +148,11 @@ void SAD_Client::receiveAutoDetectServer_IP4() { //sendAutoDetectServer will bind the socket to a port //wait a while to be sendAutoDetectServer first - this_thread::sleep_for(chrono::milliseconds(500)); + fos::Task::delayMs(500); while (s_StopSAD == false) { { - unique_lock o_LockIP4(s_MutexSocket); + fos::MutexGuard o_LockIP4(s_MutexSocket); if (s_SPITSocket == NULL) { return; } @@ -203,7 +161,7 @@ void SAD_Client::receiveAutoDetectServer_IP4() { if (intReceivedByteCount <= 0) { if (s_StopSAD == false) { //To reduce the processor load if a socket error occurs, we let the thread sleep a bit. - this_thread::sleep_for(chrono::milliseconds(100)); + fos::Task::delayMs(100); } continue; } @@ -242,77 +200,10 @@ void SAD_Client::receiveAutoDetectServer_IP4() { } int inTest = 0; } -void SAD_Client::receiveAutoDetectServer_IP6() { - char buffer[512+1]; - int intReceivedByteCount; - string stringMessage; - string stringServerIP; - - int intIndex = 0; - int intPos = 0; - int intOldPos = 0; - string stringPart; - string stringParts[9]; - - //sendto will bind the socket to a port - //wait a while to be sendto first - this_thread::sleep_for(chrono::milliseconds(500)); - - while (s_StopSAD == false) { - { - unique_lock o_LockIP6(s_MutexSocket); - if (s_SPITSocket == NULL) { - return; - } - } - - intReceivedByteCount = s_SPITSocket->receiveMessage(SPITSocket::FAMILY_IP6, buffer, 512, &stringServerIP); - if (intReceivedByteCount <= 0) { - if (s_StopSAD == false) { - //To reduce the processor load if a socket error occurs, we let the thread sleep a bit. - this_thread::sleep_for(chrono::milliseconds(100)); - } - continue; - } - if (s_StopSAD == true) { - return; - } - //add null chararcter - if (intReceivedByteCount < 512) { - buffer[intReceivedByteCount] = '\0'; - } - else { - buffer[512] = '\0'; - } - stringMessage = buffer; - //Check if precode is ok - if (stringMessage.find(SERVERAUTODETECT_PRECODE.data(),0) != 0) { - return; - } - intIndex = 0; - intPos = 0; - intOldPos = 0; - while(intIndex < 8) { - intPos = stringMessage.find(SERVERAUTODETECT_SEPERATOR.data(),intOldPos); - if (intPos == string::npos) { - stringPart = ""; - } - else { - stringPart = stringMessage.substr(intOldPos, intPos-intOldPos); - intOldPos = intPos + SERVERAUTODETECT_SEPERATOR.length(); - } - stringParts[intIndex] = stringPart; - intIndex++; - } - stringParts[8] = stringServerIP; - SAD_DetectedServer::addDetectedServer(false, stringParts[SAD_DetectedServer::PARAMINDEX_ID], stringParts); - } - int inTest = 0; -} void SAD_Client::cycleStart() { { - unique_lock o_UniqueLock(s_MutexSAD); + fos::MutexGuard o_UniqueLock(s_MutexSAD); if (s_StopSAD == true) return; } // printf("SAD CYCLE START\n"); @@ -320,7 +211,7 @@ void SAD_Client::cycleStart() { } void SAD_Client::cycleEnd() { { - unique_lock o_UniqueLock(s_MutexSAD); + fos::MutexGuard o_UniqueLock(s_MutexSAD); if (s_StopSAD == true) return; } SAD_DetectedServer::cycleEnd(s_SADClientInterface); diff --git a/components/spit/src/SAD_DetectedServer.cpp b/components/spit/src/SAD_DetectedServer.cpp index 745a7e6..5179e16 100644 --- a/components/spit/src/SAD_DetectedServer.cpp +++ b/components/spit/src/SAD_DetectedServer.cpp @@ -14,7 +14,7 @@ #include "SAD_DetectedServer.h" -recursive_mutex SAD_DetectedServer::s_MutexDetectedServer; +fos::Mutex SAD_DetectedServer::s_MutexDetectedServer; unordered_map SAD_DetectedServer::s_DetectedServers; SAD_DetectedServer::~SAD_DetectedServer() { @@ -56,7 +56,7 @@ void SAD_DetectedServer::setAdditions(string p_Additions) { void SAD_DetectedServer::cycleStart() { { - unique_locko_lock(s_MutexDetectedServer); + fos::MutexGuard o_lock(s_MutexDetectedServer); for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) { i->second->startCycleSAD(); } @@ -67,7 +67,7 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) { list o_NewServers; SAD_DetectedServer* ptrDetectedServer; { - unique_lock o_lock(s_MutexDetectedServer); + fos::MutexGuard o_lock(s_MutexDetectedServer); for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) { ptrDetectedServer = i->second; if (ptrDetectedServer->getFoundAgain() == false) { @@ -126,7 +126,7 @@ void SAD_DetectedServer::cycleEnd(SAD_ClientInterface* p_SADClientInterface) { void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, string p_ServerParams[]) { SAD_DetectedServer* ptrDetectedServer; { - unique_lock o_Lock(s_MutexDetectedServer); + fos::MutexGuard o_Lock(s_MutexDetectedServer); auto i = s_DetectedServers.find(p_ServerID); if (i != s_DetectedServers.end()) { ptrDetectedServer = i->second; @@ -138,16 +138,7 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri } ptrDetectedServer->setFoundAgain(); - if (p_ServerParams[PARAMINDEX_IP].find("127.") == 0 || - p_ServerParams[PARAMINDEX_IP].find("0:") == 0 || - p_ServerParams[PARAMINDEX_IP].find("0000:") == 0|| - p_ServerParams[PARAMINDEX_IP].find(":") == 0) { - //first prefer the loopback ip - ptrDetectedServer->setIP(p_ServerParams[PARAMINDEX_IP]); - ptrDetectedServer->setIPIsLoopback(true); - ptrDetectedServer->setClientIP(p_ServerParams[PARAMINDEX_CLIENTIP]); - } - else if (ptrDetectedServer->getIpIsLoopback() == false) { + if (ptrDetectedServer->getIpIsLoopback() == false) { //second prefer IP4 if (p_IsIP4 == true) { ptrDetectedServer->setIP(p_ServerParams[PARAMINDEX_IP]); @@ -166,7 +157,6 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri ptrDetectedServer->setAdditions(p_ServerParams[PARAMINDEX_ADDITIONS]); s_DetectedServers[ptrDetectedServer->getID()] = ptrDetectedServer; - } } @@ -177,7 +167,7 @@ void SAD_DetectedServer::addDetectedServer(bool p_IsIP4, string p_ServerID, stri void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer* p_DetectedServer) { SAD_DetectedServer* o_DetectedServer; { - unique_lock o_Lock(s_MutexDetectedServer); + fos::MutexGuard o_Lock(s_MutexDetectedServer); auto i = s_DetectedServers.find(p_DetectedServer->getID()); if (i != s_DetectedServers.end()) { o_DetectedServer = i->second; @@ -193,7 +183,7 @@ void SAD_DetectedServer::removeDetectedServer(SAD_DetectedServer* p_DetectedServ void SAD_DetectedServer::clearDetectedServers() { SAD_DetectedServer* ptrDetectedServer; { - unique_lock o_Lock(s_MutexDetectedServer); + fos::MutexGuard o_Lock(s_MutexDetectedServer); for (auto i = s_DetectedServers.begin(); i != s_DetectedServers.end(); i++) { ptrDetectedServer = i->second; i = s_DetectedServers.erase(i); diff --git a/components/spit/src/SPITObject.cpp b/components/spit/src/SPITObject.cpp index 89f3a19..b492a0e 100644 --- a/components/spit/src/SPITObject.cpp +++ b/components/spit/src/SPITObject.cpp @@ -17,11 +17,11 @@ unordered_map SPITObject::s_SPITObjects; unordered_map SPITObject::s_SPITObjectsRemote; - recursive_mutex SPITObject::s_MutexSPITObject; + fos::Mutex SPITObject::s_MutexSPITObject; SPITObject* SPITObject::getSPITObject(string p_ID) { { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); auto i = s_SPITObjects.find(p_ID); if (i == s_SPITObjects.end()) { return NULL; @@ -37,7 +37,7 @@ */ const SPITObject& SPITObject::getSPITObjectRef(string p_ID) { { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); auto i = s_SPITObjects.find(p_ID); if (i == s_SPITObjects.end()) { throw "SPITObject no SPITObject with given id"; @@ -48,7 +48,7 @@ SPITObject* SPITObject::getSPITObjectRemote(string p_ID) { { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); auto i = s_SPITObjectsRemote.find(p_ID); if (i == s_SPITObjectsRemote.end()) { return NULL; @@ -64,7 +64,7 @@ */ const SPITObject& SPITObject::getSPITObjectRemoteRef(string p_ID) { { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); auto i = s_SPITObjectsRemote.find(p_ID); if (i == s_SPITObjectsRemote.end()) { throw "Remote SPITObject no SPITObject with given id"; @@ -76,7 +76,7 @@ bool SPITObject::addSPITObject(SPITObject* p_ptrSPITObject) { if (p_ptrSPITObject == NULL) return false; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); s_SPITObjects[p_ptrSPITObject->getID()] = p_ptrSPITObject; return true; } @@ -84,7 +84,7 @@ bool SPITObject::addSPITObjectRemote(SPITObject* p_ptrSPITObject) { if (p_ptrSPITObject == NULL) return false; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); s_SPITObjectsRemote[p_ptrSPITObject->getID()] = p_ptrSPITObject; return true; } @@ -92,7 +92,7 @@ bool SPITObject::removeSPITObject(SPITObject* p_ptrSPITObject) { if (p_ptrSPITObject == NULL) return false; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); auto i = s_SPITObjects.find(p_ptrSPITObject->getID()); if (i == s_SPITObjects.end()) { return false; @@ -104,7 +104,7 @@ bool SPITObject::removeSPITObjectRemote(SPITObject* p_ptrSPITObject) { if (p_ptrSPITObject == NULL) return false; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); auto i = s_SPITObjectsRemote.find(p_ptrSPITObject->getID()); if (i == s_SPITObjectsRemote.end()) { return false; @@ -123,7 +123,7 @@ SPITObject* SPITObject::getSPITObject(int p_Index) { SPITObject* ptrSPITObject = NULL; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); if (p_Index >= 0 && p_Index < s_SPITObjects.size()) { unordered_map::iterator i = next(s_SPITObjects.begin(), p_Index); ptrSPITObject = i->second; @@ -143,7 +143,7 @@ const SPITObject& SPITObject::getSPITObjectRef(int p_Index) { SPITObject* ptrSPITObject = NULL; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); if (p_Index >= 0 && p_Index < s_SPITObjects.size()) { unordered_map::iterator i = next(s_SPITObjects.begin(), p_Index); ptrSPITObject = i->second; @@ -157,7 +157,7 @@ SPITObject* SPITObject::getSPITObjectRemote(int p_Index) { SPITObject* ptrSPITObject = NULL; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) { unordered_map::iterator i = next(s_SPITObjectsRemote.begin(), p_Index); ptrSPITObject = i->second; @@ -177,7 +177,7 @@ const SPITObject& SPITObject::getSPITObjectRemoteRef(int p_Index) { SPITObject* ptrSPITObject = NULL; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); if (p_Index >= 0 && p_Index < s_SPITObjectsRemote.size()) { unordered_map::iterator i = next(s_SPITObjectsRemote.begin(), p_Index); ptrSPITObject = i->second; @@ -192,7 +192,7 @@ void SPITObject::clearSPITObjects() { SPITObject* ptrSPITObject; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) { ptrSPITObject = i->second; i = s_SPITObjects.erase(i); @@ -206,7 +206,7 @@ void SPITObject::clearSPITObjectsRemote() { SPITObject* ptrSPITObject; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); for (auto i = s_SPITObjectsRemote.begin(); i != s_SPITObjectsRemote.end(); i++) { ptrSPITObject = i->second; i = s_SPITObjectsRemote.erase(i); @@ -227,7 +227,7 @@ void SPITObject::setConfirmedFalse() { SPITObject* ptrSPITObject; { - unique_lock o_LockObject(s_MutexSPITObject); + fos::MutexGuard o_LockObject(s_MutexSPITObject); for (auto i = s_SPITObjects.begin(); i != s_SPITObjects.end(); i++) { ptrSPITObject = i->second; ptrSPITObject->setConfirmed(false); diff --git a/components/spit/src/SPITPlayer.cpp b/components/spit/src/SPITPlayer.cpp index cae3f8b..5a8b059 100644 --- a/components/spit/src/SPITPlayer.cpp +++ b/components/spit/src/SPITPlayer.cpp @@ -14,12 +14,12 @@ #include "SPITPlayer.h" -recursive_mutex SPITPlayer::s_Mutex; +fos::Mutex SPITPlayer::s_Mutex; unordered_map SPITPlayer::s_SPITPlayers; SPITPlayer* SPITPlayer::getSPITPlayer(string p_PlayerID) { { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); auto i = s_SPITPlayers.find(p_PlayerID); if (i == s_SPITPlayers.end()) { s_SPITPlayers[p_PlayerID] = new SPITPlayer(p_PlayerID); @@ -33,7 +33,7 @@ SPITPlayer* SPITPlayer::getSPITPlayer(string p_PlayerID) { void SPITPlayer::clearPlayers() { SPITPlayer* ptrSPITPlayer; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); for (auto i = s_SPITPlayers.begin(); i != s_SPITPlayers.end(); i++) { ptrSPITPlayer = i->second; i = s_SPITPlayers.erase(i); @@ -54,7 +54,7 @@ SPITPlayer::SPITPlayer(string p_PlayerID) { SPITPlayer::~SPITPlayer() { { - unique_lock o_LockPlayer(s_Mutex); + fos::MutexGuard o_LockPlayer(s_Mutex); PlayObject* ptrPlayObject; for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) { ptrPlayObject = i->second; @@ -92,7 +92,7 @@ unordered_map* SPITPlayer::getPlayObjects() { SPITPlayer::PlayObject* SPITPlayer::addSPITObject_Playing (string p_SPITObjectID) { PlayObject* ptrPlayObject; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); auto i = m_PlayObjects.find(p_SPITObjectID); if (i == m_PlayObjects.end()) { ptrPlayObject = new PlayObject(this, p_SPITObjectID); @@ -111,7 +111,7 @@ SPITPlayer::PlayObject* SPITPlayer::addSPITObject_Playing (string p_SPITObjectID void SPITPlayer::startPlaySequence() { PlayObject* ptrPlayObject; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) { ptrPlayObject = i->second; ptrPlayObject->startPlaySequence(); @@ -131,7 +131,7 @@ SPITPlayer::PlayObject* SPITPlayer::getPlayObject(string p_SPITObjectID) { void SPITPlayer::endPlaySequence() { PlayObject* ptrPlayObject; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); for (auto i = m_PlayObjects.begin(); i != m_PlayObjects.end(); i++) { ptrPlayObject = i->second; if (ptrPlayObject->getIsPlayed() == false) { diff --git a/components/spit/src/SPITSocket.cpp b/components/spit/src/SPITSocket.cpp index 97afb73..f67ea2c 100644 --- a/components/spit/src/SPITSocket.cpp +++ b/components/spit/src/SPITSocket.cpp @@ -15,7 +15,7 @@ #include "SPITSocket.h" //----- initialize static members ----- -recursive_mutex SPITSocket::s_MutexIPAddresses; +fos::Mutex SPITSocket::s_MutexIPAddresses; unordered_map SPITSocket::s_IPAddresses; int SPITSocket::FAMILY_IP4 = 2; int SPITSocket::FAMILY_IP6 = 23; @@ -34,7 +34,7 @@ SPITSocket::SPITSocket() { SPITSocket::~SPITSocket() { { - unique_lock o_LockSocket(m_MutexSocket); + fos::MutexGuard o_LockSocket(m_MutexSocket); m_IsBound = false; } } @@ -71,7 +71,7 @@ void SPITSocket::setBoundClientFamily(int p_Family) { void SPITSocket::setIsBound(bool p_IsBound) { { - unique_lock o_LockSocket(m_MutexSocket); + fos::MutexGuard o_LockSocket(m_MutexSocket); m_IsBound = p_IsBound; } } @@ -83,7 +83,7 @@ int SPITSocket::sendMessage(const char* p_MessageBytes, int p_MessageLength) { if (m_ServerPort.length() <= 0) return -1; int intResult; { - unique_lock o_LockSocket(m_MutexSocket); + fos::MutexGuard o_LockSocket(m_MutexSocket); if (m_IsBound == false) return -1; intResult = sendMessageTo(m_BoundClientFamily, m_ServerIP, m_ServerPort, p_MessageBytes, p_MessageLength); } @@ -96,7 +96,7 @@ int SPITSocket::sendMessage(const char* p_MessageBytes, int p_MessageLength) { void SPITSocket::addIPAddress(string p_IPAddress, string p_SubnetMask) { IPAddress* o_IPAddress; { - unique_lock o_LockIP(s_MutexIPAddresses); + fos::MutexGuard o_LockIP(s_MutexIPAddresses); auto i = s_IPAddresses.find(p_IPAddress); if (i != s_IPAddresses.end()) { o_IPAddress = i->second; @@ -113,7 +113,7 @@ void SPITSocket::addIPAddress(string p_IPAddress, string p_SubnetMask) { void SPITSocket::removeIPAddress(string p_IPAddress) { IPAddress* o_IPAddress; { - unique_lock o_LockIP(s_MutexIPAddresses); + fos::MutexGuard o_LockIP(s_MutexIPAddresses); auto i = s_IPAddresses.find(p_IPAddress); if (i != s_IPAddresses.end()) { o_IPAddress = i->second; @@ -127,9 +127,8 @@ int SPITSocket::getIPAddressCount() { } SPITSocket::IPAddress* SPITSocket::getIPAddress(int p_Index) { IPAddress* ptr_IPAddress = NULL; - { - unique_lock o_LockIP(s_MutexIPAddresses); + fos::MutexGuard o_LockIP(s_MutexIPAddresses); if (p_Index <= s_IPAddresses.size()) { unordered_map::iterator i = next(s_IPAddresses.begin(), p_Index); ptr_IPAddress = i->second; @@ -140,7 +139,7 @@ SPITSocket::IPAddress* SPITSocket::getIPAddress(int p_Index) { void SPITSocket::clearIPAddresses() { IPAddress* o_IPAddress; { - unique_lock o_LockIP(s_MutexIPAddresses); + fos::MutexGuard o_LockIP(s_MutexIPAddresses); for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) { o_IPAddress = i->second; i = s_IPAddresses.erase(i); @@ -156,7 +155,7 @@ void SPITSocket::clearIPAddresses() { void SPITSocket::resetFoundIPAddresses() { IPAddress* o_IPAddress; { - unique_lock o_LockIP(s_MutexIPAddresses); + fos::MutexGuard o_LockIP(s_MutexIPAddresses); for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) { o_IPAddress = i->second; o_IPAddress->setFound(false); @@ -166,7 +165,7 @@ void SPITSocket::resetFoundIPAddresses() { void SPITSocket::removeNotFoundIPAddresses() { IPAddress* o_IPAddress; { - unique_lock o_LockIP(s_MutexIPAddresses); + fos::MutexGuard o_LockIP(s_MutexIPAddresses); for (auto i = s_IPAddresses.begin(); i != s_IPAddresses.end(); i++) { o_IPAddress = i->second; if (o_IPAddress->getFound() == false) { diff --git a/components/spit/src/SPITType.cpp b/components/spit/src/SPITType.cpp index 509b320..76f54b8 100644 --- a/components/spit/src/SPITType.cpp +++ b/components/spit/src/SPITType.cpp @@ -18,11 +18,11 @@ unordered_map SPITType::s_SPITTypes; unordered_map SPITType::s_SPITTypesRemote; - recursive_mutex SPITType::s_MutexSPITType; + fos::Mutex SPITType::s_MutexSPITType; SPITType* SPITType::getSPITType(string p_ID) { { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); auto i = s_SPITTypes.find(p_ID); if (i == s_SPITTypes.end()) { return NULL; @@ -38,7 +38,7 @@ */ const SPITType& SPITType::getSPITTypeRef(string p_ID) { { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); auto i = s_SPITTypes.find(p_ID); if (i == s_SPITTypes.end()) { throw "SPITType no SPITType with given ID"; @@ -49,7 +49,7 @@ SPITType* SPITType::getSPITTypeRemote(string p_ID) { { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); auto i = s_SPITTypesRemote.find(p_ID); if (i == s_SPITTypesRemote.end()) { return NULL; @@ -65,7 +65,7 @@ */ const SPITType& SPITType::getSPITTypeRemoteRef(string p_ID) { { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); auto i = s_SPITTypesRemote.find(p_ID); if (i == s_SPITTypesRemote.end()) { throw "Remote SPITType no SPITType with given ID"; @@ -77,7 +77,7 @@ bool SPITType::addSPITType(SPITType* p_ptrSpitType) { if (p_ptrSpitType == NULL) return false; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); s_SPITTypes[p_ptrSpitType->getID()] = p_ptrSpitType; return true; } @@ -85,7 +85,7 @@ bool SPITType::addSPITTypeRemote(SPITType* p_ptrSpitType) { if (p_ptrSpitType == NULL) return false; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); s_SPITTypesRemote[p_ptrSpitType->getID()] = p_ptrSpitType; return true; } @@ -93,7 +93,7 @@ bool SPITType::removeSPITType(SPITType* p_ptrSpitType) { if (p_ptrSpitType == NULL) return false; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); auto i = s_SPITTypes.find(p_ptrSpitType->getID()); if (i == s_SPITTypes.end()) { return false; @@ -105,7 +105,7 @@ bool SPITType::removeSPITTypeRemote(SPITType* p_ptrSpitType) { if (p_ptrSpitType == NULL) return false; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); auto i = s_SPITTypesRemote.find(p_ptrSpitType->getID()); if (i == s_SPITTypesRemote.end()) { return false; @@ -124,7 +124,7 @@ SPITType* SPITType::getSPITType(int p_Index) { SPITType* ptrSPITType = NULL; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); if (p_Index < s_SPITTypes.size()) { unordered_map::iterator i = next(s_SPITTypes.begin(), p_Index); ptrSPITType = i->second; @@ -141,7 +141,7 @@ const SPITType& SPITType::getSPITTypeRef(int p_Index) { SPITType* ptrSPITType = NULL; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); if (p_Index >= 0 && p_Index < s_SPITTypes.size()) { unordered_map::iterator i = next(s_SPITTypes.begin(), p_Index); ptrSPITType = i->second; @@ -155,7 +155,7 @@ SPITType* SPITType::getSPITTypeRemote(int p_Index) { SPITType* ptrSPITType = NULL; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); if (p_Index < s_SPITTypesRemote.size()) { unordered_map::iterator i = next(s_SPITTypesRemote.begin(), p_Index); ptrSPITType = i->second; @@ -172,7 +172,7 @@ const SPITType& SPITType::getSPITTypeRemoteRef(int p_Index) { SPITType* ptrSPITType = NULL; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); if (p_Index >= 0 && p_Index < s_SPITTypesRemote.size()) { unordered_map::iterator i = next(s_SPITTypesRemote.begin(), p_Index); ptrSPITType = i->second; @@ -187,7 +187,7 @@ void SPITType::clearSPITTypes() { SPITType* ptrSPITType; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); for (auto i = s_SPITTypes.begin(); i != s_SPITTypes.end(); i++) { ptrSPITType = i->second; i = s_SPITTypes.erase(i); @@ -201,7 +201,7 @@ void SPITType::clearSPITTypesRemote() { SPITType* ptrSPITType; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); for (auto i = s_SPITTypesRemote.begin(); i != s_SPITTypesRemote.end(); i++) { ptrSPITType = i->second; i = s_SPITTypesRemote.erase(i); @@ -220,7 +220,7 @@ void SPITType::setConfirmedFalse() { SPITType* ptrSPITType; { - unique_lock o_LockType(s_MutexSPITType); + fos::MutexGuard o_LockType(s_MutexSPITType); for (auto i = s_SPITTypes.begin(); i != s_SPITTypes.end(); i++) { ptrSPITType = i->second; ptrSPITType->setConfirmed(false); diff --git a/components/spit/src/SPIT_Client.cpp b/components/spit/src/SPIT_Client.cpp index 80d6c70..3e8aa6f 100644 --- a/components/spit/src/SPIT_Client.cpp +++ b/components/spit/src/SPIT_Client.cpp @@ -16,20 +16,16 @@ #include "SPIT_Client.h" +#include "esp_log.h" +static const char * TAG = "SPIT"; SPITSocket* SPIT_Client::createSPITSocket() { SPITSocket* ptrSPITSocket = NULL; -#if defined(OS_WINDOWS) - ptrSPITSocket = new WindowsSocket(); -#endif -#if defined(OS_MAC) || defined (OS_LINUX) ptrSPITSocket = new UnixSocket(); -#endif return ptrSPITSocket; } - /** * Create a new SPIT_client instance, should be done once by the user app.
* @param p_SPITClientInterface the user app should implement the SPIT_ClientInterface. @@ -44,12 +40,9 @@ SPIT_Client::SPIT_Client(SPIT_ClientInterface* p_SPITClientInterface): SAD_Clien m_ServerName = ""; m_ServerComputerName = ""; m_ServerStepTime = -1; -#if defined(OS_WINDOWS) - WindowsSocket::init(); -#endif -#if defined(OS_MAC) || defined (OS_LINUX) + UnixSocket::init(); -#endif + m_ID = createGUID(); m_ComputerName = getClientHostName(); m_Watchdog = new Watchdog(this); @@ -62,19 +55,10 @@ SPIT_Client::~SPIT_Client() { SPIT_Handler::closeHandler(); - -#if defined(OS_WINDOWS) - WindowsSocket::cleanUp(); -#endif -#if defined(OS_MAC) || defined (OS_LINUX) UnixSocket::cleanUp(); -#endif } - - - /** The ID of the client.
Every client instance should set an unique id.
This is done autmatically.*/ string SPIT_Client::getID() { return m_ID; @@ -123,9 +107,7 @@ void SPIT_Client::clientProjectClose() { fillHeader(ptrSMCloseSPITProject->getHeader()); ptrSMCloseSPITProject->setProjectID(m_SPITClientInterface->getClientProjectID()); if (sendSPITMessage(ptrSMCloseSPITProject) < 0) { -#if defined(DEBUGOUTPUT) - printf("SPIT_Client could not send Client's ReportSPITProject"); -#endif + ESP_LOGD(TAG, "SPIT_Client could not send Client's ReportSPITProject"); } } } @@ -144,9 +126,7 @@ void SPIT_Client::clientProjectLoaded() { ptrSMReportSPITProject->setProjectName(m_SPITClientInterface->getClientProjectName()); ptrSMReportSPITProject->setFramesPerSecond(0); if (sendSPITMessage(ptrSMReportSPITProject) < 0) { -#if defined(DEBUGOUTPUT) - printf("SPIT_CLient::clientProjectLoaded could not send Client's ReportSPITProject\n"); -#endif + ESP_LOGD(TAG, "SPIT_CLient::clientProjectLoaded could not send Client's ReportSPITProject"); } //send RequestProjectTransfer @@ -154,9 +134,7 @@ void SPIT_Client::clientProjectLoaded() { ptrSMRequestProjectTransfer = new SPIT_Message::RequestProjectTransfer(); fillHeader(ptrSMRequestProjectTransfer->getHeader()); if (sendSPITMessage(ptrSMRequestProjectTransfer) < 0) { -#if defined(DEBUGOUTPUT) - printf("SPIT_CLient::clientProjectLoaded could not send RequestProjectTransfer\n"); -#endif + ESP_LOGD(TAG, "SPIT_CLient::clientProjectLoaded could not send RequestProjectTransfer"); } } @@ -176,7 +154,7 @@ void SPIT_Client::setActionSPITType(string p_SPITTypeID, bool p_ActionAssigned) SPITType* ptrSPITType; SPITObject* ptrSPITObject; { - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); ptrSPITType = SPITType::getSPITType(p_SPITTypeID); if (ptrSPITType == NULL) return; @@ -184,7 +162,7 @@ void SPIT_Client::setActionSPITType(string p_SPITTypeID, bool p_ActionAssigned) sendSPITTypeInternal(ptrSPITType); //setActionAssigned to all SPITObjects with this SPITType - unique_lock o_LockObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockObject(*SPITObject::getMutex()); for (int i = 0; i < SPITObject::getSPITObjectCount(); i++) { ptrSPITObject = SPITObject::getSPITObject(i); @@ -207,7 +185,7 @@ void SPIT_Client::setActionSPITType(string p_SPITTypeID, bool p_ActionAssigned) void SPIT_Client::setActionSPITObject(string p_SPITObjectID, bool p_ActionAssigned) { SPITObject* ptrSPITObject; { - unique_lock o_LockObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockObject(*SPITObject::getMutex()); ptrSPITObject = SPITObject::getSPITObject(p_SPITObjectID); if (ptrSPITObject == NULL) return; @@ -225,7 +203,7 @@ void SPIT_Client::startServerAutoDetect() { if (m_DisconnectedByServer == true) { //Server has previously disconnected -> do not start ServerAutodetect directly //sleep a little to give the SPITServer time to clean up its stuff - - this_thread::sleep_for(chrono::milliseconds(1000)); + fos::Task::delayMs(1000); } SPITSocket* ptrSPITSocket; ptrSPITSocket = createSPITSocket(); @@ -274,7 +252,7 @@ void SPIT_Client::fillHeader(SPIT_Message::Header* p_Header) { int SPIT_Client::sendSPITMessage(SPIT_Message::OPPart* p_SPITMessageOPPart) { if (p_SPITMessageOPPart == NULL) return -1; { - unique_lock o_LockSocket(m_MutexSPITSocket); + fos::MutexGuard o_LockSocket(m_MutexSPITSocket); if (m_SPITSocket == NULL || m_StopReceiveSPITMessages == true) { delete p_SPITMessageOPPart; @@ -294,36 +272,32 @@ int SPIT_Client::sendSPITMessage(SPIT_Message::OPPart* p_SPITMessageOPPart) { try { intResult = m_SPITSocket->sendMessage(ptrMessageBytes, intByteCount); if (intResult < 0) { -#if defined(DEBUGOUTPUT) - printf("SPIT_Client::sendSPITMessage could not send message OPFlage: %d\n", ptrSPIT_Message->getOPFlag()); -#endif + ESP_LOGD(TAG, "SPIT_Client::sendSPITMessage could not send message OPFlage: %d", ptrSPIT_Message->getOPFlag()); } delete ptrSPIT_Message; p_SPITMessageOPPart = NULL; return intResult; } catch (...) { -#if defined(DEBUGOUTPUT) - printf("Exception SPIT_Client::sendSPITMessage OPFlag: %d\n", ptrSPIT_Message->getOPFlag()); -#endif + ESP_LOGD(TAG, "Exception SPIT_Client::sendSPITMessage OPFlag: %d", ptrSPIT_Message->getOPFlag()); return -1; } } } bool SPIT_Client::startReceiveSPITMessages() { { - unique_lock o_LockSocket(m_MutexSPITSocket); + fos::MutexGuard o_LockSocket(m_MutexSPITSocket); if (m_SPITSocket == NULL) return false; if (m_SPITSocket->getBoundClientFamily() <= 0) return false; m_StopReceiveSPITMessages = false; - m_ThreadReceive = new thread(&SPIT_Client::receiveSPITMessage, this); + m_ThreadReceive = new fos::TaskT(receiveSPITMessage, *this, "receiveTask"); } return true; } void SPIT_Client::stopReceiveSPITMessages() { { - unique_lock o_LockSocket(m_MutexSPITSocket); + fos::MutexGuard o_LockSocket(m_MutexSPITSocket); if (m_StopReceiveSPITMessages == true) return; m_StopReceiveSPITMessages = true; if (m_SPITSocket != NULL) { @@ -331,40 +305,36 @@ void SPIT_Client::stopReceiveSPITMessages() { m_SPITSocket = NULL; } } - this_thread::sleep_for(chrono::milliseconds(100)); + fos::Task::delayMs(100); if (m_ThreadReceive != NULL) { try { - if (this_thread::get_id() != m_ThreadReceive->get_id()) { - m_ThreadReceive->join(); - } + delete m_ThreadReceive; } catch(...) { -#if defined(DEBUGOUTPUT) - printf("Exception SPIT_Client::stopReceiveSPITMessages m_ThreadReceive->join()\n"); -#endif + ESP_LOGD(TAG, "Exception SPIT_Client::stopReceiveSPITMessages m_ThreadReceive->join()"); } m_ThreadReceive = NULL; } } -void SPIT_Client::receiveSPITMessage() { - if (m_SPITSocket == NULL) return; +void SPIT_Client::receiveSPITMessage(SPIT_Client& rM) { + if (rM.m_SPITSocket == NULL) return; SPIT_Message* ptrSPIT_Message; char buffer[MAXSPITMESSAGEBYTECOUNT]; int intResult; - while (m_StopReceiveSPITMessages == false) { + while (rM.m_StopReceiveSPITMessages == false) { { - unique_lock o_LockSocket(m_MutexSPITSocket); - if (m_SPITSocket == NULL || m_StopReceiveSPITMessages == true) { + fos::MutexGuard o_LockSocket(rM.m_MutexSPITSocket); + if (rM.m_SPITSocket == NULL || rM.m_StopReceiveSPITMessages == true) { return; } } - intResult = m_SPITSocket->receiveMessage(m_SPITSocket->getBoundClientFamily(), buffer, MAXSPITMESSAGEBYTECOUNT, NULL); + intResult = rM.m_SPITSocket->receiveMessage(rM.m_SPITSocket->getBoundClientFamily(), buffer, MAXSPITMESSAGEBYTECOUNT, NULL); if (intResult < 0) { - if (m_StopReceiveSPITMessages == false) { + if (rM.m_StopReceiveSPITMessages == false) { //To reduce the processor load, we let the thread sleep a little bit. - this_thread::sleep_for(chrono::milliseconds(25)); + fos::Task::delayMs(25); // printf("SPIT_Client::receiveSPITMessage intResult < 0\n"); continue; } @@ -374,7 +344,7 @@ void SPIT_Client::receiveSPITMessage() { } ptrSPIT_Message = new SPIT_Message(buffer, intResult); - parseSPIT_Message(ptrSPIT_Message); + rM.parseSPIT_Message(ptrSPIT_Message); } } @@ -383,21 +353,15 @@ void SPIT_Client::receiveSPITMessage() { void SPIT_Client::parseSPIT_Message(SPIT_Message* p_SPITMessage) { m_Watchdog->answerWatchdog(); if (p_SPITMessage == NULL) { -#if defined(DEBUGOUTPUT) - printf("SPIT_Client::parseSPIT_Message Parameter == NULL\n"); -#endif + ESP_LOGD(TAG, "SPIT_Client::parseSPIT_Message Parameter == NULL"); return; } if (p_SPITMessage->getHeader() == NULL) { -#if defined(DEBUGOUTPUT) - printf("SPIT_Client::parseSPIT_Message no Header\n"); -#endif + ESP_LOGD(TAG, "SPIT_Client::parseSPIT_Message no Header"); return; } if (p_SPITMessage->getOPPart() == NULL) { -#if defined(DEBUGOUTPUT) - printf("SPIT_Client::parseSPIT_Message no OPPart\n"); -#endif + ESP_LOGD(TAG, "SPIT_Client::parseSPIT_Message no OPPart"); return; } //----- Connection ----- @@ -478,12 +442,8 @@ void SPIT_Client::parseSPIT_Message(SPIT_Message* p_SPITMessage) { parseReportPlayStatus(o_ReportPlayStatus); } else { -#if defined(DEBUGOUTPUT) - printf("SPIT_Client::parseSPIT_Message: Unknown Message\n"); -#endif + ESP_LOGD(TAG, "SPIT_Client::parseSPIT_Message: Unknown Message"); } - - } @@ -503,7 +463,7 @@ bool SPIT_Client::connect(string p_ServerIP, string p_ServerPort, string p_Clien disconnect(); { - unique_lock o_LockSocket(m_MutexSPITSocket); + fos::MutexGuard o_LockSocket(m_MutexSPITSocket); m_SPITSocket = createSPITSocket(); m_SPITSocket->setConnectionParams(p_ServerIP, p_ServerPort, p_ClientIP, p_ClientPort); intResult = m_SPITSocket->bindSocket(p_ClientIP, p_ClientPort); @@ -531,7 +491,7 @@ bool SPIT_Client::connect(string p_ServerIP, string p_ServerPort, string p_Clien if (startReceiveSPITMessages() == false) { { - unique_lock o_LockSocket(m_MutexSPITSocket); + fos::MutexGuard o_LockSocket(m_MutexSPITSocket); m_StopReceiveSPITMessages = true; delete m_SPITSocket; m_SPITSocket = NULL; @@ -540,12 +500,12 @@ bool SPIT_Client::connect(string p_ServerIP, string p_ServerPort, string p_Clien } m_ConnectionStatus = CONNECTION_TRYCONNECTION; //sleep a little to make sure that the reception of the messages is started - this_thread::sleep_for(chrono::milliseconds(500)); + fos::Task::delayMs(500); intResult = sendSPITMessage(ptrSMConnect); if (intResult < 0) { m_ConnectionStatus = CONNECTION_NONE; { - unique_lock o_LockSocket(m_MutexSPITSocket); + fos::MutexGuard o_LockSocket(m_MutexSPITSocket); m_StopReceiveSPITMessages = true; delete m_SPITSocket; m_SPITSocket = NULL; @@ -746,24 +706,24 @@ void SPIT_Client::parseRequestProjectTransfer(SPIT_Message::RequestProjectTransf { //send all listes SPITTypes - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); intCount = SPITType::getSPITTypeCount(); for (int i = 0; i < intCount; i++) { ptrSPITType = SPITType::getSPITType(i); if (ptrSPITType == NULL) break; sendSPITTypeInternal(ptrSPITType); - this_thread::sleep_for(chrono::milliseconds(5)); + fos::Task::delayMs(10); } } { - unique_lock o_LockObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockObject(*SPITObject::getMutex()); intCount = SPITObject::getSPITObjectCount(); for (int i = 0; i < intCount; i++) { ptrSPITObject = SPITObject::getSPITObject(i); if (ptrSPITObject == NULL) break; sendSPITObjectInternal(ptrSPITObject); - this_thread::sleep_for(chrono::milliseconds(5)); + fos::Task::delayMs(10); } } @@ -795,7 +755,7 @@ void SPIT_Client::parseReportSPITType(SPIT_Message::ReportSPITType* p_ReportSPIT bool boolNew = false; { - unique_lock o_LockSPITType(*SPITType::getMutex()); + fos::MutexGuard o_LockSPITType(*SPITType::getMutex()); if (p_ReportSPITType->getServerRemote() == false) { ptrSPITType = SPITType::getSPITType(p_ReportSPITType->getID()); if (ptrSPITType == NULL) { @@ -843,7 +803,7 @@ void SPIT_Client::parseRemoveSPITType(SPIT_Message::RemoveSPITType* p_RemoveSPIT stringSPITTypeID = p_RemoveSPITType->getID(); { - unique_lock o_LockSPITType(*SPITType::getMutex()); + fos::MutexGuard o_LockSPITType(*SPITType::getMutex()); ptrSPITType = SPITType::getSPITTypeRemote(stringSPITTypeID); if (ptrSPITType != NULL) { @@ -906,7 +866,7 @@ bool SPIT_Client::changeSPITType(SPITType* p_SPITType) { if (p_SPITType == NULL) return false; SPITType* ptrSPITType; { - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); ptrSPITType = SPITType::getSPITType(p_SPITType->getID()); if (ptrSPITType == NULL) return false; @@ -939,7 +899,7 @@ int SPIT_Client::removeSPITType(string p_SPITTypeID) { SPITType* ptrSPITType; SPIT_Message::RemoveSPITType* ptrSMRemoveSPITType; { - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); ptrSPITType = SPITType::getSPITType(p_SPITTypeID); if (ptrSPITType == NULL) return -1; @@ -963,7 +923,7 @@ SPITType* SPIT_Client::createSPITType(string p_ID, string p_GroupName, string p_ SPITType* ptrSPITType; bool boolNew; { - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); ptrSPITType = SPITType::getSPITType(p_ID); if (ptrSPITType == NULL) { @@ -1005,8 +965,8 @@ void SPIT_Client::parseReportSPITObject(SPIT_Message::ReportSPITObject* p_Report { - unique_lock o_LockSPITObject(*SPITObject::getMutex()); - unique_lock o_LockSPITType(*SPITType::getMutex()); + fos::MutexGuard o_LockSPITObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockSPITType(*SPITType::getMutex()); if (p_ReportSPITObject->getServerRemote() == false) { boolRemote = false; @@ -1058,8 +1018,8 @@ void SPIT_Client::parseRemoveSPITObject(SPIT_Message::RemoveSPITObject* p_Remove { - unique_lock o_LockSPITObject(*SPITObject::getMutex()); - unique_lock o_LockSPITType(*SPITType::getMutex()); + fos::MutexGuard o_LockSPITObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockSPITType(*SPITType::getMutex()); ptrSPITObject = SPITObject::getSPITObjectRemote(p_RemoveSPITObject->getID()); if (ptrSPITObject != NULL) { @@ -1138,8 +1098,8 @@ bool SPIT_Client::changeSPITObject(SPITObject* p_SPITObject) { SPITObject* ptrSPITObject; SPITType* ptrSPITType; { - unique_lock o_LockObject(*SPITObject::getMutex()); - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); ptrSPITObject = SPITObject::getSPITObject(p_SPITObject->getID()); if (ptrSPITObject == NULL) return false; @@ -1185,8 +1145,8 @@ int SPIT_Client::removeSPITObject(string p_SPITObjectID) { SPITObject* ptrSPITObject; SPIT_Message::RemoveSPITObject* ptrSMRemoveSPITObject; { - unique_lock o_LockObject(*SPITObject::getMutex()); - unique_lock o_LockType(*SPITType::getMutex()); + fos::MutexGuard o_LockObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockType(*SPITType::getMutex()); ptrSPITObject = SPITObject::getSPITObject(p_SPITObjectID); @@ -1211,7 +1171,7 @@ void SPIT_Client::parsePlaySPITStart(SPIT_Message::PlaySPITStart* p_PlaySPITStar // printf("parse PlaySPITStart ID: %s Name: %s Running: %d\n", p_PlaySPITStart->getPlayerID(), p_PlaySPITStart->getPlayerName(), p_PlaySPITStart->getPlayerIsRunning()); SPITPlayer* ptrSPITPlayer; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITStart->getPlayerID()); if (ptrSPITPlayer == NULL) return; ptrSPITPlayer->setIsRunning(p_PlaySPITStart->getPlayerIsRunning()); @@ -1231,7 +1191,7 @@ void SPIT_Client::parsePlaySPITObject(SPIT_Message::PlaySPITObject* p_PlaySPITOb SPITObject* ptrSPITObject; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITObject->getPlayerID()); if (ptrSPITPlayer == NULL) return; @@ -1239,7 +1199,7 @@ void SPIT_Client::parsePlaySPITObject(SPIT_Message::PlaySPITObject* p_PlaySPITOb //Fetch infoemations from SPITObject { - unique_lock o_LockSPITObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockSPITObject(*SPITObject::getMutex()); ptrSPITObject = SPITObject::getSPITObject(p_PlaySPITObject->getSPITObjectID()); if (ptrSPITObject == NULL) { @@ -1264,7 +1224,7 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd* p_PlaySPITEnd) { SPITObject* ptrSPITObject = NULL; { - unique_lock o_LockSPITPlayer(*SPITPlayer::getMutex()); + fos::MutexGuard o_LockSPITPlayer(*SPITPlayer::getMutex()); ptrSPITPlayer = SPITPlayer::getSPITPlayer(p_PlaySPITEnd->getPlayerID()); if (ptrSPITPlayer == NULL) return; @@ -1286,8 +1246,8 @@ void SPIT_Client::parsePlaySPITEnd(SPIT_Message::PlaySPITEnd* p_PlaySPITEnd) { boolStopped = true; } { - unique_lock o_LockSPITObject(*SPITObject::getMutex()); - unique_lock o_LockSPITType(*SPITType::getMutex()); + fos::MutexGuard o_LockSPITObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockSPITType(*SPITType::getMutex()); ptrSPITObject = SPITObject::getSPITObject(ptrPlayObject->getSPITObjectID()); if (ptrSPITObject == NULL) { @@ -1312,8 +1272,8 @@ void SPIT_Client::parseReportPlayStatus(SPIT_Message::ReportPlayStatus* p_Report SPITType* ptrSPITType = NULL; SPITObject* ptrSPITObject = NULL; { - unique_lock o_LockSPITObject(*SPITObject::getMutex()); - unique_lock o_LockSPITType(*SPITType::getMutex()); + fos::MutexGuard o_LockSPITObject(*SPITObject::getMutex()); + fos::MutexGuard o_LockSPITType(*SPITType::getMutex()); ptrSPITObject = SPITObject::getSPITObjectRemote(p_ReportPlayStatus->getID()); if (ptrSPITObject == NULL) { @@ -1351,7 +1311,7 @@ void SPIT_Client::sendRemoteObject(string p_SPITObjectID, int p_Valueppm, char p SPIT_Message::PlaySPITEnd* ptrPlaySPITEnd; { - unique_lock o_LockRemote(*SPITObject::getMutex()); + fos::MutexGuard o_LockRemote(*SPITObject::getMutex()); ptrRemoteSPITObject = SPITObject::getSPITObjectRemote(p_SPITObjectID); if (ptrRemoteSPITObject == NULL) return; diff --git a/components/spit/src/SPIT_Handler.cpp b/components/spit/src/SPIT_Handler.cpp index 952be68..fceb0d2 100644 --- a/components/spit/src/SPIT_Handler.cpp +++ b/components/spit/src/SPIT_Handler.cpp @@ -13,43 +13,39 @@ */ #include "SPIT_Handler.h" +#include "esp_log.h" - -mutex SPIT_Handler::s_MutexLock; -condition_variable SPIT_Handler::s_ConditionVariable; +fos::Mutex SPIT_Handler::s_MutexLock; +fos::Semaphore SPIT_Handler::s_ConditionVariable(1000); list SPIT_Handler::s_Runnables; -bool SPIT_Handler::s_WakeUp = false; bool SPIT_Handler::s_Close = false; -thread SPIT_Handler::s_HandlerThread(SPIT_Handler::run); +fos::Task* SPIT_Handler::s_HandlerThread = nullptr; SPIT_Handler::Runnable::~Runnable() {} void SPIT_Handler::addRunnable(Runnable* p_Runnable) { + if(s_HandlerThread == nullptr) { + s_HandlerThread = new fos::Task(SPIT_Handler::run, "spitHandler"); + } + if (p_Runnable == NULL) return; { - unique_lock o_Unique_Lock(s_MutexLock); + fos::MutexGuard o_Unique_Lock(s_MutexLock); if (s_Close == true) return; s_Runnables.push_back(p_Runnable); - s_WakeUp = true; - } - if (this_thread::get_id() != s_HandlerThread.get_id()) { - s_ConditionVariable.notify_one(); } + s_ConditionVariable.give(); } void SPIT_Handler::closeHandler() { if (s_Close == true) return; { - unique_lock o_Unique_Lock(s_MutexLock); + fos::MutexGuard o_Unique_Lock(s_MutexLock); if (s_Close == true) return; s_Close = true; - s_WakeUp = true; - } - if (this_thread::get_id() != s_HandlerThread.get_id()) { - s_ConditionVariable.notify_one(); - s_HandlerThread.join(); } + s_ConditionVariable.give(); } void SPIT_Handler::run() { Runnable* o_Runnable; @@ -57,20 +53,16 @@ void SPIT_Handler::run() { while (s_Close == false) { boolWait = false; { - unique_lock o_Lock(s_MutexLock); + fos::MutexGuard o_Lock(s_MutexLock); if (s_Runnables.empty()) { - s_ConditionVariable.wait(o_Lock, []{return s_WakeUp;}); + s_ConditionVariable.take(); } if (s_Close == true) { - s_WakeUp = true; - return;; + return; } - s_WakeUp = false; - if (s_Runnables.empty()) { - o_Lock.unlock(); continue; } else { @@ -85,9 +77,7 @@ void SPIT_Handler::run() { delete o_Runnable; } catch(...) { -#if defined(DEBUGOUTPUT) - printf("ERROR SPITHandler o_Runable->run()\n"); -#endif + ESP_LOGW("SPIT", "ERROR SPITHandler o_Runable->run()"); } } } diff --git a/components/spit/src/UnixSocket.cpp b/components/spit/src/UnixSocket.cpp index 74b1bf5..daa7542 100644 --- a/components/spit/src/UnixSocket.cpp +++ b/components/spit/src/UnixSocket.cpp @@ -17,10 +17,9 @@ #include "_SPITMACROS.h" #include "UnixSocket.h" -// #include -#include #include "esp_netif.h" #include "esp_log.h" +#include "Task.hpp" UnixSocket::UnixSocket() : SPITSocket(){ @@ -46,7 +45,7 @@ UnixSocket::~UnixSocket() { intResultIP6 = close(m_UDPSocketIP6); m_UDPSocketIP6 = INVALID_SOCKET; } - this_thread::sleep_for(chrono::milliseconds(100)); + fos::Task::delayMs(100); } int UnixSocket::init() { diff --git a/components/spit/src/Watchdog.cpp b/components/spit/src/Watchdog.cpp index c9c7bea..748a62f 100644 --- a/components/spit/src/Watchdog.cpp +++ b/components/spit/src/Watchdog.cpp @@ -34,24 +34,22 @@ Watchdog::~Watchdog() { void Watchdog::startWatchdog() { if (s_StopWatchdog == false) return; { - unique_lock o_UniqueLock(m_MutexLock); + fos::Mutex o_UniqueLock(m_MutexLock); s_StopWatchdog = false; } - m_WatchdogThread = new thread(&Watchdog::runWatchdog, this); + m_WatchdogThread = new fos::TaskT(Watchdog::runWatchdog, *this, "spitWD"); } void Watchdog::stopWatchdog() { if (m_WatchdogThread != NULL) { { - unique_lock o_UniqueLock(m_MutexLock); + fos::Mutex o_UniqueLock(m_MutexLock); if (s_StopWatchdog == true) { return; } s_StopWatchdog = true; } - if (this_thread::get_id() != m_WatchdogThread->get_id()) { - m_ConditionVariable.notify_one(); - m_WatchdogThread->join(); - } + m_ConditionVariable.give(); + delete m_WatchdogThread; m_WatchdogThread = NULL; } } @@ -59,26 +57,24 @@ void Watchdog::answerWatchdog() { m_Answered = true; } -void Watchdog::runWatchdog() { +void Watchdog::runWatchdog(Watchdog& wd) { bool boolDone; - auto waitTime = std::chrono::milliseconds(WATCHDOGTIME); - while (s_StopWatchdog == false) { - - m_Answered = false; - m_Listener->sendWatchdog(); + while (wd.s_StopWatchdog == false) { + wd.m_Answered = false; + wd.m_Listener->sendWatchdog(); { - unique_lock o_UniqueLock(m_MutexLock); - boolDone = m_ConditionVariable.wait_for(o_UniqueLock, waitTime, []{return s_StopWatchdog;}); - if (s_StopWatchdog == true) { + fos::Mutex o_UniqueLock(wd.m_MutexLock); + boolDone = wd.m_ConditionVariable.take(WATCHDOGTIME); + if (wd.s_StopWatchdog == true) { return; } } if (boolDone) { return; } - if (m_Answered == false) { - stopWatchdog(); - m_Listener->eventWatchdogLost(); + if (wd.m_Answered == false) { + wd.stopWatchdog(); + wd.m_Listener->eventWatchdogLost(); return; } }