Alignment with C#

This commit is contained in:
Pascal Serrarens 2025-06-04 12:17:30 +02:00
parent e36f3bb45d
commit 2a83dbe7ca
6 changed files with 135 additions and 68 deletions

View File

@ -1,6 +1,10 @@
#include "Participant.h" #include "Participant.h"
#include <string.h> #include <string.h>
#include "Arduino/ArduinoParticipant.h"
#include "EspIdf/EspIdfParticipant.h"
#include "Posix/PosixParticipant.h"
#include "Windows/WindowsParticipant.h"
namespace RoboidControl { namespace RoboidControl {
@ -52,6 +56,34 @@ void Participant::Update() {
} }
} }
bool Participant::Send(IMessage* msg) {
int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0)
return true;
// std::cout << "send msg " << (static_cast<int>(this->buffer[0]) & 0xff)
// << " to " << remoteParticipant->ipAddress << std::endl;
#if defined(_WIN32) || defined(_WIN64)
Windows::ParticipantUDP* thisWindows =
static_cast<Windows::ParticipantUDP*>(this);
return thisWindows->Send(remoteParticipant, bufferSize);
#elif defined(__unix__) || defined(__APPLE__)
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this);
return thisPosix->Send(remoteParticipant, bufferSize);
#elif defined(ARDUINO)
Arduino::ParticipantUDP* thisArduino =
static_cast<Arduino::ParticipantUDP*>(this);
return thisArduino->Send(this, bufferSize);
#elif defined(IDF_VER)
EspIdf::ParticipantUDP* thisEspIdf =
static_cast<EspIdf::ParticipantUDP*>(this);
return thisEspIdf->Send(remoteParticipant, bufferSize);
#else
return false;
#endif
}
Thing* Participant::Get(unsigned char thingId) { Thing* Participant::Get(unsigned char thingId) {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing->id == thingId) if (thing->id == thingId)

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Messages/IMessage.h"
#include "Thing.h" #include "Thing.h"
namespace RoboidControl { namespace RoboidControl {
@ -132,13 +133,21 @@ class Participant {
#pragma endregion Update #pragma endregion Update
#pragma region Send
public:
char buffer[1024];
virtual bool Send(IMessage* msg);
#pragma endregion Send
#pragma region Participant Registry #pragma region Participant Registry
public: public:
static ParticipantRegistry registry; static ParticipantRegistry registry;
#pragma endregion Participant Registry #pragma endregion Participant Registry
}; };
} // namespace RoboidControl } // namespace RoboidControl

View File

@ -46,14 +46,6 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
Participant::ReplaceLocalParticipant(*this); Participant::ReplaceLocalParticipant(*this);
} }
static ParticipantUDP* isolatedParticipant = nullptr;
ParticipantUDP* ParticipantUDP::Isolated() {
if (isolatedParticipant == nullptr)
isolatedParticipant = new ParticipantUDP(0);
return isolatedParticipant;
}
void ParticipantUDP::begin() { void ParticipantUDP::begin() {
if (this->isIsolated || this->remoteSite == nullptr) if (this->isIsolated || this->remoteSite == nullptr)
return; return;
@ -105,7 +97,8 @@ void ParticipantUDP::Update() {
if (this->remoteSite == nullptr) if (this->remoteSite == nullptr)
this->Publish(msg); this->Publish(msg);
else else
this->Send(this->remoteSite, msg); this->remoteSite->Send(msg);
delete msg; delete msg;
this->nextPublishMe = currentTimeMs + this->publishInterval; this->nextPublishMe = currentTimeMs + this->publishInterval;
@ -122,7 +115,7 @@ void ParticipantUDP::PrepMyThings() {
for (Thing* thing : this->things) { for (Thing* thing : this->things) {
if (thing == nullptr) if (thing == nullptr)
continue; continue;
thing->PrepareForUpdate(); thing->PrepareForUpdate();
} }
} }
@ -137,12 +130,12 @@ void ParticipantUDP::UpdateMyThings() {
if (thing->hierarchyChanged) { if (thing->hierarchyChanged) {
if (!(this->isIsolated || this->networkId == 0)) { if (!(this->isIsolated || this->networkId == 0)) {
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
this->Send(this->remoteSite, thingMsg); this->remoteSite->Send(thingMsg);
delete thingMsg; delete thingMsg;
if (thing->nameChanged) { if (thing->nameChanged) {
NameMsg* nameMsg = new NameMsg(this->networkId, thing); NameMsg* nameMsg = new NameMsg(this->networkId, thing);
this->Send(this->remoteSite, nameMsg); this->remoteSite->Send(nameMsg);
delete nameMsg; delete nameMsg;
} }
} }
@ -159,20 +152,20 @@ void ParticipantUDP::UpdateMyThings() {
if (!(this->isIsolated || this->networkId == 0)) { if (!(this->isIsolated || this->networkId == 0)) {
if (thing->terminate) { if (thing->terminate) {
DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing); DestroyMsg* destroyMsg = new DestroyMsg(this->networkId, thing);
this->Send(this->remoteSite, destroyMsg); this->remoteSite->Send(destroyMsg);
delete destroyMsg; delete destroyMsg;
} else { } else {
// Send to remote site // Send to remote site
if (thing->nameChanged) { if (thing->nameChanged) {
NameMsg* nameMsg = new NameMsg(this->networkId, thing); NameMsg* nameMsg = new NameMsg(this->networkId, thing);
this->Send(this->remoteSite, nameMsg); this->remoteSite->Send(nameMsg);
delete nameMsg; delete nameMsg;
} }
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
this->Send(this->remoteSite, poseMsg); this->remoteSite->Send(poseMsg);
delete poseMsg; delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing); BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(this->remoteSite, binaryMsg); this->remoteSite->Send(binaryMsg);
delete binaryMsg; delete binaryMsg;
} }
} }
@ -203,10 +196,10 @@ void ParticipantUDP::UpdateOtherThings() {
for (Thing* thing : participant->things) { for (Thing* thing : participant->things) {
PoseMsg* poseMsg = new PoseMsg(participant->networkId, thing); PoseMsg* poseMsg = new PoseMsg(participant->networkId, thing);
this->Send(participant, poseMsg); participant->Send(poseMsg);
delete poseMsg; delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(participant->networkId, thing); BinaryMsg* binaryMsg = new BinaryMsg(participant->networkId, thing);
this->Send(participant, binaryMsg); participant->Send(binaryMsg);
delete binaryMsg; delete binaryMsg;
} }
} }
@ -221,49 +214,50 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
Thing* thing) { Thing* thing) {
// std::cout << "Send thing info [" << (int)thing->id << "] \n"; // std::cout << "Send thing info [" << (int)thing->id << "] \n";
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing); ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
this->Send(remoteParticipant, thingMsg); remoteParticipant->Send(thingMsg);
delete thingMsg; delete thingMsg;
NameMsg* nameMsg = new NameMsg(this->networkId, thing); NameMsg* nameMsg = new NameMsg(this->networkId, thing);
this->Send(remoteParticipant, nameMsg); remoteParticipant->Send(nameMsg);
delete nameMsg; delete nameMsg;
ModelUrlMsg* modelMsg = new ModelUrlMsg(this->networkId, thing); ModelUrlMsg* modelMsg = new ModelUrlMsg(this->networkId, thing);
this->Send(remoteParticipant, modelMsg); remoteParticipant->Send(modelMsg);
delete modelMsg; delete modelMsg;
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing, true); PoseMsg* poseMsg = new PoseMsg(this->networkId, thing, true);
this->Send(remoteParticipant, poseMsg); remoteParticipant->Send(poseMsg);
delete poseMsg; delete poseMsg;
BinaryMsg* customMsg = new BinaryMsg(this->networkId, thing); BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(remoteParticipant, customMsg); remoteParticipant->Send(binaryMsg);
delete customMsg; delete binaryMsg;
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) { // bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) {
int bufferSize = msg->Serialize(this->buffer); // int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) // if (bufferSize <= 0)
return true; // return true;
// std::cout << "send msg " << (static_cast<int>(this->buffer[0]) & 0xff) // // std::cout << "send msg " << (static_cast<int>(this->buffer[0]) & 0xff)
// << " to " << remoteParticipant->ipAddress << std::endl; // // << " to " << remoteParticipant->ipAddress << std::endl;
#if defined(_WIN32) || defined(_WIN64) // #if defined(_WIN32) || defined(_WIN64)
Windows::ParticipantUDP* thisWindows = // Windows::ParticipantUDP* thisWindows =
static_cast<Windows::ParticipantUDP*>(this); // static_cast<Windows::ParticipantUDP*>(this);
return thisWindows->Send(remoteParticipant, bufferSize); // return thisWindows->Send(remoteParticipant, bufferSize);
#elif defined(__unix__) || defined(__APPLE__) // #elif defined(__unix__) || defined(__APPLE__)
Posix::ParticipantUDP* thisPosix = static_cast<Posix::ParticipantUDP*>(this); // Posix::ParticipantUDP* thisPosix =
return thisPosix->Send(remoteParticipant, bufferSize); // static_cast<Posix::ParticipantUDP*>(this); return
#elif defined(ARDUINO) // thisPosix->Send(remoteParticipant, bufferSize);
Arduino::ParticipantUDP* thisArduino = // #elif defined(ARDUINO)
static_cast<Arduino::ParticipantUDP*>(this); // Arduino::ParticipantUDP* thisArduino =
return thisArduino->Send(remoteParticipant, bufferSize); // static_cast<Arduino::ParticipantUDP*>(this);
#elif defined(IDF_VER) // return thisArduino->Send(remoteParticipant, bufferSize);
EspIdf::ParticipantUDP* thisEspIdf = // #elif defined(IDF_VER)
static_cast<EspIdf::ParticipantUDP*>(this); // EspIdf::ParticipantUDP* thisEspIdf =
return thisEspIdf->Send(remoteParticipant, bufferSize); // static_cast<EspIdf::ParticipantUDP*>(this);
#else // return thisEspIdf->Send(remoteParticipant, bufferSize);
return false; // #else
#endif // return false;
} // #endif
// }
void ParticipantUDP::PublishThingInfo(Thing* thing) { void ParticipantUDP::PublishThingInfo(Thing* thing) {
// std::cout << "Publish thing info" << thing->networkId << "\n"; // std::cout << "Publish thing info" << thing->networkId << "\n";
@ -402,6 +396,18 @@ void ParticipantUDP::ReceiveData(unsigned char bufferSize,
Process(sender, msg); Process(sender, msg);
delete msg; delete msg;
} break; } break;
case TextMsg::id: {
TextMsg* msg = new TextMsg(this->buffer);
bufferSize -= msg->length + msg->textLength;
Process(sender, msg);
delete msg;
} break;
case DestroyMsg::id: {
DestroyMsg* msg = new DestroyMsg(this->buffer);
bufferSize -= msg->length;
Process(sender, msg);
delete msg;
} break;
}; };
// Check if the buffer has been read completely // Check if the buffer has been read completely
@ -532,6 +538,24 @@ void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
} }
} }
void ParticipantUDP::Process(Participant* sender, TextMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process TextMsg " << (int)msg->textLength << " "
<< (int)msg->text << "\n";
#endif
}
void ParticipantUDP::Process(Participant* sender, DestroyMsg* msg) {
#if defined(DEBUG)
std::cout << this->name << ": process Destroy [" << (int)msg->networkId << "/"
<< (int)msg->thingId << "]\n";
#endif
Thing* thing = sender->Get(msg->thingId);
if (thing != nullptr)
this->Remove(thing);
}
// Receive // Receive
#pragma endregion #pragma endregion

