dispatchrun / net

Go package implementing WASI socket extensions
Apache License 2.0
137 stars 7 forks source link

connected UDP sockets #36

Open marten-seemann opened 3 months ago

marten-seemann commented 3 months ago

It would be nice if we could use connected UDP sockets. In certain cases, they're significantly faster than unconnected UDP sockets, as the kernel doesn't need to perform an address lookup.

API-wise, this is a bit tricky though. In the standard library, net.DialUDP returns a *net.UDPConn (which implements the net.PacketConn interface). However, calling WriteTo on that *net.UDPConn returns an error (even when attempting to send to the address that the connection is connected to), you have to use Write instead.

I opened #35 as a draft PR to add a DialUDP function. It's a pretty small change, and it performs the necessary syscalls to connect the socket. However, the return value is really awkward, since it's a net.PacketConn. To be able to use it, you'd first have to type-assert it to an io.Writer. It would probably cleaner to return a *UDPConn struct instead. Unfortunately, we can't use the *net.UDPConn without doing some unsafe operations.

Any advice on how to solve this?