From a1fc21dfe1fa021d705f65ef2253d6e19a54be7e Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 8 Apr 2025 12:48:52 +0200 Subject: [PATCH] Receiving no longer crashes --- EspIdf/EspIdfParticipant.cpp | 38 ++++++++++++++++++++------------- EspIdf/EspIdfParticipant.h | 2 +- Participants/ParticipantUDP.cpp | 1 + 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/EspIdf/EspIdfParticipant.cpp b/EspIdf/EspIdfParticipant.cpp index 4831d7d..c8188a5 100644 --- a/EspIdf/EspIdfParticipant.cpp +++ b/EspIdf/EspIdfParticipant.cpp @@ -9,6 +9,7 @@ void ParticipantUDP::Setup(int localPort, const char* remoteIpAddress, int remotePort) { #if defined(IDF_VER) +std::cout << "Set up UDP\n"; GetBroadcastAddress(); wifi_ap_record_t ap_info; @@ -19,8 +20,8 @@ void ParticipantUDP::Setup(int localPort, } // Create a UDP socket - sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd < 0) { + this->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (this->sockfd < 0) { std::cout << "Unable to create UDP socket: errno " << errno << "\n"; vTaskDelete(NULL); return; @@ -36,7 +37,7 @@ void ParticipantUDP::Setup(int localPort, // Bind the socket to the address and port - if (bind(sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) { + if (bind(this->sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) { std::cout << "Unable to bind UDP socket: errno " << errno << "\n"; close(sockfd); vTaskDelete(NULL); @@ -79,39 +80,46 @@ void ParticipantUDP::GetBroadcastAddress() { void ParticipantUDP::Receive() { #if defined(IDF_VER) - struct pollfd fds; - fds.fd = sockfd; - fds.events = POLLIN; // We're looking for data available to read + struct pollfd fds[1]; + fds[0].fd = sockfd; + fds[0].events = POLLIN; // We're looking for data available to read // Use poll() with a timeout of 0 to return immediately - int ret = poll(&fds, 1, 0); + int ret = poll(fds, 1, 0); if (ret == -1) { std::cout << "poll() error\n"; return; } - socklen_t addr_len = sizeof(this->src_addr); + + //char buffer[1024]; + struct sockaddr_in source_addr; + + socklen_t addr_len = sizeof(source_addr); char sender_ipAddress[INET_ADDRSTRLEN]; - while (ret > 0 && fds.revents & POLLIN) { + while (ret > 0 && fds[0].revents & POLLIN) { int packetSize = recvfrom(this->sockfd, buffer, sizeof(buffer) - 1, 0, - (struct sockaddr*)&this->src_addr, &addr_len); + (struct sockaddr*)&source_addr, &addr_len); if (packetSize < 0) { std::cout << "recvfrom() error\n"; return; + } else if (packetSize == 0) { + break; } - 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); + std::cout << "receiving " << packetSize << " bytes, msgId " << (int)this->buffer[0] << "\n"; + inet_ntoa_r(source_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN); + unsigned int sender_port = ntohs(source_addr.sin_port); - // ReceiveData(packetSize, sender_ipAddress, sender_port); + ReceiveData(packetSize, sender_ipAddress, sender_port); - int ret = poll(&fds, 1, 0); + ret = poll(fds, 1, 0); if (ret == -1) { std::cout << "poll() error\n"; return; } } + //std::cout << "no more messages\n"; #endif // IDF_VER } diff --git a/EspIdf/EspIdfParticipant.h b/EspIdf/EspIdfParticipant.h index fea444f..1e80f0e 100644 --- a/EspIdf/EspIdfParticipant.h +++ b/EspIdf/EspIdfParticipant.h @@ -20,7 +20,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP { int sockfd; struct sockaddr_in dest_addr; - struct sockaddr_in src_addr; + //struct sockaddr_in src_addr; void GetBroadcastAddress(); }; diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index a443002..08270cf 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -98,6 +98,7 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) { this->nextPublishMe = currentTimeMs + this->publishInterval; } + this->ReceiveUDP(); }