dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

Missing Socket APIs mega-issue #43935

Open geoffkizer opened 4 years ago

geoffkizer commented 4 years ago

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:

    public static ValueTask<Socket> AcceptAsync (this Socket socket, CancellationToken cancellationToken);
    public static ValueTask<Socket> AcceptAsync (this Socket socket, Socket acceptSocket, CancellationToken cancellationToken);
    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);
        public ValueTask SendFileAsync(string? fileName, CancellationToken cancellationToken = default);
        public ValueTask SendFileAsync(string? fileName, ReadOnlyMemory<byte> preBuffer, ReadOnlyMemory<byte> postBuffer, TransmitFileOptions flags, CancellationToken cancellationToken = default);
        public static ValueTask DisconnectAsync(this Socket socket, bool reuseSocket, CancellationToken cancellationToken=default);

Sync Socket APIs:

    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);
        public void SendFile(string fileName, ReadOnlySpan<byte> preBuffer, ReadOnlySpan<byte> postBuffer, TransmitFileOptions flags);

Async TcpListener APIs:

    public ValueTask<Socket> AcceptSocketAsync (CancellationToken cancellationToken);
    public ValueTask<TcpClient> AcceptTcpClientAsync (CancellationToken cancellationToken);

Async TcpClient APIs:

namespace System.Net.Sockets
{
    public class TcpClient : IDisposable
    {
        public Task ConnectAsync(IPEndPoint remoteEP);
        public ValueTask ConnectAsync(IPEndPoint remoteEP, CancellationToken cancellationToken);
    }
}

UdpClient APIs:

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

ghost commented 4 years ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

GSPP commented 4 years ago

Is it common policy to not provide CancellationToken support for synchronous APIs? It seems just as useful to have it for sync as it is for async.

antonfirsov commented 4 years ago

@GSPP for sockets, it's not possible to implement reliable cancellation of synchronous calls in a cross-platform way. From https://github.com/dotnet/runtime/issues/42686#issuecomment-708937234:

Note that POSIX does not specify a way to abort on-going synchronous calls. The implementation is relying on OS specifics which work well on Linux, and less well on some other OSes. For best cross-platform behaviour applications should avoid using Dispose to abort on-going operations.

(#42686 is a typical issue around this problem)

geoffkizer commented 4 years ago

In general we don't provide CancellationToken support for sync APIs.

karelz commented 4 years ago

Triage: The APIs should be approved in the spin offs, not in this issue - this one is just tracking all the work.

Note: Some of the sub-issues are simple, so good for up-for-grabs.

antonfirsov commented 3 years ago

SendToAsync, ReceiveFromAsync, and ReceiveMessageFromAsync need Memory and CancellationToken support -- see #33148.

@geoffkizer you meant #938 I guess.

geoffkizer commented 3 years ago

@geoffkizer you meant #938 I guess.

Actually, I meant #33418, but that links to #938 too. Thanks for catching, fixed now.

wfurt commented 1 year ago

triage: we should revisit what is missing and close the mega issue