MsQuicConnection Class

A QUIC connection: its streams and its datagrams (RFC 9221).

public sealed class MsQuicConnection : IAsyncDisposable

Remarks

Create a client connection with MsQuicConnection.ConnectAsync, or accept one with MsQuicListener.AcceptConnectionAsync. Dispose the connection to close it: disposing closes it with MsQuicConnectionOptions.DefaultCloseErrorCode unless MsQuicConnection.CloseAsync closed it.

Implements

Properties

NameDescription
ClosedGets a task that completes when the connection is closed, with the exception that describes why: an exception whose MsQuicException.Error is MsQuicError.ConnectionAborted when the peer closed it, MsQuicError.OperationAborted when it was closed locally...
IsDatagramSendEnabledGets a value indicating whether datagrams can be sent: both peers enabled them and the connection is established.
IsSupportedGets a value indicating whether MsQuic is available, see MsQuicLibrary.
LocalEndPointGets the local address of the connection.
MaxDatagramSendSizeGets the maximum size of a datagram payload. It can grow during the connection, as path MTU discovery finds a larger MTU.
NegotiatedApplicationProtocolGets the application protocol (ALPN) negotiated during the handshake.
RemoteCertificateGets the certificate of the server, for a client connection.
RemoteEndPointGets the address of the peer.
TargetHostNameGets the server name: the target host of a client connection, the server name indication (SNI) the client sent for a server connection.

Gets a task that completes when the connection is closed, with the exception that describes why: an exception whose MsQuicException.Error is MsQuicError.ConnectionAborted when the peer closed it, MsQuicError.OperationAborted when it was closed locally...

public Task<MsQuicException> Closed { get; }

Property value

Task<MsQuicException>

IsDatagramSendEnabled

MsQuicConnection.cs

Gets a value indicating whether datagrams can be sent: both peers enabled them and the connection is established.

public bool IsDatagramSendEnabled { get; }

Property value

bool

Gets a value indicating whether MsQuic is available, see MsQuicLibrary.

public static bool IsSupported { get; }

Property value

bool

LocalEndPoint

MsQuicConnection.cs

Gets the local address of the connection.

public IPEndPoint? LocalEndPoint { get; }

Property value

IPEndPoint?

MaxDatagramSendSize

MsQuicConnection.cs

Gets the maximum size of a datagram payload. It can grow during the connection, as path MTU discovery finds a larger MTU.

public int MaxDatagramSendSize { get; }

Property value

int

NegotiatedApplicationProtocol

MsQuicConnection.cs

Gets the application protocol (ALPN) negotiated during the handshake.

public SslApplicationProtocol NegotiatedApplicationProtocol { get; }

RemoteCertificate

MsQuicConnection.cs

Gets the certificate of the server, for a client connection.

public X509Certificate2? RemoteCertificate { get; }

Property value

X509Certificate2?

RemoteEndPoint

MsQuicConnection.cs

Gets the address of the peer.

public IPEndPoint? RemoteEndPoint { get; }

Property value

IPEndPoint?

TargetHostName

MsQuicConnection.cs

Gets the server name: the target host of a client connection, the server name indication (SNI) the client sent for a server connection.

public string TargetHostName { get; }

Property value

string

Methods

NameDescription
AcceptInboundStreamAsync(CancellationToken)Waits for the next stream the peer opens.
CloseAsync(long, CancellationToken)Closes the connection with an application error code, and waits until the peer is notified.
ConnectAsync(MsQuicClientConnectionOptions, CancellationToken)Connects to a QUIC server.
DisposeAsync()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
GetStatistics()Gets the statistics of the connection: round-trip time, bytes and packets sent and received, losses...
OpenOutboundStreamAsync(MsQuicStreamType, bool, CancellationToken)Opens a stream.
OpenOutboundStreamAsync(MsQuicStreamType, CancellationToken)Opens a stream.
ReceiveDatagramAsync(CancellationToken)Waits for the next datagram the peer sends.
SendDatagramAsync(ReadOnlyMemory<byte>, CancellationToken)Sends a QUIC datagram (RFC 9221). Datagrams are not retransmitted: the peer may not receive it.

AcceptInboundStreamAsync(CancellationToken)

MsQuicConnection.cs

