Open
Description
We still have a few gaps in our Socket APIs where we are missing support for Task, Span/Memory, or CancellationToken.
This is a mega-issue to track all remaining work here for 6.0.
Async Socket APIs:
- AcceptAsync needs CancellationToken support -- implemented in add AcceptAsync cancellation overloads #53340
public static ValueTask<Socket> AcceptAsync (this Socket socket, CancellationToken cancellationToken);
public static ValueTask<Socket> AcceptAsync (this Socket socket, Socket acceptSocket, CancellationToken cancellationToken);
- SendToAsync, ReceiveFromAsync, and ReceiveMessageFromAsync need Memory and CancellationToken support -- see Socket API changes mega-issue #33418. (Note, this should probably wait until Sockets: Reimplement remaining Task-based async methods using SocketAsyncEventArgs #41502 is completed.)
public static ValueTask<int> SendToAsync(this Socket socket, ReadOnlyMemory<byte> buffer, SocketFlags socketFlags, EndPoint remoteEP, CancellationToken cancellationToken = null);
public static ValueTask<SocketReceiveFromResult> ReceiveFromAsync(this Socket socket, Memory<byte> buffer, SocketFlags socketFlags, EndPoint remoteEP, CancellationToken cancellationToken = null);
public static ValueTask<SocketReceiveMessageFromResult> ReceiveMessageFromAsync(this Socket socket, Memory<byte> buffer, SocketFlags socketFlags, EndPoint remoteEP, CancellationToken cancellationToken = null);
- Add Task-based SendFileAsync with Memory and CancellationToken support -- implemented in Socket.SendFileAsync based on SendPacketsAsync #52208 and implement cancellation support for SendFileAsync and DisconnectAsync #53062
public ValueTask SendFileAsync(string? fileName, CancellationToken cancellationToken = default);
public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory<byte> preBuffer, ReadOnlyMemory<byte> postBuffer, TransmitFileOptions flags, CancellationToken cancellationToken = default);
- Add Task-based DisconnectAsync -- implemented in add Task-based DisconnectAsync and refactor APM methods on top of it #51213
public static ValueTask DisconnectAsync(this Socket socket, bool reuseSocket, CancellationToken cancellationToken=default);
Sync Socket APIs:
- SendTo, ReceiveFrom, ReceiveMessageFrom need Span support -- implemented in Adds synchronous span APIs for datagram sockets. #51956 and adding span version of ReceiveMessageFrom #46285
public int SendTo(ReadOnlySpan<byte> buffer, EndPoint remoteEP);
public int SendTo(ReadOnlySpan<byte> buffer, SocketFlags socketFlags, EndPoint remoteEP);
public int ReceiveFrom(Span<byte> buffer, ref EndPoint remoteEP);
public int ReceiveFrom(Span<byte> buffer, SocketFlags socketFlags, ref EndPoint remoteEP);
public int ReceiveMessageFrom(Span<byte> buffer, ref System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, out System.Net.Sockets.IPPacketInformation ipPacketInformation);
- SendFile needs Span support -- implemented in Added Span overloads for Socket.SendFile #47230
public void SendFile(string fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpan<byte> postBuffer, TransmitFileOptions flags);
Async TcpListener APIs:
- Accept async methods need CancellationToken support -- implemented in add AcceptAsync cancellation overloads #53340
public ValueTask<Socket> AcceptSocketAsync (CancellationToken cancellationToken);
public ValueTask<TcpClient> AcceptTcpClientAsync (CancellationToken cancellationToken);
Async TcpClient APIs:
- Add missing ConnectAsync overloads -- implemented in TcpClient.ConnectAsync(EndPoint) #44110
namespace System.Net.Sockets
{
public class TcpClient : IDisposable
{
public Task ConnectAsync(IPEndPoint remoteEP);
public ValueTask ConnectAsync(IPEndPoint remoteEP, CancellationToken cancellationToken);
}
}
UdpClient APIs:
- UdpClient Span, Memory, and CancellationToken support -- see UdpClient - add Span support #864
public class UdpClient
{
public int Send(ReadOnlySpan<byte> datagram);
public int Send(ReadOnlySpan<byte> datagram, IPEndPoint endPoint);
public int Send(ReadOnlySpan<byte> datagram, string hostname, int port);
public ValueTask<int> SendAsync(ReadOnlyMemory<byte> datagram, CancellationToken cancellationToken);
public ValueTask<int> SendAsync(ReadOnlyMemory<byte> datagram, IPEndPoint endPoint, CancellationToken cancellationToken);
public ValueTask<UdpReceiveResult> ReceiveAsync(CancellationToken cancellationToken);
}
Related issues
- Move Task-based methods from SocketTaskExtensions to Socket -- see Move SocketTaskExtensions methods to Socket class itself #43901
- Memory support for SendPacketElements -- see Add Memory support to SendPacketElements #45267
- Consider overloads for Task-based methods that remove #SocketFlags argument -- see Consider adding Socket Send/ReceiveAsync overloads that elide SocketFlags argument #43934
- Remove Socket finalizer and ensure SafeSocketHandle finalization works correctly -- see Remove Socket finalizer and ensure SafeSocketHandle finalization works correctly #43383
- New APIs for zero-allocation connectionless sockets -- see Proposal: Zero allocation connectionless sockets #30797