| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Buffers; |
| | 4 | | using System.IO.Pipelines; |
| | 5 | |
|
| | 6 | | namespace IceRpc.Internal; |
| | 7 | |
|
| | 8 | | /// <summary>Implements a stateless and therefore shareable PipeReader over an empty sequence.</summary> |
| | 9 | | /// <remarks>Because this implementation is stateless, it does not implement CancelPendingRead correctly; in |
| | 10 | | /// practice this discrepancy should not be noticeable.</remarks> |
| | 11 | | internal sealed class EmptyPipeReader : PipeReader |
| | 12 | | { |
| | 13 | | /// <summary>Gets the shared instance of the empty pipe reader.</summary> |
| 26820 | 14 | | internal static PipeReader Instance { get; } = new EmptyPipeReader(); |
| | 15 | |
|
| | 16 | | /// <summary>The ReadResult returned by all read methods.</summary> |
| 14 | 17 | | private readonly ReadResult _readResult = |
| 14 | 18 | | new(ReadOnlySequence<byte>.Empty, isCanceled: false, isCompleted: true); |
| | 19 | |
|
| | 20 | | /// <inheritdoc/> |
| | 21 | | public override void AdvanceTo(SequencePosition consumed) |
| 1499 | 22 | | { |
| | 23 | | // no-op |
| 1499 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <inheritdoc/> |
| | 27 | | public override void AdvanceTo(SequencePosition consumed, SequencePosition examined) |
| 0 | 28 | | { |
| | 29 | | // no-op |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <inheritdoc/> |
| | 33 | | public override void CancelPendingRead() |
| 0 | 34 | | { |
| | 35 | | // no-op |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <inheritdoc/> |
| | 39 | | public override void Complete(Exception? exception) |
| 13345 | 40 | | { |
| | 41 | | // no-op |
| 13345 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <inheritdoc/> |
| 3295 | 45 | | public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken) => new(_readResult); |
| | 46 | |
|
| | 47 | | /// <inheritdoc/> |
| | 48 | | public override bool TryRead(out ReadResult result) |
| 1485 | 49 | | { |
| 1485 | 50 | | result = _readResult; |
| 1485 | 51 | | return true; |
| 1485 | 52 | | } |
| | 53 | |
|
| 14 | 54 | | private EmptyPipeReader() |
| 14 | 55 | | { |
| | 56 | | // ensures there is only one instance |
| 14 | 57 | | } |
| | 58 | | } |