Spanification, ValueTask, and cancellation
This proposal spanifies connectionless methods on Socket, uses a ValueTask return, and adds cancellation support. There are already such overloads in Send() and Receive() functions for connection oriented sockets (TCP) and this is complement for UDP.
Proposed APIs
namespaceSystem.Net.Sockets{// Open question: consider defaulting SocketFlags to SocketFlags.None to reduce API space? Might challenge IntelliSense.publicpartialclassSocket:IDisposable{// existing: public int SendTo(byte[] buffer, EndPoint remoteEP);publicintSendTo(ReadOnlySpan<byte>buffer,EndPointremoteEP);// existing: public int SendTo(byte[] buffer, SocketFlags socketFlags, EndPoint remoteEP);publicintSendTo(ReadOnlySpan<byte>buffer,SocketFlagssocketFlags,EndPointremoteEP);// existing: public int ReceiveFrom(byte[] buffer, ref EndPoint remoteEP);publicintReceiveFrom(Span<byte>buffer,refEndPointremoteEP);// existing: public int ReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP);publicintReceiveFrom(Span<byte>buffer,SocketFlagssocketFlags,refEndPointremoteEP);}publicstaticclassSocketTaskExtensions{// existing: public static Task<int> SendToAsync(this Socket socket, ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEP);publicstaticValueTask<int>SendToAsync(thisSocketsocket,ReadOnlyMemory<byte>buffer,EndPointremoteEP,CancellationTokencancellationToken=null);publicstaticValueTask<int>SendToAsync(thisSocketsocket,ReadOnlyMemory<byte>buffer,SocketFlagssocketFlags,EndPointremoteEP,CancellationTokencancellationToken=null);// existing: public static Task<SocketReceiveFromResult> ReceiveFromAsync(this Socket socket, ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEndPoint);publicstaticValueTask<SocketReceiveFromResult>ReceiveFromAsync(thisSocketsocket,Memory<byte>buffer,EndPointremoteEP,CancellationTokencancellationToken=null);publicstaticValueTask<SocketReceiveFromResult>ReceiveFromAsync(thisSocketsocket,Memory<byte>buffer,SocketFlagssocketFlags,EndPointremoteEP,CancellationTokencancellationToken=null);// existing: public static Task<SocketReceiveMessageFromResult> ReceiveMessageFromAsync(this Socket socket, ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEndPoint);publicstaticValueTask<SocketReceiveMessageFromResult>ReceiveMessageFromAsync(thisSocketsocket,Memory<byte>buffer,EndPointremoteEP,CancellationTokencancellationToken=null);publicstaticValueTask<SocketReceiveMessageFromResult>ReceiveMessageFromAsync(thisSocketsocket,Memory<byte>buffer,SocketFlagssocketFlags,EndPointremoteEP,CancellationTokencancellationToken=null);}}Most of the underlying code is already ready, this is about exposing it as public API.
cc: @stephentoub
Spanification, ValueTask, and cancellation
This proposal spanifies connectionless methods on
Socket, uses aValueTaskreturn, and adds cancellation support. There are already such overloads in Send() and Receive() functions for connection oriented sockets (TCP) and this is complement for UDP.Proposed APIs
Most of the underlying code is already ready, this is about exposing it as public API.
cc: @stephentoub