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