From 12d91378e5fe126b85141123aefb27a4c33f96e2 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 8 Apr 2025 10:43:20 +0200 Subject: [PATCH] Fix refactoring issues --- EspIdf/EspIdfParticipant.cpp | 2 + EspIdf/EspIdfParticipant.h | 7 +++- Windows/WindowsParticipant.cpp | 73 +++++----------------------------- test/participant_test.cc | 2 +- test/thing_test.cc | 2 +- 5 files changed, 19 insertions(+), 67 deletions(-) diff --git a/EspIdf/EspIdfParticipant.cpp b/EspIdf/EspIdfParticipant.cpp index 4831d7d..5dd9ca0 100644 --- a/EspIdf/EspIdfParticipant.cpp +++ b/EspIdf/EspIdfParticipant.cpp @@ -1,6 +1,8 @@ #include "EspIdfParticipant.h" +#if defined(IDF_VER) #include "esp_wifi.h" +#endif namespace RoboidControl { namespace EspIdf { diff --git a/EspIdf/EspIdfParticipant.h b/EspIdf/EspIdfParticipant.h index fea444f..ecdad07 100644 --- a/EspIdf/EspIdfParticipant.h +++ b/EspIdf/EspIdfParticipant.h @@ -2,7 +2,9 @@ #include "Participants/ParticipantUDP.h" +#if defined(IDF_VER) #include "lwip/sockets.h" +#endif namespace RoboidControl { namespace EspIdf { @@ -15,12 +17,13 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP { bool Publish(IMessage* msg); protected: - char broadcastIpAddress[INET_ADDRSTRLEN]; // IPv4 address can have a max of 15 characters - // + null terminator +#if defined(IDF_VER) + char broadcastIpAddress[INET_ADDRSTRLEN]; int sockfd; struct sockaddr_in dest_addr; struct sockaddr_in src_addr; +#endif void GetBroadcastAddress(); }; diff --git a/Windows/WindowsParticipant.cpp b/Windows/WindowsParticipant.cpp index a321b2d..d64eb1e 100644 --- a/Windows/WindowsParticipant.cpp +++ b/Windows/WindowsParticipant.cpp @@ -4,36 +4,22 @@ #include #include #pragma comment(lib, "ws2_32.lib") - -#elif defined(__unix__) || defined(__APPLE__) -#include -#include // For fcntl -#include -#include -#include #endif namespace RoboidControl { namespace Windows { -void Setup(int localPort, const char* remoteIpAddress, int remotePort) { +void ParticipantUDP::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; @@ -41,13 +27,8 @@ void Setup(int localPort, const char* remoteIpAddress, int remotePort) { } // 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 @@ -56,12 +37,8 @@ void Setup(int localPort, const char* remoteIpAddress, int remotePort) { 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; } } @@ -72,30 +49,22 @@ void Setup(int localPort, const char* remoteIpAddress, int remotePort) { 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 +#endif // _WIN32 || _WIN64 } -void Receive() { +void ParticipantUDP::Receive() { #if defined(_WIN32) || defined(_WIN64) // char ip_str[INET_ADDRSTRLEN]; // inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN); @@ -103,21 +72,13 @@ void 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); @@ -138,16 +99,16 @@ void Receive() { // ReceiveData(packetSize, remoteParticipant); } -#endif +#endif // _WIN32 || _WIN64 } -bool Send(Participant* remoteParticipant, int bufferSize) { +bool ParticipantUDP::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; @@ -155,18 +116,11 @@ bool Send(Participant* remoteParticipant, int bufferSize) { WSACleanup(); return false; } -#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 +#endif // _WIN32 || _WIN64 return true; } -bool Publish(IMessage* msg) { +bool ParticipantUDP::Publish(IMessage* msg) { #if defined(_WIN32) || defined(_WIN64) int bufferSize = msg->Serialize(this->buffer); if (bufferSize <= 0) @@ -176,7 +130,7 @@ bool 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; @@ -184,14 +138,7 @@ bool Publish(IMessage* msg) { WSACleanup(); return false; } -#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 +#endif // _WIN32 || _WIN64 return true; } diff --git a/test/participant_test.cc b/test/participant_test.cc index d82c9e7..c826863 100644 --- a/test/participant_test.cc +++ b/test/participant_test.cc @@ -9,7 +9,7 @@ // #include #include "Participant.h" -#include "SiteServer.h" +#include "Participants/SiteServer.h" #include "Thing.h" using namespace RoboidControl; diff --git a/test/thing_test.cc b/test/thing_test.cc index 015c43c..b8ceea1 100644 --- a/test/thing_test.cc +++ b/test/thing_test.cc @@ -4,7 +4,7 @@ // not supported using Visual Studio 2022 compiler... #include -#include "LocalParticipant.h" +#include "Participants/ParticipantUDP.h" #include "Thing.h" using namespace RoboidControl;