| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.IO.Pipelines; |
| | | 5 | | using System.Net.Quic; |
| | | 6 | | using System.Runtime.Versioning; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Transports.Quic.Internal; |
| | | 9 | | |
| | | 10 | | /// <summary>The QUIC multiplexed stream implements an <see cref="IMultiplexedStream"/>.</summary> |
| | | 11 | | [SupportedOSPlatform("linux")] |
| | | 12 | | [SupportedOSPlatform("macos")] |
| | | 13 | | [SupportedOSPlatform("windows")] |
| | | 14 | | internal class QuicMultiplexedStream : IMultiplexedStream |
| | | 15 | | { |
| | 22 | 16 | | public ulong Id { get; } |
| | | 17 | | |
| | | 18 | | public PipeReader Input => |
| | 27560 | 19 | | _inputPipeReader ?? throw new InvalidOperationException("A local unidirectional stream has no Input."); |
| | | 20 | | |
| | 372 | 21 | | public bool IsBidirectional { get; } |
| | | 22 | | |
| | 14 | 23 | | public bool IsRemote { get; } |
| | | 24 | | |
| | 2 | 25 | | public bool IsStarted => true; |
| | | 26 | | |
| | | 27 | | public PipeWriter Output => |
| | 6412 | 28 | | _outputPipeWriter ?? throw new InvalidOperationException("A remote unidirectional stream has no Output."); |
| | | 29 | | |
| | 12 | 30 | | public Task WritesClosed => _outputPipeWriter?.Closed ?? Task.CompletedTask; |
| | | 31 | | |
| | | 32 | | private readonly QuicPipeReader? _inputPipeReader; |
| | | 33 | | private readonly QuicPipeWriter? _outputPipeWriter; |
| | | 34 | | private readonly QuicStream _stream; |
| | | 35 | | |
| | 572 | 36 | | internal QuicMultiplexedStream( |
| | 572 | 37 | | QuicStream stream, |
| | 572 | 38 | | bool isRemote, |
| | 572 | 39 | | MemoryPool<byte> pool, |
| | 572 | 40 | | int minSegmentSize, |
| | 572 | 41 | | Action throwIfConnectionClosedOrDisposed) |
| | 572 | 42 | | { |
| | 572 | 43 | | Id = (ulong)stream.Id; |
| | 572 | 44 | | IsRemote = isRemote; |
| | 572 | 45 | | IsBidirectional = stream.Type == QuicStreamType.Bidirectional; |
| | | 46 | | |
| | 572 | 47 | | int streamCount = stream.CanRead && stream.CanWrite ? 2 : 1; |
| | | 48 | | |
| | 572 | 49 | | _stream = stream; |
| | 572 | 50 | | _inputPipeReader = stream.CanRead ? |
| | 572 | 51 | | new QuicPipeReader( |
| | 572 | 52 | | stream, |
| | 572 | 53 | | pool, |
| | 572 | 54 | | minSegmentSize, |
| | 572 | 55 | | ReleaseStream, |
| | 572 | 56 | | throwIfConnectionClosedOrDisposed) : |
| | 572 | 57 | | null; |
| | 572 | 58 | | _outputPipeWriter = stream.CanWrite ? |
| | 572 | 59 | | new QuicPipeWriter( |
| | 572 | 60 | | stream, |
| | 572 | 61 | | pool, |
| | 572 | 62 | | minSegmentSize, |
| | 572 | 63 | | ReleaseStream, |
| | 572 | 64 | | throwIfConnectionClosedOrDisposed) : |
| | 572 | 65 | | null; |
| | | 66 | | |
| | | 67 | | void ReleaseStream() |
| | 1004 | 68 | | { |
| | 1004 | 69 | | if (Interlocked.Decrement(ref streamCount) == 0) |
| | 551 | 70 | | { |
| | 551 | 71 | | _ = _stream.DisposeAsync().AsTask(); |
| | 551 | 72 | | } |
| | 1004 | 73 | | } |
| | 572 | 74 | | } |
| | | 75 | | } |