View File

@ -9,6 +9,7 @@
#include "Messages/PoseMsg.h" #include "Messages/PoseMsg.h"
#include "Messages/NetworkIdMsg.h" #include "Messages/NetworkIdMsg.h"
#include "Messages/ThingMsg.h" #include "Messages/ThingMsg.h"
#include "Messages/TextMsg.h"
#include "Participant.h" #include "Participant.h"
#if !defined(NO_STD) #if !defined(NO_STD)
@ -43,6 +44,7 @@ constexpr int MAX_SENDER_COUNT = 256;
/// RoboidControl::IsolatedParticipant::Isolated(). /// RoboidControl::IsolatedParticipant::Isolated().
/// @sa RoboidControl::Thing::Thing() /// @sa RoboidControl::Thing::Thing()
class ParticipantUDP : public Participant { class ParticipantUDP : public Participant {
#pragma region Init #pragma region Init
public: public:
@ -58,19 +60,15 @@ class ParticipantUDP : public Participant {
/// @param localPort The port used by the local participant /// @param localPort The port used by the local participant
ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681); ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681);
/// @brief Isolated participant is used when the application is run without
/// networking
/// @return A participant without networking support
static ParticipantUDP* Isolated();
/// @brief True if the participant is running isolated.
/// Isolated participants do not communicate with other participants
#pragma endregion Init #pragma endregion Init
#pragma region Properties
public:
/// @brief True if the participant is running isolated. /// @brief True if the participant is running isolated.
/// Isolated participants do not communicate with other participants /// Isolated participants do not communicate with other participants
bool isIsolated = false; bool isIsolated = false;
/// @brief The remote site when this participant is connected to a site /// @brief The remote site when this participant is connected to a site
Participant* remoteSite = nullptr; Participant* remoteSite = nullptr;
@ -94,6 +92,8 @@ class ParticipantUDP : public Participant {
void begin(); void begin();
bool connected = false; bool connected = false;
#pragma endregion Properties
#pragma region Update #pragma region Update
public: public:
@ -114,7 +114,7 @@ class ParticipantUDP : public Participant {
void SendThingInfo(Participant* remoteParticipant, Thing* thing); void SendThingInfo(Participant* remoteParticipant, Thing* thing);
void PublishThingInfo(Thing* thing); void PublishThingInfo(Thing* thing);
bool Send(Participant* remoteParticipant, IMessage* msg); //bool Send(Participant* remoteParticipant, IMessage* msg);
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
#pragma endregion Send #pragma endregion Send
@ -139,6 +139,8 @@ protected:
virtual void Process(Participant* sender, ModelUrlMsg* msg); virtual void Process(Participant* sender, ModelUrlMsg* msg);
virtual void Process(Participant* sender, PoseMsg* msg); virtual void Process(Participant* sender, PoseMsg* msg);
virtual void Process(Participant* sender, BinaryMsg* msg); virtual void Process(Participant* sender, BinaryMsg* msg);
virtual void Process(Participant* sender, TextMsg* msg);
virtual void Process(Participant* sender, DestroyMsg* msg);
#pragma endregion Receive #pragma endregion Receive

View File

@ -42,10 +42,10 @@ void SiteServer::UpdateMyThings() {
continue; continue;
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
this->Send(participant, poseMsg); participant->Send(poseMsg);
delete poseMsg; delete poseMsg;
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing); BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(participant, binaryMsg); participant->Send(binaryMsg);
delete binaryMsg; delete binaryMsg;
} }
} }
@ -62,7 +62,7 @@ void SiteServer::Process(Participant* sender, ParticipantMsg* msg) {
// sender->ipAddress // sender->ipAddress
// << ":" << (int)sender->port << "\n"; // << ":" << (int)sender->port << "\n";
NetworkIdMsg* msg = new NetworkIdMsg(sender->networkId); NetworkIdMsg* msg = new NetworkIdMsg(sender->networkId);
this->Send(sender, msg); sender->Send(msg);
delete msg; delete msg;
} }
} }

View File

@ -2,11 +2,11 @@
#include "ParticipantUDP.h" #include "ParticipantUDP.h"
#if !defined(NO_STD) // #if !defined(NO_STD)
#include <functional> // #include <functional>
#include <memory> // #include <memory>
#include <unordered_map> // #include <unordered_map>
#endif // #endif
namespace RoboidControl { namespace RoboidControl {