migrate fully to freeRTOS

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