Skip to content

Commit

Permalink
fix(lwip): Add early out in NetworkUDP::parsePacket() when socket h…
Browse files Browse the repository at this point in the history
…as no data (espressif#10075)

* fix(lwip): Add early out in `NetworkUDP::parsePacket()` when socket has no data

Previously, `NetworkUDP::parsePacket()` would take the time to allocate a 1460 byte buffer
to call `recvfrom()` with, immediately freeing it if there was no data read.

This change has it check if there is available data via `ioctl()` with `FIONREAD` first,
saving the allocation and thus significantly increasing performance in no data situations.

* fix(lwip): Initialize `len` to ensure it's set before check
  • Loading branch information
nitz committed Jul 31, 2024
1 parent 7d731e0 commit ad5aaf8
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion libraries/Network/src/NetworkUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,14 @@ int NetworkUDP::parsePacket() {
}
struct sockaddr_storage si_other_storage; // enough storage for v4 and v6
socklen_t slen = sizeof(sockaddr_storage);
int len;
int len = 0;
if (ioctl(udp_server, FIONREAD, &len) == -1) {
log_e("could not check for data in buffer length: %d", errno);
return 0;
}
if (!len) {
return 0;
}
char *buf = (char *)malloc(1460);
if (!buf) {
return 0;
Expand Down

0 comments on commit ad5aaf8

Please sign in to comment.