earlephilhower / arduino-pico

Raspberry Pi Pico Arduino core, for all RP2040 and RP2350 boards
GNU Lesser General Public License v2.1
2.06k stars 431 forks source link

WiFiUdp flush() purpose #2617

Open metanav opened 1 day ago

metanav commented 1 day ago

What is the intended behavior of the flush() function in the WiFiUdp? It calls the endPacket() function.

https://github.com/earlephilhower/arduino-pico/blob/master/libraries/WiFi/src/WiFiUdp.cpp#L226

It should clear the buffer instead of calling the endPacket() function.

I consider this an issue because the code calling endPacket() followed by flush() sends a correct packet and then an empty packet.

udp_client.beginPacket(locator->address, locator->port);
size_t sent = udp_client.write(buf, len);
udp_client.endPacket()
udp_client.flush();
maxgerhardt commented 15 hours ago

Maybe the API is intended for in-between flushes?

udp_client.beginPacket(locator->address, locator->port);
size_t sent = udp_client.write(buf, len);
udp_client.flush(); // send this important data right now
size_t sent2 = udp_client.write("other data", 10); // continue writing
udp_client.endPacket(); // actual end of packet 
earlephilhower commented 15 hours ago

flush exists on the IP stack because it's part of Stream (IIRC) which requires an implementation of it. "flush pushes out whatever data is in the pipe to the wire" is the general idea here. Serial1.flush() makes sure anything in the HW FIFO has moved to the transmitter, for example.

For UDP, endPacket also guarantees it's sent to the Ethernet stack. So a flush isn't really needed but should be a no-op, not a 0-byte UDP ping, after an endPacket. So I think that may be a bug.

But conceptually, especially with UDP not having any advanced packet recombination like TCP, I am not sure what the use case of calling it would be. We don't have a connection you can partially send data down, UDP is always built packet-by-packet here.