| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Internal; |
| | 4 | | using IceRpc.Transports.Internal; |
| | 5 | | using System.Buffers; |
| | 6 | | using System.Collections.Concurrent; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.IO.Pipelines; |
| | 9 | | using System.Security.Authentication; |
| | 10 | | using System.Threading.Channels; |
| | 11 | | using ZeroC.Slice; |
| | 12 | |
|
| | 13 | | namespace IceRpc.Transports.Slic.Internal; |
| | 14 | |
|
| | 15 | | /// <summary>The Slic connection implements an <see cref="IMultiplexedConnection" /> on top of a <see |
| | 16 | | /// cref="IDuplexConnection" />.</summary> |
| | 17 | | internal class SlicConnection : IMultiplexedConnection |
| | 18 | | { |
| | 19 | | /// <summary>Gets a value indicating whether or not this is the server-side of the connection.</summary> |
| 33440 | 20 | | internal bool IsServer { get; } |
| | 21 | |
|
| | 22 | | /// <summary>Gets the minimum size of the segment requested from <see cref="Pool" />.</summary> |
| 10814 | 23 | | internal int MinSegmentSize { get; } |
| | 24 | |
|
| | 25 | | /// <summary>Gets the peer's initial stream window size. This property is set to the <see |
| | 26 | | /// cref="ParameterKey.InitialStreamWindowSize"/> value carried by the <see cref="FrameType.Initialize" /> |
| | 27 | | /// frame.</summary> |
| 6545 | 28 | | internal int PeerInitialStreamWindowSize { get; private set; } |
| | 29 | |
|
| | 30 | | /// <summary>Gets the maximum size of stream frames accepted by the peer. This property is set to the <see |
| | 31 | | /// cref="ParameterKey.MaxStreamFrameSize"/> value carried by the <see cref="FrameType.Initialize" /> |
| | 32 | | /// frame.</summary> |
| 19448 | 33 | | internal int PeerMaxStreamFrameSize { get; private set; } |
| | 34 | |
|
| | 35 | | /// <summary>Gets the <see cref="MemoryPool{T}" /> used for obtaining memory buffers.</summary> |
| 10814 | 36 | | internal MemoryPool<byte> Pool { get; } |
| | 37 | |
|
| | 38 | | /// <summary>Gets the initial stream window size.</summary> |
| 18615 | 39 | | internal int InitialStreamWindowSize { get; } |
| | 40 | |
|
| | 41 | | /// <summary>Gets the window update threshold. When the window size is increased and this threshold reached, a <see |
| | 42 | | /// cref="FrameType.StreamWindowUpdate" /> frame is sent.</summary> |
| 12043 | 43 | | internal int StreamWindowUpdateThreshold => InitialStreamWindowSize / StreamWindowUpdateRatio; |
| | 44 | |
|
| | 45 | | // The ratio used to compute the StreamWindowUpdateThreshold. For now, the stream window update is sent when the |
| | 46 | | // window size grows over InitialStreamWindowSize / StreamWindowUpdateRatio. |
| | 47 | | private const int StreamWindowUpdateRatio = 2; |
| | 48 | |
|
| | 49 | | private readonly Channel<IMultiplexedStream> _acceptStreamChannel; |
| | 50 | | private int _bidirectionalStreamCount; |
| | 51 | | private SemaphoreSlim? _bidirectionalStreamSemaphore; |
| | 52 | | private readonly CancellationToken _closedCancellationToken; |
| 1373 | 53 | | private readonly CancellationTokenSource _closedCts = new(); |
| | 54 | | private string? _closedMessage; |
| | 55 | | private Task<TransportConnectionInformation>? _connectTask; |
| 1373 | 56 | | private readonly CancellationTokenSource _disposedCts = new(); |
| | 57 | | private Task? _disposeTask; |
| | 58 | | private readonly SlicDuplexConnectionDecorator _duplexConnection; |
| | 59 | | private readonly DuplexConnectionReader _duplexConnectionReader; |
| | 60 | | private readonly SlicDuplexConnectionWriter _duplexConnectionWriter; |
| | 61 | | private bool _isClosed; |
| | 62 | | private ulong? _lastRemoteBidirectionalStreamId; |
| | 63 | | private ulong? _lastRemoteUnidirectionalStreamId; |
| | 64 | | private readonly TimeSpan _localIdleTimeout; |
| | 65 | | private readonly int _maxBidirectionalStreams; |
| | 66 | | private readonly int _maxStreamFrameSize; |
| | 67 | | private readonly int _maxUnidirectionalStreams; |
| | 68 | | // _mutex ensure the assignment of _lastRemoteXxx members and the addition of the stream to _streams is |
| | 69 | | // an atomic operation. |
| 1373 | 70 | | private readonly object _mutex = new(); |
| | 71 | | private ulong _nextBidirectionalId; |
| | 72 | | private ulong _nextUnidirectionalId; |
| | 73 | | private IceRpcError? _peerCloseError; |
| 1373 | 74 | | private TimeSpan _peerIdleTimeout = Timeout.InfiniteTimeSpan; |
| | 75 | | private int _pendingPongCount; |
| | 76 | | private Task? _readFramesTask; |
| | 77 | |
|
| 1373 | 78 | | private readonly ConcurrentDictionary<ulong, SlicStream> _streams = new(); |
| | 79 | | private int _streamSemaphoreWaitCount; |
| 1373 | 80 | | private readonly TaskCompletionSource _streamSemaphoreWaitClosed = |
| 1373 | 81 | | new(TaskCreationOptions.RunContinuationsAsynchronously); |
| | 82 | |
|
| | 83 | | private int _unidirectionalStreamCount; |
| | 84 | | private SemaphoreSlim? _unidirectionalStreamSemaphore; |
| | 85 | |
|
| | 86 | | // This is only set for server connections to ensure that _duplexConnectionWriter.Write is not called after |
| | 87 | | // _duplexConnectionWriter.Shutdown. This can occur if the client-side of the connection sends the close frame |
| | 88 | | // followed by the shutdown of the duplex connection and if CloseAsync is called at the same time on the server |
| | 89 | | // connection. |
| | 90 | | private bool _writerIsShutdown; |
| | 91 | |
|
| | 92 | | public async ValueTask<IMultiplexedStream> AcceptStreamAsync(CancellationToken cancellationToken) |
| 4645 | 93 | | { |
| 4645 | 94 | | lock (_mutex) |
| 4645 | 95 | | { |
| 4645 | 96 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | 97 | |
|
| 4643 | 98 | | if (_connectTask is null || !_connectTask.IsCompletedSuccessfully) |
| 2 | 99 | | { |
| 2 | 100 | | throw new InvalidOperationException("Cannot accept stream before connecting the Slic connection."); |
| | 101 | | } |
| 4641 | 102 | | if (_isClosed) |
| 15 | 103 | | { |
| 15 | 104 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | 105 | | } |
| 4626 | 106 | | } |
| | 107 | |
|
| | 108 | | try |
| 4626 | 109 | | { |
| 4626 | 110 | | return await _acceptStreamChannel.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); |
| | 111 | | } |
| 232 | 112 | | catch (ChannelClosedException exception) |
| 232 | 113 | | { |
| 232 | 114 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| 231 | 115 | | Debug.Assert(exception.InnerException is not null); |
| | 116 | | // The exception given to ChannelWriter.Complete(Exception? exception) is the InnerException. |
| 231 | 117 | | throw ExceptionUtil.Throw(exception.InnerException); |
| | 118 | | } |
| 3968 | 119 | | } |
| | 120 | |
|
| | 121 | | public Task<TransportConnectionInformation> ConnectAsync(CancellationToken cancellationToken) |
| 1335 | 122 | | { |
| 1335 | 123 | | lock (_mutex) |
| 1335 | 124 | | { |
| 1335 | 125 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | 126 | |
|
| 1335 | 127 | | if (_connectTask is not null) |
| 2 | 128 | | { |
| 2 | 129 | | throw new InvalidOperationException("Cannot connect twice a Slic connection."); |
| | 130 | | } |
| 1333 | 131 | | if (_isClosed) |
| 0 | 132 | | { |
| 0 | 133 | | throw new InvalidOperationException("Cannot connect a closed Slic connection."); |
| | 134 | | } |
| 1333 | 135 | | _connectTask = PerformConnectAsync(); |
| 1333 | 136 | | } |
| 1333 | 137 | | return _connectTask; |
| | 138 | |
|
| | 139 | | async Task<TransportConnectionInformation> PerformConnectAsync() |
| 1333 | 140 | | { |
| 1333 | 141 | | await Task.Yield(); // Exit mutex lock |
| | 142 | |
|
| | 143 | | // Connect the duplex connection. |
| | 144 | | TransportConnectionInformation transportConnectionInformation; |
| 1333 | 145 | | TimeSpan peerIdleTimeout = TimeSpan.MaxValue; |
| | 146 | |
|
| | 147 | | try |
| 1333 | 148 | | { |
| 1333 | 149 | | transportConnectionInformation = await _duplexConnection.ConnectAsync(cancellationToken) |
| 1333 | 150 | | .ConfigureAwait(false); |
| | 151 | |
|
| | 152 | | // Initialize the Slic connection. |
| 1289 | 153 | | if (IsServer) |
| 648 | 154 | | { |
| | 155 | | // Read the Initialize frame. |
| 648 | 156 | | (ulong version, InitializeBody? initializeBody) = await ReadFrameAsync( |
| 648 | 157 | | DecodeInitialize, |
| 648 | 158 | | cancellationToken).ConfigureAwait(false); |
| | 159 | |
|
| 636 | 160 | | if (initializeBody is null) |
| 4 | 161 | | { |
| | 162 | | // Unsupported version, try to negotiate another version by sending a Version frame with the |
| | 163 | | // Slic versions supported by this server. |
| 4 | 164 | | ulong[] supportedVersions = new ulong[] { SlicDefinitions.V1 }; |
| | 165 | |
|
| 4 | 166 | | WriteConnectionFrame(FrameType.Version, new VersionBody(supportedVersions).Encode); |
| | 167 | |
|
| 4 | 168 | | (version, initializeBody) = await ReadFrameAsync( |
| 4 | 169 | | (frameType, buffer) => |
| 4 | 170 | | { |
| 4 | 171 | | if (frameType is null) |
| 2 | 172 | | { |
| 4 | 173 | | // The client shut down the connection because it doesn't support any of the |
| 4 | 174 | | // server's supported Slic versions. |
| 2 | 175 | | throw new IceRpcException( |
| 2 | 176 | | IceRpcError.ConnectionRefused, |
| 2 | 177 | | $"The connection was refused because the client Slic version {version} is not su |
| 4 | 178 | | } |
| 4 | 179 | | else |
| 2 | 180 | | { |
| 2 | 181 | | return DecodeInitialize(frameType, buffer); |
| 4 | 182 | | } |
| 2 | 183 | | }, |
| 4 | 184 | | cancellationToken).ConfigureAwait(false); |
| 2 | 185 | | } |
| | 186 | |
|
| 634 | 187 | | Debug.Assert(initializeBody is not null); |
| | 188 | |
|
| 634 | 189 | | DecodeParameters(initializeBody.Value.Parameters); |
| | 190 | |
|
| | 191 | | // Write back an InitializeAck frame. |
| 634 | 192 | | WriteConnectionFrame(FrameType.InitializeAck, new InitializeAckBody(EncodeParameters()).Encode); |
| 634 | 193 | | } |
| | 194 | | else |
| 641 | 195 | | { |
| | 196 | | // Write the Initialize frame. |
| 641 | 197 | | WriteConnectionFrame( |
| 641 | 198 | | FrameType.Initialize, |
| 641 | 199 | | (ref SliceEncoder encoder) => |
| 641 | 200 | | { |
| 641 | 201 | | encoder.EncodeVarUInt62(SlicDefinitions.V1); |
| 641 | 202 | | new InitializeBody(EncodeParameters()).Encode(ref encoder); |
| 1282 | 203 | | }); |
| | 204 | |
|
| | 205 | | // Read and decode the InitializeAck or Version frame. |
| 641 | 206 | | (InitializeAckBody? initializeAckBody, VersionBody? versionBody) = await ReadFrameAsync( |
| 641 | 207 | | DecodeInitializeAckOrVersion, |
| 641 | 208 | | cancellationToken).ConfigureAwait(false); |
| | 209 | |
|
| 594 | 210 | | Debug.Assert(initializeAckBody is not null || versionBody is not null); |
| | 211 | |
|
| 594 | 212 | | if (initializeAckBody is not null) |
| 590 | 213 | | { |
| 590 | 214 | | DecodeParameters(initializeAckBody.Value.Parameters); |
| 590 | 215 | | } |
| | 216 | |
|
| 594 | 217 | | if (versionBody is not null) |
| 4 | 218 | | { |
| 4 | 219 | | if (versionBody.Value.Versions.Contains(SlicDefinitions.V1)) |
| 2 | 220 | | { |
| 2 | 221 | | throw new InvalidDataException( |
| 2 | 222 | | "The server supported versions include the version initially requested."); |
| | 223 | | } |
| | 224 | | else |
| 2 | 225 | | { |
| | 226 | | // We only support V1 and the peer rejected V1. |
| 2 | 227 | | throw new IceRpcException( |
| 2 | 228 | | IceRpcError.ConnectionRefused, |
| 2 | 229 | | $"The connection was refused because the server only supports Slic version(s) {string.Jo |
| | 230 | | } |
| | 231 | | } |
| 590 | 232 | | } |
| 1224 | 233 | | } |
| 12 | 234 | | catch (InvalidDataException exception) |
| 12 | 235 | | { |
| 12 | 236 | | throw new IceRpcException( |
| 12 | 237 | | IceRpcError.IceRpcError, |
| 12 | 238 | | "The connection was aborted by a Slic protocol error.", |
| 12 | 239 | | exception); |
| | 240 | | } |
| 49 | 241 | | catch (OperationCanceledException) |
| 49 | 242 | | { |
| 49 | 243 | | throw; |
| | 244 | | } |
| 8 | 245 | | catch (AuthenticationException) |
| 8 | 246 | | { |
| 8 | 247 | | throw; |
| | 248 | | } |
| 40 | 249 | | catch (IceRpcException) |
| 40 | 250 | | { |
| 40 | 251 | | throw; |
| | 252 | | } |
| 0 | 253 | | catch (Exception exception) |
| 0 | 254 | | { |
| 0 | 255 | | Debug.Fail($"ConnectAsync failed with an unexpected exception: {exception}"); |
| 0 | 256 | | throw; |
| | 257 | | } |
| | 258 | |
|
| | 259 | | // Enable the idle timeout checks after the connection establishment. The Ping frames sent by the keep alive |
| | 260 | | // check are not expected until the Slic connection initialization completes. The idle timeout check uses |
| | 261 | | // the smallest idle timeout. |
| 1224 | 262 | | TimeSpan idleTimeout = _peerIdleTimeout == Timeout.InfiniteTimeSpan ? _localIdleTimeout : |
| 1224 | 263 | | (_peerIdleTimeout < _localIdleTimeout ? _peerIdleTimeout : _localIdleTimeout); |
| | 264 | |
|
| 1224 | 265 | | if (idleTimeout != Timeout.InfiniteTimeSpan) |
| 1220 | 266 | | { |
| 1220 | 267 | | _duplexConnection.Enable(idleTimeout); |
| 1220 | 268 | | } |
| | 269 | |
|
| 1224 | 270 | | _readFramesTask = ReadFramesAsync(_disposedCts.Token); |
| | 271 | |
|
| 1224 | 272 | | return transportConnectionInformation; |
| 1224 | 273 | | } |
| | 274 | |
|
| | 275 | | static (ulong, InitializeBody?) DecodeInitialize(FrameType? frameType, ReadOnlySequence<byte> buffer) |
| 640 | 276 | | { |
| 640 | 277 | | if (frameType != FrameType.Initialize) |
| 0 | 278 | | { |
| 0 | 279 | | throw new InvalidDataException($"Received unexpected {frameType} frame."); |
| | 280 | | } |
| | 281 | |
|
| 640 | 282 | | return SliceEncoding.Slice2.DecodeBuffer<(ulong, InitializeBody?)>( |
| 640 | 283 | | buffer, |
| 640 | 284 | | (ref SliceDecoder decoder) => |
| 640 | 285 | | { |
| 640 | 286 | | ulong version = decoder.DecodeVarUInt62(); |
| 638 | 287 | | if (version == SlicDefinitions.V1) |
| 634 | 288 | | { |
| 634 | 289 | | return (version, new InitializeBody(ref decoder)); |
| 640 | 290 | | } |
| 640 | 291 | | else |
| 4 | 292 | | { |
| 4 | 293 | | decoder.Skip((int)(buffer.Length - decoder.Consumed)); |
| 4 | 294 | | return (version, null); |
| 640 | 295 | | } |
| 1278 | 296 | | }); |
| 638 | 297 | | } |
| | 298 | |
|
| | 299 | | static (InitializeAckBody?, VersionBody?) DecodeInitializeAckOrVersion( |
| | 300 | | FrameType? frameType, |
| | 301 | | ReadOnlySequence<byte> buffer) => |
| 598 | 302 | | frameType switch |
| 598 | 303 | | { |
| 592 | 304 | | FrameType.InitializeAck => ( |
| 592 | 305 | | SliceEncoding.Slice2.DecodeBuffer( |
| 592 | 306 | | buffer, |
| 592 | 307 | | (ref SliceDecoder decoder) => new InitializeAckBody(ref decoder)), |
| 592 | 308 | | null), |
| 6 | 309 | | FrameType.Version => ( |
| 6 | 310 | | null, |
| 6 | 311 | | SliceEncoding.Slice2.DecodeBuffer( |
| 6 | 312 | | buffer, |
| 12 | 313 | | (ref SliceDecoder decoder) => new VersionBody(ref decoder))), |
| 0 | 314 | | _ => throw new InvalidDataException($"Received unexpected Slic frame: '{frameType}'."), |
| 598 | 315 | | }; |
| | 316 | |
|
| | 317 | | async ValueTask<T> ReadFrameAsync<T>( |
| | 318 | | Func<FrameType?, ReadOnlySequence<byte>, T> decodeFunc, |
| | 319 | | CancellationToken cancellationToken) |
| 1293 | 320 | | { |
| 1293 | 321 | | (FrameType FrameType, int FrameSize, ulong?)? header = |
| 1293 | 322 | | await ReadFrameHeaderAsync(cancellationToken).ConfigureAwait(false); |
| | 323 | |
|
| | 324 | | ReadOnlySequence<byte> buffer; |
| 1240 | 325 | | if (header is null || header.Value.FrameSize == 0) |
| 8 | 326 | | { |
| 8 | 327 | | buffer = ReadOnlySequence<byte>.Empty; |
| 8 | 328 | | } |
| | 329 | | else |
| 1232 | 330 | | { |
| 1232 | 331 | | buffer = await _duplexConnectionReader.ReadAtLeastAsync( |
| 1232 | 332 | | header.Value.FrameSize, |
| 1232 | 333 | | cancellationToken).ConfigureAwait(false); |
| 1232 | 334 | | if (buffer.Length > header.Value.FrameSize) |
| 13 | 335 | | { |
| 13 | 336 | | buffer = buffer.Slice(0, header.Value.FrameSize); |
| 13 | 337 | | } |
| 1232 | 338 | | } |
| | 339 | |
|
| 1240 | 340 | | T decodedFrame = decodeFunc(header?.FrameType, buffer); |
| 1232 | 341 | | _duplexConnectionReader.AdvanceTo(buffer.End); |
| 1232 | 342 | | return decodedFrame; |
| 1232 | 343 | | } |
| 1333 | 344 | | } |
| | 345 | |
|
| | 346 | | public async Task CloseAsync(MultiplexedConnectionCloseError closeError, CancellationToken cancellationToken) |
| 199 | 347 | | { |
| 199 | 348 | | lock (_mutex) |
| 199 | 349 | | { |
| 199 | 350 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | 351 | |
|
| 199 | 352 | | if (_connectTask is null || !_connectTask.IsCompletedSuccessfully) |
| 2 | 353 | | { |
| 2 | 354 | | throw new InvalidOperationException("Cannot close a Slic connection before connecting it."); |
| | 355 | | } |
| 197 | 356 | | } |
| | 357 | |
|
| 197 | 358 | | bool waitForWriterShutdown = false; |
| 197 | 359 | | if (TryClose(new IceRpcException(IceRpcError.OperationAborted), "The connection was closed.")) |
| 152 | 360 | | { |
| 152 | 361 | | lock (_mutex) |
| 152 | 362 | | { |
| | 363 | | // The duplex connection writer of a server connection might already be shutdown |
| | 364 | | // (_writerIsShutdown=true) if the client-side sent the Close frame and shut down the duplex connection. |
| | 365 | | // This doesn't apply to the client-side since the server-side doesn't shutdown the duplex connection |
| | 366 | | // writer after sending the Close frame. |
| 152 | 367 | | if (!IsServer || !_writerIsShutdown) |
| 150 | 368 | | { |
| 150 | 369 | | WriteFrame(FrameType.Close, streamId: null, new CloseBody((ulong)closeError).Encode); |
| 150 | 370 | | if (IsServer) |
| 77 | 371 | | { |
| 77 | 372 | | _duplexConnectionWriter.Flush(); |
| 77 | 373 | | } |
| | 374 | | else |
| 73 | 375 | | { |
| | 376 | | // The sending of the client-side Close frame is followed by the shutdown of the duplex |
| | 377 | | // connection. For TCP, it's important to always shutdown the connection on the client-side |
| | 378 | | // first to avoid TIME_WAIT states on the server-side. |
| 73 | 379 | | _duplexConnectionWriter.Shutdown(); |
| 73 | 380 | | waitForWriterShutdown = true; |
| 73 | 381 | | } |
| 150 | 382 | | } |
| 152 | 383 | | } |
| 152 | 384 | | } |
| | 385 | |
|
| 197 | 386 | | if (waitForWriterShutdown) |
| 73 | 387 | | { |
| 73 | 388 | | await _duplexConnectionWriter.WriterTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 73 | 389 | | } |
| | 390 | |
|
| | 391 | | // Now, wait for the peer to close the write side of the connection, which will terminate the read frames task. |
| 197 | 392 | | Debug.Assert(_readFramesTask is not null); |
| 197 | 393 | | await _readFramesTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 194 | 394 | | } |
| | 395 | |
|
| | 396 | | public async ValueTask<IMultiplexedStream> CreateStreamAsync( |
| | 397 | | bool bidirectional, |
| | 398 | | CancellationToken cancellationToken) |
| 4097 | 399 | | { |
| 4097 | 400 | | lock (_mutex) |
| 4097 | 401 | | { |
| 4097 | 402 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | 403 | |
|
| 4091 | 404 | | if (_connectTask is null || !_connectTask.IsCompletedSuccessfully) |
| 4 | 405 | | { |
| 4 | 406 | | throw new InvalidOperationException("Cannot create stream before connecting the Slic connection."); |
| | 407 | | } |
| 4087 | 408 | | if (_isClosed) |
| 13 | 409 | | { |
| 13 | 410 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | 411 | | } |
| | 412 | |
|
| 4074 | 413 | | ++_streamSemaphoreWaitCount; |
| 4074 | 414 | | } |
| | 415 | |
|
| | 416 | | try |
| 4074 | 417 | | { |
| 4074 | 418 | | using var createStreamCts = CancellationTokenSource.CreateLinkedTokenSource( |
| 4074 | 419 | | _closedCancellationToken, |
| 4074 | 420 | | cancellationToken); |
| | 421 | |
|
| 4074 | 422 | | SemaphoreSlim? streamCountSemaphore = bidirectional ? |
| 4074 | 423 | | _bidirectionalStreamSemaphore : |
| 4074 | 424 | | _unidirectionalStreamSemaphore; |
| | 425 | |
|
| 4074 | 426 | | if (streamCountSemaphore is null) |
| 2 | 427 | | { |
| | 428 | | // The stream semaphore is null if the peer's max streams configuration is 0. In this case, we let |
| | 429 | | // CreateStreamAsync hang indefinitely until the connection is closed. |
| 2 | 430 | | await Task.Delay(-1, createStreamCts.Token).ConfigureAwait(false); |
| 0 | 431 | | } |
| | 432 | | else |
| 4072 | 433 | | { |
| 4072 | 434 | | await streamCountSemaphore.WaitAsync(createStreamCts.Token).ConfigureAwait(false); |
| 4055 | 435 | | } |
| | 436 | |
|
| 4055 | 437 | | return new SlicStream(this, bidirectional, isRemote: false); |
| | 438 | | } |
| 19 | 439 | | catch (OperationCanceledException) |
| 19 | 440 | | { |
| 19 | 441 | | cancellationToken.ThrowIfCancellationRequested(); |
| 13 | 442 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| 12 | 443 | | Debug.Assert(_isClosed); |
| 12 | 444 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.OperationAborted, _closedMessage); |
| | 445 | | } |
| | 446 | | finally |
| 4074 | 447 | | { |
| 4074 | 448 | | lock (_mutex) |
| 4074 | 449 | | { |
| 4074 | 450 | | --_streamSemaphoreWaitCount; |
| 4074 | 451 | | if (_isClosed && _streamSemaphoreWaitCount == 0) |
| 13 | 452 | | { |
| 13 | 453 | | _streamSemaphoreWaitClosed.SetResult(); |
| 13 | 454 | | } |
| 4074 | 455 | | } |
| 4074 | 456 | | } |
| 4055 | 457 | | } |
| | 458 | |
|
| | 459 | | public ValueTask DisposeAsync() |
| 1898 | 460 | | { |
| 1898 | 461 | | lock (_mutex) |
| 1898 | 462 | | { |
| 1898 | 463 | | _disposeTask ??= PerformDisposeAsync(); |
| 1898 | 464 | | } |
| 1898 | 465 | | return new(_disposeTask); |
| | 466 | |
|
| | 467 | | async Task PerformDisposeAsync() |
| 1371 | 468 | | { |
| | 469 | | // Make sure we execute the code below without holding the mutex lock. |
| 1371 | 470 | | await Task.Yield(); |
| 1371 | 471 | | TryClose(new IceRpcException(IceRpcError.OperationAborted), "The connection was disposed."); |
| | 472 | |
|
| 1371 | 473 | | _disposedCts.Cancel(); |
| | 474 | |
|
| | 475 | | try |
| 1371 | 476 | | { |
| 1371 | 477 | | await Task.WhenAll( |
| 1371 | 478 | | _connectTask ?? Task.CompletedTask, |
| 1371 | 479 | | _readFramesTask ?? Task.CompletedTask, |
| 1371 | 480 | | _streamSemaphoreWaitClosed.Task).ConfigureAwait(false); |
| 774 | 481 | | } |
| 597 | 482 | | catch |
| 597 | 483 | | { |
| | 484 | | // Expected if any of these tasks failed or was canceled. Each task takes care of handling unexpected |
| | 485 | | // exceptions so there's no need to handle them here. |
| 597 | 486 | | } |
| | 487 | |
|
| | 488 | | // Clean-up the streams that might still be queued on the channel. |
| 1424 | 489 | | while (_acceptStreamChannel.Reader.TryRead(out IMultiplexedStream? stream)) |
| 53 | 490 | | { |
| 53 | 491 | | if (stream.IsBidirectional) |
| 10 | 492 | | { |
| 10 | 493 | | stream.Output.Complete(); |
| 10 | 494 | | stream.Input.Complete(); |
| 10 | 495 | | } |
| 43 | 496 | | else if (stream.IsRemote) |
| 43 | 497 | | { |
| 43 | 498 | | stream.Input.Complete(); |
| 43 | 499 | | } |
| | 500 | | else |
| 0 | 501 | | { |
| 0 | 502 | | stream.Output.Complete(); |
| 0 | 503 | | } |
| 53 | 504 | | } |
| | 505 | |
|
| | 506 | | try |
| 1371 | 507 | | { |
| | 508 | | // Prevents unobserved task exceptions. |
| 1371 | 509 | | await _acceptStreamChannel.Reader.Completion.ConfigureAwait(false); |
| 0 | 510 | | } |
| 1371 | 511 | | catch |
| 1371 | 512 | | { |
| 1371 | 513 | | } |
| | 514 | |
|
| 1371 | 515 | | await _duplexConnectionWriter.DisposeAsync().ConfigureAwait(false); |
| 1371 | 516 | | _duplexConnectionReader.Dispose(); |
| 1371 | 517 | | _duplexConnection.Dispose(); |
| | 518 | |
|
| 1371 | 519 | | _disposedCts.Dispose(); |
| 1371 | 520 | | _bidirectionalStreamSemaphore?.Dispose(); |
| 1371 | 521 | | _unidirectionalStreamSemaphore?.Dispose(); |
| 1371 | 522 | | _closedCts.Dispose(); |
| 1371 | 523 | | } |
| 1898 | 524 | | } |
| | 525 | |
|
| 1373 | 526 | | internal SlicConnection( |
| 1373 | 527 | | IDuplexConnection duplexConnection, |
| 1373 | 528 | | MultiplexedConnectionOptions options, |
| 1373 | 529 | | SlicTransportOptions slicOptions, |
| 1373 | 530 | | bool isServer) |
| 1373 | 531 | | { |
| 1373 | 532 | | IsServer = isServer; |
| | 533 | |
|
| 1373 | 534 | | Pool = options.Pool; |
| 1373 | 535 | | MinSegmentSize = options.MinSegmentSize; |
| 1373 | 536 | | _maxBidirectionalStreams = options.MaxBidirectionalStreams; |
| 1373 | 537 | | _maxUnidirectionalStreams = options.MaxUnidirectionalStreams; |
| | 538 | |
|
| 1373 | 539 | | InitialStreamWindowSize = slicOptions.InitialStreamWindowSize; |
| 1373 | 540 | | _localIdleTimeout = slicOptions.IdleTimeout; |
| 1373 | 541 | | _maxStreamFrameSize = slicOptions.MaxStreamFrameSize; |
| | 542 | |
|
| 1373 | 543 | | _acceptStreamChannel = Channel.CreateUnbounded<IMultiplexedStream>(new UnboundedChannelOptions |
| 1373 | 544 | | { |
| 1373 | 545 | | SingleReader = true, |
| 1373 | 546 | | SingleWriter = true |
| 1373 | 547 | | }); |
| | 548 | |
|
| 1373 | 549 | | _closedCancellationToken = _closedCts.Token; |
| | 550 | |
|
| | 551 | | // Only the client-side sends pings to keep the connection alive when idle timeout (set later) is not infinite. |
| 1373 | 552 | | _duplexConnection = IsServer ? |
| 1373 | 553 | | new SlicDuplexConnectionDecorator(duplexConnection) : |
| 1373 | 554 | | new SlicDuplexConnectionDecorator(duplexConnection, SendReadPing, SendWritePing); |
| | 555 | |
|
| 1373 | 556 | | _duplexConnectionReader = new DuplexConnectionReader(_duplexConnection, options.Pool, options.MinSegmentSize); |
| 1373 | 557 | | _duplexConnectionWriter = new SlicDuplexConnectionWriter( |
| 1373 | 558 | | _duplexConnection, |
| 1373 | 559 | | options.Pool, |
| 1373 | 560 | | options.MinSegmentSize); |
| | 561 | |
|
| | 562 | | // We use the same stream ID numbering scheme as Quic. |
| 1373 | 563 | | if (IsServer) |
| 677 | 564 | | { |
| 677 | 565 | | _nextBidirectionalId = 1; |
| 677 | 566 | | _nextUnidirectionalId = 3; |
| 677 | 567 | | } |
| | 568 | | else |
| 696 | 569 | | { |
| 696 | 570 | | _nextBidirectionalId = 0; |
| 696 | 571 | | _nextUnidirectionalId = 2; |
| 696 | 572 | | } |
| | 573 | |
|
| | 574 | | void SendPing(long payload) |
| 27 | 575 | | { |
| | 576 | | try |
| 27 | 577 | | { |
| 27 | 578 | | WriteConnectionFrame(FrameType.Ping, new PingBody(payload).Encode); |
| 25 | 579 | | } |
| 2 | 580 | | catch (IceRpcException) |
| 2 | 581 | | { |
| | 582 | | // Expected if the connection is closed. |
| 2 | 583 | | } |
| 0 | 584 | | catch (Exception exception) |
| 0 | 585 | | { |
| 0 | 586 | | Debug.Fail($"The sending of a Ping frame failed with an unexpected exception: {exception}"); |
| 0 | 587 | | throw; |
| | 588 | | } |
| 27 | 589 | | } |
| | 590 | |
|
| | 591 | | void SendReadPing() |
| 27 | 592 | | { |
| | 593 | | // This local function is no-op if there is already a pending Pong. |
| 27 | 594 | | if (Interlocked.CompareExchange(ref _pendingPongCount, 1, 0) == 0) |
| 27 | 595 | | { |
| 27 | 596 | | SendPing(1L); |
| 27 | 597 | | } |
| 27 | 598 | | } |
| | 599 | |
|
| | 600 | | void SendWritePing() |
| 0 | 601 | | { |
| | 602 | | // _pendingPongCount can be <= 0 if an unexpected pong is received. If it's the case, the connection is |
| | 603 | | // being torn down and there's no point in sending a ping frame. |
| 0 | 604 | | if (Interlocked.Increment(ref _pendingPongCount) > 0) |
| 0 | 605 | | { |
| 0 | 606 | | SendPing(0L); |
| 0 | 607 | | } |
| 0 | 608 | | } |
| 1373 | 609 | | } |
| | 610 | |
|
| | 611 | | /// <summary>Fills the given writer with stream data received on the connection.</summary> |
| | 612 | | /// <param name="bufferWriter">The destination buffer writer.</param> |
| | 613 | | /// <param name="byteCount">The amount of stream data to read.</param> |
| | 614 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 615 | | internal ValueTask FillBufferWriterAsync( |
| | 616 | | IBufferWriter<byte> bufferWriter, |
| | 617 | | int byteCount, |
| | 618 | | CancellationToken cancellationToken) => |
| 18914 | 619 | | _duplexConnectionReader.FillBufferWriterAsync(bufferWriter, byteCount, cancellationToken); |
| | 620 | |
|
| | 621 | | /// <summary>Releases a stream from the connection. The connection stream count is decremented and if this is a |
| | 622 | | /// client allow a new stream to be started.</summary> |
| | 623 | | /// <param name="stream">The released stream.</param> |
| | 624 | | internal void ReleaseStream(SlicStream stream) |
| 8046 | 625 | | { |
| 8046 | 626 | | Debug.Assert(stream.IsStarted); |
| | 627 | |
|
| 8046 | 628 | | _streams.Remove(stream.Id, out SlicStream? _); |
| | 629 | |
|
| 8046 | 630 | | if (stream.IsRemote) |
| 4021 | 631 | | { |
| 4021 | 632 | | if (stream.IsBidirectional) |
| 1262 | 633 | | { |
| 1262 | 634 | | Interlocked.Decrement(ref _bidirectionalStreamCount); |
| 1262 | 635 | | } |
| | 636 | | else |
| 2759 | 637 | | { |
| 2759 | 638 | | Interlocked.Decrement(ref _unidirectionalStreamCount); |
| 2759 | 639 | | } |
| 4021 | 640 | | } |
| 4025 | 641 | | else if (!_isClosed) |
| 3287 | 642 | | { |
| 3287 | 643 | | if (stream.IsBidirectional) |
| 1102 | 644 | | { |
| 1102 | 645 | | _bidirectionalStreamSemaphore!.Release(); |
| 1102 | 646 | | } |
| | 647 | | else |
| 2185 | 648 | | { |
| 2185 | 649 | | _unidirectionalStreamSemaphore!.Release(); |
| 2185 | 650 | | } |
| 3287 | 651 | | } |
| 8046 | 652 | | } |
| | 653 | |
|
| | 654 | | /// <summary>Throws the connection closure exception if the connection is already closed.</summary> |
| | 655 | | internal void ThrowIfClosed() |
| 15637 | 656 | | { |
| 15637 | 657 | | lock (_mutex) |
| 15637 | 658 | | { |
| 15637 | 659 | | if (_isClosed) |
| 19 | 660 | | { |
| 19 | 661 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | 662 | | } |
| 15618 | 663 | | } |
| 15618 | 664 | | } |
| | 665 | |
|
| | 666 | | /// <summary>Writes a connection frame.</summary> |
| | 667 | | /// <param name="frameType">The frame type.</param> |
| | 668 | | /// <param name="encode">The action to encode the frame.</param> |
| | 669 | | internal void WriteConnectionFrame(FrameType frameType, EncodeAction? encode) |
| 1331 | 670 | | { |
| 1331 | 671 | | Debug.Assert(frameType < FrameType.Stream); |
| | 672 | |
|
| 1331 | 673 | | lock (_mutex) |
| 1331 | 674 | | { |
| 1331 | 675 | | if (_isClosed) |
| 2 | 676 | | { |
| 2 | 677 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | 678 | | } |
| 1329 | 679 | | WriteFrame(frameType, streamId: null, encode); |
| 1329 | 680 | | _duplexConnectionWriter.Flush(); |
| 1329 | 681 | | } |
| 1329 | 682 | | } |
| | 683 | |
|
| | 684 | | /// <summary>Writes a stream frame.</summary> |
| | 685 | | /// <param name="stream">The stream to write the frame for.</param> |
| | 686 | | /// <param name="frameType">The frame type.</param> |
| | 687 | | /// <param name="encode">The action to encode the frame.</param> |
| | 688 | | /// <param name="writeReadsClosedFrame"><see langword="true" /> if a <see cref="FrameType.StreamReadsClosed" /> |
| | 689 | | /// frame should be written after the stream frame.</param> |
| | 690 | | /// <remarks>This method is called by streams and might be called on a closed connection. The connection might |
| | 691 | | /// also be closed concurrently while it's in progress.</remarks> |
| | 692 | | internal void WriteStreamFrame( |
| | 693 | | SlicStream stream, |
| | 694 | | FrameType frameType, |
| | 695 | | EncodeAction? encode, |
| | 696 | | bool writeReadsClosedFrame) |
| 5850 | 697 | | { |
| | 698 | | // Ensure that this method is called for any FrameType.StreamXxx frame type except FrameType.Stream. |
| 5850 | 699 | | Debug.Assert(frameType >= FrameType.StreamLast && stream.IsStarted); |
| | 700 | |
|
| 5850 | 701 | | lock (_mutex) |
| 5850 | 702 | | { |
| 5850 | 703 | | if (_isClosed) |
| 1 | 704 | | { |
| 1 | 705 | | return; |
| | 706 | | } |
| | 707 | |
|
| 5849 | 708 | | WriteFrame(frameType, stream.Id, encode); |
| 5849 | 709 | | if (writeReadsClosedFrame) |
| 153 | 710 | | { |
| 153 | 711 | | WriteFrame(FrameType.StreamReadsClosed, stream.Id, encode: null); |
| 153 | 712 | | } |
| 5849 | 713 | | if (frameType == FrameType.StreamLast) |
| 1114 | 714 | | { |
| | 715 | | // Notify the stream that the last stream frame is considered sent at this point. This will close |
| | 716 | | // writes on the stream and allow the stream to be released if reads are also closed. |
| 1114 | 717 | | stream.WroteLastStreamFrame(); |
| 1114 | 718 | | } |
| 5849 | 719 | | _duplexConnectionWriter.Flush(); |
| 5849 | 720 | | } |
| 5850 | 721 | | } |
| | 722 | |
|
| | 723 | | /// <summary>Writes a stream data frame.</summary> |
| | 724 | | /// <param name="stream">The stream to write the frame for.</param> |
| | 725 | | /// <param name="source1">The first stream frame data source.</param> |
| | 726 | | /// <param name="source2">The second stream frame data source.</param> |
| | 727 | | /// <param name="endStream"><see langword="true" /> to write a <see cref="FrameType.StreamLast" /> frame and |
| | 728 | | /// <see langword="false" /> to write a <see cref="FrameType.Stream" /> frame.</param> |
| | 729 | | /// <param name="writeReadsClosedFrame"><see langword="true" /> if a <see cref="FrameType.StreamReadsClosed" /> |
| | 730 | | /// frame should be written after the stream frame.</param> |
| | 731 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 732 | | /// <remarks>This method is called by streams and might be called on a closed connection. The connection might |
| | 733 | | /// also be closed concurrently while it's in progress.</remarks> |
| | 734 | | internal async ValueTask<FlushResult> WriteStreamDataFrameAsync( |
| | 735 | | SlicStream stream, |
| | 736 | | ReadOnlySequence<byte> source1, |
| | 737 | | ReadOnlySequence<byte> source2, |
| | 738 | | bool endStream, |
| | 739 | | bool writeReadsClosedFrame, |
| | 740 | | CancellationToken cancellationToken) |
| 15592 | 741 | | { |
| 15592 | 742 | | Debug.Assert(!source1.IsEmpty || endStream); |
| | 743 | |
|
| 15592 | 744 | | if (_connectTask is null) |
| 0 | 745 | | { |
| 0 | 746 | | throw new InvalidOperationException("Cannot send a stream frame before calling ConnectAsync."); |
| | 747 | | } |
| | 748 | |
|
| 15592 | 749 | | using var writeCts = CancellationTokenSource.CreateLinkedTokenSource( |
| 15592 | 750 | | _closedCancellationToken, |
| 15592 | 751 | | cancellationToken); |
| | 752 | |
|
| | 753 | | try |
| 15592 | 754 | | { |
| | 755 | | do |
| 20248 | 756 | | { |
| | 757 | | // Next, ensure send credit is available. If not, this will block until the receiver allows sending |
| | 758 | | // additional data. |
| 20248 | 759 | | int sendCredit = 0; |
| 20248 | 760 | | if (!source1.IsEmpty || !source2.IsEmpty) |
| 20238 | 761 | | { |
| 20238 | 762 | | sendCredit = await stream.AcquireSendCreditAsync(writeCts.Token).ConfigureAwait(false); |
| 18210 | 763 | | Debug.Assert(sendCredit > 0); |
| 18210 | 764 | | } |
| | 765 | |
|
| | 766 | | // Gather data from source1 or source2 up to sendCredit bytes or the peer maximum stream frame size. |
| 18220 | 767 | | int sendMaxSize = Math.Min(sendCredit, PeerMaxStreamFrameSize); |
| | 768 | | ReadOnlySequence<byte> sendSource1; |
| | 769 | | ReadOnlySequence<byte> sendSource2; |
| 18220 | 770 | | if (!source1.IsEmpty) |
| 16211 | 771 | | { |
| 16211 | 772 | | int length = Math.Min((int)source1.Length, sendMaxSize); |
| 16211 | 773 | | sendSource1 = source1.Slice(0, length); |
| 16211 | 774 | | source1 = source1.Slice(length); |
| 16211 | 775 | | } |
| | 776 | | else |
| 2009 | 777 | | { |
| 2009 | 778 | | sendSource1 = ReadOnlySequence<byte>.Empty; |
| 2009 | 779 | | } |
| | 780 | |
|
| 18220 | 781 | | if (source1.IsEmpty && !source2.IsEmpty) |
| 4072 | 782 | | { |
| 4072 | 783 | | int length = Math.Min((int)source2.Length, sendMaxSize - (int)sendSource1.Length); |
| 4072 | 784 | | sendSource2 = source2.Slice(0, length); |
| 4072 | 785 | | source2 = source2.Slice(length); |
| 4072 | 786 | | } |
| | 787 | | else |
| 14148 | 788 | | { |
| 14148 | 789 | | sendSource2 = ReadOnlySequence<byte>.Empty; |
| 14148 | 790 | | } |
| | 791 | |
|
| | 792 | | // If there's no data left to send and endStream is true, it's the last stream frame. |
| 18220 | 793 | | bool lastStreamFrame = endStream && source1.IsEmpty && source2.IsEmpty; |
| | 794 | |
|
| 18220 | 795 | | lock (_mutex) |
| 18220 | 796 | | { |
| 18220 | 797 | | if (_isClosed) |
| 0 | 798 | | { |
| 0 | 799 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | 800 | | } |
| | 801 | |
|
| 18220 | 802 | | if (!stream.IsStarted) |
| 4025 | 803 | | { |
| 4025 | 804 | | if (stream.IsBidirectional) |
| 1260 | 805 | | { |
| 1260 | 806 | | AddStream(_nextBidirectionalId, stream); |
| 1260 | 807 | | _nextBidirectionalId += 4; |
| 1260 | 808 | | } |
| | 809 | | else |
| 2765 | 810 | | { |
| 2765 | 811 | | AddStream(_nextUnidirectionalId, stream); |
| 2765 | 812 | | _nextUnidirectionalId += 4; |
| 2765 | 813 | | } |
| 4025 | 814 | | } |
| | 815 | |
|
| | 816 | | // Notify the stream that we're consuming sendSize credit. It's important to call this before |
| | 817 | | // sending the stream frame to avoid race conditions where the StreamWindowUpdate frame could be |
| | 818 | | // received before the send credit was updated. |
| 18220 | 819 | | if (sendCredit > 0) |
| 18210 | 820 | | { |
| 18210 | 821 | | stream.ConsumedSendCredit((int)(sendSource1.Length + sendSource2.Length)); |
| 18210 | 822 | | } |
| | 823 | |
|
| 18220 | 824 | | EncodeStreamFrameHeader(stream.Id, sendSource1.Length + sendSource2.Length, lastStreamFrame); |
| | 825 | |
|
| 18220 | 826 | | if (lastStreamFrame) |
| 1462 | 827 | | { |
| | 828 | | // Notify the stream that the last stream frame is considered sent at this point. This will |
| | 829 | | // complete writes on the stream and allow the stream to be released if reads are also |
| | 830 | | // completed. |
| 1462 | 831 | | stream.WroteLastStreamFrame(); |
| 1462 | 832 | | } |
| | 833 | |
|
| | 834 | | // Write and flush the stream frame. |
| 18220 | 835 | | if (!sendSource1.IsEmpty) |
| 16211 | 836 | | { |
| 16211 | 837 | | _duplexConnectionWriter.Write(sendSource1); |
| 16211 | 838 | | } |
| 18220 | 839 | | if (!sendSource2.IsEmpty) |
| 4072 | 840 | | { |
| 4072 | 841 | | _duplexConnectionWriter.Write(sendSource2); |
| 4072 | 842 | | } |
| | 843 | |
|
| 18220 | 844 | | if (writeReadsClosedFrame) |
| 695 | 845 | | { |
| 695 | 846 | | WriteFrame(FrameType.StreamReadsClosed, stream.Id, encode: null); |
| 695 | 847 | | } |
| 18220 | 848 | | _duplexConnectionWriter.Flush(); |
| 18220 | 849 | | } |
| 18220 | 850 | | } |
| 18220 | 851 | | while (!source1.IsEmpty || !source2.IsEmpty); // Loop until there's no data left to send. |
| 13564 | 852 | | } |
| 2028 | 853 | | catch (OperationCanceledException) |
| 2028 | 854 | | { |
| 2028 | 855 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 856 | |
|
| 0 | 857 | | Debug.Assert(_isClosed); |
| 0 | 858 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.OperationAborted, _closedMessage); |
| | 859 | | } |
| | 860 | |
|
| 13564 | 861 | | return new FlushResult(isCanceled: false, isCompleted: false); |
| | 862 | |
|
| | 863 | | void EncodeStreamFrameHeader(ulong streamId, long size, bool lastStreamFrame) |
| 18220 | 864 | | { |
| 18220 | 865 | | var encoder = new SliceEncoder(_duplexConnectionWriter, SliceEncoding.Slice2); |
| 18220 | 866 | | encoder.EncodeFrameType(!lastStreamFrame ? FrameType.Stream : FrameType.StreamLast); |
| 18220 | 867 | | Span<byte> sizePlaceholder = encoder.GetPlaceholderSpan(4); |
| 18220 | 868 | | int startPos = encoder.EncodedByteCount; |
| 18220 | 869 | | encoder.EncodeVarUInt62(streamId); |
| 18220 | 870 | | SliceEncoder.EncodeVarUInt62((ulong)(encoder.EncodedByteCount - startPos + size), sizePlaceholder); |
| 18220 | 871 | | } |
| 13564 | 872 | | } |
| | 873 | |
|
| | 874 | | private void AddStream(ulong id, SlicStream stream) |
| 8046 | 875 | | { |
| 8046 | 876 | | lock (_mutex) |
| 8046 | 877 | | { |
| 8046 | 878 | | if (_isClosed) |
| 0 | 879 | | { |
| 0 | 880 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | 881 | | } |
| | 882 | |
|
| 8046 | 883 | | _streams[id] = stream; |
| | 884 | |
|
| | 885 | | // Assign the stream ID within the mutex to ensure that the addition of the stream to the connection and the |
| | 886 | | // stream ID assignment are atomic. |
| 8046 | 887 | | stream.Id = id; |
| | 888 | |
|
| | 889 | | // Keep track of the last assigned stream ID. This is used to figure out if the stream is known or unknown. |
| 8046 | 890 | | if (stream.IsRemote) |
| 4021 | 891 | | { |
| 4021 | 892 | | if (stream.IsBidirectional) |
| 1262 | 893 | | { |
| 1262 | 894 | | _lastRemoteBidirectionalStreamId = id; |
| 1262 | 895 | | } |
| | 896 | | else |
| 2759 | 897 | | { |
| 2759 | 898 | | _lastRemoteUnidirectionalStreamId = id; |
| 2759 | 899 | | } |
| 4021 | 900 | | } |
| 8046 | 901 | | } |
| 8046 | 902 | | } |
| | 903 | |
|
| | 904 | | private void DecodeParameters(IDictionary<ParameterKey, IList<byte>> parameters) |
| 1224 | 905 | | { |
| 1224 | 906 | | int? maxStreamFrameSize = null; |
| 1224 | 907 | | int? peerInitialStreamWindowSize = null; |
| 15494 | 908 | | foreach ((ParameterKey key, IList<byte> buffer) in parameters) |
| 5911 | 909 | | { |
| 5911 | 910 | | switch (key) |
| | 911 | | { |
| | 912 | | case ParameterKey.MaxBidirectionalStreams: |
| 1095 | 913 | | { |
| 1095 | 914 | | int value = DecodeParamValue(buffer); |
| 1095 | 915 | | if (value > 0) |
| 1095 | 916 | | { |
| 1095 | 917 | | _bidirectionalStreamSemaphore = new SemaphoreSlim(value, value); |
| 1095 | 918 | | } |
| 1095 | 919 | | break; |
| | 920 | | } |
| | 921 | | case ParameterKey.MaxUnidirectionalStreams: |
| 1186 | 922 | | { |
| 1186 | 923 | | int value = DecodeParamValue(buffer); |
| 1186 | 924 | | if (value > 0) |
| 1186 | 925 | | { |
| 1186 | 926 | | _unidirectionalStreamSemaphore = new SemaphoreSlim(value, value); |
| 1186 | 927 | | } |
| 1186 | 928 | | break; |
| | 929 | | } |
| | 930 | | case ParameterKey.IdleTimeout: |
| 1182 | 931 | | { |
| 1182 | 932 | | _peerIdleTimeout = TimeSpan.FromMilliseconds(DecodeParamValue(buffer)); |
| 1182 | 933 | | if (_peerIdleTimeout == TimeSpan.Zero) |
| 0 | 934 | | { |
| 0 | 935 | | throw new InvalidDataException( |
| 0 | 936 | | "The IdleTimeout Slic connection parameter is invalid, it must be greater than 0 s."); |
| | 937 | | } |
| 1182 | 938 | | break; |
| | 939 | | } |
| | 940 | | case ParameterKey.MaxStreamFrameSize: |
| 1224 | 941 | | { |
| 1224 | 942 | | maxStreamFrameSize = DecodeParamValue(buffer); |
| 1224 | 943 | | if (maxStreamFrameSize < 1024) |
| 0 | 944 | | { |
| 0 | 945 | | throw new InvalidDataException( |
| 0 | 946 | | "The MaxStreamFrameSize connection parameter is invalid, it must be greater than 1KB."); |
| | 947 | | } |
| 1224 | 948 | | break; |
| | 949 | | } |
| | 950 | | case ParameterKey.InitialStreamWindowSize: |
| 1224 | 951 | | { |
| 1224 | 952 | | peerInitialStreamWindowSize = DecodeParamValue(buffer); |
| 1224 | 953 | | if (peerInitialStreamWindowSize < 1024) |
| 0 | 954 | | { |
| 0 | 955 | | throw new InvalidDataException( |
| 0 | 956 | | "The InitialStreamWindowSize connection parameter is invalid, it must be greater than 1KB.") |
| | 957 | | } |
| 1224 | 958 | | break; |
| | 959 | | } |
| | 960 | | // Ignore unsupported parameter. |
| | 961 | | } |
| 5911 | 962 | | } |
| | 963 | |
|
| 1224 | 964 | | if (maxStreamFrameSize is null) |
| 0 | 965 | | { |
| 0 | 966 | | throw new InvalidDataException( |
| 0 | 967 | | "The peer didn't send the required MaxStreamFrameSize connection parameter."); |
| | 968 | | } |
| | 969 | | else |
| 1224 | 970 | | { |
| 1224 | 971 | | PeerMaxStreamFrameSize = maxStreamFrameSize.Value; |
| 1224 | 972 | | } |
| | 973 | |
|
| 1224 | 974 | | if (peerInitialStreamWindowSize is null) |
| 0 | 975 | | { |
| 0 | 976 | | throw new InvalidDataException( |
| 0 | 977 | | "The peer didn't send the required InitialStreamWindowSize connection parameter."); |
| | 978 | | } |
| | 979 | | else |
| 1224 | 980 | | { |
| 1224 | 981 | | PeerInitialStreamWindowSize = peerInitialStreamWindowSize.Value; |
| 1224 | 982 | | } |
| | 983 | |
|
| | 984 | | // all parameter values are currently integers in the range 0..Int32Max encoded as varuint62. |
| | 985 | | static int DecodeParamValue(IList<byte> buffer) |
| 5911 | 986 | | { |
| | 987 | | // The IList<byte> decoded by the IceRPC + Slice integration is backed by an array |
| 5911 | 988 | | ulong value = SliceEncoding.Slice2.DecodeBuffer( |
| 5911 | 989 | | new ReadOnlySequence<byte>((byte[])buffer), |
| 11822 | 990 | | (ref SliceDecoder decoder) => decoder.DecodeVarUInt62()); |
| | 991 | | try |
| 5911 | 992 | | { |
| 5911 | 993 | | return checked((int)value); |
| | 994 | | } |
| 0 | 995 | | catch (OverflowException exception) |
| 0 | 996 | | { |
| 0 | 997 | | throw new InvalidDataException("The value is out of the varuint32 accepted range.", exception); |
| | 998 | | } |
| 5911 | 999 | | } |
| 1224 | 1000 | | } |
| | 1001 | |
|
| | 1002 | | private Dictionary<ParameterKey, IList<byte>> EncodeParameters() |
| 1275 | 1003 | | { |
| 1275 | 1004 | | var parameters = new List<KeyValuePair<ParameterKey, IList<byte>>> |
| 1275 | 1005 | | { |
| 1275 | 1006 | | // Required parameters. |
| 1275 | 1007 | | EncodeParameter(ParameterKey.MaxStreamFrameSize, (ulong)_maxStreamFrameSize), |
| 1275 | 1008 | | EncodeParameter(ParameterKey.InitialStreamWindowSize, (ulong)InitialStreamWindowSize) |
| 1275 | 1009 | | }; |
| | 1010 | |
|
| | 1011 | | // Optional parameters. |
| 1275 | 1012 | | if (_localIdleTimeout != Timeout.InfiniteTimeSpan) |
| 1271 | 1013 | | { |
| 1271 | 1014 | | parameters.Add(EncodeParameter(ParameterKey.IdleTimeout, (ulong)_localIdleTimeout.TotalMilliseconds)); |
| 1271 | 1015 | | } |
| 1275 | 1016 | | if (_maxBidirectionalStreams > 0) |
| 1169 | 1017 | | { |
| 1169 | 1018 | | parameters.Add(EncodeParameter(ParameterKey.MaxBidirectionalStreams, (ulong)_maxBidirectionalStreams)); |
| 1169 | 1019 | | } |
| 1275 | 1020 | | if (_maxUnidirectionalStreams > 0) |
| 1275 | 1021 | | { |
| 1275 | 1022 | | parameters.Add(EncodeParameter(ParameterKey.MaxUnidirectionalStreams, (ulong)_maxUnidirectionalStreams)); |
| 1275 | 1023 | | } |
| | 1024 | |
|
| 1275 | 1025 | | return new Dictionary<ParameterKey, IList<byte>>(parameters); |
| | 1026 | |
|
| | 1027 | | static KeyValuePair<ParameterKey, IList<byte>> EncodeParameter(ParameterKey key, ulong value) |
| 6265 | 1028 | | { |
| 6265 | 1029 | | int sizeLength = SliceEncoder.GetVarUInt62EncodedSize(value); |
| 6265 | 1030 | | byte[] buffer = new byte[sizeLength]; |
| 6265 | 1031 | | SliceEncoder.EncodeVarUInt62(value, buffer); |
| 6265 | 1032 | | return new(key, buffer); |
| 6265 | 1033 | | } |
| 1275 | 1034 | | } |
| | 1035 | |
|
| | 1036 | | private bool IsUnknownStream(ulong streamId) |
| 9607 | 1037 | | { |
| 9607 | 1038 | | bool isRemote = streamId % 2 == (IsServer ? 0ul : 1ul); |
| 9607 | 1039 | | bool isBidirectional = streamId % 4 < 2; |
| 9607 | 1040 | | if (isRemote) |
| 5238 | 1041 | | { |
| 5238 | 1042 | | if (isBidirectional) |
| 2410 | 1043 | | { |
| 2410 | 1044 | | return _lastRemoteBidirectionalStreamId is null || streamId > _lastRemoteBidirectionalStreamId; |
| | 1045 | | } |
| | 1046 | | else |
| 2828 | 1047 | | { |
| 2828 | 1048 | | return _lastRemoteUnidirectionalStreamId is null || streamId > _lastRemoteUnidirectionalStreamId; |
| | 1049 | | } |
| | 1050 | | } |
| | 1051 | | else |
| 4369 | 1052 | | { |
| 4369 | 1053 | | if (isBidirectional) |
| 2133 | 1054 | | { |
| 2133 | 1055 | | return streamId >= _nextBidirectionalId; |
| | 1056 | | } |
| | 1057 | | else |
| 2236 | 1058 | | { |
| 2236 | 1059 | | return streamId >= _nextUnidirectionalId; |
| | 1060 | | } |
| | 1061 | | } |
| 9607 | 1062 | | } |
| | 1063 | |
|
| | 1064 | | private Task ReadFrameAsync(FrameType frameType, int size, ulong? streamId, CancellationToken cancellationToken) |
| 24755 | 1065 | | { |
| 24755 | 1066 | | if (frameType >= FrameType.Stream && streamId is null) |
| 0 | 1067 | | { |
| 0 | 1068 | | throw new InvalidDataException("Received stream frame without stream ID."); |
| | 1069 | | } |
| | 1070 | |
|
| 24755 | 1071 | | switch (frameType) |
| | 1072 | | { |
| | 1073 | | case FrameType.Close: |
| 154 | 1074 | | { |
| 154 | 1075 | | return ReadCloseFrameAsync(size, cancellationToken); |
| | 1076 | | } |
| | 1077 | | case FrameType.Ping: |
| 29 | 1078 | | { |
| 29 | 1079 | | return ReadPingFrameAndWritePongFrameAsync(size, cancellationToken); |
| | 1080 | | } |
| | 1081 | | case FrameType.Pong: |
| 31 | 1082 | | { |
| 31 | 1083 | | return ReadPongFrameAsync(size, cancellationToken); |
| | 1084 | | } |
| | 1085 | | case FrameType.Stream: |
| | 1086 | | case FrameType.StreamLast: |
| 19116 | 1087 | | { |
| 19116 | 1088 | | return ReadStreamDataFrameAsync(frameType, size, streamId!.Value, cancellationToken); |
| | 1089 | | } |
| | 1090 | | case FrameType.StreamWindowUpdate: |
| 1983 | 1091 | | { |
| 1983 | 1092 | | if (IsUnknownStream(streamId!.Value)) |
| 2 | 1093 | | { |
| 2 | 1094 | | throw new InvalidDataException($"Received {frameType} frame for unknown stream."); |
| | 1095 | | } |
| | 1096 | |
|
| 1981 | 1097 | | return ReadStreamWindowUpdateFrameAsync(size, streamId!.Value, cancellationToken); |
| | 1098 | | } |
| | 1099 | | case FrameType.StreamReadsClosed: |
| | 1100 | | case FrameType.StreamWritesClosed: |
| 3436 | 1101 | | { |
| 3436 | 1102 | | if (size > 0) |
| 4 | 1103 | | { |
| 4 | 1104 | | throw new InvalidDataException($"Unexpected body for {frameType} frame."); |
| | 1105 | | } |
| 3432 | 1106 | | if (IsUnknownStream(streamId!.Value)) |
| 4 | 1107 | | { |
| 4 | 1108 | | throw new InvalidDataException($"Received {frameType} frame for unknown stream."); |
| | 1109 | | } |
| | 1110 | |
|
| 3428 | 1111 | | if (_streams.TryGetValue(streamId.Value, out SlicStream? stream)) |
| 2602 | 1112 | | { |
| 2602 | 1113 | | if (frameType == FrameType.StreamWritesClosed) |
| 82 | 1114 | | { |
| 82 | 1115 | | stream.ReceivedWritesClosedFrame(); |
| 82 | 1116 | | } |
| | 1117 | | else |
| 2520 | 1118 | | { |
| 2520 | 1119 | | stream.ReceivedReadsClosedFrame(); |
| 2520 | 1120 | | } |
| 2602 | 1121 | | } |
| 3428 | 1122 | | return Task.CompletedTask; |
| | 1123 | | } |
| | 1124 | | default: |
| 6 | 1125 | | { |
| 6 | 1126 | | throw new InvalidDataException($"Received unexpected {frameType} frame."); |
| | 1127 | | } |
| | 1128 | | } |
| | 1129 | |
|
| | 1130 | | async Task ReadCloseFrameAsync(int size, CancellationToken cancellationToken) |
| 154 | 1131 | | { |
| 154 | 1132 | | CloseBody closeBody = await ReadFrameBodyAsync( |
| 154 | 1133 | | FrameType.Close, |
| 154 | 1134 | | size, |
| 152 | 1135 | | (ref SliceDecoder decoder) => new CloseBody(ref decoder), |
| 154 | 1136 | | cancellationToken).ConfigureAwait(false); |
| | 1137 | |
|
| 150 | 1138 | | IceRpcError? peerCloseError = closeBody.ApplicationErrorCode switch |
| 150 | 1139 | | { |
| 106 | 1140 | | (ulong)MultiplexedConnectionCloseError.NoError => IceRpcError.ConnectionClosedByPeer, |
| 8 | 1141 | | (ulong)MultiplexedConnectionCloseError.Refused => IceRpcError.ConnectionRefused, |
| 16 | 1142 | | (ulong)MultiplexedConnectionCloseError.ServerBusy => IceRpcError.ServerBusy, |
| 10 | 1143 | | (ulong)MultiplexedConnectionCloseError.Aborted => IceRpcError.ConnectionAborted, |
| 10 | 1144 | | _ => null |
| 150 | 1145 | | }; |
| | 1146 | |
|
| | 1147 | | bool notAlreadyClosed; |
| 150 | 1148 | | if (peerCloseError is null) |
| 10 | 1149 | | { |
| 10 | 1150 | | notAlreadyClosed = TryClose( |
| 10 | 1151 | | new IceRpcException(IceRpcError.ConnectionAborted), |
| 10 | 1152 | | $"The connection was closed by the peer with an unknown application error code: '{closeBody.Applicat |
| 10 | 1153 | | IceRpcError.ConnectionAborted); |
| 10 | 1154 | | } |
| | 1155 | | else |
| 140 | 1156 | | { |
| 140 | 1157 | | notAlreadyClosed = TryClose( |
| 140 | 1158 | | new IceRpcException(peerCloseError.Value), |
| 140 | 1159 | | "The connection was closed by the peer.", |
| 140 | 1160 | | peerCloseError); |
| 140 | 1161 | | } |
| | 1162 | |
|
| | 1163 | | // The server-side of the duplex connection is only shutdown once the client-side is shutdown. When using |
| | 1164 | | // TCP, this ensures that the server TCP connection won't end-up in the TIME_WAIT state on the server-side. |
| 150 | 1165 | | if (notAlreadyClosed && !IsServer) |
| 63 | 1166 | | { |
| | 1167 | | // DisposeAsync waits for the reads frames task to complete before disposing the writer. |
| 63 | 1168 | | lock (_mutex) |
| 63 | 1169 | | { |
| 63 | 1170 | | _duplexConnectionWriter.Shutdown(); |
| 63 | 1171 | | } |
| 63 | 1172 | | await _duplexConnectionWriter.WriterTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 61 | 1173 | | } |
| 148 | 1174 | | } |
| | 1175 | |
|
| | 1176 | | async Task ReadPingFrameAndWritePongFrameAsync(int size, CancellationToken cancellationToken) |
| 29 | 1177 | | { |
| | 1178 | | // Read the ping frame. |
| 29 | 1179 | | PingBody pingBody = await ReadFrameBodyAsync( |
| 29 | 1180 | | FrameType.Ping, |
| 29 | 1181 | | size, |
| 27 | 1182 | | (ref SliceDecoder decoder) => new PingBody(ref decoder), |
| 29 | 1183 | | cancellationToken).ConfigureAwait(false); |
| | 1184 | |
|
| | 1185 | | // Return a pong frame with the ping payload. |
| 25 | 1186 | | WriteConnectionFrame(FrameType.Pong, new PongBody(pingBody.Payload).Encode); |
| 25 | 1187 | | } |
| | 1188 | |
|
| | 1189 | | async Task ReadPongFrameAsync(int size, CancellationToken cancellationToken) |
| 31 | 1190 | | { |
| 31 | 1191 | | if (Interlocked.Decrement(ref _pendingPongCount) >= 0) |
| 25 | 1192 | | { |
| | 1193 | | // Ensure the pong frame payload value is expected. |
| | 1194 | |
|
| 25 | 1195 | | PongBody pongBody = await ReadFrameBodyAsync( |
| 25 | 1196 | | FrameType.Pong, |
| 25 | 1197 | | size, |
| 25 | 1198 | | (ref SliceDecoder decoder) => new PongBody(ref decoder), |
| 25 | 1199 | | cancellationToken).ConfigureAwait(false); |
| | 1200 | |
|
| | 1201 | | // For now, we only send a 0 or 1 payload value (0 for "write ping" and 1 for "read ping"). |
| 25 | 1202 | | if (pongBody.Payload != 0L && pongBody.Payload != 1L) |
| 0 | 1203 | | { |
| 0 | 1204 | | throw new InvalidDataException($"Received {nameof(FrameType.Pong)} with unexpected payload."); |
| | 1205 | | } |
| 25 | 1206 | | } |
| | 1207 | | else |
| 6 | 1208 | | { |
| | 1209 | | // If not waiting for a pong frame, this pong frame is unexpected. |
| 6 | 1210 | | throw new InvalidDataException($"Received unexpected {nameof(FrameType.Pong)} frame."); |
| | 1211 | | } |
| 25 | 1212 | | } |
| | 1213 | |
|
| | 1214 | | async Task ReadStreamWindowUpdateFrameAsync(int size, ulong streamId, CancellationToken cancellationToken) |
| 1981 | 1215 | | { |
| 1981 | 1216 | | StreamWindowUpdateBody frame = await ReadFrameBodyAsync( |
| 1981 | 1217 | | FrameType.StreamWindowUpdate, |
| 1981 | 1218 | | size, |
| 1981 | 1219 | | (ref SliceDecoder decoder) => new StreamWindowUpdateBody(ref decoder), |
| 1981 | 1220 | | cancellationToken).ConfigureAwait(false); |
| 1981 | 1221 | | if (_streams.TryGetValue(streamId, out SlicStream? stream)) |
| 1911 | 1222 | | { |
| 1911 | 1223 | | stream.ReceivedWindowUpdateFrame(frame); |
| 1911 | 1224 | | } |
| 1981 | 1225 | | } |
| | 1226 | |
|
| | 1227 | | async Task<T> ReadFrameBodyAsync<T>( |
| | 1228 | | FrameType frameType, |
| | 1229 | | int size, |
| | 1230 | | DecodeFunc<T> decodeFunc, |
| | 1231 | | CancellationToken cancellationToken) |
| 2189 | 1232 | | { |
| 2189 | 1233 | | if (size <= 0) |
| 4 | 1234 | | { |
| 4 | 1235 | | throw new InvalidDataException($"Unexpected empty body for {frameType} frame."); |
| | 1236 | | } |
| | 1237 | |
|
| 2185 | 1238 | | ReadOnlySequence<byte> buffer = await _duplexConnectionReader.ReadAtLeastAsync(size, cancellationToken) |
| 2185 | 1239 | | .ConfigureAwait(false); |
| | 1240 | |
|
| 2185 | 1241 | | if (buffer.Length > size) |
| 1405 | 1242 | | { |
| 1405 | 1243 | | buffer = buffer.Slice(0, size); |
| 1405 | 1244 | | } |
| | 1245 | |
|
| 2185 | 1246 | | T decodedFrame = SliceEncoding.Slice2.DecodeBuffer(buffer, decodeFunc); |
| 2181 | 1247 | | _duplexConnectionReader.AdvanceTo(buffer.End); |
| 2181 | 1248 | | return decodedFrame; |
| 2181 | 1249 | | } |
| 24739 | 1250 | | } |
| | 1251 | |
|
| | 1252 | | private async ValueTask<(FrameType FrameType, int FrameSize, ulong? StreamId)?> ReadFrameHeaderAsync( |
| | 1253 | | CancellationToken cancellationToken) |
| 27232 | 1254 | | { |
| 27232 | 1255 | | while (true) |
| 27232 | 1256 | | { |
| | 1257 | | // Read data from the pipe reader. |
| 27232 | 1258 | | if (!_duplexConnectionReader.TryRead(out ReadOnlySequence<byte> buffer)) |
| 17021 | 1259 | | { |
| 17021 | 1260 | | buffer = await _duplexConnectionReader.ReadAsync(cancellationToken).ConfigureAwait(false); |
| 16054 | 1261 | | } |
| | 1262 | |
|
| 26265 | 1263 | | if (buffer.IsEmpty) |
| 262 | 1264 | | { |
| 262 | 1265 | | return null; |
| | 1266 | | } |
| | 1267 | |
|
| 26003 | 1268 | | if (TryDecodeHeader( |
| 26003 | 1269 | | buffer, |
| 26003 | 1270 | | out (FrameType FrameType, int FrameSize, ulong? StreamId) header, |
| 26003 | 1271 | | out int consumed)) |
| 25993 | 1272 | | { |
| 25993 | 1273 | | _duplexConnectionReader.AdvanceTo(buffer.GetPosition(consumed)); |
| 25993 | 1274 | | return header; |
| | 1275 | | } |
| | 1276 | | else |
| 0 | 1277 | | { |
| 0 | 1278 | | _duplexConnectionReader.AdvanceTo(buffer.Start, buffer.End); |
| 0 | 1279 | | } |
| 0 | 1280 | | } |
| | 1281 | |
|
| | 1282 | | static bool TryDecodeHeader( |
| | 1283 | | ReadOnlySequence<byte> buffer, |
| | 1284 | | out (FrameType FrameType, int FrameSize, ulong? StreamId) header, |
| | 1285 | | out int consumed) |
| 26003 | 1286 | | { |
| 26003 | 1287 | | header = default; |
| 26003 | 1288 | | consumed = default; |
| | 1289 | |
|
| 26003 | 1290 | | var decoder = new SliceDecoder(buffer, SliceEncoding.Slice2); |
| | 1291 | |
|
| | 1292 | | // Decode the frame type and frame size. |
| 26003 | 1293 | | if (!decoder.TryDecodeUInt8(out byte frameType) || !decoder.TryDecodeVarUInt62(out ulong frameSize)) |
| 0 | 1294 | | { |
| 0 | 1295 | | return false; |
| | 1296 | | } |
| | 1297 | |
|
| 26003 | 1298 | | header.FrameType = frameType.AsFrameType(); |
| | 1299 | | try |
| 25997 | 1300 | | { |
| 25997 | 1301 | | header.FrameSize = checked((int)frameSize); |
| 25997 | 1302 | | } |
| 0 | 1303 | | catch (OverflowException exception) |
| 0 | 1304 | | { |
| 0 | 1305 | | throw new InvalidDataException("The frame size can't be larger than int.MaxValue.", exception); |
| | 1306 | | } |
| | 1307 | |
|
| | 1308 | | // If it's a stream frame, try to decode the stream ID |
| 25997 | 1309 | | if (header.FrameType >= FrameType.Stream) |
| 24539 | 1310 | | { |
| 24539 | 1311 | | if (header.FrameSize == 0) |
| 2 | 1312 | | { |
| 2 | 1313 | | throw new InvalidDataException("Invalid stream frame size."); |
| | 1314 | | } |
| | 1315 | |
|
| 24537 | 1316 | | consumed = (int)decoder.Consumed; |
| 24537 | 1317 | | if (!decoder.TryDecodeVarUInt62(out ulong streamId)) |
| 0 | 1318 | | { |
| 0 | 1319 | | return false; |
| | 1320 | | } |
| 24537 | 1321 | | header.StreamId = streamId; |
| 24537 | 1322 | | header.FrameSize -= (int)decoder.Consumed - consumed; |
| | 1323 | |
|
| 24537 | 1324 | | if (header.FrameSize < 0) |
| 2 | 1325 | | { |
| 2 | 1326 | | throw new InvalidDataException("Invalid stream frame size."); |
| | 1327 | | } |
| 24535 | 1328 | | } |
| | 1329 | |
|
| 25993 | 1330 | | consumed = (int)decoder.Consumed; |
| 25993 | 1331 | | return true; |
| 25993 | 1332 | | } |
| 26255 | 1333 | | } |
| | 1334 | |
|
| | 1335 | | private async Task ReadFramesAsync(CancellationToken cancellationToken) |
| 1224 | 1336 | | { |
| | 1337 | | try |
| 1224 | 1338 | | { |
| 25939 | 1339 | | while (true) |
| 25939 | 1340 | | { |
| 25939 | 1341 | | (FrameType Type, int Size, ulong? StreamId)? header = await ReadFrameHeaderAsync(cancellationToken) |
| 25939 | 1342 | | .ConfigureAwait(false); |
| | 1343 | |
|
| 25015 | 1344 | | if (header is null) |
| 260 | 1345 | | { |
| 260 | 1346 | | lock (_mutex) |
| 260 | 1347 | | { |
| 260 | 1348 | | if (!_isClosed) |
| 0 | 1349 | | { |
| | 1350 | | // Unexpected duplex connection shutdown. |
| 0 | 1351 | | throw new IceRpcException(IceRpcError.ConnectionAborted); |
| | 1352 | | } |
| 260 | 1353 | | } |
| | 1354 | | // The peer has shut down the duplex connection. |
| 260 | 1355 | | break; |
| | 1356 | | } |
| | 1357 | |
|
| 24755 | 1358 | | await ReadFrameAsync(header.Value.Type, header.Value.Size, header.Value.StreamId, cancellationToken) |
| 24755 | 1359 | | .ConfigureAwait(false); |
| 24715 | 1360 | | } |
| | 1361 | |
|
| 260 | 1362 | | if (IsServer) |
| 134 | 1363 | | { |
| 134 | 1364 | | Debug.Assert(_isClosed); |
| | 1365 | |
|
| | 1366 | | // The server-side of the duplex connection is only shutdown once the client-side is shutdown. When |
| | 1367 | | // using TCP, this ensures that the server TCP connection won't end-up in the TIME_WAIT state on the |
| | 1368 | | // server-side. |
| | 1369 | |
|
| | 1370 | | // DisposeAsync waits for the reads frames task to complete before disposing the writer. |
| 134 | 1371 | | lock (_mutex) |
| 134 | 1372 | | { |
| 134 | 1373 | | _duplexConnectionWriter.Shutdown(); |
| | 1374 | |
|
| | 1375 | | // Make sure that CloseAsync doesn't call Write on the writer if it's called shortly after the peer |
| | 1376 | | // shutdown its side of the connection (which triggers ReadFrameHeaderAsync to return null). |
| 134 | 1377 | | _writerIsShutdown = true; |
| 134 | 1378 | | } |
| | 1379 | |
|
| 134 | 1380 | | await _duplexConnectionWriter.WriterTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 132 | 1381 | | } |
| 258 | 1382 | | } |
| 476 | 1383 | | catch (OperationCanceledException) |
| 476 | 1384 | | { |
| | 1385 | | // Expected, DisposeAsync was called. |
| 476 | 1386 | | } |
| 452 | 1387 | | catch (IceRpcException exception) |
| 452 | 1388 | | { |
| 452 | 1389 | | TryClose(exception, "The connection was lost.", IceRpcError.ConnectionAborted); |
| 452 | 1390 | | throw; |
| | 1391 | | } |
| 38 | 1392 | | catch (InvalidDataException exception) |
| 38 | 1393 | | { |
| 38 | 1394 | | var rpcException = new IceRpcException( |
| 38 | 1395 | | IceRpcError.IceRpcError, |
| 38 | 1396 | | "The connection was aborted by a Slic protocol error.", |
| 38 | 1397 | | exception); |
| 38 | 1398 | | TryClose(rpcException, rpcException.Message, IceRpcError.IceRpcError); |
| 38 | 1399 | | throw rpcException; |
| | 1400 | | } |
| 0 | 1401 | | catch (Exception exception) |
| 0 | 1402 | | { |
| 0 | 1403 | | Debug.Fail($"The read frames task completed due to an unhandled exception: {exception}"); |
| 0 | 1404 | | TryClose(exception, "The connection was lost.", IceRpcError.ConnectionAborted); |
| 0 | 1405 | | throw; |
| | 1406 | | } |
| 734 | 1407 | | } |
| | 1408 | |
|
| | 1409 | | private async Task ReadStreamDataFrameAsync( |
| | 1410 | | FrameType type, |
| | 1411 | | int size, |
| | 1412 | | ulong streamId, |
| | 1413 | | CancellationToken cancellationToken) |
| 19116 | 1414 | | { |
| 19116 | 1415 | | bool endStream = type == FrameType.StreamLast; |
| 19116 | 1416 | | bool isRemote = streamId % 2 == (IsServer ? 0ul : 1ul); |
| 19116 | 1417 | | bool isBidirectional = streamId % 4 < 2; |
| | 1418 | |
|
| 19116 | 1419 | | if (!isBidirectional && !isRemote) |
| 0 | 1420 | | { |
| 0 | 1421 | | throw new InvalidDataException( |
| 0 | 1422 | | "Received unexpected stream frame on local unidirectional stream."); |
| | 1423 | | } |
| 19116 | 1424 | | else if (size == 0 && !endStream) |
| 2 | 1425 | | { |
| 2 | 1426 | | throw new InvalidDataException($"Received invalid {nameof(FrameType.Stream)} frame."); |
| | 1427 | | } |
| | 1428 | |
|
| 19114 | 1429 | | if (!_streams.TryGetValue(streamId, out SlicStream? stream) && isRemote && IsUnknownStream(streamId)) |
| 4021 | 1430 | | { |
| | 1431 | | // Create a new remote stream. |
| | 1432 | |
|
| 4021 | 1433 | | if (size == 0) |
| 0 | 1434 | | { |
| 0 | 1435 | | throw new InvalidDataException("Received empty stream frame on new stream."); |
| | 1436 | | } |
| | 1437 | |
|
| 4021 | 1438 | | if (isBidirectional) |
| 1262 | 1439 | | { |
| 1262 | 1440 | | if (streamId > _lastRemoteBidirectionalStreamId + 4) |
| 0 | 1441 | | { |
| 0 | 1442 | | throw new InvalidDataException("Invalid stream ID."); |
| | 1443 | | } |
| | 1444 | |
|
| 1262 | 1445 | | if (_bidirectionalStreamCount == _maxBidirectionalStreams) |
| 0 | 1446 | | { |
| 0 | 1447 | | throw new IceRpcException( |
| 0 | 1448 | | IceRpcError.IceRpcError, |
| 0 | 1449 | | $"The maximum bidirectional stream count {_maxBidirectionalStreams} was reached."); |
| | 1450 | | } |
| 1262 | 1451 | | Interlocked.Increment(ref _bidirectionalStreamCount); |
| 1262 | 1452 | | } |
| | 1453 | | else |
| 2759 | 1454 | | { |
| 2759 | 1455 | | if (streamId > _lastRemoteUnidirectionalStreamId + 4) |
| 0 | 1456 | | { |
| 0 | 1457 | | throw new InvalidDataException("Invalid stream ID."); |
| | 1458 | | } |
| | 1459 | |
|
| 2759 | 1460 | | if (_unidirectionalStreamCount == _maxUnidirectionalStreams) |
| 0 | 1461 | | { |
| 0 | 1462 | | throw new IceRpcException( |
| 0 | 1463 | | IceRpcError.IceRpcError, |
| 0 | 1464 | | $"The maximum unidirectional stream count {_maxUnidirectionalStreams} was reached"); |
| | 1465 | | } |
| 2759 | 1466 | | Interlocked.Increment(ref _unidirectionalStreamCount); |
| 2759 | 1467 | | } |
| | 1468 | |
|
| | 1469 | | // The stream is registered with the connection and queued on the channel. The caller of AcceptStreamAsync |
| | 1470 | | // is responsible for cleaning up the stream. |
| 4021 | 1471 | | stream = new SlicStream(this, isBidirectional, isRemote: true); |
| | 1472 | |
|
| | 1473 | | try |
| 4021 | 1474 | | { |
| 4021 | 1475 | | AddStream(streamId, stream); |
| | 1476 | |
|
| | 1477 | | try |
| 4021 | 1478 | | { |
| 4021 | 1479 | | await _acceptStreamChannel.Writer.WriteAsync( |
| 4021 | 1480 | | stream, |
| 4021 | 1481 | | cancellationToken).ConfigureAwait(false); |
| 4021 | 1482 | | } |
| 0 | 1483 | | catch (ChannelClosedException exception) |
| 0 | 1484 | | { |
| | 1485 | | // The exception given to ChannelWriter.Complete(Exception? exception) is the InnerException. |
| 0 | 1486 | | Debug.Assert(exception.InnerException is not null); |
| 0 | 1487 | | throw ExceptionUtil.Throw(exception.InnerException); |
| | 1488 | | } |
| 4021 | 1489 | | } |
| 0 | 1490 | | catch (IceRpcException) |
| 0 | 1491 | | { |
| | 1492 | | // The two methods above throw IceRpcException if the connection has been closed (either by CloseAsync |
| | 1493 | | // or because the close frame was received). We cleanup up the stream but don't throw to not abort the |
| | 1494 | | // reading. The connection graceful closure still needs to read on the connection to figure out when the |
| | 1495 | | // peer shuts down the duplex connection. |
| 0 | 1496 | | Debug.Assert(_isClosed); |
| 0 | 1497 | | stream.Input.Complete(); |
| 0 | 1498 | | if (isBidirectional) |
| 0 | 1499 | | { |
| 0 | 1500 | | stream.Output.Complete(); |
| 0 | 1501 | | } |
| 0 | 1502 | | } |
| 4021 | 1503 | | } |
| | 1504 | |
|
| 19114 | 1505 | | bool isDataConsumed = false; |
| 19114 | 1506 | | if (stream is not null) |
| 18929 | 1507 | | { |
| | 1508 | | // Let the stream consume the stream frame data. |
| 18929 | 1509 | | isDataConsumed = await stream.ReceivedDataFrameAsync( |
| 18929 | 1510 | | size, |
| 18929 | 1511 | | endStream, |
| 18929 | 1512 | | cancellationToken).ConfigureAwait(false); |
| 18928 | 1513 | | } |
| | 1514 | |
|
| 19113 | 1515 | | if (!isDataConsumed) |
| 200 | 1516 | | { |
| | 1517 | | // The stream (if any) didn't consume the data. Read and ignore the data using a helper pipe. |
| 200 | 1518 | | var pipe = new Pipe( |
| 200 | 1519 | | new PipeOptions( |
| 200 | 1520 | | pool: Pool, |
| 200 | 1521 | | pauseWriterThreshold: 0, |
| 200 | 1522 | | minimumSegmentSize: MinSegmentSize, |
| 200 | 1523 | | useSynchronizationContext: false)); |
| | 1524 | |
|
| 200 | 1525 | | await _duplexConnectionReader.FillBufferWriterAsync( |
| 200 | 1526 | | pipe.Writer, |
| 200 | 1527 | | size, |
| 200 | 1528 | | cancellationToken).ConfigureAwait(false); |
| | 1529 | |
|
| 195 | 1530 | | pipe.Writer.Complete(); |
| 195 | 1531 | | pipe.Reader.Complete(); |
| 195 | 1532 | | } |
| 19108 | 1533 | | } |
| | 1534 | |
|
| | 1535 | | private bool TryClose(Exception exception, string closeMessage, IceRpcError? peerCloseError = null) |
| 2208 | 1536 | | { |
| 2208 | 1537 | | lock (_mutex) |
| 2208 | 1538 | | { |
| 2208 | 1539 | | if (_isClosed) |
| 837 | 1540 | | { |
| 837 | 1541 | | return false; |
| | 1542 | | } |
| 1371 | 1543 | | _isClosed = true; |
| 1371 | 1544 | | _closedMessage = closeMessage; |
| 1371 | 1545 | | _peerCloseError = peerCloseError; |
| 1371 | 1546 | | if (_streamSemaphoreWaitCount == 0) |
| 1358 | 1547 | | { |
| 1358 | 1548 | | _streamSemaphoreWaitClosed.SetResult(); |
| 1358 | 1549 | | } |
| 1371 | 1550 | | } |
| | 1551 | |
|
| | 1552 | | // Cancel pending CreateStreamAsync, AcceptStreamAsync and WriteStreamDataFrameAsync operations. |
| 1371 | 1553 | | _closedCts.Cancel(); |
| 1371 | 1554 | | _acceptStreamChannel.Writer.TryComplete(exception); |
| | 1555 | |
|
| | 1556 | | // Close streams. |
| 6737 | 1557 | | foreach (SlicStream stream in _streams.Values) |
| 1312 | 1558 | | { |
| 1312 | 1559 | | stream.Close(exception); |
| 1312 | 1560 | | } |
| | 1561 | |
|
| 1371 | 1562 | | return true; |
| 2208 | 1563 | | } |
| | 1564 | |
|
| | 1565 | | private void WriteFrame(FrameType frameType, ulong? streamId, EncodeAction? encode) |
| 8176 | 1566 | | { |
| 8176 | 1567 | | var encoder = new SliceEncoder(_duplexConnectionWriter, SliceEncoding.Slice2); |
| 8176 | 1568 | | encoder.EncodeFrameType(frameType); |
| 8176 | 1569 | | Span<byte> sizePlaceholder = encoder.GetPlaceholderSpan(4); |
| 8176 | 1570 | | int startPos = encoder.EncodedByteCount; |
| 8176 | 1571 | | if (streamId is not null) |
| 6697 | 1572 | | { |
| 6697 | 1573 | | encoder.EncodeVarUInt62(streamId.Value); |
| 6697 | 1574 | | } |
| 8176 | 1575 | | encode?.Invoke(ref encoder); |
| 8176 | 1576 | | SliceEncoder.EncodeVarUInt62((ulong)(encoder.EncodedByteCount - startPos), sizePlaceholder); |
| 8176 | 1577 | | } |
| | 1578 | | } |