| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Transports; |
| | 4 | | using System.Buffers; |
| | 5 | | using System.IO.Pipelines; |
| | 6 | |
|
| | 7 | | namespace IceRpc.Internal; |
| | 8 | |
|
| | 9 | | /// <summary>Provides a multiplexed stream decorator that allows <see cref="IceRpcProtocolConnection" /> to intercept |
| | 10 | | /// the completion of the stream input and output.</summary> |
| | 11 | | internal sealed class MultiplexedStreamDecorator : IMultiplexedStream |
| | 12 | | { |
| 2809 | 13 | | public ulong Id => _decoratee.Id; |
| | 14 | |
|
| 13947 | 15 | | public bool IsBidirectional => _decoratee.IsBidirectional; |
| | 16 | |
|
| 0 | 17 | | public bool IsRemote => _decoratee.IsRemote; |
| | 18 | |
|
| 26 | 19 | | public bool IsStarted => _decoratee.IsStarted; |
| | 20 | |
|
| 4319 | 21 | | public Task WritesClosed => _decoratee.WritesClosed; |
| | 22 | |
|
| 4269 | 23 | | public PipeReader Input => _input ?? _decoratee.Input; |
| | 24 | |
|
| 6357 | 25 | | public PipeWriter Output => _output ?? _decoratee.Output; |
| | 26 | |
|
| | 27 | | private readonly IMultiplexedStream _decoratee; |
| | 28 | | private readonly PipeReader? _input; |
| | 29 | | private readonly PipeWriter? _output; |
| | 30 | |
|
| | 31 | | /// <summary>Constructs a multiplexed stream decorator.</summary> |
| | 32 | | /// <param name="decoratee">The decoratee.</param> |
| | 33 | | /// <param name="onCompleted">An action that is executed when stream input or output is completed; it's executed |
| | 34 | | /// up to 2 times for a bidirectional stream, and up to 1 time for a unidirectional stream.</param> |
| 5582 | 35 | | internal MultiplexedStreamDecorator(IMultiplexedStream decoratee, Action onCompleted) |
| 5582 | 36 | | { |
| 5582 | 37 | | _decoratee = decoratee; |
| | 38 | |
|
| 5582 | 39 | | if (decoratee.IsBidirectional || decoratee.IsRemote) |
| 3558 | 40 | | { |
| 3558 | 41 | | _input = new InputDecorator(decoratee.Input, onCompleted); |
| 3558 | 42 | | } |
| 5582 | 43 | | if (decoratee.IsBidirectional || !decoratee.IsRemote) |
| 3562 | 44 | | { |
| 3562 | 45 | | _output = new OutputDecorator((ReadOnlySequencePipeWriter)decoratee.Output, onCompleted); |
| 3562 | 46 | | } |
| 5582 | 47 | | } |
| | 48 | |
|
| | 49 | | private class InputDecorator : PipeReader |
| | 50 | | { |
| | 51 | | private readonly PipeReader _decoratee; |
| | 52 | | private bool _isCompleted; |
| | 53 | | private readonly Action _onCompleted; |
| | 54 | |
|
| | 55 | | public override void AdvanceTo(SequencePosition consumed) => |
| 3589 | 56 | | _decoratee.AdvanceTo(consumed); |
| | 57 | |
|
| | 58 | | public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => |
| 8 | 59 | | _decoratee.AdvanceTo(consumed, examined); |
| | 60 | |
|
| | 61 | | // When leaveOpen is false, we use the default implementation of AsStream: we want the disposal of the stream |
| | 62 | | // to call Complete on this decorator. |
| | 63 | | public override Stream AsStream(bool leaveOpen = false) => |
| 4 | 64 | | leaveOpen ? _decoratee.AsStream(leaveOpen) : base.AsStream(leaveOpen); |
| | 65 | |
|
| 0 | 66 | | public override void CancelPendingRead() => _decoratee.CancelPendingRead(); |
| | 67 | |
|
| | 68 | | public override void Complete(Exception? exception = null) |
| 3586 | 69 | | { |
| 3586 | 70 | | if (!_isCompleted) |
| 3550 | 71 | | { |
| 3550 | 72 | | _isCompleted = true; |
| 3550 | 73 | | _decoratee.Complete(exception); |
| 3550 | 74 | | _onCompleted(); |
| 3550 | 75 | | } |
| 3586 | 76 | | } |
| | 77 | |
|
| | 78 | | public override Task CopyToAsync(PipeWriter destination, CancellationToken cancellationToken = default) => |
| 0 | 79 | | _decoratee.CopyToAsync(destination, cancellationToken); |
| | 80 | |
|
| | 81 | | public override Task CopyToAsync(Stream destination, CancellationToken cancellationToken = default) => |
| 0 | 82 | | _decoratee.CopyToAsync(destination, cancellationToken); |
| | 83 | |
|
| | 84 | | public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) => |
| 3588 | 85 | | _decoratee.ReadAsync(cancellationToken); |
| | 86 | |
|
| 71 | 87 | | public override bool TryRead(out ReadResult result) => _decoratee.TryRead(out result); |
| | 88 | |
|
| | 89 | | protected override ValueTask<ReadResult> ReadAtLeastAsyncCore( |
| | 90 | | int minimumSize, |
| 26 | 91 | | CancellationToken cancellationToken) => _decoratee.ReadAtLeastAsync(minimumSize, cancellationToken); |
| | 92 | |
|
| 3558 | 93 | | internal InputDecorator(PipeReader decoratee, Action onCompleted) |
| 3558 | 94 | | { |
| 3558 | 95 | | _decoratee = decoratee; |
| 3558 | 96 | | _onCompleted = onCompleted; |
| 3558 | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| | 100 | | private class OutputDecorator : ReadOnlySequencePipeWriter |
| | 101 | | { |
| 0 | 102 | | public override bool CanGetUnflushedBytes => _decoratee.CanGetUnflushedBytes; |
| | 103 | |
|
| 7080 | 104 | | public override long UnflushedBytes => _decoratee.UnflushedBytes; |
| | 105 | |
|
| | 106 | | private readonly ReadOnlySequencePipeWriter _decoratee; |
| | 107 | | private bool _isCompleted; |
| | 108 | | private readonly Action _onCompleted; |
| | 109 | |
|
| 16428 | 110 | | public override void Advance(int bytes) => _decoratee.Advance(bytes); |
| | 111 | |
|
| | 112 | | // When leaveOpen is false, we use the default implementation of AsStream: we want the disposal of the stream |
| | 113 | | // to call Complete on this decorator. |
| | 114 | | public override Stream AsStream(bool leaveOpen = false) => |
| 4 | 115 | | leaveOpen ? _decoratee.AsStream(leaveOpen) : base.AsStream(leaveOpen); |
| | 116 | |
|
| 0 | 117 | | public override void CancelPendingFlush() => _decoratee.CancelPendingFlush(); |
| | 118 | |
|
| | 119 | | public override void Complete(Exception? exception = null) |
| 3571 | 120 | | { |
| 3571 | 121 | | if (!_isCompleted) |
| 3562 | 122 | | { |
| 3562 | 123 | | _isCompleted = true; |
| 3562 | 124 | | _decoratee.Complete(exception); |
| 3562 | 125 | | _onCompleted(); |
| 3562 | 126 | | } |
| 3571 | 127 | | } |
| | 128 | |
|
| | 129 | | public override ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default) => |
| 13 | 130 | | _decoratee.FlushAsync(cancellationToken); |
| | 131 | |
|
| 0 | 132 | | public override Memory<byte> GetMemory(int sizeHint = 0) => _decoratee.GetMemory(sizeHint); |
| | 133 | |
|
| 16432 | 134 | | public override Span<byte> GetSpan(int sizeHint = 0) => _decoratee.GetSpan(sizeHint); |
| | 135 | |
|
| | 136 | | public override ValueTask<FlushResult> WriteAsync( |
| | 137 | | ReadOnlyMemory<byte> source, |
| 8 | 138 | | CancellationToken cancellationToken = default) => _decoratee.WriteAsync(source, cancellationToken); |
| | 139 | |
|
| | 140 | | public override ValueTask<FlushResult> WriteAsync( |
| | 141 | | ReadOnlySequence<byte> source, |
| | 142 | | bool endStream, |
| | 143 | | CancellationToken cancellationToken = default) => |
| 3532 | 144 | | _decoratee.WriteAsync(source, endStream, cancellationToken); |
| | 145 | |
|
| 3562 | 146 | | internal OutputDecorator(ReadOnlySequencePipeWriter decoratee, Action onCompleted) |
| 3562 | 147 | | { |
| 3562 | 148 | | _decoratee = decoratee; |
| 3562 | 149 | | _onCompleted = onCompleted; |
| 3562 | 150 | | } |
| | 151 | | } |
| | 152 | | } |