Skip to content

Move SocketTaskExtensions methods to Socket class itself #43901

@geoffkizer

Description

@geoffkizer

Background and Motivation

For historical reasons, all of the Task-based APIs on Socket are defined as extension methods in SocketTaskExtensions.

We should move these methods to Socket itself, and mark the existing extension methods in SocketTaskExtensions as [EditorBrowsable(EditorBrowsableState.Never)].

Proposed API

namespace System.Net.Sockets
{
    public partial class Socket
    {
        // Existing SocketTaskExtensions APIs, converted to instance methods

        public Task<Socket> AcceptAsync();
        public Task<Socket> AcceptAsync(Socket? acceptSocket);

        public Task ConnectAsync(EndPoint remoteEP);
        public ValueTask ConnectAsync(EndPoint remoteEP, CancellationToken cancellationToken);
        public Task ConnectAsync(IPAddress address, int port);
        public ValueTask ConnectAsync(IPAddress address, int port, CancellationToken cancellationToken);
        public Task ConnectAsync(IPAddress[] addresses, int port);
        public ValueTask ConnectAsync(IPAddress[] addresses, int port, CancellationToken cancellationToken);
        public Task ConnectAsync(string host, int port);
        public ValueTask ConnectAsync(string host, int port, CancellationToken cancellationToken);

        public Task<int> ReceiveAsync(ArraySegment<byte> buffer, SocketFlags socketFlags);
        public ValueTask<int> ReceiveAsync(Memory<byte> buffer, SocketFlags socketFlags, CancellationToken cancellationToken = default);
        public Task<int> ReceiveAsync(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags);
        public Task<SocketReceiveFromResult> ReceiveFromAsync(ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEndPoint);
        public Task<SocketReceiveMessageFromResult> ReceiveMessageFromAsync(ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEndPoint);

        public Task<int> SendAsync(ArraySegment<byte> buffer, SocketFlags socketFlags);
        public ValueTask<int> SendAsync(ReadOnlyMemory<byte> buffer, SocketFlags socketFlags, CancellationToken cancellationToken = default);
        public Task<int> SendAsync(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags);
        public Task<int> SendToAsync(ArraySegment<byte> buffer, SocketFlags socketFlags, EndPoint remoteEP);
    }
}

Note this change should apply to the following planned 6.0 APIs as well, which have already been approved as new extension methods on SocketTaskExtensions.

#1608
#42591
#33418

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Net.Socketshelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions