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