Open ramondeklein opened 3 years ago
libstd's UdpSocket
doesn't expose vectored recv either; the only use of recvmsg
in libstd is for UnixDatagram. So vectored recv for UdpSocket
would need to be implemented in the whole stack - libstd, then mio, then tokio.
Yeah, vectored reads for UdpSocket
are currently not supported.
@Thomasdezeeuw What are your thoughts on this?
I am using Tokio v1.1.0 and attempt to use
UdpSocket::try_recv_buf_from
and use vectored I/O to read the datagram directly in two vectors. I am using the following code:The
key
vector 16 bytes and thevalue
vector is 65.491 bytes so all UDP datagrams should fit in these buffers. When 12.345 bytes are received, the first 16 bytes should go into thekey
vector and the remaining 12.329 bytes should be stored in thevalue
vector.Unfortunately, this doesn't work and the behaviour is a bit different on Windows and Linux. Windows returns error-code 10040 (WSAEMSGSIZE) to indicate that the datagram doesn't fit in the buffer. On Linux it only returns the first 16 bytes. When looking at the implementation of
try_recv_buf_from
it becomes clear why this doesn't work:An attempt is made to read the entire datagram in the first buffer. Windows reports an error when reading a datagram in a buffer that is too small. Linux reads the data that fits the first buffer and discards the rest of the data.
Vectored I/O cannot be implemented in a wrapper (without data copying), but should be implemented at the OS level. Both Windows and Linux have support for vectored I/O when reading datagrams. On Linux based systems, it should have called recvmsg(2) instead and Windows supports a scatter/gather call as well with WSARecvFrom.