Receiving no longer crashes

This commit is contained in:
Pascal Serrarens 2025-04-08 12:48:52 +02:00
parent b594bd59f4
commit a1fc21dfe1
3 changed files with 25 additions and 16 deletions

View File

@ -9,6 +9,7 @@ void ParticipantUDP::Setup(int localPort,
const char* remoteIpAddress, const char* remoteIpAddress,
int remotePort) { int remotePort) {
#if defined(IDF_VER) #if defined(IDF_VER)
std::cout << "Set up UDP\n";
GetBroadcastAddress(); GetBroadcastAddress();
wifi_ap_record_t ap_info; wifi_ap_record_t ap_info;
@ -19,8 +20,8 @@ void ParticipantUDP::Setup(int localPort,
} }
// Create a UDP socket // Create a UDP socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0); this->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd < 0) { if (this->sockfd < 0) {
std::cout << "Unable to create UDP socket: errno " << errno << "\n"; std::cout << "Unable to create UDP socket: errno " << errno << "\n";
vTaskDelete(NULL); vTaskDelete(NULL);
return; return;
@ -36,7 +37,7 @@ void ParticipantUDP::Setup(int localPort,
// Bind the socket to the address and port // 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"; std::cout << "Unable to bind UDP socket: errno " << errno << "\n";
close(sockfd); close(sockfd);
vTaskDelete(NULL); vTaskDelete(NULL);
@ -79,39 +80,46 @@ void ParticipantUDP::GetBroadcastAddress() {
void ParticipantUDP::Receive() { void ParticipantUDP::Receive() {
#if defined(IDF_VER) #if defined(IDF_VER)
struct pollfd fds; struct pollfd fds[1];
fds.fd = sockfd; fds[0].fd = sockfd;
fds.events = POLLIN; // We're looking for data available to read fds[0].events = POLLIN; // We're looking for data available to read
// Use poll() with a timeout of 0 to return immediately // 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) { if (ret == -1) {
std::cout << "poll() error\n"; std::cout << "poll() error\n";
return; 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]; 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, 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) { if (packetSize < 0) {
std::cout << "recvfrom() error\n"; std::cout << "recvfrom() error\n";
return; return;
} else if (packetSize == 0) {
break;
} }
std::cout << "receiving " << packetSize << " bytes\n"; std::cout << "receiving " << packetSize << " bytes, msgId " << (int)this->buffer[0] << "\n";
// inet_ntoa_r(this->src_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN); inet_ntoa_r(source_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN);
// unsigned int sender_port = ntohs(this->src_addr.sin_port); 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) { if (ret == -1) {
std::cout << "poll() error\n"; std::cout << "poll() error\n";
return; return;
} }
} }
//std::cout << "no more messages\n";
#endif // IDF_VER #endif // IDF_VER
} }

View File

@ -20,7 +20,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
int sockfd; int sockfd;
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
struct sockaddr_in src_addr; //struct sockaddr_in src_addr;
void GetBroadcastAddress(); void GetBroadcastAddress();
}; };

View File

@ -98,6 +98,7 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
this->nextPublishMe = currentTimeMs + this->publishInterval; this->nextPublishMe = currentTimeMs + this->publishInterval;
} }
this->ReceiveUDP(); this->ReceiveUDP();
} }