| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.IO.Pipelines; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Ice.Operations.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary>Provides extension methods for <see cref="PipeReader" /> to read payloads.</summary> |
| | | 9 | | internal static class PipeReaderExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary>Reads the full payload from a pipe reader.</summary> |
| | | 12 | | /// <param name="reader">The pipe reader.</param> |
| | | 13 | | /// <param name="maxSize">The maximum size of this payload.</param> |
| | | 14 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 15 | | /// <returns>A read result with the payload read from the reader unless <see cref="ReadResult.IsCanceled" /> is |
| | | 16 | | /// <see langword="true" />.</returns> |
| | | 17 | | /// <exception cref="InvalidDataException">Thrown when the payload size exceeds <paramref name="maxSize" />. |
| | | 18 | | /// </exception> |
| | | 19 | | /// <remarks>The caller must call AdvanceTo on the reader, as usual. This method reads all the remaining bytes in |
| | | 20 | | /// the reader.</remarks> |
| | | 21 | | internal static async ValueTask<ReadResult> ReadFullPayloadAsync( |
| | | 22 | | this PipeReader reader, |
| | | 23 | | int maxSize, |
| | | 24 | | CancellationToken cancellationToken) |
| | 16 | 25 | | { |
| | 16 | 26 | | Debug.Assert(maxSize is > 0 and < int.MaxValue); |
| | | 27 | | |
| | | 28 | | // This method does not attempt to read the reader synchronously. A caller that wants a sync attempt can |
| | | 29 | | // call TryReadFullPayload. |
| | | 30 | | |
| | | 31 | | // We read everything up to the maxSize + 1. |
| | | 32 | | // It's maxSize + 1 and not maxSize because if the payload's size is maxSize, we could get |
| | | 33 | | // readResult.IsCompleted == false even though the full payload was read. |
| | 16 | 34 | | ReadResult readResult = await reader.ReadAtLeastAsync(maxSize + 1, cancellationToken).ConfigureAwait(false); |
| | | 35 | | |
| | 16 | 36 | | if (readResult.IsCompleted && readResult.Buffer.Length <= maxSize) |
| | 16 | 37 | | { |
| | 16 | 38 | | return readResult; |
| | | 39 | | } |
| | | 40 | | else |
| | 0 | 41 | | { |
| | 0 | 42 | | reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End); |
| | 0 | 43 | | throw new InvalidDataException("The payload size exceeds the maximum value."); |
| | | 44 | | } |
| | 16 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary>Attempts to read the full payload from a pipe reader.</summary> |
| | | 48 | | /// <param name="reader">The pipe reader.</param> |
| | | 49 | | /// <param name="maxSize">The maximum size of this payload.</param> |
| | | 50 | | /// <param name="readResult">The read result.</param> |
| | | 51 | | /// <returns><see langword="true" /> when <paramref name="readResult" /> contains the payload read synchronously, or |
| | | 52 | | /// the call was cancelled; otherwise, <see langword="false" />.</returns> |
| | | 53 | | /// <exception cref="InvalidDataException">Thrown when the payload size exceeds the max payload size.</exception> |
| | | 54 | | /// <remarks>When this method returns <see langword="true" />, the caller must call AdvanceTo on the reader, as |
| | | 55 | | /// usual. When this method returns <see langword="false" />, the caller must call |
| | | 56 | | /// <see cref="ReadFullPayloadAsync" />.</remarks> |
| | | 57 | | internal static bool TryReadFullPayload( |
| | | 58 | | this PipeReader reader, |
| | | 59 | | int maxSize, |
| | | 60 | | out ReadResult readResult) |
| | 225 | 61 | | { |
| | 225 | 62 | | Debug.Assert(maxSize is > 0 and < int.MaxValue); |
| | | 63 | | |
| | 225 | 64 | | if (reader.TryRead(out readResult)) |
| | 225 | 65 | | { |
| | 225 | 66 | | if (readResult.IsCanceled) |
| | 0 | 67 | | { |
| | 0 | 68 | | return true; // and the buffer does not matter |
| | | 69 | | } |
| | | 70 | | |
| | 225 | 71 | | if (readResult.Buffer.Length > maxSize) |
| | 0 | 72 | | { |
| | 0 | 73 | | reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End); |
| | 0 | 74 | | throw new InvalidDataException("The payload size exceeds the maximum value."); |
| | | 75 | | } |
| | | 76 | | |
| | 225 | 77 | | if (readResult.IsCompleted) |
| | 225 | 78 | | { |
| | 225 | 79 | | return true; |
| | | 80 | | } |
| | | 81 | | else |
| | 0 | 82 | | { |
| | | 83 | | // don't consume anything but mark the whole buffer as examined - we need more. |
| | 0 | 84 | | reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End); |
| | 0 | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | readResult = default; |
| | 0 | 89 | | return false; |
| | 225 | 90 | | } |
| | | 91 | | } |