| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.IO.Pipelines; |
| | | 4 | | |
| | | 5 | | namespace IceRpc.Internal; |
| | | 6 | | |
| | | 7 | | /// <summary>A PipeReader decorator to workaround a bug from the System.IO.Pipelines StreamPipeReader implementation, |
| | | 8 | | /// see https://github.com/dotnet/runtime/issues/82594.</summary> |
| | | 9 | | internal sealed class BugFixStreamPipeReaderDecorator : PipeReader |
| | | 10 | | { |
| | | 11 | | private readonly PipeReader _decoratee; |
| | | 12 | | |
| | 2 | 13 | | public override void AdvanceTo(SequencePosition consumed) => _decoratee.AdvanceTo(consumed); |
| | | 14 | | |
| | | 15 | | public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) => |
| | 0 | 16 | | _decoratee.AdvanceTo(consumed, examined); |
| | | 17 | | |
| | 0 | 18 | | public override void CancelPendingRead() => _decoratee.CancelPendingRead(); |
| | | 19 | | |
| | 4 | 20 | | public override void Complete(Exception? exception = null) => _decoratee.Complete(exception); |
| | | 21 | | |
| | | 22 | | public override Task CopyToAsync(Stream destination, CancellationToken cancellationToken) => |
| | 0 | 23 | | _decoratee.CopyToAsync(destination, cancellationToken); |
| | | 24 | | |
| | | 25 | | public override Task CopyToAsync(PipeWriter writer, CancellationToken cancellationToken) => |
| | 0 | 26 | | _decoratee.CopyToAsync(writer, cancellationToken); |
| | | 27 | | |
| | | 28 | | public override async ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default) |
| | 4 | 29 | | { |
| | | 30 | | try |
| | 4 | 31 | | { |
| | 4 | 32 | | return await _decoratee.ReadAsync(cancellationToken).ConfigureAwait(false); |
| | | 33 | | } |
| | 0 | 34 | | catch (OperationCanceledException exception) when (exception.CancellationToken != cancellationToken) |
| | 0 | 35 | | { |
| | | 36 | | // Workaround |
| | 0 | 37 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 38 | | throw; |
| | | 39 | | } |
| | 4 | 40 | | } |
| | | 41 | | |
| | 2 | 42 | | public override bool TryRead(out ReadResult result) => _decoratee.TryRead(out result); |
| | | 43 | | |
| | | 44 | | protected override async ValueTask<ReadResult> ReadAtLeastAsyncCore( |
| | | 45 | | int minimumSize, |
| | | 46 | | CancellationToken cancellationToken) |
| | 0 | 47 | | { |
| | | 48 | | try |
| | 0 | 49 | | { |
| | 0 | 50 | | return await _decoratee.ReadAtLeastAsync(minimumSize, cancellationToken).ConfigureAwait(false); |
| | | 51 | | } |
| | 0 | 52 | | catch (OperationCanceledException exception) when (exception.CancellationToken != cancellationToken) |
| | 0 | 53 | | { |
| | | 54 | | // Workaround |
| | 0 | 55 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 56 | | throw; |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | 8 | 60 | | internal BugFixStreamPipeReaderDecorator(PipeReader decoratee) => _decoratee = decoratee; |
| | | 61 | | } |