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