| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Buffers; |
| | 4 | | using System.Net.Quic; |
| | 5 | | using System.Net.Sockets; |
| | 6 | |
|
| | 7 | | namespace IceRpc.Transports.Quic.Internal; |
| | 8 | |
|
| | 9 | | /// <summary>The Quic multiplexed connection implements an <see cref="IMultiplexedConnection" />.</summary> |
| | 10 | | internal abstract class QuicMultiplexedConnection : IMultiplexedConnection |
| | 11 | | { |
| | 12 | | private protected QuicConnection? _connection; |
| | 13 | | private volatile bool _isClosed; |
| | 14 | | private volatile bool _isDisposed; |
| | 15 | | private readonly int _minSegmentSize; |
| | 16 | | private readonly MemoryPool<byte> _pool; |
| | 17 | |
|
| 456 | 18 | | private protected QuicMultiplexedConnection(MultiplexedConnectionOptions options) |
| 456 | 19 | | { |
| 456 | 20 | | _minSegmentSize = options.MinSegmentSize; |
| 456 | 21 | | _pool = options.Pool; |
| 456 | 22 | | } |
| | 23 | |
|
| | 24 | | public async ValueTask<IMultiplexedStream> AcceptStreamAsync(CancellationToken cancellationToken) |
| 596 | 25 | | { |
| 596 | 26 | | if (_connection is null) |
| 0 | 27 | | { |
| 0 | 28 | | throw new InvalidOperationException("The Quic connection is not connected."); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | try |
| 596 | 32 | | { |
| 596 | 33 | | QuicStream stream = await _connection.AcceptInboundStreamAsync(cancellationToken).ConfigureAwait(false); |
| 559 | 34 | | return new QuicMultiplexedStream(stream, isRemote: true, _pool, _minSegmentSize, ThrowIfClosedOrDisposed); |
| | 35 | | } |
| 32 | 36 | | catch (QuicException exception) |
| 32 | 37 | | { |
| 32 | 38 | | throw exception.ToIceRpcException(); |
| | 39 | | } |
| 0 | 40 | | catch (SocketException exception) |
| 0 | 41 | | { |
| 0 | 42 | | throw exception.ToIceRpcException(); |
| | 43 | | } |
| 559 | 44 | | } |
| | 45 | |
|
| | 46 | | public abstract Task<TransportConnectionInformation> ConnectAsync(CancellationToken cancellationToken); |
| | 47 | |
|
| | 48 | | public async Task CloseAsync(MultiplexedConnectionCloseError closeError, CancellationToken cancellationToken) |
| 60 | 49 | | { |
| | 50 | | try |
| 60 | 51 | | { |
| 60 | 52 | | _isClosed = true; |
| 60 | 53 | | if (_connection is not null) |
| 60 | 54 | | { |
| 60 | 55 | | await _connection.CloseAsync((long)closeError, cancellationToken).ConfigureAwait(false); |
| 60 | 56 | | } |
| 60 | 57 | | } |
| 0 | 58 | | catch (QuicException exception) |
| 0 | 59 | | { |
| 0 | 60 | | throw exception.ToIceRpcException(); |
| | 61 | | } |
| 0 | 62 | | catch (SocketException exception) |
| 0 | 63 | | { |
| 0 | 64 | | throw exception.ToIceRpcException(); |
| | 65 | | } |
| 60 | 66 | | } |
| | 67 | |
|
| | 68 | | public async ValueTask<IMultiplexedStream> CreateStreamAsync( |
| | 69 | | bool bidirectional, |
| | 70 | | CancellationToken cancellationToken) |
| 618 | 71 | | { |
| 618 | 72 | | if (_connection is null) |
| 2 | 73 | | { |
| 2 | 74 | | throw new InvalidOperationException("The Quic connection is not connected."); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | QuicStream stream; |
| | 78 | | try |
| 616 | 79 | | { |
| 616 | 80 | | stream = await _connection.OpenOutboundStreamAsync( |
| 616 | 81 | | bidirectional ? QuicStreamType.Bidirectional : QuicStreamType.Unidirectional, |
| 616 | 82 | | cancellationToken).ConfigureAwait(false); |
| 583 | 83 | | } |
| 22 | 84 | | catch (QuicException exception) |
| 22 | 85 | | { |
| 22 | 86 | | throw exception.ToIceRpcException(); |
| | 87 | | } |
| 0 | 88 | | catch (SocketException exception) |
| 0 | 89 | | { |
| 0 | 90 | | throw exception.ToIceRpcException(); |
| | 91 | | } |
| 7 | 92 | | catch (ObjectDisposedException) |
| 7 | 93 | | { |
| 7 | 94 | | cancellationToken.ThrowIfCancellationRequested(); |
| 7 | 95 | | throw; |
| | 96 | | } |
| | 97 | |
|
| 583 | 98 | | return new QuicMultiplexedStream( |
| 583 | 99 | | stream, |
| 583 | 100 | | isRemote: false, |
| 583 | 101 | | _pool, |
| 583 | 102 | | _minSegmentSize, |
| 583 | 103 | | ThrowIfClosedOrDisposed); |
| 583 | 104 | | } |
| | 105 | |
|
| | 106 | | public async ValueTask DisposeAsync() |
| 469 | 107 | | { |
| 469 | 108 | | _isDisposed = true; |
| | 109 | |
|
| 469 | 110 | | if (_connection is not null) |
| 463 | 111 | | { |
| | 112 | | try |
| 463 | 113 | | { |
| 463 | 114 | | await _connection.DisposeAsync().ConfigureAwait(false); |
| 463 | 115 | | } |
| 0 | 116 | | catch |
| 0 | 117 | | { |
| | 118 | | // Workaround for https://github.com/dotnet/runtime/issues/78641. |
| 0 | 119 | | } |
| 463 | 120 | | } |
| 469 | 121 | | } |
| | 122 | |
|
| | 123 | | private void ThrowIfClosedOrDisposed() |
| 38100 | 124 | | { |
| 38100 | 125 | | if (_isClosed) |
| 4 | 126 | | { |
| 4 | 127 | | throw new IceRpcException(IceRpcError.ConnectionAborted, "The connection was closed."); |
| | 128 | | } |
| 38096 | 129 | | else if (_isDisposed) |
| 4 | 130 | | { |
| 4 | 131 | | throw new IceRpcException(IceRpcError.ConnectionAborted, "The connection was disposed."); |
| | 132 | | } |
| 38092 | 133 | | } |
| | 134 | | } |
| | 135 | |
|
| | 136 | | internal class QuicMultiplexedClientConnection : QuicMultiplexedConnection |
| | 137 | | { |
| | 138 | | private readonly QuicClientConnectionOptions _quicClientConnectionOptions; |
| | 139 | |
|
| | 140 | | public override async Task<TransportConnectionInformation> ConnectAsync(CancellationToken cancellationToken) |
| | 141 | | { |
| | 142 | | // Establish the Quic connection. |
| | 143 | | try |
| | 144 | | { |
| | 145 | | _connection = await QuicConnection.ConnectAsync( |
| | 146 | | _quicClientConnectionOptions, |
| | 147 | | cancellationToken).ConfigureAwait(false); |
| | 148 | | } |
| | 149 | | catch (QuicException exception) |
| | 150 | | { |
| | 151 | | throw exception.ToIceRpcException(); |
| | 152 | | } |
| | 153 | | catch (SocketException exception) |
| | 154 | | { |
| | 155 | | throw exception.ToIceRpcException(); |
| | 156 | | } |
| | 157 | |
|
| | 158 | | return new TransportConnectionInformation( |
| | 159 | | localNetworkAddress: _connection.LocalEndPoint, |
| | 160 | | remoteNetworkAddress: _connection.RemoteEndPoint, |
| | 161 | | _connection.RemoteCertificate); |
| | 162 | | } |
| | 163 | |
|
| | 164 | | internal QuicMultiplexedClientConnection( |
| | 165 | | MultiplexedConnectionOptions options, |
| | 166 | | QuicClientConnectionOptions quicOptions) |
| | 167 | | : base(options) => _quicClientConnectionOptions = quicOptions; |
| | 168 | | } |
| | 169 | |
|
| | 170 | | internal class QuicMultiplexedServerConnection : QuicMultiplexedConnection |
| | 171 | | { |
| | 172 | | public override Task<TransportConnectionInformation> ConnectAsync(CancellationToken cancellationToken) => |
| | 173 | | Task.FromResult(new TransportConnectionInformation( |
| | 174 | | localNetworkAddress: _connection!.LocalEndPoint, |
| | 175 | | remoteNetworkAddress: _connection.RemoteEndPoint, |
| | 176 | | _connection.RemoteCertificate)); |
| | 177 | |
|
| | 178 | | internal QuicMultiplexedServerConnection( |
| | 179 | | QuicConnection connection, |
| | 180 | | MultiplexedConnectionOptions options) |
| | 181 | | : base(options) => _connection = connection; |
| | 182 | | } |