smoltcp-rs / smoltcp

a smol tcp/ip stack
BSD Zero Clause License
3.64k stars 404 forks source link

add send_queue() for udp::Socket? #838

Open phire opened 10 months ago

phire commented 10 months ago

tcp::Socket has a send_queue() method to check the status of tx_buffer (which can be used to implement flush), but udp::Socket is missing any equivalent functionality to check tx_buffer status (except for can_send)

My usecase: I want to create a UDP socket, send a packet to a broadcast address, and then immediately destroy the socket. The same memory is reused to create TCP socket and listen for an incoming connection. This loops after a timeout, destroying the TCP socket to reuse the memory for a UDP socket.

There is no sane way to implement this with the current api, as dropping or closing the UDP socket too soon drops any unsent packets before they even reach the network driver. Yes, I know that delivery of UDP packets isn't meant to be reliable, but this results in zero packets being delivered.
The only workaround is to insert a sleep before destroying the UDP socket and hope it's the delay is long enough to allow for transmission, but I'd rather not do that.

If send_queue() was added to udp::Socket, then I could flush it just like a TCP socket before dropping.

Dirbaio commented 10 months ago

if it's a single packet, you can kinda already do it: set the buffer size to 1 packet, then wait until can_send() == true.

I agree having send_queue/recv_queue would be nice, it helps if you want to flush more than 1 packet. PRs welcome! (If you do, please add it to IcmpSocket, RawSocket as well, as they should be consistent)