Receiving no longer crashes
This commit is contained in:
parent
b594bd59f4
commit
a1fc21dfe1
@ -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
|
||||
}
|
||||
|
@ -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();
|
||||
};
|
||||
|
@ -98,6 +98,7 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
|
||||
|
||||
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
||||
}
|
||||
|
||||
this->ReceiveUDP();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user