Compare commits
No commits in common. "12d91378e5fe126b85141123aefb27a4c33f96e2" and "6353af4a29f4735b94e2086ef4cda0988ba52e5c" have entirely different histories.
12d91378e5
...
6353af4a29
@ -19,7 +19,7 @@
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
void ParticipantUDP::Setup(int localPort,
|
||||
void LocalParticipant::Setup(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
@ -44,7 +44,7 @@ void ParticipantUDP::Setup(int localPort,
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::GetBroadcastAddress() {
|
||||
void LocalParticipant::GetBroadcastAddress() {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
IPAddress broadcastAddress = WiFi.localIP();
|
||||
broadcastAddress[3] = 255;
|
||||
@ -56,7 +56,7 @@ void ParticipantUDP::GetBroadcastAddress() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Receive() {
|
||||
void LocalParticipant::Receive() {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
int packetSize = udp.parsePacket();
|
||||
while (packetSize > 0) {
|
||||
@ -86,7 +86,7 @@ void ParticipantUDP::Receive() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
// std::cout << "Sending to:\n " << remoteParticipant->ipAddress << ":"
|
||||
// << remoteParticipant->port << "\n";
|
||||
@ -106,7 +106,7 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(ARDUINO) && defined(HAS_WIFI)
|
||||
int bufferSize = msg->Serialize((char*)this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
#include "../LocalParticipant.h"
|
||||
|
||||
#if defined(HAS_WIFI)
|
||||
#include <WiFiUdp.h>
|
||||
@ -9,7 +9,7 @@
|
||||
namespace RoboidControl {
|
||||
namespace Arduino {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class LocalParticipant : public RoboidControl::LocalParticipant {
|
||||
public:
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
|
@ -9,7 +9,6 @@ file(GLOB srcs
|
||||
Windows/*.cpp
|
||||
EspIdf/*.cpp
|
||||
LinearAlgebra/*.cpp
|
||||
Participants/*.cpp
|
||||
)
|
||||
|
||||
if(ESP_PLATFORM)
|
||||
|
@ -1,13 +1,11 @@
|
||||
#include "EspIdfParticipant.h"
|
||||
|
||||
#if defined(IDF_VER)
|
||||
#include "esp_wifi.h"
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace EspIdf {
|
||||
|
||||
void ParticipantUDP::Setup(int localPort,
|
||||
void LocalParticipant::Setup(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
#if defined(IDF_VER)
|
||||
@ -45,41 +43,41 @@ void ParticipantUDP::Setup(int localPort,
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize the dest_addr structure
|
||||
memset(&this->dest_addr, 0, sizeof(this->dest_addr)); // Clear the entire structure
|
||||
this->dest_addr.sin_family = AF_INET;
|
||||
this->dest_addr.sin_port = htons(this->remoteSite->port);
|
||||
inet_pton(AF_INET, this->remoteSite->ipAddress, &this->dest_addr.sin_addr.s_addr);
|
||||
// struct sockaddr_in dest_addr;
|
||||
memset(dest_addr.sin_zero, 0, sizeof(dest_addr.sin_zero));
|
||||
dest_addr.sin_family = AF_INET;
|
||||
dest_addr.sin_port = htons(this->remoteSite->port);
|
||||
inet_pton(AF_INET, this->remoteSite->ipAddress, &dest_addr.sin_addr.s_addr);
|
||||
|
||||
|
||||
std::cout << "Wifi sync started local " << this->port << ", remote "
|
||||
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port
|
||||
<< "\n";
|
||||
#endif // IDF_VER
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::GetBroadcastAddress() {
|
||||
// SOMEHOW, THIS FUNCTION RESULTS IN MEMORY CORRUPION...
|
||||
void LocalParticipant::GetBroadcastAddress() {
|
||||
esp_netif_ip_info_t ip_info;
|
||||
esp_netif_t* esp_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
|
||||
// esp_netif_ip_info_t ip_info;
|
||||
// esp_netif_t* esp_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||
// Get IP information (IP address, netmask, gateway)
|
||||
if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK) {
|
||||
std::cout << "Failed to get IP info\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// // Get IP information (IP address, netmask, gateway)
|
||||
// if (esp_netif_get_ip_info(esp_netif, &ip_info) != ESP_OK) {
|
||||
// std::cout << "Failed to get IP info\n";
|
||||
// return;
|
||||
// }
|
||||
ip_addr_t broadcast_addr = {};
|
||||
broadcast_addr.u_addr.ip4.addr =
|
||||
(ip_info.ip.addr & ip_info.netmask.addr) | ~ip_info.netmask.addr;
|
||||
|
||||
// ip_addr_t broadcast_addr = {};
|
||||
// broadcast_addr.u_addr.ip4.addr =
|
||||
// (ip_info.ip.addr & ip_info.netmask.addr) | ~ip_info.netmask.addr;
|
||||
|
||||
// snprintf(this->broadcastIpAddress, INET_ADDRSTRLEN, IPSTR,
|
||||
// IP2STR(&broadcast_addr.u_addr.ip4));
|
||||
// std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n";
|
||||
this->broadcastIpAddress = new char[16]; // IPv4 address can have a max of 15
|
||||
// characters + null terminator
|
||||
snprintf(this->broadcastIpAddress, 16, IPSTR,
|
||||
IP2STR(&broadcast_addr.u_addr.ip4));
|
||||
std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n";
|
||||
}
|
||||
|
||||
void ParticipantUDP::Receive() {
|
||||
void LocalParticipant::Receive() {
|
||||
#if defined(IDF_VER)
|
||||
struct pollfd fds;
|
||||
fds.fd = sockfd;
|
||||
@ -92,21 +90,20 @@ void ParticipantUDP::Receive() {
|
||||
return;
|
||||
}
|
||||
socklen_t addr_len = sizeof(this->src_addr);
|
||||
char sender_ipAddress[INET_ADDRSTRLEN];
|
||||
|
||||
while (ret > 0 && fds.revents & POLLIN) {
|
||||
int packetSize = recvfrom(this->sockfd, buffer, sizeof(buffer) - 1, 0,
|
||||
int packetSize = recvfrom(sockfd, buffer, sizeof(buffer) - 1, 0,
|
||||
(struct sockaddr*)&this->src_addr, &addr_len);
|
||||
if (packetSize < 0) {
|
||||
std::cout << "recvfrom() error\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "receiving " << packetSize << " bytes\n";
|
||||
// inet_ntoa_r(this->src_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN);
|
||||
// unsigned int sender_port = ntohs(this->src_addr.sin_port);
|
||||
char sender_ipAddress[16];
|
||||
inet_ntoa_r(this->src_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN);
|
||||
unsigned int sender_port = ntohs(this->src_addr.sin_port);
|
||||
|
||||
// ReceiveData(packetSize, sender_ipAddress, sender_port);
|
||||
ReceiveData(packetSize, sender_ipAddress, sender_port);
|
||||
|
||||
int ret = poll(&fds, 1, 0);
|
||||
if (ret == -1) {
|
||||
@ -115,15 +112,15 @@ void ParticipantUDP::Receive() {
|
||||
}
|
||||
}
|
||||
|
||||
#endif // IDF_VER
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
#if defined(IDF_VER)
|
||||
std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
|
||||
<< remoteParticipant->port << "\n";
|
||||
|
||||
int err = sendto(this->sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
|
||||
int err = sendto(sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
|
||||
sizeof(dest_addr));
|
||||
if (errno != 0)
|
||||
std::cout << "Send error " << err << " or " << errno << "\n";
|
||||
@ -132,7 +129,7 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(IDF_VER)
|
||||
int bufferSize = msg->Serialize((char*)this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
|
@ -1,15 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
#include "../LocalParticipant.h"
|
||||
|
||||
#if defined(IDF_VER)
|
||||
#include "lwip/sockets.h"
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace EspIdf {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class LocalParticipant : public RoboidControl::LocalParticipant {
|
||||
public:
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
@ -17,13 +15,13 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
bool Publish(IMessage* msg);
|
||||
|
||||
protected:
|
||||
#if defined(IDF_VER)
|
||||
char broadcastIpAddress[INET_ADDRSTRLEN];
|
||||
// const char* remoteIpAddress = nullptr;
|
||||
// unsigned short remotePort = 0;
|
||||
char* broadcastIpAddress = nullptr;
|
||||
|
||||
int sockfd;
|
||||
struct sockaddr_in dest_addr;
|
||||
struct sockaddr_in src_addr;
|
||||
#endif
|
||||
|
||||
void GetBroadcastAddress();
|
||||
};
|
||||
|
@ -18,15 +18,15 @@ unsigned char IMessage::Serialize(char* buffer) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// bool IMessage::SendMsg(ParticipantUDP *client, IMessage msg) {
|
||||
// bool IMessage::SendMsg(LocalParticipant *client, IMessage msg) {
|
||||
// // return SendMsg(client, client.buffer, );nameLength
|
||||
// return client->SendBuffer(msg.Serialize(client->buffer));
|
||||
// }
|
||||
|
||||
// bool IMessage::Publish(ParticipantUDP *participant) {
|
||||
// bool IMessage::Publish(LocalParticipant *participant) {
|
||||
// return participant->PublishBuffer(Serialize(participant->buffer));
|
||||
// }
|
||||
// bool IMessage::SendTo(ParticipantUDP *participant) {
|
||||
// bool IMessage::SendTo(LocalParticipant *participant) {
|
||||
// return participant->SendBuffer(Serialize(participant->buffer));
|
||||
// }
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
class ParticipantUDP;
|
||||
class LocalParticipant;
|
||||
|
||||
class IMessage {
|
||||
public:
|
||||
@ -15,8 +15,8 @@ class IMessage {
|
||||
|
||||
static unsigned char* ReceiveMsg(unsigned char packetSize);
|
||||
|
||||
// bool Publish(ParticipantUDP *participant);
|
||||
// bool SendTo(ParticipantUDP *participant);
|
||||
// bool Publish(LocalParticipant *participant);
|
||||
// bool SendTo(LocalParticipant *participant);
|
||||
};
|
||||
|
||||
} // namespace RoboidControl
|
||||
|
@ -19,7 +19,7 @@ unsigned char ParticipantMsg::Serialize(char* buffer) {
|
||||
return ParticipantMsg::length;
|
||||
}
|
||||
|
||||
// bool ParticipantMsg::Send(ParticipantUDP *participant, unsigned char networkId) {
|
||||
// bool ParticipantMsg::Send(LocalParticipant *participant, unsigned char networkId) {
|
||||
// ParticipantMsg msg = ParticipantMsg()
|
||||
// }
|
||||
// Client Msg
|
||||
|
@ -27,8 +27,6 @@ Participant::~Participant() {
|
||||
delete[] this->ipAddress;
|
||||
}
|
||||
|
||||
void Participant::Update(unsigned long currentTimeMs) {}
|
||||
|
||||
Thing* Participant::Get(unsigned char networkId, unsigned char thingId) {
|
||||
for (Thing* thing : this->things) {
|
||||
// if (thing->networkId == networkId && thing->id == thingId)
|
||||
|
@ -33,8 +33,6 @@ class Participant {
|
||||
/// @brief Destructor for the participant
|
||||
~Participant();
|
||||
|
||||
virtual void Update(unsigned long currentTimeMs = 0);
|
||||
|
||||
protected:
|
||||
#if defined(NO_STD)
|
||||
unsigned char thingCount = 0;
|
||||
|
@ -1,14 +0,0 @@
|
||||
#include "IsolatedParticipant.h"
|
||||
#include "ParticipantUDP.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
static ParticipantUDP* isolatedParticipant = nullptr;
|
||||
|
||||
Participant* IsolatedParticipant::Isolated() {
|
||||
if (isolatedParticipant == nullptr)
|
||||
isolatedParticipant = new ParticipantUDP(0);
|
||||
return isolatedParticipant;
|
||||
}
|
||||
|
||||
} // namespace RoboidControl
|
@ -1,13 +0,0 @@
|
||||
#include "Participant.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
class IsolatedParticipant {
|
||||
public:
|
||||
/// @brief Isolated participant is used when the application is run without
|
||||
/// networking
|
||||
/// @return A participant without networking support
|
||||
static Participant* Isolated();
|
||||
};
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
#include "ParticipantUDP.h"
|
||||
#include "LocalParticipant.h"
|
||||
|
||||
#include "Thing.h"
|
||||
|
||||
@ -25,14 +25,14 @@
|
||||
|
||||
namespace RoboidControl {
|
||||
|
||||
ParticipantUDP::ParticipantUDP(int port) {
|
||||
LocalParticipant::LocalParticipant(int port) {
|
||||
this->ipAddress = "0.0.0.0";
|
||||
this->port = port;
|
||||
if (this->port == 0)
|
||||
this->isIsolated = true;
|
||||
}
|
||||
|
||||
ParticipantUDP::ParticipantUDP(const char* ipAddress,
|
||||
LocalParticipant::LocalParticipant(const char* ipAddress,
|
||||
int port,
|
||||
int localPort)
|
||||
: Participant("127.0.0.1", localPort) {
|
||||
@ -42,45 +42,45 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress,
|
||||
this->remoteSite = new Participant(ipAddress, port);
|
||||
}
|
||||
|
||||
static ParticipantUDP* isolatedParticipant = nullptr;
|
||||
static LocalParticipant* isolatedParticipant = nullptr;
|
||||
|
||||
ParticipantUDP* ParticipantUDP::Isolated() {
|
||||
LocalParticipant* LocalParticipant::Isolated() {
|
||||
if (isolatedParticipant == nullptr)
|
||||
isolatedParticipant = new ParticipantUDP(0);
|
||||
isolatedParticipant = new LocalParticipant(0);
|
||||
return isolatedParticipant;
|
||||
}
|
||||
|
||||
void ParticipantUDP::begin() {
|
||||
void LocalParticipant::begin() {
|
||||
if (this->isIsolated)
|
||||
return;
|
||||
|
||||
SetupUDP(this->port, this->remoteSite->ipAddress, this->remoteSite->port);
|
||||
}
|
||||
|
||||
void ParticipantUDP::SetupUDP(int localPort,
|
||||
void LocalParticipant::SetupUDP(int localPort,
|
||||
const char* remoteIpAddress,
|
||||
int remotePort) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
thisWindows->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix =
|
||||
static_cast<Posix::ParticipantUDP*>(this);
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
thisPosix->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
thisArduino->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
EspIdf::LocalParticipant* thisEspIdf =
|
||||
static_cast<EspIdf::LocalParticipant*>(this);
|
||||
thisEspIdf->Setup(localPort, remoteIpAddress, remotePort);
|
||||
#endif
|
||||
this->connected = true;
|
||||
}
|
||||
|
||||
void ParticipantUDP::Update(unsigned long currentTimeMs) {
|
||||
void LocalParticipant::Update(unsigned long currentTimeMs) {
|
||||
if (currentTimeMs == 0)
|
||||
currentTimeMs = Thing::GetTimeMs();
|
||||
|
||||
@ -114,27 +114,27 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::ReceiveUDP() {
|
||||
void LocalParticipant::ReceiveUDP() {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
thisWindows->Receive();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix =
|
||||
static_cast<Posix::ParticipantUDP*>(this);
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
thisPosix->Receive();
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
thisArduino->Receive();
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
EspIdf::LocalParticipant* thisEspIdf =
|
||||
static_cast<EspIdf::LocalParticipant*>(this);
|
||||
thisEspIdf->Receive();
|
||||
#endif
|
||||
}
|
||||
|
||||
Participant* ParticipantUDP::GetParticipant(const char* ipAddress, int port) {
|
||||
Participant* LocalParticipant::GetParticipant(const char* ipAddress, int port) {
|
||||
for (Participant* sender : this->senders) {
|
||||
if (strcmp(sender->ipAddress, ipAddress) == 0 && sender->port == port)
|
||||
return sender;
|
||||
@ -142,7 +142,7 @@ Participant* ParticipantUDP::GetParticipant(const char* ipAddress, int port) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) {
|
||||
Participant* LocalParticipant::AddParticipant(const char* ipAddress, int port) {
|
||||
// std::cout << "New Participant " << ipAddress << ":" << port << "\n";
|
||||
Participant* participant = new Participant(ipAddress, port);
|
||||
#if defined(NO_STD)
|
||||
@ -157,7 +157,7 @@ Participant* ParticipantUDP::AddParticipant(const char* ipAddress, int port) {
|
||||
|
||||
#pragma region Send
|
||||
|
||||
void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
|
||||
void LocalParticipant::SendThingInfo(Participant* remoteParticipant,
|
||||
Thing* thing) {
|
||||
// std::cout << "Send thing info " << (int)thing->id << " \n";
|
||||
ThingMsg* thingMsg = new ThingMsg(this->networkId, thing);
|
||||
@ -177,33 +177,33 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
|
||||
delete customMsg;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) {
|
||||
bool LocalParticipant::Send(Participant* remoteParticipant, IMessage* msg) {
|
||||
int bufferSize = msg->Serialize(this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
return true;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
return thisWindows->Send(remoteParticipant, bufferSize);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix =
|
||||
static_cast<Posix::ParticipantUDP*>(this);
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
return thisPosix->Send(remoteParticipant, bufferSize);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
return thisArduino->Send(remoteParticipant, bufferSize);
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
EspIdf::LocalParticipant* thisEspIdf =
|
||||
static_cast<EspIdf::LocalParticipant*>(this);
|
||||
return thisEspIdf->Send(remoteParticipant, bufferSize);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::PublishThingInfo(Thing* thing) {
|
||||
void LocalParticipant::PublishThingInfo(Thing* thing) {
|
||||
// std::cout << "Publish thing info" << thing->networkId << "\n";
|
||||
// Strange, when publishing, the network id is irrelevant, because it is
|
||||
// connected to a specific site...
|
||||
@ -224,22 +224,22 @@ void ParticipantUDP::PublishThingInfo(Thing* thing) {
|
||||
delete customMsg;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Windows::ParticipantUDP* thisWindows =
|
||||
static_cast<Windows::ParticipantUDP*>(this);
|
||||
Windows::LocalParticipant* thisWindows =
|
||||
static_cast<Windows::LocalParticipant*>(this);
|
||||
return thisWindows->Publish(msg);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
Posix::ParticipantUDP* thisPosix =
|
||||
static_cast<Posix::ParticipantUDP*>(this);
|
||||
Posix::LocalParticipant* thisPosix =
|
||||
static_cast<Posix::LocalParticipant*>(this);
|
||||
return thisPosix->Publish(msg);
|
||||
#elif defined(ARDUINO)
|
||||
Arduino::ParticipantUDP* thisArduino =
|
||||
static_cast<Arduino::ParticipantUDP*>(this);
|
||||
Arduino::LocalParticipant* thisArduino =
|
||||
static_cast<Arduino::LocalParticipant*>(this);
|
||||
return thisArduino->Publish(msg);
|
||||
#elif defined(IDF_VER)
|
||||
EspIdf::ParticipantUDP* thisEspIdf =
|
||||
static_cast<EspIdf::ParticipantUDP*>(this);
|
||||
EspIdf::LocalParticipant* thisEspIdf =
|
||||
static_cast<EspIdf::LocalParticipant*>(this);
|
||||
return thisEspIdf->Publish(msg);
|
||||
#else
|
||||
return false;
|
||||
@ -251,7 +251,7 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
|
||||
#pragma region Receive
|
||||
|
||||
void ParticipantUDP::ReceiveData(unsigned char packetSize,
|
||||
void LocalParticipant::ReceiveData(unsigned char packetSize,
|
||||
char* senderIpAddress,
|
||||
unsigned int senderPort) {
|
||||
Participant* remoteParticipant =
|
||||
@ -268,7 +268,7 @@ void ParticipantUDP::ReceiveData(unsigned char packetSize,
|
||||
ReceiveData(packetSize, remoteParticipant);
|
||||
}
|
||||
|
||||
void ParticipantUDP::ReceiveData(unsigned char bufferSize,
|
||||
void LocalParticipant::ReceiveData(unsigned char bufferSize,
|
||||
Participant* remoteParticipant) {
|
||||
unsigned char msgId = this->buffer[0];
|
||||
// std::cout << "receive msg " << (int)msgId << "\n";
|
||||
@ -311,9 +311,9 @@ void ParticipantUDP::ReceiveData(unsigned char bufferSize,
|
||||
};
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, ParticipantMsg* msg) {}
|
||||
void LocalParticipant::Process(Participant* sender, ParticipantMsg* msg) {}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
|
||||
void LocalParticipant::Process(Participant* sender, SiteMsg* msg) {
|
||||
std::cout << this->name << ": process Site Id " << (int)this->networkId
|
||||
<< "->" << (int)msg->networkId << "\n";
|
||||
if (this->networkId != msg->networkId) {
|
||||
@ -324,14 +324,25 @@ void ParticipantUDP::Process(Participant* sender, SiteMsg* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, InvestigateMsg* msg) {}
|
||||
void LocalParticipant::Process(Participant* sender, InvestigateMsg* msg) {}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, ThingMsg* msg) {
|
||||
void LocalParticipant::Process(Participant* sender, ThingMsg* msg) {
|
||||
std::cout << this->name << ": process Thing [" << (int)msg->networkId << "/"
|
||||
<< (int)msg->thingId << "]\n";
|
||||
#if !defined(NO_STD)
|
||||
auto thingMsgProcessor = thingMsgProcessors.find(msg->thingType);
|
||||
//Thing* newThing;
|
||||
if (thingMsgProcessor != thingMsgProcessors.end()) { // found item
|
||||
//newThing =
|
||||
thingMsgProcessor->second(sender, msg->networkId, msg->thingId);
|
||||
} else
|
||||
//newThing =
|
||||
new Thing(sender, msg->networkId, msg->thingId,
|
||||
(Thing::Type)msg->thingType);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
|
||||
void LocalParticipant::Process(Participant* sender, NameMsg* msg) {
|
||||
std::cout << this->name << ": process Name [" << (int)msg->networkId << "/"
|
||||
<< (int)msg->thingId << "]\n";
|
||||
|
||||
@ -358,14 +369,14 @@ void ParticipantUDP::Process(Participant* sender, NameMsg* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, PoseMsg* msg) {
|
||||
void LocalParticipant::Process(Participant* sender, PoseMsg* msg) {
|
||||
std::cout << this->name << ": process Pose [" << (int)this->networkId << "/"
|
||||
<< (int)msg->networkId << "]\n";
|
||||
}
|
||||
|
||||
void ParticipantUDP::Process(Participant* sender, BinaryMsg* msg) {
|
||||
std::cout << this->name << ": process Binary [" << (int)msg->networkId << "/"
|
||||
<< (int)msg->thingId << "]\n";
|
||||
void LocalParticipant::Process(Participant* sender, BinaryMsg* msg) {
|
||||
std::cout << this->name << ": process Binary [" << (int)this->networkId << "/"
|
||||
<< (int)msg->networkId << "]\n";
|
||||
Thing* thing = sender->Get(msg->networkId, msg->thingId);
|
||||
if (thing != nullptr)
|
||||
thing->ProcessBinary(msg->bytes);
|
||||
|
@ -23,6 +23,8 @@
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(ARDUINO)
|
||||
// #include <WiFiUdp.h>
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
@ -38,20 +40,20 @@ constexpr int MAX_SENDER_COUNT = 256;
|
||||
/// It is possible to work with an hidden participant by creating things without
|
||||
/// specifying a participant in the constructor. In that case an hidden isolated
|
||||
/// participant is created which can be obtained using
|
||||
/// RoboidControl::IsolatedParticipant::Isolated().
|
||||
/// RoboidControl::LocalParticipant::Isolated().
|
||||
/// @sa RoboidControl::Thing::Thing()
|
||||
class ParticipantUDP : public Participant {
|
||||
class LocalParticipant : public Participant {
|
||||
public:
|
||||
/// @brief Create a participant without connecting to a site
|
||||
/// @param port The port on which the participant communicates
|
||||
/// These participant typically broadcast Participant messages to let site
|
||||
/// servers on the local network know their presence. Alternatively they can
|
||||
/// broadcast information which can be used directly by other participants.
|
||||
ParticipantUDP(int port = 7681);
|
||||
LocalParticipant(int port = 7681);
|
||||
/// @brief Create a participant which will try to connect to a site.
|
||||
/// @param ipAddress The IP address of the site
|
||||
/// @param port The port used by the site
|
||||
ParticipantUDP(const char* ipAddress,
|
||||
LocalParticipant(const char* ipAddress,
|
||||
int port = 7681,
|
||||
int localPort = 7681);
|
||||
// Note to self: one cannot specify the port used by the local participant
|
||||
@ -60,7 +62,7 @@ class ParticipantUDP : public Participant {
|
||||
/// @brief Isolated participant is used when the application is run without
|
||||
/// networking
|
||||
/// @return A participant without networking support
|
||||
static ParticipantUDP* Isolated();
|
||||
static LocalParticipant* Isolated();
|
||||
|
||||
/// @brief True if the participant is running isolated.
|
||||
/// Isolated participants do not communicate with other participants
|
||||
@ -71,7 +73,7 @@ class ParticipantUDP : public Participant {
|
||||
long publishInterval = 3000; // 3 seconds
|
||||
|
||||
/// @brief The name of the participant
|
||||
const char* name = "ParticipantUDP";
|
||||
const char* name = "LocalParticipant";
|
||||
|
||||
// int localPort = 0;
|
||||
|
||||
@ -99,7 +101,7 @@ class ParticipantUDP : public Participant {
|
||||
void begin();
|
||||
bool connected = false;
|
||||
|
||||
virtual void Update(unsigned long currentTimeMs = 0) override;
|
||||
virtual void Update(unsigned long currentTimeMs = 0);
|
||||
|
||||
void SendThingInfo(Participant* remoteParticipant, Thing* thing);
|
||||
void PublishThingInfo(Thing* thing);
|
||||
@ -140,31 +142,31 @@ class ParticipantUDP : public Participant {
|
||||
virtual void Process(Participant* sender, BinaryMsg* msg);
|
||||
|
||||
#if !defined(NO_STD)
|
||||
// public:
|
||||
// using ThingConstructor = std::function<Thing*(Participant* participant,
|
||||
// unsigned char networkId,
|
||||
// unsigned char thingId)>;
|
||||
public:
|
||||
using ThingConstructor = std::function<Thing*(Participant* participant,
|
||||
unsigned char networkId,
|
||||
unsigned char thingId)>;
|
||||
|
||||
// template <typename ThingClass>
|
||||
// void Register(unsigned char thingType) {
|
||||
// thingMsgProcessors[thingType] = [](Participant* participant,
|
||||
// unsigned char networkId,
|
||||
// unsigned char thingId) {
|
||||
// return new ThingClass(participant, networkId, thingId);
|
||||
// };
|
||||
// };
|
||||
template <typename ThingClass>
|
||||
void Register(unsigned char thingType) {
|
||||
thingMsgProcessors[thingType] = [](Participant* participant,
|
||||
unsigned char networkId,
|
||||
unsigned char thingId) {
|
||||
return new ThingClass(participant, networkId, thingId);
|
||||
};
|
||||
};
|
||||
|
||||
// template <typename ThingClass>
|
||||
// void Register2(unsigned char thingType, ThingConstructor f) {
|
||||
// thingMsgProcessors[thingType] = [f](Participant* participant,
|
||||
// unsigned char networkId,
|
||||
// unsigned char thingId) {
|
||||
// return f(participant, networkId, thingId);
|
||||
// };
|
||||
// };
|
||||
template <typename ThingClass>
|
||||
void Register2(unsigned char thingType, ThingConstructor f) {
|
||||
thingMsgProcessors[thingType] = [f](Participant* participant,
|
||||
unsigned char networkId,
|
||||
unsigned char thingId) {
|
||||
return f(participant, networkId, thingId);
|
||||
};
|
||||
};
|
||||
|
||||
// protected:
|
||||
// std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
|
||||
protected:
|
||||
std::unordered_map<unsigned char, ThingConstructor> thingMsgProcessors;
|
||||
|
||||
#endif
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "ParticipantUDP.h"
|
||||
#include "LocalParticipant.h"
|
||||
|
||||
#if !defined(NO_STD)
|
||||
#include <functional>
|
||||
@ -11,7 +11,7 @@
|
||||
namespace RoboidControl {
|
||||
|
||||
/// @brief A participant is device which can communicate with other participants
|
||||
class SiteServer : public ParticipantUDP {
|
||||
class SiteServer : public LocalParticipant {
|
||||
public:
|
||||
SiteServer(int port = 7681);
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
namespace RoboidControl {
|
||||
namespace Posix {
|
||||
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
|
||||
// Create a UDP socket
|
||||
@ -63,7 +63,7 @@ void Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void Receive() {
|
||||
void LocalParticipant::Receive() {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
sockaddr_in client_addr;
|
||||
socklen_t len = sizeof(client_addr);
|
||||
@ -90,7 +90,7 @@ void Receive() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
// std::cout << "Send to " << remoteParticipant->ipAddress << ":" << ntohs(remoteParticipant->port)
|
||||
// << "\n";
|
||||
@ -113,7 +113,7 @@ bool Send(Participant* remoteParticipant, int bufferSize) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Publish(IMessage* msg) {
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
int bufferSize = msg->Serialize(this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
|
@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
#include "../LocalParticipant.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Posix {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class LocalParticipant : public RoboidControl::LocalParticipant {
|
||||
public:
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include "Thing.h"
|
||||
|
||||
#include "Participant.h"
|
||||
#include "Participants/IsolatedParticipant.h"
|
||||
#include "LocalParticipant.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@ -22,7 +21,7 @@ namespace RoboidControl {
|
||||
// return isolatedParticipant;
|
||||
// }
|
||||
|
||||
Thing::Thing(int thingType) : Thing(IsolatedParticipant::Isolated(), thingType) {}
|
||||
Thing::Thing(int thingType) : Thing(LocalParticipant::Isolated(), thingType) {}
|
||||
|
||||
Thing::Thing(Participant* owner, Type thingType)
|
||||
: Thing(owner, (unsigned char)thingType) {}
|
||||
@ -202,7 +201,7 @@ void Thing::Update(unsigned long currentTimeMs, bool recursive) {
|
||||
}
|
||||
|
||||
void Thing::UpdateThings(unsigned long currentTimeMs) {
|
||||
IsolatedParticipant::Isolated()->Update(currentTimeMs);
|
||||
LocalParticipant::Isolated()->Update(currentTimeMs);
|
||||
}
|
||||
|
||||
void Thing::GenerateBinary(char* buffer, unsigned char* ix) {
|
||||
|
2
Thing.h
2
Thing.h
@ -10,7 +10,7 @@
|
||||
namespace RoboidControl {
|
||||
|
||||
class Participant;
|
||||
class ParticipantUDP;
|
||||
class LocalParticipant;
|
||||
|
||||
#define THING_STORE_SIZE 256
|
||||
// IMPORTANT: values higher than 256 will need to change the Thing::id type
|
||||
|
@ -4,22 +4,36 @@
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h> // For fcntl
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Windows {
|
||||
|
||||
void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
void LocalParticipant::Setup(int localPort, const char* remoteIpAddress, int remotePort) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
// Create a UDP socket
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
// Windows-specific Winsock initialization
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
std::cerr << "WSAStartup failed" << std::endl;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
this->sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
#endif
|
||||
|
||||
if (this->sock < 0) {
|
||||
std::cerr << "Error creating socket" << std::endl;
|
||||
@ -27,8 +41,13 @@ void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remot
|
||||
}
|
||||
|
||||
// Set the socket to non-blocking mode
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
u_long mode = 1; // 1 to enable non-blocking socket
|
||||
ioctlsocket(this->sock, FIONBIO, &mode);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
int flags = fcntl(this->sock, F_GETFL, 0);
|
||||
fcntl(this->sock, F_SETFL, flags | O_NONBLOCK);
|
||||
#endif
|
||||
|
||||
if (remotePort != 0) {
|
||||
// Set up the address to send to
|
||||
@ -37,8 +56,12 @@ void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remot
|
||||
remote_addr.sin_port = htons((u_short)remotePort);
|
||||
if (inet_pton(AF_INET, remoteIpAddress, &remote_addr.sin_addr) <= 0) {
|
||||
std::cerr << "Invalid address" << std::endl;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
close(sock);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -49,22 +72,30 @@ void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remot
|
||||
server_addr.sin_port = htons((u_short)localPort);
|
||||
if (inet_pton(AF_INET, "0.0.0.0", &server_addr.sin_addr) <= 0) {
|
||||
std::cerr << "Invalid address" << std::endl;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
close(sock);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind the socket to the specified port
|
||||
if (bind(this->sock, (const struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
|
||||
std::cerr << "Bind failed" << std::endl;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
closesocket(sock);
|
||||
WSACleanup();
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
close(sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // _WIN32 || _WIN64
|
||||
#endif
|
||||
}
|
||||
|
||||
void ParticipantUDP::Receive() {
|
||||
void LocalParticipant::Receive() {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
// char ip_str[INET_ADDRSTRLEN];
|
||||
// inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
|
||||
@ -72,20 +103,28 @@ void ParticipantUDP::Receive() {
|
||||
// << ntohs(server_addr.sin_port) << "\n";
|
||||
|
||||
sockaddr_in client_addr;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int len = sizeof(client_addr);
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
socklen_t len = sizeof(client_addr);
|
||||
#endif
|
||||
int packetSize = recvfrom(this->sock, buffer, sizeof(buffer), 0, (struct sockaddr*)&client_addr, &len);
|
||||
// std::cout << "received data " << packetSize << "\n";
|
||||
if (packetSize < 0) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int error_code = WSAGetLastError(); // Get the error code on Windows
|
||||
if (error_code != WSAEWOULDBLOCK)
|
||||
std::cerr << "recvfrom failed with error: " << error_code << std::endl;
|
||||
#else
|
||||
// std::cerr << "recvfrom failed with error: " << packetSize << std::endl;
|
||||
#endif
|
||||
} else if (packetSize > 0) {
|
||||
char sender_ipAddress[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(client_addr.sin_addr), sender_ipAddress, INET_ADDRSTRLEN);
|
||||
unsigned int sender_port = ntohs(client_addr.sin_port);
|
||||
|
||||
ReceiveData(packetSize, sender_ipAddress, sender_port);
|
||||
// RoboidControl::ParticipantUDP* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port);
|
||||
// RoboidControl::LocalParticipant* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port);
|
||||
// if (remoteParticipant == nullptr) {
|
||||
// remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port);
|
||||
// // std::cout << "New sender " << sender_ipAddress << ":"
|
||||
@ -99,16 +138,16 @@ void ParticipantUDP::Receive() {
|
||||
|
||||
// ReceiveData(packetSize, remoteParticipant);
|
||||
}
|
||||
#endif // _WIN32 || _WIN64
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
bool LocalParticipant::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
char ip_str[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(remote_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
|
||||
std::cout << "Send to " << ip_str << ":" << ntohs(remote_addr.sin_port) << "\n";
|
||||
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr*)&remote_addr, sizeof(remote_addr));
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (sent_bytes <= SOCKET_ERROR) {
|
||||
int error_code = WSAGetLastError(); // Get the error code on Windows
|
||||
std::cerr << "sendto failed with error: " << error_code << std::endl;
|
||||
@ -116,11 +155,18 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
#endif // _WIN32 || _WIN64
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
if (sent_bytes < 0) {
|
||||
std::cerr << "sendto failed with error: " << sent_bytes << std::endl;
|
||||
close(sock);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
bool LocalParticipant::Publish(IMessage* msg) {
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int bufferSize = msg->Serialize(this->buffer);
|
||||
if (bufferSize <= 0)
|
||||
@ -130,7 +176,7 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
inet_ntop(AF_INET, &(broadcast_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
|
||||
std::cout << "Publish to " << ip_str << ":" << ntohs(broadcast_addr.sin_port) << "\n";
|
||||
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr*)&broadcast_addr, sizeof(broadcast_addr));
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (sent_bytes <= SOCKET_ERROR) {
|
||||
int error_code = WSAGetLastError(); // Get the error code on Windows
|
||||
std::cerr << "sendto failed with error: " << error_code << std::endl;
|
||||
@ -138,7 +184,14 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
#endif // _WIN32 || _WIN64
|
||||
#elif defined(__unix__) || defined(__APPLE__)
|
||||
if (sent_bytes < 0) {
|
||||
std::cerr << "sendto failed with error: " << sent_bytes << std::endl;
|
||||
close(sock);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
#include "../LocalParticipant.h"
|
||||
|
||||
namespace RoboidControl {
|
||||
namespace Windows {
|
||||
|
||||
class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
||||
class LocalParticipant : public RoboidControl::LocalParticipant {
|
||||
public:
|
||||
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
|
||||
void Receive();
|
||||
|
@ -9,7 +9,7 @@
|
||||
// #include <ws2tcpip.h>
|
||||
|
||||
#include "Participant.h"
|
||||
#include "Participants/SiteServer.h"
|
||||
#include "SiteServer.h"
|
||||
#include "Thing.h"
|
||||
|
||||
using namespace RoboidControl;
|
||||
@ -52,8 +52,8 @@ protected:
|
||||
|
||||
|
||||
|
||||
// TEST_F(ParticipantSuite, ParticipantUDP) {
|
||||
// ParticipantUDP* participant = new ParticipantUDP("127.0.0.1", 7681);
|
||||
// TEST_F(ParticipantSuite, LocalParticipant) {
|
||||
// LocalParticipant* participant = new LocalParticipant("127.0.0.1", 7681);
|
||||
|
||||
// unsigned long milliseconds = get_time_ms();
|
||||
// unsigned long startTime = milliseconds;
|
||||
@ -80,7 +80,7 @@ protected:
|
||||
|
||||
// TEST_F(ParticipantSuite, SiteParticipant) {
|
||||
// SiteServer site = SiteServer(7681);
|
||||
// ParticipantUDP participant = ParticipantUDP("127.0.0.1", 7681);
|
||||
// LocalParticipant participant = LocalParticipant("127.0.0.1", 7681);
|
||||
|
||||
// unsigned long milliseconds = get_time_ms();
|
||||
// unsigned long startTime = milliseconds;
|
||||
@ -96,7 +96,7 @@ protected:
|
||||
|
||||
// TEST_F(ParticipantSuite, Thing) {
|
||||
// SiteServer site = SiteServer(7681);
|
||||
// ParticipantUDP participant = ParticipantUDP("127.0.0.1", 7681);
|
||||
// LocalParticipant participant = LocalParticipant("127.0.0.1", 7681);
|
||||
// Thing thing = Thing(&participant);
|
||||
|
||||
// unsigned long milliseconds = get_time_ms();
|
||||
|
@ -4,7 +4,7 @@
|
||||
// not supported using Visual Studio 2022 compiler...
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Participants/ParticipantUDP.h"
|
||||
#include "LocalParticipant.h"
|
||||
#include "Thing.h"
|
||||
|
||||
using namespace RoboidControl;
|
||||
@ -23,7 +23,7 @@ TEST(RoboidControlSuite, HiddenParticipant) {
|
||||
}
|
||||
|
||||
TEST(RoboidControlSuite, IsolatedParticipant) {
|
||||
ParticipantUDP* participant = ParticipantUDP::Isolated();
|
||||
LocalParticipant* participant = LocalParticipant::Isolated();
|
||||
Thing* thing = new Thing(participant);
|
||||
|
||||
unsigned long milliseconds = Thing::GetTimeMs();
|
||||
|
Loading…
x
Reference in New Issue
Block a user