Waits for the next stream the peer opens.

net10.0

public ValueTask<MsQuicStream> AcceptInboundStreamAsync(CancellationToken cancellationToken = default)

net11.0

public ValueTask<MsQuicStream> AcceptInboundStreamAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask<MsQuicStream>

Exceptions

MsQuicException
The connection is closed.

CloseAsync(long, CancellationToken)

MsQuicConnection.cs

Closes the connection with an application error code, and waits until the peer is notified.

net10.0

public ValueTask CloseAsync(long errorCode, CancellationToken cancellationToken = default)

net11.0

public ValueTask CloseAsync(long errorCode, CancellationToken cancellationToken = default)

Parameters

errorCode long
The application error code, from 0 to 2^62-1.
cancellationToken CancellationToken
The token to stop waiting.

Returns

ValueTask

ConnectAsync(MsQuicClientConnectionOptions, CancellationToken)

MsQuicConnection.cs

Connects to a QUIC server.

net10.0

public static ValueTask<MsQuicConnection> ConnectAsync(MsQuicClientConnectionOptions options, CancellationToken cancellationToken = default)

net11.0

public static ValueTask<MsQuicConnection> ConnectAsync(MsQuicClientConnectionOptions options, CancellationToken cancellationToken = default)

Parameters

options MsQuicClientConnectionOptions
cancellationToken CancellationToken

Returns

ValueTask<MsQuicConnection>

Exceptions

PlatformNotSupportedException
MsQuic is not available.
MsQuicException
The connection failed.

DisposeAsync()

MsQuicConnection.cs

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous dispose operation.

GetStatistics()

MsQuicConnection.cs

Gets the statistics of the connection: round-trip time, bytes and packets sent and received, losses...

public MsQuicConnectionStatistics GetStatistics()

OpenOutboundStreamAsync overloads

OpenOutboundStreamAsync(MsQuicStreamType, bool, CancellationToken)

MsQuicConnection.cs

Opens a stream.

net10.0

public ValueTask<MsQuicStream> OpenOutboundStreamAsync(MsQuicStreamType type, bool waitUntilAvailable, CancellationToken cancellationToken = default)

net11.0

public ValueTask<MsQuicStream> OpenOutboundStreamAsync(MsQuicStreamType type, bool waitUntilAvailable, CancellationToken cancellationToken = default)
Parameters
type MsQuicStreamType
The type of the stream.
waitUntilAvailable bool
true to wait until the peer allows one more stream; false to fail with MsQuicError.StreamLimitReached when it does not allow one now.
cancellationToken CancellationToken
The token to cancel the operation.
Returns

ValueTask<MsQuicStream>

OpenOutboundStreamAsync(MsQuicStreamType, CancellationToken)

MsQuicConnection.cs

Opens a stream.

public ValueTask<MsQuicStream> OpenOutboundStreamAsync(MsQuicStreamType type, CancellationToken cancellationToken = default)
Parameters
type MsQuicStreamType
The type of the stream.
cancellationToken CancellationToken
The token to cancel the operation.
Returns

ValueTask<MsQuicStream> The stream, once the peer allows it (its stream limit).

ReceiveDatagramAsync(CancellationToken)

MsQuicConnection.cs

Waits for the next datagram the peer sends.

net10.0

public ValueTask<byte[]> ReceiveDatagramAsync(CancellationToken cancellationToken = default)

net11.0

public ValueTask<byte[]> ReceiveDatagramAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask<byte[]>

Exceptions

MsQuicException
The connection is closed.

SendDatagramAsync(ReadOnlyMemory<byte>, CancellationToken)

MsQuicConnection.cs

Sends a QUIC datagram (RFC 9221). Datagrams are not retransmitted: the peer may not receive it.

public Task SendDatagramAsync(ReadOnlyMemory<byte> datagram, CancellationToken cancellationToken = default)

Parameters

datagram ReadOnlyMemory<byte>
The payload, at most MsQuicConnection.MaxDatagramSendSize bytes.
cancellationToken CancellationToken
The token to stop waiting. The datagram is still sent.

Returns

Task A task that completes when the datagram is sent on the network.

Exceptions

MsQuicException
Datagrams are not enabled, the datagram is too large, or the connection closed before it was sent.