Skip to content

UdpClient - add Span support #864

Closed
@billknye

Description

@billknye

[EDIT] Note: Scoped to UdpClient only - see https://github.com/dotnet/corefx/issues/28131#issuecomment-529619141

Checking out the new Span based socket apis in 2.1 preview 1, I noticed the api of UdpClient doesn't have span based overloads.

Falling back to Socket, it appears there too the SendTo api is missing any overloads for Span and friends.

Digging deeper into SocketPal.Windows, there's no SendTo supporting span there but SocketPal.Unix does seem to have some Span support.

The corresponding receive methods are also lacking Span support.

To add actual non-allocating support, it would also appear that endpoint serialization in these methods would need to be tweaked but that seems to be somewhat the point of SocketAddress so maybe calling code would need to pass the SocketAddress instead of the IPEndPoint/EndPoint?

Was this an intentional gap in the api or just something that wasn't priority / low use case?

Edited by @geoffkizer: Current API proposal:

class UdpClient
{
	// existing: public int Send(byte[] dgram, int bytes);
	public int Send(ReadOnlySpan<byte> datagram);
	
	// existing: public int Send(byte[] dgram, int bytes, IPEndPoint endPoint);
	public int Send(ReadOnlySpan<byte> datagram, IPEndPoint endPoint);
	
	// existing: public int Send(byte[] dgram, int bytes, string hostname, int port);
	public int Send(ReadOnlySpan<byte> datagram, string hostname, int port);
	
	// existing: public Task<int> SendAsync(byte[] datagram, int bytes);
	public ValueTask<int> SendAsync(ReadOnlyMemory<byte> datagram, CancellationToken cancellationToken);
	
	// existing: public Task<int> SendAsync(byte[] datagram, int bytes, IPEndPoint endPoint);
	public ValueTask<int> SendAsync(ReadOnlyMemory<byte> datagram, IPEndPoint endPoint, CancellationToken cancellationToken);

	// existing: public Task<UdpReceiveResult> ReceiveAsync();
	public ValueTask<UdpReceiveResult> ReceiveAsync(CancellationToken cancellationToken);
}

Metadata

Metadata

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