| | | 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.Codec; |
| | | 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> |
| | 17323 | 20 | | internal bool IsServer { get; } |
| | | 21 | | |
| | | 22 | | /// <summary>Gets the minimum size of the segment requested from <see cref="Pool" />.</summary> |
| | 5708 | 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> |
| | 3504 | 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> |
| | 9900 | 33 | | internal int PeerMaxStreamFrameSize { get; private set; } |
| | | 34 | | |
| | | 35 | | /// <summary>Gets the <see cref="MemoryPool{T}" /> used for obtaining memory buffers.</summary> |
| | 5708 | 36 | | internal MemoryPool<byte> Pool { get; } |
| | | 37 | | |
| | | 38 | | /// <summary>Gets the initial stream window size.</summary> |
| | 10965 | 39 | | internal int InitialStreamWindowSize { get; } |
| | | 40 | | |
| | | 41 | | /// <summary>Gets the pause writer threshold for the connection's outbound pipe.</summary> |
| | 769 | 42 | | internal int PauseWriterThreshold { get; } |
| | | 43 | | |
| | | 44 | | /// <summary>Gets the window update threshold. When the window size is increased and this threshold reached, a <see |
| | | 45 | | /// cref="FrameType.StreamWindowUpdate" /> frame is sent.</summary> |
| | 7447 | 46 | | internal int StreamWindowUpdateThreshold => InitialStreamWindowSize / StreamWindowUpdateRatio; |
| | | 47 | | |
| | | 48 | | // The maximum body size for non-stream frames (Initialize, InitializeAck, Version, Close, Ping, Pong). This |
| | | 49 | | // value is the maximum value that can be encoded as a 2-byte varuint62, which allows WriteFrame to use a 2-byte |
| | | 50 | | // size placeholder. Stream data frames are not subject to this limit; they are gated by per-stream flow control. |
| | | 51 | | private const int MaxControlFrameBodySize = 16_383; |
| | | 52 | | |
| | | 53 | | // The ratio used to compute the StreamWindowUpdateThreshold. For now, the stream window update is sent when the |
| | | 54 | | // window size grows over InitialStreamWindowSize / StreamWindowUpdateRatio. |
| | | 55 | | private const int StreamWindowUpdateRatio = 2; |
| | | 56 | | |
| | | 57 | | private readonly Channel<IMultiplexedStream> _acceptStreamChannel; |
| | | 58 | | private int _bidirectionalStreamCount; |
| | | 59 | | private SemaphoreSlim? _bidirectionalStreamSemaphore; |
| | | 60 | | private readonly CancellationToken _closedCancellationToken; |
| | 769 | 61 | | private readonly CancellationTokenSource _closedCts = new(); |
| | | 62 | | private string? _closedMessage; |
| | | 63 | | private Task<TransportConnectionInformation>? _connectTask; |
| | 769 | 64 | | private readonly CancellationTokenSource _disposedCts = new(); |
| | | 65 | | private Task? _disposeTask; |
| | | 66 | | private readonly SlicDuplexConnectionDecorator _duplexConnection; |
| | | 67 | | private readonly DuplexConnectionReader _duplexConnectionReader; |
| | | 68 | | private readonly SlicDuplexConnectionWriter _duplexConnectionWriter; |
| | | 69 | | |
| | | 70 | | // Invariant: _isClosed only ever transitions false -> true (under _mutex, by TryClose). Every writer site |
| | | 71 | | // (WriteConnectionFrameAsync, WriteStreamFrame, WriteStreamDataFrameAsync, CloseAsync) re-checks _isClosed under |
| | | 72 | | // _mutex *after* acquiring _writeSemaphore, so it bails out before issuing any new Write/WriteFrame on |
| | | 73 | | // _duplexConnectionWriter once _isClosed has been observed true. |
| | | 74 | | private bool _isClosed; |
| | | 75 | | private ulong? _lastRemoteBidirectionalStreamId; |
| | | 76 | | private ulong? _lastRemoteUnidirectionalStreamId; |
| | | 77 | | private readonly TimeSpan _localIdleTimeout; |
| | | 78 | | private readonly int _maxBidirectionalStreams; |
| | | 79 | | private readonly int _maxOutstandingPongs; |
| | | 80 | | private readonly int _maxStreamFrameSize; |
| | | 81 | | private readonly int _maxUnidirectionalStreams; |
| | | 82 | | // _mutex ensure the assignment of _lastRemoteXxx members and the addition of the stream to _streams is |
| | | 83 | | // an atomic operation. |
| | 769 | 84 | | private readonly Lock _mutex = new(); |
| | | 85 | | private ulong _nextBidirectionalId; |
| | | 86 | | private ulong _nextUnidirectionalId; |
| | | 87 | | |
| | | 88 | | // The number of Pong frames queued for sending (in response to Ping frames) but not yet written to the duplex |
| | | 89 | | // connection. The connection is aborted when a Ping frame is received while this count has reached |
| | | 90 | | // _maxOutstandingPongs. |
| | | 91 | | private int _outstandingPongCount; |
| | | 92 | | private IceRpcError? _peerCloseError; |
| | 769 | 93 | | private TimeSpan _peerIdleTimeout = Timeout.InfiniteTimeSpan; |
| | | 94 | | |
| | | 95 | | // The number of Ping frames sent to the peer that have not been answered yet by a Pong frame. |
| | | 96 | | private int _pendingPongCount; |
| | | 97 | | private Task? _readFramesTask; |
| | | 98 | | |
| | 769 | 99 | | private readonly ConcurrentDictionary<ulong, SlicStream> _streams = new(); |
| | | 100 | | private int _streamSemaphoreWaitCount; |
| | 769 | 101 | | private readonly TaskCompletionSource _streamSemaphoreWaitClosed = |
| | 769 | 102 | | new(TaskCreationOptions.RunContinuationsAsynchronously); |
| | | 103 | | |
| | | 104 | | private int _unidirectionalStreamCount; |
| | | 105 | | private SemaphoreSlim? _unidirectionalStreamSemaphore; |
| | | 106 | | |
| | | 107 | | // Serializes writes to _duplexConnectionWriter so that frame bytes are appended to the outbound pipe in order and |
| | | 108 | | // the pipe's pauseWriterThreshold is observed strictly. This async lock is held across the FlushAsync call, so a |
| | | 109 | | // single parked flush blocks all other connection writers until the background writer task drains enough data. |
| | | 110 | | // Not disposed: background fire-and-forget writes (e.g. StreamWindowUpdate from sync code paths) may attempt to |
| | | 111 | | // acquire it after DisposeAsync, and we don't want to have to handle ObjectDisposedException at every call site. |
| | | 112 | | // Skipping Dispose is harmless here because we never access SemaphoreSlim.AvailableWaitHandle, so no unmanaged |
| | | 113 | | // wait handle is ever allocated. |
| | | 114 | | #pragma warning disable CA2213 |
| | 769 | 115 | | private readonly SemaphoreSlim _writeSemaphore = new(1, 1); |
| | | 116 | | #pragma warning restore CA2213 |
| | | 117 | | |
| | | 118 | | // This is only set for server connections to ensure that _duplexConnectionWriter.Write is not called after |
| | | 119 | | // _duplexConnectionWriter.Shutdown. This can occur if the client-side of the connection sends the close frame |
| | | 120 | | // followed by the shutdown of the duplex connection and if CloseAsync is called at the same time on the server |
| | | 121 | | // connection. Guarded by _writeSemaphore. |
| | | 122 | | private bool _writerIsShutdown; |
| | | 123 | | |
| | | 124 | | public async ValueTask<IMultiplexedStream> AcceptStreamAsync(CancellationToken cancellationToken) |
| | 2463 | 125 | | { |
| | | 126 | | lock (_mutex) |
| | 2463 | 127 | | { |
| | 2463 | 128 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | | 129 | | |
| | 2462 | 130 | | if (_connectTask is null || !_connectTask.IsCompletedSuccessfully) |
| | 1 | 131 | | { |
| | 1 | 132 | | throw new InvalidOperationException("Cannot accept stream before connecting the Slic connection."); |
| | | 133 | | } |
| | 2461 | 134 | | if (_isClosed) |
| | 13 | 135 | | { |
| | 13 | 136 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | | 137 | | } |
| | 2448 | 138 | | } |
| | | 139 | | |
| | | 140 | | try |
| | 2448 | 141 | | { |
| | 2448 | 142 | | return await _acceptStreamChannel.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); |
| | | 143 | | } |
| | 118 | 144 | | catch (ChannelClosedException exception) |
| | 118 | 145 | | { |
| | 118 | 146 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | 117 | 147 | | Debug.Assert(exception.InnerException is not null); |
| | | 148 | | // The exception given to ChannelWriter.Complete(Exception? exception) is the InnerException. |
| | 117 | 149 | | throw ExceptionUtil.Throw(exception.InnerException); |
| | | 150 | | } |
| | 2082 | 151 | | } |
| | | 152 | | |
| | | 153 | | public Task<TransportConnectionInformation> ConnectAsync(CancellationToken cancellationToken) |
| | 750 | 154 | | { |
| | | 155 | | lock (_mutex) |
| | 750 | 156 | | { |
| | 750 | 157 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | | 158 | | |
| | 750 | 159 | | if (_connectTask is not null) |
| | 1 | 160 | | { |
| | 1 | 161 | | throw new InvalidOperationException("Cannot connect twice a Slic connection."); |
| | | 162 | | } |
| | 749 | 163 | | if (_isClosed) |
| | 0 | 164 | | { |
| | 0 | 165 | | throw new InvalidOperationException("Cannot connect a closed Slic connection."); |
| | | 166 | | } |
| | 749 | 167 | | _connectTask = PerformConnectAsync(); |
| | 749 | 168 | | } |
| | 749 | 169 | | return _connectTask; |
| | | 170 | | |
| | | 171 | | async Task<TransportConnectionInformation> PerformConnectAsync() |
| | 749 | 172 | | { |
| | 749 | 173 | | await Task.Yield(); // Exit mutex lock |
| | | 174 | | |
| | | 175 | | // Connect the duplex connection. |
| | | 176 | | TransportConnectionInformation transportConnectionInformation; |
| | 749 | 177 | | TimeSpan peerIdleTimeout = TimeSpan.MaxValue; |
| | | 178 | | |
| | | 179 | | try |
| | 749 | 180 | | { |
| | 749 | 181 | | transportConnectionInformation = await _duplexConnection.ConnectAsync(cancellationToken) |
| | 749 | 182 | | .ConfigureAwait(false); |
| | | 183 | | |
| | | 184 | | // Initialize the Slic connection. |
| | 727 | 185 | | if (IsServer) |
| | 370 | 186 | | { |
| | | 187 | | // Read the Initialize frame. |
| | 370 | 188 | | (ulong version, InitializeBody? initializeBody) = await ReadFrameAsync( |
| | 370 | 189 | | DecodeInitialize, |
| | 370 | 190 | | cancellationToken).ConfigureAwait(false); |
| | | 191 | | |
| | 363 | 192 | | if (initializeBody is null) |
| | 2 | 193 | | { |
| | | 194 | | // Unsupported version, try to negotiate another version by sending a Version frame with the |
| | | 195 | | // Slic versions supported by this server. |
| | 2 | 196 | | ulong[] supportedVersions = new ulong[] { SlicDefinitions.V1 }; |
| | | 197 | | |
| | 2 | 198 | | await WriteConnectionFrameAsync( |
| | 2 | 199 | | FrameType.Version, |
| | 2 | 200 | | new VersionBody(supportedVersions).Encode, |
| | 2 | 201 | | cancellationToken).ConfigureAwait(false); |
| | | 202 | | |
| | 2 | 203 | | (version, initializeBody) = await ReadFrameAsync( |
| | 2 | 204 | | (frameType, buffer) => |
| | 2 | 205 | | { |
| | 2 | 206 | | if (frameType is null) |
| | 1 | 207 | | { |
| | 2 | 208 | | // The client shut down the connection because it doesn't support any of the |
| | 2 | 209 | | // server's supported Slic versions. |
| | 1 | 210 | | throw new IceRpcException( |
| | 1 | 211 | | IceRpcError.ConnectionRefused, |
| | 1 | 212 | | $"The connection was refused because the client Slic version {version} is not su |
| | 2 | 213 | | } |
| | 2 | 214 | | else |
| | 1 | 215 | | { |
| | 1 | 216 | | return DecodeInitialize(frameType, buffer); |
| | 2 | 217 | | } |
| | 1 | 218 | | }, |
| | 2 | 219 | | cancellationToken).ConfigureAwait(false); |
| | 1 | 220 | | } |
| | | 221 | | |
| | 362 | 222 | | Debug.Assert(initializeBody is not null); |
| | | 223 | | |
| | 362 | 224 | | DecodeParameters(initializeBody.Value.Parameters); |
| | | 225 | | |
| | | 226 | | // Write back an InitializeAck frame. |
| | 361 | 227 | | await WriteConnectionFrameAsync( |
| | 361 | 228 | | FrameType.InitializeAck, |
| | 361 | 229 | | new InitializeAckBody(EncodeParameters()).Encode, |
| | 361 | 230 | | cancellationToken).ConfigureAwait(false); |
| | 361 | 231 | | } |
| | | 232 | | else |
| | 357 | 233 | | { |
| | | 234 | | // Write the Initialize frame. |
| | 357 | 235 | | await WriteConnectionFrameAsync( |
| | 357 | 236 | | FrameType.Initialize, |
| | 357 | 237 | | (ref SliceEncoder encoder) => |
| | 357 | 238 | | { |
| | 357 | 239 | | encoder.EncodeVarUInt62(SlicDefinitions.V1); |
| | 357 | 240 | | new InitializeBody(EncodeParameters()).Encode(ref encoder); |
| | 357 | 241 | | }, |
| | 357 | 242 | | cancellationToken).ConfigureAwait(false); |
| | | 243 | | |
| | | 244 | | // Read and decode the InitializeAck or Version frame. |
| | 357 | 245 | | (InitializeAckBody? initializeAckBody, VersionBody? versionBody) = await ReadFrameAsync( |
| | 357 | 246 | | DecodeInitializeAckOrVersion, |
| | 357 | 247 | | cancellationToken).ConfigureAwait(false); |
| | | 248 | | |
| | 333 | 249 | | Debug.Assert(initializeAckBody is not null || versionBody is not null); |
| | | 250 | | |
| | 333 | 251 | | if (initializeAckBody is not null) |
| | 331 | 252 | | { |
| | 331 | 253 | | DecodeParameters(initializeAckBody.Value.Parameters); |
| | 331 | 254 | | } |
| | | 255 | | |
| | 333 | 256 | | if (versionBody is not null) |
| | 2 | 257 | | { |
| | 2 | 258 | | if (versionBody.Value.Versions.Contains(SlicDefinitions.V1)) |
| | 1 | 259 | | { |
| | 1 | 260 | | throw new InvalidDataException( |
| | 1 | 261 | | "The server supported versions include the version initially requested."); |
| | | 262 | | } |
| | | 263 | | else |
| | 1 | 264 | | { |
| | | 265 | | // We only support V1 and the peer rejected V1. |
| | 1 | 266 | | throw new IceRpcException( |
| | 1 | 267 | | IceRpcError.ConnectionRefused, |
| | 1 | 268 | | $"The connection was refused because the server only supports Slic version(s) {string.Jo |
| | | 269 | | } |
| | | 270 | | } |
| | 331 | 271 | | } |
| | 692 | 272 | | } |
| | 8 | 273 | | catch (InvalidDataException exception) |
| | 8 | 274 | | { |
| | 8 | 275 | | throw new IceRpcException( |
| | 8 | 276 | | IceRpcError.IceRpcError, |
| | 8 | 277 | | "The connection was aborted by a Slic protocol error.", |
| | 8 | 278 | | exception); |
| | | 279 | | } |
| | 25 | 280 | | catch (OperationCanceledException) |
| | 25 | 281 | | { |
| | 25 | 282 | | throw; |
| | | 283 | | } |
| | 4 | 284 | | catch (AuthenticationException) |
| | 4 | 285 | | { |
| | 4 | 286 | | throw; |
| | | 287 | | } |
| | 20 | 288 | | catch (IceRpcException) |
| | 20 | 289 | | { |
| | 20 | 290 | | throw; |
| | | 291 | | } |
| | 0 | 292 | | catch (Exception exception) |
| | 0 | 293 | | { |
| | 0 | 294 | | Debug.Fail($"ConnectAsync failed with an unexpected exception: {exception}"); |
| | 0 | 295 | | throw; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // Enable the idle timeout checks after the connection establishment. The Ping frames sent by the keep alive |
| | | 299 | | // check are not expected until the Slic connection initialization completes. The idle timeout check uses |
| | | 300 | | // the smallest idle timeout. Timeout.InfiniteTimeSpan is -1 ms so we can't compare it directly with |
| | | 301 | | // positive timeouts. |
| | | 302 | | TimeSpan idleTimeout; |
| | 692 | 303 | | if (_localIdleTimeout == Timeout.InfiniteTimeSpan) |
| | 2 | 304 | | { |
| | 2 | 305 | | idleTimeout = _peerIdleTimeout; |
| | 2 | 306 | | } |
| | 690 | 307 | | else if (_peerIdleTimeout == Timeout.InfiniteTimeSpan) |
| | 27 | 308 | | { |
| | 27 | 309 | | idleTimeout = _localIdleTimeout; |
| | 27 | 310 | | } |
| | | 311 | | else |
| | 663 | 312 | | { |
| | 663 | 313 | | idleTimeout = _peerIdleTimeout < _localIdleTimeout ? _peerIdleTimeout : _localIdleTimeout; |
| | 663 | 314 | | } |
| | | 315 | | |
| | 692 | 316 | | if (idleTimeout != Timeout.InfiniteTimeSpan) |
| | 690 | 317 | | { |
| | 690 | 318 | | _duplexConnection.Enable(idleTimeout); |
| | 690 | 319 | | } |
| | | 320 | | |
| | 692 | 321 | | _readFramesTask = ReadFramesAsync(_disposedCts.Token); |
| | | 322 | | |
| | 692 | 323 | | return transportConnectionInformation; |
| | 692 | 324 | | } |
| | | 325 | | |
| | | 326 | | static (ulong, InitializeBody?) DecodeInitialize(FrameType? frameType, ReadOnlySequence<byte> buffer) |
| | 365 | 327 | | { |
| | 365 | 328 | | if (frameType != FrameType.Initialize) |
| | 0 | 329 | | { |
| | 0 | 330 | | throw new InvalidDataException($"Received unexpected {frameType} frame."); |
| | | 331 | | } |
| | | 332 | | |
| | 365 | 333 | | return buffer.DecodeSliceBuffer<(ulong, InitializeBody?)>( |
| | 365 | 334 | | (ref SliceDecoder decoder) => |
| | 365 | 335 | | { |
| | 365 | 336 | | ulong version = decoder.DecodeVarUInt62(); |
| | 364 | 337 | | if (version == SlicDefinitions.V1) |
| | 362 | 338 | | { |
| | 362 | 339 | | return (version, new InitializeBody(ref decoder)); |
| | 365 | 340 | | } |
| | 365 | 341 | | else |
| | 2 | 342 | | { |
| | 2 | 343 | | decoder.Skip((int)(buffer.Length - decoder.Consumed)); |
| | 2 | 344 | | return (version, null); |
| | 365 | 345 | | } |
| | 729 | 346 | | }); |
| | 364 | 347 | | } |
| | | 348 | | |
| | | 349 | | static (InitializeAckBody?, VersionBody?) DecodeInitializeAckOrVersion( |
| | | 350 | | FrameType? frameType, |
| | | 351 | | ReadOnlySequence<byte> buffer) => |
| | 335 | 352 | | frameType switch |
| | 335 | 353 | | { |
| | 332 | 354 | | FrameType.InitializeAck => ( |
| | 332 | 355 | | buffer.DecodeSliceBuffer((ref SliceDecoder decoder) => new InitializeAckBody(ref decoder)), |
| | 332 | 356 | | null), |
| | 3 | 357 | | FrameType.Version => ( |
| | 3 | 358 | | null, |
| | 6 | 359 | | buffer.DecodeSliceBuffer((ref SliceDecoder decoder) => new VersionBody(ref decoder))), |
| | 0 | 360 | | _ => throw new InvalidDataException($"Received unexpected Slic frame: '{frameType}'."), |
| | 335 | 361 | | }; |
| | | 362 | | |
| | | 363 | | async ValueTask<T> ReadFrameAsync<T>( |
| | | 364 | | Func<FrameType?, ReadOnlySequence<byte>, T> decodeFunc, |
| | | 365 | | CancellationToken cancellationToken) |
| | 729 | 366 | | { |
| | 729 | 367 | | (FrameType FrameType, int FrameSize, ulong?)? header = |
| | 729 | 368 | | await ReadFrameHeaderAsync(cancellationToken).ConfigureAwait(false); |
| | | 369 | | |
| | | 370 | | ReadOnlySequence<byte> buffer; |
| | 701 | 371 | | if (header is null || header.Value.FrameSize == 0) |
| | 4 | 372 | | { |
| | 4 | 373 | | buffer = ReadOnlySequence<byte>.Empty; |
| | 4 | 374 | | } |
| | | 375 | | else |
| | 697 | 376 | | { |
| | 697 | 377 | | buffer = await _duplexConnectionReader.ReadAtLeastAsync( |
| | 697 | 378 | | header.Value.FrameSize, |
| | 697 | 379 | | cancellationToken).ConfigureAwait(false); |
| | 697 | 380 | | if (buffer.Length > header.Value.FrameSize) |
| | 0 | 381 | | { |
| | 0 | 382 | | buffer = buffer.Slice(0, header.Value.FrameSize); |
| | 0 | 383 | | } |
| | 697 | 384 | | } |
| | | 385 | | |
| | 701 | 386 | | T decodedFrame = decodeFunc(header?.FrameType, buffer); |
| | 697 | 387 | | _duplexConnectionReader.AdvanceTo(buffer.End); |
| | 697 | 388 | | return decodedFrame; |
| | 697 | 389 | | } |
| | 749 | 390 | | } |
| | | 391 | | |
| | | 392 | | public async Task CloseAsync(MultiplexedConnectionCloseError closeError, CancellationToken cancellationToken) |
| | 110 | 393 | | { |
| | | 394 | | lock (_mutex) |
| | 110 | 395 | | { |
| | 110 | 396 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | | 397 | | |
| | 110 | 398 | | if (_connectTask is null || !_connectTask.IsCompletedSuccessfully) |
| | 1 | 399 | | { |
| | 1 | 400 | | throw new InvalidOperationException("Cannot close a Slic connection before connecting it."); |
| | | 401 | | } |
| | 109 | 402 | | } |
| | | 403 | | |
| | 109 | 404 | | bool waitForWriterShutdown = false; |
| | 109 | 405 | | if (TryClose(new IceRpcException(IceRpcError.OperationAborted), "The connection was closed.")) |
| | 102 | 406 | | { |
| | 102 | 407 | | using (await _writeSemaphore.AcquireAsync(cancellationToken).ConfigureAwait(false)) |
| | 102 | 408 | | { |
| | 102 | 409 | | if (IsServer && _writerIsShutdown) |
| | 0 | 410 | | { |
| | | 411 | | // ReadFramesAsync already shut down the writer because the client-side sent its Close frame and |
| | | 412 | | // shut down the duplex connection. Nothing more to send. The client-side is unaffected: it never |
| | | 413 | | // shuts down the writer from ReadFramesAsync. |
| | 0 | 414 | | } |
| | | 415 | | else |
| | 102 | 416 | | { |
| | 102 | 417 | | WriteFrame(FrameType.Close, streamId: null, new CloseBody((ulong)closeError).Encode); |
| | 102 | 418 | | if (IsServer) |
| | 53 | 419 | | { |
| | | 420 | | // Link with _disposedCts so a concurrent DisposeAsync can break out of a flush parked on |
| | | 421 | | // PauseWriterThreshold. Without the link, server CloseAsync(None) would deadlock with |
| | | 422 | | // DisposeAsync: CloseAsync holds _writeSemaphore across this flush while DisposeAsync waits to |
| | | 423 | | // acquire it before disposing the writer (which is what would otherwise unblock the flush). |
| | 53 | 424 | | using var flushCts = CancellationTokenSource.CreateLinkedTokenSource( |
| | 53 | 425 | | cancellationToken, |
| | 53 | 426 | | _disposedCts.Token); |
| | | 427 | | try |
| | 53 | 428 | | { |
| | 53 | 429 | | await _duplexConnectionWriter.FlushAsync(flushCts.Token).ConfigureAwait(false); |
| | 52 | 430 | | } |
| | 1 | 431 | | catch (OperationCanceledException) when ( |
| | 1 | 432 | | _disposedCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) |
| | 1 | 433 | | { |
| | 1 | 434 | | throw new IceRpcException(IceRpcError.OperationAborted, "The connection was disposed."); |
| | | 435 | | } |
| | 52 | 436 | | } |
| | | 437 | | else |
| | 49 | 438 | | { |
| | | 439 | | // The sending of the client-side Close frame is followed by the shutdown of the duplex |
| | | 440 | | // connection. For TCP, it's important to always shutdown the connection on the client-side firs |
| | | 441 | | // to avoid TIME_WAIT states on the server-side. |
| | 49 | 442 | | _duplexConnectionWriter.Shutdown(); |
| | 49 | 443 | | waitForWriterShutdown = true; |
| | 49 | 444 | | } |
| | 101 | 445 | | } |
| | 101 | 446 | | } |
| | 101 | 447 | | } |
| | | 448 | | |
| | 108 | 449 | | if (waitForWriterShutdown) |
| | 49 | 450 | | { |
| | 49 | 451 | | await _duplexConnectionWriter.WriterTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | 49 | 452 | | } |
| | | 453 | | |
| | | 454 | | // Now, wait for the peer to close the write side of the connection, which will terminate the read frames task. |
| | 108 | 455 | | Debug.Assert(_readFramesTask is not null); |
| | 108 | 456 | | await _readFramesTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | 107 | 457 | | } |
| | | 458 | | |
| | | 459 | | public async ValueTask<IMultiplexedStream> CreateStreamAsync( |
| | | 460 | | bool bidirectional, |
| | | 461 | | CancellationToken cancellationToken) |
| | 2145 | 462 | | { |
| | | 463 | | lock (_mutex) |
| | 2145 | 464 | | { |
| | 2145 | 465 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | | 466 | | |
| | 2142 | 467 | | if (_connectTask is null || !_connectTask.IsCompletedSuccessfully) |
| | 2 | 468 | | { |
| | 2 | 469 | | throw new InvalidOperationException("Cannot create stream before connecting the Slic connection."); |
| | | 470 | | } |
| | 2140 | 471 | | if (_isClosed) |
| | 6 | 472 | | { |
| | 6 | 473 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | | 474 | | } |
| | | 475 | | |
| | 2134 | 476 | | ++_streamSemaphoreWaitCount; |
| | 2134 | 477 | | } |
| | | 478 | | |
| | | 479 | | try |
| | 2134 | 480 | | { |
| | 2134 | 481 | | using var createStreamCts = CancellationTokenSource.CreateLinkedTokenSource( |
| | 2134 | 482 | | _closedCancellationToken, |
| | 2134 | 483 | | cancellationToken); |
| | | 484 | | |
| | 2134 | 485 | | SemaphoreSlim? streamCountSemaphore = bidirectional ? |
| | 2134 | 486 | | _bidirectionalStreamSemaphore : |
| | 2134 | 487 | | _unidirectionalStreamSemaphore; |
| | | 488 | | |
| | 2134 | 489 | | if (streamCountSemaphore is null) |
| | 1 | 490 | | { |
| | | 491 | | // The stream semaphore is null if the peer's max streams configuration is 0. In this case, we let |
| | | 492 | | // CreateStreamAsync hang indefinitely until the connection is closed. |
| | 1 | 493 | | await Task.Delay(-1, createStreamCts.Token).ConfigureAwait(false); |
| | 0 | 494 | | } |
| | | 495 | | else |
| | 2133 | 496 | | { |
| | 2133 | 497 | | await streamCountSemaphore.WaitAsync(createStreamCts.Token).ConfigureAwait(false); |
| | 2122 | 498 | | } |
| | | 499 | | |
| | 2122 | 500 | | return new SlicStream(this, bidirectional, isRemote: false); |
| | | 501 | | } |
| | 12 | 502 | | catch (OperationCanceledException) |
| | 12 | 503 | | { |
| | 12 | 504 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 7 | 505 | | ObjectDisposedException.ThrowIf(_disposeTask is not null, this); |
| | 6 | 506 | | Debug.Assert(_isClosed); |
| | 6 | 507 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.OperationAborted, _closedMessage); |
| | | 508 | | } |
| | | 509 | | finally |
| | 2134 | 510 | | { |
| | | 511 | | lock (_mutex) |
| | 2134 | 512 | | { |
| | 2134 | 513 | | --_streamSemaphoreWaitCount; |
| | 2134 | 514 | | if (_isClosed && _streamSemaphoreWaitCount == 0) |
| | 7 | 515 | | { |
| | 7 | 516 | | _streamSemaphoreWaitClosed.SetResult(); |
| | 7 | 517 | | } |
| | 2134 | 518 | | } |
| | 2134 | 519 | | } |
| | 2122 | 520 | | } |
| | | 521 | | |
| | | 522 | | public ValueTask DisposeAsync() |
| | 1047 | 523 | | { |
| | | 524 | | lock (_mutex) |
| | 1047 | 525 | | { |
| | 1047 | 526 | | _disposeTask ??= PerformDisposeAsync(); |
| | 1047 | 527 | | } |
| | 1047 | 528 | | return new(_disposeTask); |
| | | 529 | | |
| | | 530 | | async Task PerformDisposeAsync() |
| | 768 | 531 | | { |
| | | 532 | | // Make sure we execute the code below without holding the mutex lock. |
| | 768 | 533 | | await Task.Yield(); |
| | 768 | 534 | | TryClose(new IceRpcException(IceRpcError.OperationAborted), "The connection was disposed."); |
| | | 535 | | |
| | 768 | 536 | | _disposedCts.Cancel(); |
| | | 537 | | |
| | | 538 | | try |
| | 768 | 539 | | { |
| | 768 | 540 | | await Task.WhenAll( |
| | 768 | 541 | | _connectTask ?? Task.CompletedTask, |
| | 768 | 542 | | _readFramesTask ?? Task.CompletedTask, |
| | 768 | 543 | | _streamSemaphoreWaitClosed.Task).ConfigureAwait(false); |
| | 428 | 544 | | } |
| | 340 | 545 | | catch |
| | 340 | 546 | | { |
| | | 547 | | // Expected if any of these tasks failed or was canceled. Each task takes care of handling unexpected |
| | | 548 | | // exceptions so there's no need to handle them here. |
| | 340 | 549 | | } |
| | | 550 | | |
| | | 551 | | // Clean-up the streams that might still be queued on the channel. |
| | 792 | 552 | | while (_acceptStreamChannel.Reader.TryRead(out IMultiplexedStream? stream)) |
| | 24 | 553 | | { |
| | 24 | 554 | | if (stream.IsBidirectional) |
| | 5 | 555 | | { |
| | 5 | 556 | | stream.Output.Complete(); |
| | 5 | 557 | | stream.Input.Complete(); |
| | 5 | 558 | | } |
| | 19 | 559 | | else if (stream.IsRemote) |
| | 19 | 560 | | { |
| | 19 | 561 | | stream.Input.Complete(); |
| | 19 | 562 | | } |
| | | 563 | | else |
| | 0 | 564 | | { |
| | 0 | 565 | | stream.Output.Complete(); |
| | 0 | 566 | | } |
| | 24 | 567 | | } |
| | | 568 | | |
| | | 569 | | try |
| | 768 | 570 | | { |
| | | 571 | | // Prevents unobserved task exceptions. |
| | 768 | 572 | | await _acceptStreamChannel.Reader.Completion.ConfigureAwait(false); |
| | 0 | 573 | | } |
| | 768 | 574 | | catch |
| | 768 | 575 | | { |
| | 768 | 576 | | } |
| | | 577 | | |
| | | 578 | | // Acquire (and never release) the write semaphore so no in-flight writer (e.g. a stream frame parked on |
| | | 579 | | // FlushAsync due to PauseWriterThreshold) can race with the writer disposal below. The wait is bounded: |
| | | 580 | | // every writer site uses a cancellation token derived from _closedCancellationToken or _disposedCts.Token, |
| | | 581 | | // both of which are cancelled by the time we reach this point. |
| | 768 | 582 | | await _writeSemaphore.WaitAsync(CancellationToken.None).ConfigureAwait(false); |
| | | 583 | | |
| | 768 | 584 | | await _duplexConnectionWriter.DisposeAsync().ConfigureAwait(false); |
| | 768 | 585 | | _duplexConnectionReader.Dispose(); |
| | 768 | 586 | | _duplexConnection.Dispose(); |
| | | 587 | | |
| | 768 | 588 | | _disposedCts.Dispose(); |
| | 768 | 589 | | _bidirectionalStreamSemaphore?.Dispose(); |
| | 768 | 590 | | _unidirectionalStreamSemaphore?.Dispose(); |
| | 768 | 591 | | _closedCts.Dispose(); |
| | 768 | 592 | | } |
| | 1047 | 593 | | } |
| | | 594 | | |
| | 769 | 595 | | internal SlicConnection( |
| | 769 | 596 | | IDuplexConnection duplexConnection, |
| | 769 | 597 | | MultiplexedConnectionOptions options, |
| | 769 | 598 | | SlicTransportOptions slicOptions, |
| | 769 | 599 | | bool isServer) |
| | 769 | 600 | | { |
| | 769 | 601 | | IsServer = isServer; |
| | | 602 | | |
| | 769 | 603 | | Pool = options.Pool; |
| | 769 | 604 | | MinSegmentSize = options.MinSegmentSize; |
| | 769 | 605 | | _maxBidirectionalStreams = options.MaxBidirectionalStreams; |
| | 769 | 606 | | _maxUnidirectionalStreams = options.MaxUnidirectionalStreams; |
| | | 607 | | |
| | 769 | 608 | | InitialStreamWindowSize = slicOptions.InitialStreamWindowSize; |
| | 769 | 609 | | PauseWriterThreshold = slicOptions.PauseWriterThreshold; |
| | 769 | 610 | | _localIdleTimeout = slicOptions.IdleTimeout; |
| | 769 | 611 | | _maxOutstandingPongs = slicOptions.MaxOutstandingPongs; |
| | 769 | 612 | | _maxStreamFrameSize = slicOptions.MaxStreamFrameSize; |
| | | 613 | | |
| | 769 | 614 | | _acceptStreamChannel = Channel.CreateUnbounded<IMultiplexedStream>(new UnboundedChannelOptions |
| | 769 | 615 | | { |
| | 769 | 616 | | SingleReader = true, |
| | 769 | 617 | | SingleWriter = true |
| | 769 | 618 | | }); |
| | | 619 | | |
| | 769 | 620 | | _closedCancellationToken = _closedCts.Token; |
| | | 621 | | |
| | | 622 | | // Only the client-side sends pings to keep the connection alive when idle timeout (set later) is not infinite. |
| | 769 | 623 | | _duplexConnection = IsServer ? |
| | 769 | 624 | | new SlicDuplexConnectionDecorator(duplexConnection) : |
| | 769 | 625 | | new SlicDuplexConnectionDecorator(duplexConnection, SendReadPing, SendWritePing); |
| | | 626 | | |
| | 769 | 627 | | _duplexConnectionReader = new DuplexConnectionReader(_duplexConnection, options.Pool, options.MinSegmentSize); |
| | 769 | 628 | | _duplexConnectionWriter = new SlicDuplexConnectionWriter( |
| | 769 | 629 | | _duplexConnection, |
| | 769 | 630 | | options.Pool, |
| | 769 | 631 | | options.MinSegmentSize, |
| | 769 | 632 | | PauseWriterThreshold); |
| | | 633 | | |
| | | 634 | | // We use the same stream ID numbering scheme as QUIC. |
| | 769 | 635 | | if (IsServer) |
| | 385 | 636 | | { |
| | 385 | 637 | | _nextBidirectionalId = 1; |
| | 385 | 638 | | _nextUnidirectionalId = 3; |
| | 385 | 639 | | } |
| | | 640 | | else |
| | 384 | 641 | | { |
| | 384 | 642 | | _nextBidirectionalId = 0; |
| | 384 | 643 | | _nextUnidirectionalId = 2; |
| | 384 | 644 | | } |
| | | 645 | | |
| | | 646 | | async Task SendPingAsync(long payload) |
| | 15 | 647 | | { |
| | | 648 | | try |
| | 15 | 649 | | { |
| | 15 | 650 | | await WriteConnectionFrameAsync( |
| | 15 | 651 | | FrameType.Ping, |
| | 15 | 652 | | new PingBody(payload).Encode, |
| | 15 | 653 | | _closedCancellationToken).ConfigureAwait(false); |
| | 15 | 654 | | } |
| | 0 | 655 | | catch (IceRpcException) |
| | 0 | 656 | | { |
| | | 657 | | // Expected if the connection is closed. |
| | 0 | 658 | | } |
| | 0 | 659 | | catch (OperationCanceledException) |
| | 0 | 660 | | { |
| | | 661 | | // Expected if the connection is closed. |
| | 0 | 662 | | } |
| | 0 | 663 | | catch (Exception exception) |
| | 0 | 664 | | { |
| | 0 | 665 | | Debug.Fail($"The sending of a Ping frame failed with an unexpected exception: {exception}"); |
| | 0 | 666 | | } |
| | 15 | 667 | | } |
| | | 668 | | |
| | | 669 | | void SendReadPing() |
| | 15 | 670 | | { |
| | | 671 | | // No-op if there is already a pending Pong. |
| | 15 | 672 | | if (Interlocked.CompareExchange(ref _pendingPongCount, 1, 0) == 0) |
| | 15 | 673 | | { |
| | | 674 | | // Timer callbacks cannot await; fire-and-forget. SendPingAsync swallows expected exceptions and |
| | | 675 | | // Debug.Fails on unexpected ones, so the unobserved task carries no exception. |
| | 15 | 676 | | _ = SendPingAsync(1L); |
| | 15 | 677 | | } |
| | 15 | 678 | | } |
| | | 679 | | |
| | | 680 | | void SendWritePing() |
| | 0 | 681 | | { |
| | | 682 | | // _pendingPongCount can be <= 0 if an unexpected pong is received. If it's the case, the connection is |
| | | 683 | | // being torn down and there's no point in sending a ping frame. |
| | 0 | 684 | | if (Interlocked.Increment(ref _pendingPongCount) > 0) |
| | 0 | 685 | | { |
| | 0 | 686 | | _ = SendPingAsync(0L); |
| | 0 | 687 | | } |
| | 0 | 688 | | } |
| | 769 | 689 | | } |
| | | 690 | | |
| | | 691 | | /// <summary>Fills the given writer with stream data received on the connection.</summary> |
| | | 692 | | /// <param name="bufferWriter">The destination buffer writer.</param> |
| | | 693 | | /// <param name="byteCount">The amount of stream data to read.</param> |
| | | 694 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 695 | | internal ValueTask FillBufferWriterAsync( |
| | | 696 | | IBufferWriter<byte> bufferWriter, |
| | | 697 | | int byteCount, |
| | | 698 | | CancellationToken cancellationToken) => |
| | 8703 | 699 | | _duplexConnectionReader.FillBufferWriterAsync(bufferWriter, byteCount, cancellationToken); |
| | | 700 | | |
| | | 701 | | /// <summary>Releases a stream from the connection. The connection stream count is decremented and if this is a |
| | | 702 | | /// client allow a new stream to be started.</summary> |
| | | 703 | | /// <param name="stream">The released stream.</param> |
| | | 704 | | internal void ReleaseStream(SlicStream stream) |
| | 4225 | 705 | | { |
| | | 706 | | // Only a started stream has an Id and is registered in _streams. |
| | 4225 | 707 | | if (stream.IsStarted) |
| | 4209 | 708 | | { |
| | 4209 | 709 | | _streams.Remove(stream.Id, out SlicStream? _); |
| | 4209 | 710 | | } |
| | | 711 | | |
| | 4225 | 712 | | if (stream.IsRemote) |
| | 2108 | 713 | | { |
| | 2108 | 714 | | if (stream.IsBidirectional) |
| | 688 | 715 | | { |
| | 688 | 716 | | Interlocked.Decrement(ref _bidirectionalStreamCount); |
| | 688 | 717 | | } |
| | | 718 | | else |
| | 1420 | 719 | | { |
| | 1420 | 720 | | Interlocked.Decrement(ref _unidirectionalStreamCount); |
| | 1420 | 721 | | } |
| | 2108 | 722 | | } |
| | 2117 | 723 | | else if (!_isClosed) |
| | 1743 | 724 | | { |
| | 1743 | 725 | | if (stream.IsBidirectional) |
| | 633 | 726 | | { |
| | 633 | 727 | | _bidirectionalStreamSemaphore!.Release(); |
| | 633 | 728 | | } |
| | | 729 | | else |
| | 1110 | 730 | | { |
| | 1110 | 731 | | _unidirectionalStreamSemaphore!.Release(); |
| | 1110 | 732 | | } |
| | 1743 | 733 | | } |
| | 4225 | 734 | | } |
| | | 735 | | |
| | | 736 | | /// <summary>Throws the connection closure exception if the connection is already closed.</summary> |
| | | 737 | | internal void ThrowIfClosed() |
| | 7984 | 738 | | { |
| | | 739 | | lock (_mutex) |
| | 7984 | 740 | | { |
| | 7984 | 741 | | if (_isClosed) |
| | 8 | 742 | | { |
| | 8 | 743 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | | 744 | | } |
| | 7976 | 745 | | } |
| | 7976 | 746 | | } |
| | | 747 | | |
| | | 748 | | /// <summary>Writes a connection frame.</summary> |
| | | 749 | | /// <param name="frameType">The frame type.</param> |
| | | 750 | | /// <param name="encode">The action to encode the frame.</param> |
| | | 751 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 752 | | internal async ValueTask WriteConnectionFrameAsync( |
| | | 753 | | FrameType frameType, |
| | | 754 | | EncodeAction? encode, |
| | | 755 | | CancellationToken cancellationToken) |
| | 755 | 756 | | { |
| | 755 | 757 | | Debug.Assert(frameType < FrameType.Stream); |
| | | 758 | | |
| | 755 | 759 | | using (await _writeSemaphore.AcquireAsync(cancellationToken).ConfigureAwait(false)) |
| | 753 | 760 | | { |
| | | 761 | | lock (_mutex) |
| | 753 | 762 | | { |
| | 753 | 763 | | if (_isClosed) |
| | 2 | 764 | | { |
| | 2 | 765 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | | 766 | | } |
| | 751 | 767 | | } |
| | 751 | 768 | | WriteFrame(frameType, streamId: null, encode); |
| | 751 | 769 | | await _duplexConnectionWriter.FlushAsync(cancellationToken).ConfigureAwait(false); |
| | 751 | 770 | | } |
| | 751 | 771 | | } |
| | | 772 | | |
| | | 773 | | /// <summary>Writes a stream frame as a fire-and-forget operation. Used by sync code paths (e.g. |
| | | 774 | | /// <see cref="SlicPipeReader.Complete"/>, <see cref="SlicPipeWriter.Complete"/>, window updates) that cannot |
| | | 775 | | /// await.</summary> |
| | | 776 | | /// <param name="stream">The stream to write the frame for.</param> |
| | | 777 | | /// <param name="frameType">The frame type.</param> |
| | | 778 | | /// <param name="encode">The action to encode the frame.</param> |
| | | 779 | | /// <param name="writeReadsClosedFrame"><see langword="true" /> if a <see cref="FrameType.StreamReadsClosed" /> |
| | | 780 | | /// frame should be written after the stream frame.</param> |
| | | 781 | | /// <remarks>This method is called by streams and might be called on a closed connection. The connection might |
| | | 782 | | /// also be closed concurrently while it's in progress.</remarks> |
| | | 783 | | internal void WriteStreamFrame( |
| | | 784 | | SlicStream stream, |
| | | 785 | | FrameType frameType, |
| | | 786 | | EncodeAction? encode, |
| | | 787 | | bool writeReadsClosedFrame) |
| | 3249 | 788 | | { |
| | | 789 | | // Ensure that this method is called for any FrameType.StreamXxx frame type except FrameType.Stream. |
| | 3249 | 790 | | Debug.Assert(frameType >= FrameType.StreamLast && stream.IsStarted); |
| | | 791 | | |
| | | 792 | | // SemaphoreSlim.WaitAsync atomically updates the semaphore state (acquires it or enqueues the waiter) |
| | | 793 | | // synchronously, before the await can yield. Two sequential calls from the same thread therefore enqueue |
| | | 794 | | // in call order, preserving wire ordering of the resulting frames. |
| | 3249 | 795 | | _ = WriteStreamFrameAsync(); |
| | | 796 | | |
| | | 797 | | async Task WriteStreamFrameAsync() |
| | 3249 | 798 | | { |
| | | 799 | | SemaphoreLock semaphoreLock; |
| | | 800 | | try |
| | 3249 | 801 | | { |
| | 3249 | 802 | | semaphoreLock = await _writeSemaphore.AcquireAsync(_closedCancellationToken).ConfigureAwait(false); |
| | 3247 | 803 | | } |
| | 2 | 804 | | catch (OperationCanceledException) |
| | 2 | 805 | | { |
| | | 806 | | // The connection was closed while waiting for the semaphore. |
| | 2 | 807 | | return; |
| | | 808 | | } |
| | | 809 | | |
| | 3247 | 810 | | using (semaphoreLock) |
| | 3247 | 811 | | { |
| | | 812 | | lock (_mutex) |
| | 3247 | 813 | | { |
| | 3247 | 814 | | if (_isClosed) |
| | 0 | 815 | | { |
| | 0 | 816 | | return; |
| | | 817 | | } |
| | 3247 | 818 | | } |
| | | 819 | | |
| | 3247 | 820 | | WriteFrame(frameType, stream.Id, encode); |
| | 3247 | 821 | | if (writeReadsClosedFrame) |
| | 101 | 822 | | { |
| | 101 | 823 | | WriteFrame(FrameType.StreamReadsClosed, stream.Id, encode: null); |
| | 101 | 824 | | } |
| | 3247 | 825 | | if (frameType == FrameType.StreamLast) |
| | 588 | 826 | | { |
| | | 827 | | // Notify the stream that the last stream frame is considered sent at this point. This will |
| | | 828 | | // close writes on the stream and allow the stream to be released if reads are also closed. |
| | 588 | 829 | | stream.WroteLastStreamFrame(); |
| | 588 | 830 | | } |
| | | 831 | | |
| | | 832 | | try |
| | 3247 | 833 | | { |
| | 3247 | 834 | | await _duplexConnectionWriter.FlushAsync(_closedCancellationToken).ConfigureAwait(false); |
| | 3242 | 835 | | } |
| | 4 | 836 | | catch (OperationCanceledException) |
| | 4 | 837 | | { |
| | | 838 | | // The connection was closed while flushing. |
| | 4 | 839 | | } |
| | 0 | 840 | | catch (InvalidOperationException) |
| | 0 | 841 | | { |
| | | 842 | | // The pipe writer was completed (Shutdown called) — connection is going away. |
| | 0 | 843 | | } |
| | 1 | 844 | | catch (IceRpcException exception) |
| | 1 | 845 | | { |
| | | 846 | | // The duplex connection write failed. Since this fire-and-forget task has no caller to observe |
| | | 847 | | // the failure, close the connection. |
| | 1 | 848 | | TryClose(exception, "The connection was lost.", IceRpcError.ConnectionAborted); |
| | 1 | 849 | | } |
| | 0 | 850 | | catch (Exception exception) |
| | 0 | 851 | | { |
| | | 852 | | // A duplex connection is only expected to fail a write with an IceRpcException. Rethrow to |
| | | 853 | | // generate an unobserved task exception. |
| | 0 | 854 | | Debug.Fail($"The stream frame write failed with an unexpected exception: {exception}"); |
| | 0 | 855 | | throw; |
| | | 856 | | } |
| | 3247 | 857 | | } |
| | 3249 | 858 | | } |
| | 3249 | 859 | | } |
| | | 860 | | |
| | | 861 | | /// <summary>Writes a stream data frame.</summary> |
| | | 862 | | /// <param name="stream">The stream to write the frame for.</param> |
| | | 863 | | /// <param name="source1">The first stream frame data source.</param> |
| | | 864 | | /// <param name="source2">The second stream frame data source.</param> |
| | | 865 | | /// <param name="endStream"><see langword="true" /> to write a <see cref="FrameType.StreamLast" /> frame and |
| | | 866 | | /// <see langword="false" /> to write a <see cref="FrameType.Stream" /> frame.</param> |
| | | 867 | | /// <param name="writeReadsClosedFrame"><see langword="true" /> if a <see cref="FrameType.StreamReadsClosed" /> |
| | | 868 | | /// frame should be written after the stream frame.</param> |
| | | 869 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 870 | | /// <remarks>This method is called by streams and might be called on a closed connection. The connection might |
| | | 871 | | /// also be closed concurrently while it's in progress.</remarks> |
| | | 872 | | internal async ValueTask<FlushResult> WriteStreamDataFrameAsync( |
| | | 873 | | SlicStream stream, |
| | | 874 | | ReadOnlySequence<byte> source1, |
| | | 875 | | ReadOnlySequence<byte> source2, |
| | | 876 | | bool endStream, |
| | | 877 | | bool writeReadsClosedFrame, |
| | | 878 | | CancellationToken cancellationToken) |
| | 7956 | 879 | | { |
| | 7956 | 880 | | Debug.Assert(!source1.IsEmpty || endStream); |
| | | 881 | | |
| | 7956 | 882 | | if (_connectTask is null) |
| | 0 | 883 | | { |
| | 0 | 884 | | throw new InvalidOperationException("Cannot send a stream frame before calling ConnectAsync."); |
| | | 885 | | } |
| | | 886 | | |
| | 7956 | 887 | | using var writeCts = CancellationTokenSource.CreateLinkedTokenSource( |
| | 7956 | 888 | | _closedCancellationToken, |
| | 7956 | 889 | | cancellationToken); |
| | | 890 | | |
| | | 891 | | try |
| | 7956 | 892 | | { |
| | | 893 | | do |
| | 9239 | 894 | | { |
| | | 895 | | // Next, ensure send credit is available. If not, this will block until the receiver allows sending |
| | | 896 | | // additional data. |
| | 9239 | 897 | | int sendCredit = 0; |
| | 9239 | 898 | | if (!source1.IsEmpty || !source2.IsEmpty) |
| | 9234 | 899 | | { |
| | 9234 | 900 | | sendCredit = await stream.AcquireSendCreditAsync(writeCts.Token).ConfigureAwait(false); |
| | 9201 | 901 | | Debug.Assert(sendCredit > 0); |
| | 9201 | 902 | | } |
| | | 903 | | |
| | | 904 | | // Gather data from source1 or source2 up to sendCredit bytes or the peer maximum stream frame size. |
| | 9206 | 905 | | int sendMaxSize = Math.Min(sendCredit, PeerMaxStreamFrameSize); |
| | | 906 | | ReadOnlySequence<byte> sendSource1; |
| | | 907 | | ReadOnlySequence<byte> sendSource2; |
| | 9206 | 908 | | if (!source1.IsEmpty) |
| | 8220 | 909 | | { |
| | 8220 | 910 | | int length = Math.Min((int)source1.Length, sendMaxSize); |
| | 8220 | 911 | | sendSource1 = source1.Slice(0, length); |
| | 8220 | 912 | | source1 = source1.Slice(length); |
| | 8220 | 913 | | } |
| | | 914 | | else |
| | 986 | 915 | | { |
| | 986 | 916 | | sendSource1 = ReadOnlySequence<byte>.Empty; |
| | 986 | 917 | | } |
| | | 918 | | |
| | 9206 | 919 | | if (source1.IsEmpty && !source2.IsEmpty) |
| | 2040 | 920 | | { |
| | 2040 | 921 | | int length = Math.Min((int)source2.Length, sendMaxSize - (int)sendSource1.Length); |
| | 2040 | 922 | | sendSource2 = source2.Slice(0, length); |
| | 2040 | 923 | | source2 = source2.Slice(length); |
| | 2040 | 924 | | } |
| | | 925 | | else |
| | 7166 | 926 | | { |
| | 7166 | 927 | | sendSource2 = ReadOnlySequence<byte>.Empty; |
| | 7166 | 928 | | } |
| | | 929 | | |
| | | 930 | | // If there's no data left to send and endStream is true, it's the last stream frame. |
| | 9206 | 931 | | bool lastStreamFrame = endStream && source1.IsEmpty && source2.IsEmpty; |
| | | 932 | | |
| | 9206 | 933 | | using (await _writeSemaphore.AcquireAsync(writeCts.Token).ConfigureAwait(false)) |
| | 8230 | 934 | | { |
| | | 935 | | lock (_mutex) |
| | 8230 | 936 | | { |
| | 8230 | 937 | | if (_isClosed) |
| | 0 | 938 | | { |
| | 0 | 939 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | | 940 | | } |
| | 8230 | 941 | | } |
| | | 942 | | |
| | 8230 | 943 | | if (!stream.IsStarted) |
| | 2103 | 944 | | { |
| | 2103 | 945 | | if (stream.IsBidirectional) |
| | 682 | 946 | | { |
| | 682 | 947 | | AddStream(_nextBidirectionalId, stream); |
| | 682 | 948 | | _nextBidirectionalId += 4; |
| | 682 | 949 | | } |
| | | 950 | | else |
| | 1421 | 951 | | { |
| | 1421 | 952 | | AddStream(_nextUnidirectionalId, stream); |
| | 1421 | 953 | | _nextUnidirectionalId += 4; |
| | 1421 | 954 | | } |
| | 2103 | 955 | | } |
| | | 956 | | |
| | | 957 | | // Notify the stream that we're consuming sendSize credit. It's important to call this before |
| | | 958 | | // sending the stream frame to avoid race conditions where the StreamWindowUpdate frame could |
| | | 959 | | // be received before the send credit was updated. |
| | 8230 | 960 | | if (sendCredit > 0) |
| | 8225 | 961 | | { |
| | 8225 | 962 | | stream.ConsumedSendCredit((int)(sendSource1.Length + sendSource2.Length)); |
| | 8225 | 963 | | } |
| | | 964 | | |
| | 8230 | 965 | | EncodeStreamFrameHeader(stream.Id, sendSource1.Length + sendSource2.Length, lastStreamFrame); |
| | | 966 | | |
| | 8230 | 967 | | if (lastStreamFrame) |
| | 802 | 968 | | { |
| | | 969 | | // Notify the stream that the last stream frame is considered sent at this point. This |
| | | 970 | | // will complete writes on the stream and allow the stream to be released if reads are |
| | | 971 | | // also completed. |
| | 802 | 972 | | stream.WroteLastStreamFrame(); |
| | 802 | 973 | | } |
| | | 974 | | |
| | | 975 | | // Write the stream frame. |
| | 8230 | 976 | | if (!sendSource1.IsEmpty) |
| | 8220 | 977 | | { |
| | 8220 | 978 | | _duplexConnectionWriter.Write(sendSource1); |
| | 8220 | 979 | | } |
| | 8230 | 980 | | if (!sendSource2.IsEmpty) |
| | 1064 | 981 | | { |
| | 1064 | 982 | | _duplexConnectionWriter.Write(sendSource2); |
| | 1064 | 983 | | } |
| | | 984 | | |
| | 8230 | 985 | | if (writeReadsClosedFrame) |
| | 381 | 986 | | { |
| | 381 | 987 | | WriteFrame(FrameType.StreamReadsClosed, stream.Id, encode: null); |
| | 381 | 988 | | } |
| | | 989 | | |
| | | 990 | | // Flush the stream frame. This may block if the outbound pipe's pauseWriterThreshold has been |
| | | 991 | | // reached — the connection's write semaphore is held during the await, so all other connection |
| | | 992 | | // writers wait until the background writer task drains enough data. |
| | 8230 | 993 | | await _duplexConnectionWriter.FlushAsync(writeCts.Token).ConfigureAwait(false); |
| | 8221 | 994 | | } |
| | 8221 | 995 | | } |
| | 8221 | 996 | | while (!source1.IsEmpty || !source2.IsEmpty); // Loop until there's no data left to send. |
| | 6938 | 997 | | } |
| | 1018 | 998 | | catch (OperationCanceledException) |
| | 1018 | 999 | | { |
| | 1018 | 1000 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 1001 | | |
| | 0 | 1002 | | Debug.Assert(_isClosed); |
| | 0 | 1003 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.OperationAborted, _closedMessage); |
| | | 1004 | | } |
| | | 1005 | | |
| | 6938 | 1006 | | return new FlushResult(isCanceled: false, isCompleted: false); |
| | | 1007 | | |
| | | 1008 | | void EncodeStreamFrameHeader(ulong streamId, long size, bool lastStreamFrame) |
| | 8230 | 1009 | | { |
| | 8230 | 1010 | | var encoder = new SliceEncoder(_duplexConnectionWriter); |
| | 8230 | 1011 | | encoder.EncodeFrameType(!lastStreamFrame ? FrameType.Stream : FrameType.StreamLast); |
| | 8230 | 1012 | | Span<byte> sizePlaceholder = encoder.GetPlaceholderSpan(4); |
| | 8230 | 1013 | | int startPos = encoder.EncodedByteCount; |
| | 8230 | 1014 | | encoder.EncodeVarUInt62(streamId); |
| | 8230 | 1015 | | SliceEncoder.EncodeVarUInt62((ulong)(encoder.EncodedByteCount - startPos + size), sizePlaceholder); |
| | 8230 | 1016 | | } |
| | 6938 | 1017 | | } |
| | | 1018 | | |
| | | 1019 | | private void AddStream(ulong id, SlicStream stream) |
| | 4211 | 1020 | | { |
| | | 1021 | | lock (_mutex) |
| | 4211 | 1022 | | { |
| | 4211 | 1023 | | if (_isClosed) |
| | 2 | 1024 | | { |
| | 2 | 1025 | | throw new IceRpcException(_peerCloseError ?? IceRpcError.ConnectionAborted, _closedMessage); |
| | | 1026 | | } |
| | | 1027 | | |
| | 4209 | 1028 | | _streams[id] = stream; |
| | | 1029 | | |
| | | 1030 | | // Assign the stream ID within the mutex to ensure that the addition of the stream to the connection and the |
| | | 1031 | | // stream ID assignment are atomic. |
| | 4209 | 1032 | | stream.Id = id; |
| | | 1033 | | |
| | | 1034 | | // Keep track of the last assigned stream ID. This is used to figure out if the stream is known or unknown. |
| | 4209 | 1035 | | if (stream.IsRemote) |
| | 2106 | 1036 | | { |
| | 2106 | 1037 | | if (stream.IsBidirectional) |
| | 687 | 1038 | | { |
| | 687 | 1039 | | _lastRemoteBidirectionalStreamId = id; |
| | 687 | 1040 | | } |
| | | 1041 | | else |
| | 1419 | 1042 | | { |
| | 1419 | 1043 | | _lastRemoteUnidirectionalStreamId = id; |
| | 1419 | 1044 | | } |
| | 2106 | 1045 | | } |
| | 4209 | 1046 | | } |
| | 4209 | 1047 | | } |
| | | 1048 | | |
| | | 1049 | | private void DecodeParameters(IDictionary<ParameterKey, IList<byte>> parameters) |
| | 693 | 1050 | | { |
| | 693 | 1051 | | int? maxStreamFrameSize = null; |
| | 693 | 1052 | | int? peerInitialStreamWindowSize = null; |
| | 8720 | 1053 | | foreach ((ParameterKey key, IList<byte> buffer) in parameters) |
| | 3321 | 1054 | | { |
| | 3321 | 1055 | | switch (key) |
| | | 1056 | | { |
| | | 1057 | | case ParameterKey.MaxBidirectionalStreams: |
| | 608 | 1058 | | { |
| | 608 | 1059 | | int value = DecodeParamValue(buffer); |
| | 608 | 1060 | | if (value > 0) |
| | 608 | 1061 | | { |
| | 608 | 1062 | | _bidirectionalStreamSemaphore = new SemaphoreSlim(value, value); |
| | 608 | 1063 | | } |
| | 608 | 1064 | | break; |
| | | 1065 | | } |
| | | 1066 | | case ParameterKey.MaxUnidirectionalStreams: |
| | 665 | 1067 | | { |
| | 665 | 1068 | | int value = DecodeParamValue(buffer); |
| | 665 | 1069 | | if (value > 0) |
| | 665 | 1070 | | { |
| | 665 | 1071 | | _unidirectionalStreamSemaphore = new SemaphoreSlim(value, value); |
| | 665 | 1072 | | } |
| | 665 | 1073 | | break; |
| | | 1074 | | } |
| | | 1075 | | case ParameterKey.IdleTimeout: |
| | 663 | 1076 | | { |
| | 663 | 1077 | | _peerIdleTimeout = TimeSpan.FromMilliseconds(DecodeParamValue(buffer)); |
| | 663 | 1078 | | if (_peerIdleTimeout == TimeSpan.Zero) |
| | 0 | 1079 | | { |
| | 0 | 1080 | | throw new InvalidDataException( |
| | 0 | 1081 | | "The IdleTimeout Slic connection parameter is invalid, it must be greater than 0 s."); |
| | | 1082 | | } |
| | 663 | 1083 | | break; |
| | | 1084 | | } |
| | | 1085 | | case ParameterKey.MaxStreamFrameSize: |
| | 693 | 1086 | | { |
| | 693 | 1087 | | maxStreamFrameSize = DecodeParamValue(buffer); |
| | 693 | 1088 | | if (maxStreamFrameSize < 1024) |
| | 0 | 1089 | | { |
| | 0 | 1090 | | throw new InvalidDataException( |
| | 0 | 1091 | | "The MaxStreamFrameSize connection parameter is invalid, it must be at least 1 KB."); |
| | | 1092 | | } |
| | 693 | 1093 | | if (maxStreamFrameSize > SlicTransportOptions.MaxStreamFrameSizeCeiling) |
| | 1 | 1094 | | { |
| | 1 | 1095 | | throw new InvalidDataException( |
| | 1 | 1096 | | $"The MaxStreamFrameSize connection parameter is invalid, it cannot exceed {SlicTransportOpt |
| | | 1097 | | } |
| | 692 | 1098 | | break; |
| | | 1099 | | } |
| | | 1100 | | case ParameterKey.InitialStreamWindowSize: |
| | 692 | 1101 | | { |
| | 692 | 1102 | | peerInitialStreamWindowSize = DecodeParamValue(buffer); |
| | 692 | 1103 | | if (peerInitialStreamWindowSize < 1024) |
| | 0 | 1104 | | { |
| | 0 | 1105 | | throw new InvalidDataException( |
| | 0 | 1106 | | "The InitialStreamWindowSize connection parameter is invalid, it must be at least 1 KB."); |
| | | 1107 | | } |
| | 692 | 1108 | | break; |
| | | 1109 | | } |
| | | 1110 | | // Ignore unsupported parameter. |
| | | 1111 | | } |
| | 3320 | 1112 | | } |
| | | 1113 | | |
| | 692 | 1114 | | if (maxStreamFrameSize is null) |
| | 0 | 1115 | | { |
| | 0 | 1116 | | throw new InvalidDataException( |
| | 0 | 1117 | | "The peer didn't send the required MaxStreamFrameSize connection parameter."); |
| | | 1118 | | } |
| | | 1119 | | else |
| | 692 | 1120 | | { |
| | 692 | 1121 | | PeerMaxStreamFrameSize = maxStreamFrameSize.Value; |
| | 692 | 1122 | | } |
| | | 1123 | | |
| | 692 | 1124 | | if (peerInitialStreamWindowSize is null) |
| | 0 | 1125 | | { |
| | 0 | 1126 | | throw new InvalidDataException( |
| | 0 | 1127 | | "The peer didn't send the required InitialStreamWindowSize connection parameter."); |
| | | 1128 | | } |
| | | 1129 | | else |
| | 692 | 1130 | | { |
| | 692 | 1131 | | PeerInitialStreamWindowSize = peerInitialStreamWindowSize.Value; |
| | 692 | 1132 | | } |
| | | 1133 | | |
| | | 1134 | | // all parameter values are currently integers in the range 0..Int32Max encoded as varuint62. |
| | | 1135 | | static int DecodeParamValue(IList<byte> buffer) |
| | 3321 | 1136 | | { |
| | | 1137 | | // The IList<byte> decoded by the IceRPC + Slice integration is backed by an array |
| | 3321 | 1138 | | ulong value = new ReadOnlySequence<byte>((byte[])buffer).DecodeSliceBuffer( |
| | 6642 | 1139 | | (ref SliceDecoder decoder) => decoder.DecodeVarUInt62()); |
| | | 1140 | | try |
| | 3321 | 1141 | | { |
| | 3321 | 1142 | | return checked((int)value); |
| | | 1143 | | } |
| | 0 | 1144 | | catch (OverflowException exception) |
| | 0 | 1145 | | { |
| | 0 | 1146 | | throw new InvalidDataException("The value is out of the varuint32 accepted range.", exception); |
| | | 1147 | | } |
| | 3321 | 1148 | | } |
| | 692 | 1149 | | } |
| | | 1150 | | |
| | | 1151 | | private Dictionary<ParameterKey, IList<byte>> EncodeParameters() |
| | 718 | 1152 | | { |
| | 718 | 1153 | | var parameters = new List<KeyValuePair<ParameterKey, IList<byte>>> |
| | 718 | 1154 | | { |
| | 718 | 1155 | | // Required parameters. |
| | 718 | 1156 | | EncodeParameter(ParameterKey.MaxStreamFrameSize, (ulong)_maxStreamFrameSize), |
| | 718 | 1157 | | EncodeParameter(ParameterKey.InitialStreamWindowSize, (ulong)InitialStreamWindowSize) |
| | 718 | 1158 | | }; |
| | | 1159 | | |
| | | 1160 | | // Optional parameters. |
| | 718 | 1161 | | if (_localIdleTimeout != Timeout.InfiniteTimeSpan) |
| | 716 | 1162 | | { |
| | 716 | 1163 | | parameters.Add(EncodeParameter(ParameterKey.IdleTimeout, (ulong)_localIdleTimeout.TotalMilliseconds)); |
| | 716 | 1164 | | } |
| | 718 | 1165 | | if (_maxBidirectionalStreams > 0) |
| | 653 | 1166 | | { |
| | 653 | 1167 | | parameters.Add(EncodeParameter(ParameterKey.MaxBidirectionalStreams, (ulong)_maxBidirectionalStreams)); |
| | 653 | 1168 | | } |
| | 718 | 1169 | | if (_maxUnidirectionalStreams > 0) |
| | 718 | 1170 | | { |
| | 718 | 1171 | | parameters.Add(EncodeParameter(ParameterKey.MaxUnidirectionalStreams, (ulong)_maxUnidirectionalStreams)); |
| | 718 | 1172 | | } |
| | | 1173 | | |
| | 718 | 1174 | | return new Dictionary<ParameterKey, IList<byte>>(parameters); |
| | | 1175 | | |
| | | 1176 | | static KeyValuePair<ParameterKey, IList<byte>> EncodeParameter(ParameterKey key, ulong value) |
| | 3523 | 1177 | | { |
| | 3523 | 1178 | | int sizeLength = SliceEncoder.GetVarUInt62EncodedSize(value); |
| | 3523 | 1179 | | byte[] buffer = new byte[sizeLength]; |
| | 3523 | 1180 | | SliceEncoder.EncodeVarUInt62(value, buffer); |
| | 3523 | 1181 | | return new(key, buffer); |
| | 3523 | 1182 | | } |
| | 718 | 1183 | | } |
| | | 1184 | | |
| | | 1185 | | private bool IsUnknownStream(ulong streamId) |
| | 5286 | 1186 | | { |
| | 5286 | 1187 | | bool isRemote = streamId % 2 == (IsServer ? 0ul : 1ul); |
| | 5286 | 1188 | | bool isBidirectional = streamId % 4 < 2; |
| | 5286 | 1189 | | if (isRemote) |
| | 2798 | 1190 | | { |
| | 2798 | 1191 | | if (isBidirectional) |
| | 1349 | 1192 | | { |
| | 1349 | 1193 | | return _lastRemoteBidirectionalStreamId is null || streamId > _lastRemoteBidirectionalStreamId; |
| | | 1194 | | } |
| | | 1195 | | else |
| | 1449 | 1196 | | { |
| | 1449 | 1197 | | return _lastRemoteUnidirectionalStreamId is null || streamId > _lastRemoteUnidirectionalStreamId; |
| | | 1198 | | } |
| | | 1199 | | } |
| | | 1200 | | else |
| | 2488 | 1201 | | { |
| | 2488 | 1202 | | if (isBidirectional) |
| | 1304 | 1203 | | { |
| | 1304 | 1204 | | return streamId >= _nextBidirectionalId; |
| | | 1205 | | } |
| | | 1206 | | else |
| | 1184 | 1207 | | { |
| | 1184 | 1208 | | return streamId >= _nextUnidirectionalId; |
| | | 1209 | | } |
| | | 1210 | | } |
| | 5286 | 1211 | | } |
| | | 1212 | | |
| | | 1213 | | private Task ReadFrameAsync(FrameType frameType, int size, ulong? streamId, CancellationToken cancellationToken) |
| | 12078 | 1214 | | { |
| | 12078 | 1215 | | if (frameType >= FrameType.Stream && streamId is null) |
| | 0 | 1216 | | { |
| | 0 | 1217 | | throw new InvalidDataException("Received stream frame without stream ID."); |
| | | 1218 | | } |
| | | 1219 | | |
| | 12078 | 1220 | | switch (frameType) |
| | | 1221 | | { |
| | | 1222 | | case FrameType.Close: |
| | 102 | 1223 | | { |
| | 102 | 1224 | | return ReadCloseFrameAsync(size, cancellationToken); |
| | | 1225 | | } |
| | | 1226 | | case FrameType.Ping: |
| | 23 | 1227 | | { |
| | 23 | 1228 | | return ReadPingFrameAndWritePongFrameAsync(size, cancellationToken); |
| | | 1229 | | } |
| | | 1230 | | case FrameType.Pong: |
| | 18 | 1231 | | { |
| | 18 | 1232 | | return ReadPongFrameAsync(size, cancellationToken); |
| | | 1233 | | } |
| | | 1234 | | case FrameType.Stream: |
| | | 1235 | | case FrameType.StreamLast: |
| | 8805 | 1236 | | { |
| | 8805 | 1237 | | return ReadStreamDataFrameAsync(frameType, size, streamId!.Value, cancellationToken); |
| | | 1238 | | } |
| | | 1239 | | case FrameType.StreamWindowUpdate: |
| | 1252 | 1240 | | { |
| | 1252 | 1241 | | if (IsUnknownStream(streamId!.Value)) |
| | 1 | 1242 | | { |
| | 1 | 1243 | | throw new InvalidDataException($"Received {frameType} frame for unknown stream."); |
| | | 1244 | | } |
| | | 1245 | | |
| | 1251 | 1246 | | return ReadStreamWindowUpdateFrameAsync(size, streamId!.Value, cancellationToken); |
| | | 1247 | | } |
| | | 1248 | | case FrameType.StreamReadsClosed: |
| | | 1249 | | case FrameType.StreamWritesClosed: |
| | 1875 | 1250 | | { |
| | 1875 | 1251 | | if (size > 0) |
| | 2 | 1252 | | { |
| | 2 | 1253 | | throw new InvalidDataException($"Unexpected body for {frameType} frame."); |
| | | 1254 | | } |
| | 1873 | 1255 | | if (IsUnknownStream(streamId!.Value)) |
| | 2 | 1256 | | { |
| | 2 | 1257 | | throw new InvalidDataException($"Received {frameType} frame for unknown stream."); |
| | | 1258 | | } |
| | | 1259 | | |
| | 1871 | 1260 | | if (_streams.TryGetValue(streamId.Value, out SlicStream? stream)) |
| | 1375 | 1261 | | { |
| | 1375 | 1262 | | if (frameType == FrameType.StreamWritesClosed) |
| | 48 | 1263 | | { |
| | 48 | 1264 | | stream.ReceivedWritesClosedFrame(); |
| | 48 | 1265 | | } |
| | | 1266 | | else |
| | 1327 | 1267 | | { |
| | 1327 | 1268 | | stream.ReceivedReadsClosedFrame(); |
| | 1327 | 1269 | | } |
| | 1375 | 1270 | | } |
| | 1871 | 1271 | | return Task.CompletedTask; |
| | | 1272 | | } |
| | | 1273 | | default: |
| | 3 | 1274 | | { |
| | 3 | 1275 | | throw new InvalidDataException($"Received unexpected {frameType} frame."); |
| | | 1276 | | } |
| | | 1277 | | } |
| | | 1278 | | |
| | | 1279 | | async Task ReadCloseFrameAsync(int size, CancellationToken cancellationToken) |
| | 102 | 1280 | | { |
| | 102 | 1281 | | CloseBody closeBody = await ReadFrameBodyAsync( |
| | 102 | 1282 | | FrameType.Close, |
| | 102 | 1283 | | size, |
| | 101 | 1284 | | (ref SliceDecoder decoder) => new CloseBody(ref decoder), |
| | 102 | 1285 | | cancellationToken).ConfigureAwait(false); |
| | | 1286 | | |
| | 100 | 1287 | | IceRpcError? peerCloseError = closeBody.ApplicationErrorCode switch |
| | 100 | 1288 | | { |
| | 78 | 1289 | | (ulong)MultiplexedConnectionCloseError.NoError => IceRpcError.ConnectionClosedByPeer, |
| | 4 | 1290 | | (ulong)MultiplexedConnectionCloseError.Refused => IceRpcError.ConnectionRefused, |
| | 8 | 1291 | | (ulong)MultiplexedConnectionCloseError.ServerBusy => IceRpcError.ServerBusy, |
| | 5 | 1292 | | (ulong)MultiplexedConnectionCloseError.Aborted => IceRpcError.ConnectionAborted, |
| | 5 | 1293 | | _ => null |
| | 100 | 1294 | | }; |
| | | 1295 | | |
| | | 1296 | | bool notAlreadyClosed; |
| | 100 | 1297 | | if (peerCloseError is null) |
| | 5 | 1298 | | { |
| | 5 | 1299 | | notAlreadyClosed = TryClose( |
| | 5 | 1300 | | new IceRpcException(IceRpcError.ConnectionAborted), |
| | 5 | 1301 | | $"The connection was closed by the peer with an unknown application error code: '{closeBody.Applicat |
| | 5 | 1302 | | IceRpcError.ConnectionAborted); |
| | 5 | 1303 | | } |
| | | 1304 | | else |
| | 95 | 1305 | | { |
| | 95 | 1306 | | notAlreadyClosed = TryClose( |
| | 95 | 1307 | | new IceRpcException(peerCloseError.Value), |
| | 95 | 1308 | | "The connection was closed by the peer.", |
| | 95 | 1309 | | peerCloseError); |
| | 95 | 1310 | | } |
| | | 1311 | | |
| | | 1312 | | // The server-side of the duplex connection is only shutdown once the client-side is shutdown. When using |
| | | 1313 | | // TCP, this ensures that the server TCP connection won't end-up in the TIME_WAIT state on the server-side. |
| | 100 | 1314 | | if (notAlreadyClosed && !IsServer) |
| | 23 | 1315 | | { |
| | | 1316 | | // DisposeAsync waits for the reads frames task to complete before disposing the writer. |
| | | 1317 | | // _writeSemaphore alone serializes access to the writer. |
| | 23 | 1318 | | using (await _writeSemaphore.AcquireAsync(cancellationToken).ConfigureAwait(false)) |
| | 23 | 1319 | | { |
| | 23 | 1320 | | _duplexConnectionWriter.Shutdown(); |
| | 23 | 1321 | | } |
| | 23 | 1322 | | await _duplexConnectionWriter.WriterTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | 23 | 1323 | | } |
| | 100 | 1324 | | } |
| | | 1325 | | |
| | | 1326 | | async Task ReadPingFrameAndWritePongFrameAsync(int size, CancellationToken cancellationToken) |
| | 23 | 1327 | | { |
| | | 1328 | | // Read the ping frame. |
| | 23 | 1329 | | PingBody pingBody = await ReadFrameBodyAsync( |
| | 23 | 1330 | | FrameType.Ping, |
| | 23 | 1331 | | size, |
| | 22 | 1332 | | (ref SliceDecoder decoder) => new PingBody(ref decoder), |
| | 23 | 1333 | | cancellationToken).ConfigureAwait(false); |
| | | 1334 | | |
| | 21 | 1335 | | if (Interlocked.Increment(ref _outstandingPongCount) > _maxOutstandingPongs) |
| | 1 | 1336 | | { |
| | 1 | 1337 | | throw new IceRpcException( |
| | 1 | 1338 | | IceRpcError.IceRpcError, |
| | 1 | 1339 | | $"Received a {nameof(FrameType.Ping)} frame while {_maxOutstandingPongs} {nameof(FrameType.Pong)} fr |
| | | 1340 | | } |
| | | 1341 | | |
| | | 1342 | | // Return a pong frame with the ping payload, written in the background: writing it from the read frames |
| | | 1343 | | // loop would block the loop when another writer holds _writeSemaphore while parked on a full outbound |
| | | 1344 | | // pipe, suppressing further reads (and idle timeout detection) until the pipe drains. |
| | 20 | 1345 | | _ = WritePongFrameAsync(pingBody.Payload); |
| | 20 | 1346 | | } |
| | | 1347 | | |
| | | 1348 | | async Task WritePongFrameAsync(long payload) |
| | 20 | 1349 | | { |
| | | 1350 | | try |
| | 20 | 1351 | | { |
| | 20 | 1352 | | await WriteConnectionFrameAsync( |
| | 20 | 1353 | | FrameType.Pong, |
| | 20 | 1354 | | new PongBody(payload).Encode, |
| | 20 | 1355 | | _closedCancellationToken).ConfigureAwait(false); |
| | 16 | 1356 | | } |
| | 2 | 1357 | | catch (IceRpcException) |
| | 2 | 1358 | | { |
| | | 1359 | | // Expected if the connection is closed. |
| | 2 | 1360 | | } |
| | 2 | 1361 | | catch (OperationCanceledException) |
| | 2 | 1362 | | { |
| | | 1363 | | // Expected if the connection is closed. |
| | 2 | 1364 | | } |
| | 0 | 1365 | | catch (Exception exception) |
| | 0 | 1366 | | { |
| | 0 | 1367 | | Debug.Fail($"The sending of a Pong frame failed with an unexpected exception: {exception}"); |
| | | 1368 | | |
| | | 1369 | | // Rethrow so in release builds the exception is not swallowed and can be presented to the application |
| | | 1370 | | // as an unobserved task exception. |
| | 0 | 1371 | | throw; |
| | | 1372 | | } |
| | | 1373 | | finally |
| | 20 | 1374 | | { |
| | 20 | 1375 | | Interlocked.Decrement(ref _outstandingPongCount); |
| | 20 | 1376 | | } |
| | 20 | 1377 | | } |
| | | 1378 | | |
| | | 1379 | | async Task ReadPongFrameAsync(int size, CancellationToken cancellationToken) |
| | 18 | 1380 | | { |
| | 18 | 1381 | | if (Interlocked.Decrement(ref _pendingPongCount) >= 0) |
| | 15 | 1382 | | { |
| | | 1383 | | // Ensure the pong frame payload value is expected. |
| | | 1384 | | |
| | 15 | 1385 | | PongBody pongBody = await ReadFrameBodyAsync( |
| | 15 | 1386 | | FrameType.Pong, |
| | 15 | 1387 | | size, |
| | 15 | 1388 | | (ref SliceDecoder decoder) => new PongBody(ref decoder), |
| | 15 | 1389 | | cancellationToken).ConfigureAwait(false); |
| | | 1390 | | |
| | | 1391 | | // For now, we only send a 0 or 1 payload value (0 for "write ping" and 1 for "read ping"). |
| | 15 | 1392 | | if (pongBody.Payload != 0L && pongBody.Payload != 1L) |
| | 0 | 1393 | | { |
| | 0 | 1394 | | throw new InvalidDataException($"Received {nameof(FrameType.Pong)} with unexpected payload."); |
| | | 1395 | | } |
| | 15 | 1396 | | } |
| | | 1397 | | else |
| | 3 | 1398 | | { |
| | | 1399 | | // If not waiting for a pong frame, this pong frame is unexpected. |
| | 3 | 1400 | | throw new InvalidDataException($"Received unexpected {nameof(FrameType.Pong)} frame."); |
| | | 1401 | | } |
| | 15 | 1402 | | } |
| | | 1403 | | |
| | | 1404 | | async Task ReadStreamWindowUpdateFrameAsync(int size, ulong streamId, CancellationToken cancellationToken) |
| | 1251 | 1405 | | { |
| | 1251 | 1406 | | StreamWindowUpdateBody frame = await ReadFrameBodyAsync( |
| | 1251 | 1407 | | FrameType.StreamWindowUpdate, |
| | 1251 | 1408 | | size, |
| | 1251 | 1409 | | (ref SliceDecoder decoder) => new StreamWindowUpdateBody(ref decoder), |
| | 1251 | 1410 | | cancellationToken).ConfigureAwait(false); |
| | 1251 | 1411 | | if (_streams.TryGetValue(streamId, out SlicStream? stream)) |
| | 1212 | 1412 | | { |
| | 1212 | 1413 | | stream.ReceivedWindowUpdateFrame(frame); |
| | 1211 | 1414 | | } |
| | 1250 | 1415 | | } |
| | | 1416 | | |
| | | 1417 | | async Task<T> ReadFrameBodyAsync<T>( |
| | | 1418 | | FrameType frameType, |
| | | 1419 | | int size, |
| | | 1420 | | DecodeFunc<T> decodeFunc, |
| | | 1421 | | CancellationToken cancellationToken) |
| | 1391 | 1422 | | { |
| | 1391 | 1423 | | if (size <= 0) |
| | 2 | 1424 | | { |
| | 2 | 1425 | | throw new InvalidDataException($"Unexpected empty body for {frameType} frame."); |
| | | 1426 | | } |
| | | 1427 | | |
| | 1389 | 1428 | | ReadOnlySequence<byte> buffer = await _duplexConnectionReader.ReadAtLeastAsync(size, cancellationToken) |
| | 1389 | 1429 | | .ConfigureAwait(false); |
| | | 1430 | | |
| | 1389 | 1431 | | if (buffer.Length > size) |
| | 923 | 1432 | | { |
| | 923 | 1433 | | buffer = buffer.Slice(0, size); |
| | 923 | 1434 | | } |
| | | 1435 | | |
| | 1389 | 1436 | | T decodedFrame = buffer.DecodeSliceBuffer(decodeFunc); |
| | 1387 | 1437 | | _duplexConnectionReader.AdvanceTo(buffer.End); |
| | 1387 | 1438 | | return decodedFrame; |
| | 1387 | 1439 | | } |
| | 12070 | 1440 | | } |
| | | 1441 | | |
| | | 1442 | | private async ValueTask<(FrameType FrameType, int FrameSize, ulong? StreamId)?> ReadFrameHeaderAsync( |
| | | 1443 | | CancellationToken cancellationToken) |
| | 13478 | 1444 | | { |
| | 13478 | 1445 | | while (true) |
| | 13478 | 1446 | | { |
| | | 1447 | | // Read data from the pipe reader. |
| | 13478 | 1448 | | if (!_duplexConnectionReader.TryRead(out ReadOnlySequence<byte> buffer)) |
| | 9307 | 1449 | | { |
| | 9307 | 1450 | | buffer = await _duplexConnectionReader.ReadAsync(cancellationToken).ConfigureAwait(false); |
| | 8755 | 1451 | | } |
| | | 1452 | | |
| | 12926 | 1453 | | if (buffer.IsEmpty) |
| | 142 | 1454 | | { |
| | 142 | 1455 | | return null; |
| | | 1456 | | } |
| | | 1457 | | |
| | 12784 | 1458 | | if (TryDecodeHeader( |
| | 12784 | 1459 | | buffer, |
| | 12784 | 1460 | | out (FrameType FrameType, int FrameSize, ulong? StreamId) header, |
| | 12784 | 1461 | | out int consumed)) |
| | 12778 | 1462 | | { |
| | 12778 | 1463 | | _duplexConnectionReader.AdvanceTo(buffer.GetPosition(consumed)); |
| | 12778 | 1464 | | return header; |
| | | 1465 | | } |
| | | 1466 | | else |
| | 0 | 1467 | | { |
| | 0 | 1468 | | _duplexConnectionReader.AdvanceTo(buffer.Start, buffer.End); |
| | 0 | 1469 | | } |
| | 0 | 1470 | | } |
| | | 1471 | | |
| | | 1472 | | static bool TryDecodeHeader( |
| | | 1473 | | ReadOnlySequence<byte> buffer, |
| | | 1474 | | out (FrameType FrameType, int FrameSize, ulong? StreamId) header, |
| | | 1475 | | out int consumed) |
| | 12784 | 1476 | | { |
| | 12784 | 1477 | | header = default; |
| | 12784 | 1478 | | consumed = default; |
| | | 1479 | | |
| | 12784 | 1480 | | var decoder = new SliceDecoder(buffer); |
| | | 1481 | | |
| | | 1482 | | // Decode the frame type and frame size. |
| | 12784 | 1483 | | if (!decoder.TryDecodeUInt8(out byte frameType) || !decoder.TryDecodeVarUInt62(out ulong frameSize)) |
| | 0 | 1484 | | { |
| | 0 | 1485 | | return false; |
| | | 1486 | | } |
| | | 1487 | | |
| | 12784 | 1488 | | header.FrameType = frameType.AsFrameType(); |
| | | 1489 | | try |
| | 12781 | 1490 | | { |
| | 12781 | 1491 | | header.FrameSize = checked((int)frameSize); |
| | 12781 | 1492 | | } |
| | 0 | 1493 | | catch (OverflowException exception) |
| | 0 | 1494 | | { |
| | 0 | 1495 | | throw new InvalidDataException("The frame size can't be larger than int.MaxValue.", exception); |
| | | 1496 | | } |
| | | 1497 | | |
| | | 1498 | | // Reject oversized control frame bodies before any buffering occurs. |
| | 12781 | 1499 | | if (header.FrameType < FrameType.Stream && header.FrameSize > MaxControlFrameBodySize) |
| | 1 | 1500 | | { |
| | 1 | 1501 | | throw new InvalidDataException( |
| | 1 | 1502 | | $"The {header.FrameType} frame body size ({header.FrameSize}) exceeds the maximum allowed size ({Max |
| | | 1503 | | } |
| | | 1504 | | |
| | | 1505 | | // If it's a stream frame, try to decode the stream ID |
| | 12780 | 1506 | | if (header.FrameType >= FrameType.Stream) |
| | 11934 | 1507 | | { |
| | 11934 | 1508 | | if (header.FrameSize == 0) |
| | 1 | 1509 | | { |
| | 1 | 1510 | | throw new InvalidDataException("Invalid stream frame size."); |
| | | 1511 | | } |
| | | 1512 | | |
| | 11933 | 1513 | | consumed = (int)decoder.Consumed; |
| | 11933 | 1514 | | if (!decoder.TryDecodeVarUInt62(out ulong streamId)) |
| | 0 | 1515 | | { |
| | 0 | 1516 | | return false; |
| | | 1517 | | } |
| | 11933 | 1518 | | header.StreamId = streamId; |
| | 11933 | 1519 | | header.FrameSize -= (int)decoder.Consumed - consumed; |
| | | 1520 | | |
| | 11933 | 1521 | | if (header.FrameSize < 0) |
| | 1 | 1522 | | { |
| | 1 | 1523 | | throw new InvalidDataException("Invalid stream frame size."); |
| | | 1524 | | } |
| | 11932 | 1525 | | } |
| | | 1526 | | |
| | 12778 | 1527 | | consumed = (int)decoder.Consumed; |
| | 12778 | 1528 | | return true; |
| | 12778 | 1529 | | } |
| | 12920 | 1530 | | } |
| | | 1531 | | |
| | | 1532 | | private async Task ReadFramesAsync(CancellationToken cancellationToken) |
| | 692 | 1533 | | { |
| | | 1534 | | try |
| | 692 | 1535 | | { |
| | 12749 | 1536 | | while (true) |
| | 12749 | 1537 | | { |
| | 12749 | 1538 | | (FrameType Type, int Size, ulong? StreamId)? header = await ReadFrameHeaderAsync(cancellationToken) |
| | 12749 | 1539 | | .ConfigureAwait(false); |
| | | 1540 | | |
| | 12219 | 1541 | | if (header is null) |
| | 141 | 1542 | | { |
| | | 1543 | | lock (_mutex) |
| | 141 | 1544 | | { |
| | 141 | 1545 | | if (!_isClosed) |
| | 0 | 1546 | | { |
| | | 1547 | | // Unexpected duplex connection shutdown. |
| | 0 | 1548 | | throw new IceRpcException(IceRpcError.ConnectionAborted); |
| | | 1549 | | } |
| | 141 | 1550 | | } |
| | | 1551 | | // The peer has shut down the duplex connection. |
| | 141 | 1552 | | break; |
| | | 1553 | | } |
| | | 1554 | | |
| | 12078 | 1555 | | await ReadFrameAsync(header.Value.Type, header.Value.Size, header.Value.StreamId, cancellationToken) |
| | 12078 | 1556 | | .ConfigureAwait(false); |
| | 12057 | 1557 | | } |
| | | 1558 | | |
| | 141 | 1559 | | if (IsServer) |
| | 72 | 1560 | | { |
| | 72 | 1561 | | Debug.Assert(_isClosed); |
| | | 1562 | | |
| | | 1563 | | // The server-side of the duplex connection is only shutdown once the client-side is shutdown. When |
| | | 1564 | | // using TCP, this ensures that the server TCP connection won't end-up in the TIME_WAIT state on the |
| | | 1565 | | // server-side. |
| | | 1566 | | |
| | | 1567 | | // DisposeAsync waits for the reads frames task to complete before disposing the writer. |
| | | 1568 | | // _writeSemaphore alone serializes access to the writer and guards _writerIsShutdown. |
| | 72 | 1569 | | using (await _writeSemaphore.AcquireAsync(cancellationToken).ConfigureAwait(false)) |
| | 72 | 1570 | | { |
| | 72 | 1571 | | _duplexConnectionWriter.Shutdown(); |
| | | 1572 | | |
| | | 1573 | | // Make sure that CloseAsync doesn't call Write on the writer if it's called shortly after the peer |
| | | 1574 | | // shutdown its side of the connection (which triggers ReadFrameHeaderAsync to return null). |
| | 72 | 1575 | | _writerIsShutdown = true; |
| | 72 | 1576 | | } |
| | | 1577 | | |
| | 72 | 1578 | | await _duplexConnectionWriter.WriterTask.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | 71 | 1579 | | } |
| | 140 | 1580 | | } |
| | 268 | 1581 | | catch (OperationCanceledException) |
| | 268 | 1582 | | { |
| | | 1583 | | // Expected, DisposeAsync was called. |
| | 268 | 1584 | | } |
| | 262 | 1585 | | catch (IceRpcException exception) |
| | 262 | 1586 | | { |
| | 262 | 1587 | | TryClose(exception, "The connection was lost.", IceRpcError.ConnectionAborted); |
| | 262 | 1588 | | throw; |
| | | 1589 | | } |
| | 22 | 1590 | | catch (InvalidDataException exception) |
| | 22 | 1591 | | { |
| | 22 | 1592 | | var rpcException = new IceRpcException( |
| | 22 | 1593 | | IceRpcError.IceRpcError, |
| | 22 | 1594 | | "The connection was aborted by a Slic protocol error.", |
| | 22 | 1595 | | exception); |
| | 22 | 1596 | | TryClose(rpcException, rpcException.Message, IceRpcError.IceRpcError); |
| | 22 | 1597 | | throw rpcException; |
| | | 1598 | | } |
| | 0 | 1599 | | catch (Exception exception) |
| | 0 | 1600 | | { |
| | 0 | 1601 | | Debug.Fail($"The read frames task completed due to an unhandled exception: {exception}"); |
| | 0 | 1602 | | TryClose(exception, "The connection was lost.", IceRpcError.ConnectionAborted); |
| | 0 | 1603 | | throw; |
| | | 1604 | | } |
| | 408 | 1605 | | } |
| | | 1606 | | |
| | | 1607 | | private async Task ReadStreamDataFrameAsync( |
| | | 1608 | | FrameType type, |
| | | 1609 | | int size, |
| | | 1610 | | ulong streamId, |
| | | 1611 | | CancellationToken cancellationToken) |
| | 8805 | 1612 | | { |
| | 8805 | 1613 | | bool endStream = type == FrameType.StreamLast; |
| | 8805 | 1614 | | bool isRemote = streamId % 2 == (IsServer ? 0ul : 1ul); |
| | 8805 | 1615 | | bool isBidirectional = streamId % 4 < 2; |
| | | 1616 | | |
| | 8805 | 1617 | | if (!isBidirectional && !isRemote) |
| | 0 | 1618 | | { |
| | 0 | 1619 | | throw new InvalidDataException( |
| | 0 | 1620 | | "Received unexpected stream frame on local unidirectional stream."); |
| | | 1621 | | } |
| | 8805 | 1622 | | else if (size == 0 && !endStream) |
| | 1 | 1623 | | { |
| | 1 | 1624 | | throw new InvalidDataException($"Received invalid {nameof(FrameType.Stream)} frame."); |
| | | 1625 | | } |
| | 8804 | 1626 | | else if (size > _maxStreamFrameSize) |
| | 1 | 1627 | | { |
| | 1 | 1628 | | throw new InvalidDataException( |
| | 1 | 1629 | | $"Received stream frame with size {size} exceeding the advertised maximum of {_maxStreamFrameSize} bytes |
| | | 1630 | | } |
| | | 1631 | | |
| | 8803 | 1632 | | if (!_streams.TryGetValue(streamId, out SlicStream? stream) && isRemote && IsUnknownStream(streamId)) |
| | 2110 | 1633 | | { |
| | | 1634 | | // Create a new remote stream. |
| | | 1635 | | |
| | 2110 | 1636 | | if (size == 0) |
| | 0 | 1637 | | { |
| | 0 | 1638 | | throw new InvalidDataException("Received empty stream frame on new stream."); |
| | | 1639 | | } |
| | | 1640 | | |
| | 2110 | 1641 | | if (isBidirectional) |
| | 689 | 1642 | | { |
| | 689 | 1643 | | ulong expectedStreamId = _lastRemoteBidirectionalStreamId is ulong lastId |
| | 689 | 1644 | | ? lastId + 4 |
| | 689 | 1645 | | : (IsServer ? 0ul : 1ul); |
| | 689 | 1646 | | if (streamId != expectedStreamId) |
| | 1 | 1647 | | { |
| | 1 | 1648 | | throw new InvalidDataException("Invalid stream ID."); |
| | | 1649 | | } |
| | | 1650 | | |
| | 688 | 1651 | | if (_bidirectionalStreamCount == _maxBidirectionalStreams) |
| | 0 | 1652 | | { |
| | 0 | 1653 | | throw new IceRpcException( |
| | 0 | 1654 | | IceRpcError.IceRpcError, |
| | 0 | 1655 | | $"The maximum bidirectional stream count {_maxBidirectionalStreams} was reached."); |
| | | 1656 | | } |
| | 688 | 1657 | | Interlocked.Increment(ref _bidirectionalStreamCount); |
| | 688 | 1658 | | } |
| | | 1659 | | else |
| | 1421 | 1660 | | { |
| | 1421 | 1661 | | ulong expectedStreamId = _lastRemoteUnidirectionalStreamId is ulong lastId |
| | 1421 | 1662 | | ? lastId + 4 |
| | 1421 | 1663 | | : (IsServer ? 2ul : 3ul); |
| | 1421 | 1664 | | if (streamId != expectedStreamId) |
| | 1 | 1665 | | { |
| | 1 | 1666 | | throw new InvalidDataException("Invalid stream ID."); |
| | | 1667 | | } |
| | | 1668 | | |
| | 1420 | 1669 | | if (_unidirectionalStreamCount == _maxUnidirectionalStreams) |
| | 0 | 1670 | | { |
| | 0 | 1671 | | throw new IceRpcException( |
| | 0 | 1672 | | IceRpcError.IceRpcError, |
| | 0 | 1673 | | $"The maximum unidirectional stream count {_maxUnidirectionalStreams} was reached."); |
| | | 1674 | | } |
| | 1420 | 1675 | | Interlocked.Increment(ref _unidirectionalStreamCount); |
| | 1420 | 1676 | | } |
| | | 1677 | | |
| | | 1678 | | // The stream is registered with the connection and queued on the channel. The caller of AcceptStreamAsync |
| | | 1679 | | // is responsible for cleaning up the stream. |
| | 2108 | 1680 | | stream = new SlicStream(this, isBidirectional, isRemote: true); |
| | | 1681 | | |
| | | 1682 | | try |
| | 2108 | 1683 | | { |
| | 2108 | 1684 | | AddStream(streamId, stream); |
| | | 1685 | | |
| | | 1686 | | try |
| | 2106 | 1687 | | { |
| | 2106 | 1688 | | await _acceptStreamChannel.Writer.WriteAsync( |
| | 2106 | 1689 | | stream, |
| | 2106 | 1690 | | cancellationToken).ConfigureAwait(false); |
| | 2106 | 1691 | | } |
| | 0 | 1692 | | catch (ChannelClosedException exception) |
| | 0 | 1693 | | { |
| | | 1694 | | // The exception given to ChannelWriter.Complete(Exception? exception) is the InnerException. |
| | 0 | 1695 | | Debug.Assert(exception.InnerException is not null); |
| | 0 | 1696 | | throw ExceptionUtil.Throw(exception.InnerException); |
| | | 1697 | | } |
| | 2106 | 1698 | | } |
| | 2 | 1699 | | catch (IceRpcException) |
| | 2 | 1700 | | { |
| | | 1701 | | // The two methods above throw IceRpcException if the connection has been closed (either by CloseAsync |
| | | 1702 | | // or because the close frame was received). We cleanup up the stream but don't throw to not abort the |
| | | 1703 | | // reading. The connection graceful closure still needs to read on the connection to figure out when the |
| | | 1704 | | // peer shuts down the duplex connection. |
| | 2 | 1705 | | Debug.Assert(_isClosed); |
| | 2 | 1706 | | stream.Input.Complete(); |
| | 2 | 1707 | | if (isBidirectional) |
| | 1 | 1708 | | { |
| | 1 | 1709 | | stream.Output.Complete(); |
| | 1 | 1710 | | } |
| | 2 | 1711 | | } |
| | 2108 | 1712 | | } |
| | | 1713 | | |
| | 8801 | 1714 | | bool isDataConsumed = false; |
| | 8801 | 1715 | | if (stream is not null) |
| | 8736 | 1716 | | { |
| | | 1717 | | // Let the stream consume the stream frame data. |
| | 8736 | 1718 | | isDataConsumed = await stream.ReceivedDataFrameAsync( |
| | 8736 | 1719 | | size, |
| | 8736 | 1720 | | endStream, |
| | 8736 | 1721 | | cancellationToken).ConfigureAwait(false); |
| | 8736 | 1722 | | } |
| | | 1723 | | |
| | 8801 | 1724 | | if (!isDataConsumed) |
| | 98 | 1725 | | { |
| | | 1726 | | // The stream (if any) didn't consume the data. Read and ignore the data using a helper pipe. |
| | 98 | 1727 | | var pipe = new Pipe( |
| | 98 | 1728 | | new PipeOptions( |
| | 98 | 1729 | | pool: Pool, |
| | 98 | 1730 | | pauseWriterThreshold: 0, |
| | 98 | 1731 | | minimumSegmentSize: MinSegmentSize, |
| | 98 | 1732 | | useSynchronizationContext: false)); |
| | | 1733 | | |
| | 98 | 1734 | | await _duplexConnectionReader.FillBufferWriterAsync( |
| | 98 | 1735 | | pipe.Writer, |
| | 98 | 1736 | | size, |
| | 98 | 1737 | | cancellationToken).ConfigureAwait(false); |
| | | 1738 | | |
| | 98 | 1739 | | pipe.Writer.Complete(); |
| | 98 | 1740 | | pipe.Reader.Complete(); |
| | 98 | 1741 | | } |
| | 8801 | 1742 | | } |
| | | 1743 | | |
| | | 1744 | | private bool TryClose(Exception exception, string closeMessage, IceRpcError? peerCloseError = null) |
| | 1262 | 1745 | | { |
| | | 1746 | | lock (_mutex) |
| | 1262 | 1747 | | { |
| | 1262 | 1748 | | if (_isClosed) |
| | 494 | 1749 | | { |
| | 494 | 1750 | | return false; |
| | | 1751 | | } |
| | 768 | 1752 | | _isClosed = true; |
| | 768 | 1753 | | _closedMessage = closeMessage; |
| | 768 | 1754 | | _peerCloseError = peerCloseError; |
| | 768 | 1755 | | if (_streamSemaphoreWaitCount == 0) |
| | 761 | 1756 | | { |
| | 761 | 1757 | | _streamSemaphoreWaitClosed.SetResult(); |
| | 761 | 1758 | | } |
| | 768 | 1759 | | } |
| | | 1760 | | |
| | | 1761 | | // Cancel pending CreateStreamAsync, AcceptStreamAsync and WriteStreamDataFrameAsync operations. |
| | 768 | 1762 | | _closedCts.Cancel(); |
| | 768 | 1763 | | _acceptStreamChannel.Writer.TryComplete(exception); |
| | | 1764 | | |
| | | 1765 | | // Close streams. |
| | 3646 | 1766 | | foreach (SlicStream stream in _streams.Values) |
| | 671 | 1767 | | { |
| | 671 | 1768 | | stream.Close(exception); |
| | 671 | 1769 | | } |
| | | 1770 | | |
| | 768 | 1771 | | return true; |
| | 1262 | 1772 | | } |
| | | 1773 | | |
| | | 1774 | | private void WriteFrame(FrameType frameType, ulong? streamId, EncodeAction? encode) |
| | 4582 | 1775 | | { |
| | 4582 | 1776 | | var encoder = new SliceEncoder(_duplexConnectionWriter); |
| | 4582 | 1777 | | encoder.EncodeFrameType(frameType); |
| | | 1778 | | // 2 bytes is sufficient: control frame bodies are limited to MaxControlFrameBodySize (16,383) and the |
| | | 1779 | | // stream frames encoded by WriteFrame carry at most a stream ID + a small body (e.g., StreamWindowUpdate). |
| | 4582 | 1780 | | Span<byte> sizePlaceholder = encoder.GetPlaceholderSpan(2); |
| | 4582 | 1781 | | int startPos = encoder.EncodedByteCount; |
| | 4582 | 1782 | | if (streamId is not null) |
| | 3729 | 1783 | | { |
| | 3729 | 1784 | | encoder.EncodeVarUInt62(streamId.Value); |
| | 3729 | 1785 | | } |
| | 4582 | 1786 | | encode?.Invoke(ref encoder); |
| | 4582 | 1787 | | SliceEncoder.EncodeVarUInt62((ulong)(encoder.EncodedByteCount - startPos), sizePlaceholder); |
| | 4582 | 1788 | | } |
| | | 1789 | | } |