| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Internal; |
| | | 5 | | using System.IO.Pipelines; |
| | | 6 | | using ZeroC.Slice.Codec; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Slice.Operations.Internal; |
| | | 9 | | |
| | | 10 | | /// <summary>Provides extension methods for <see cref="IncomingFrame" /> to decode its payload when this payload is |
| | | 11 | | /// encoded with the Slice encoding.</summary> |
| | | 12 | | internal static class IncomingFrameExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary>Decodes arguments or a response value from a pipe reader.</summary> |
| | | 15 | | /// <param name="frame">The incoming frame.</param> |
| | | 16 | | /// <param name="feature">The Slice feature.</param> |
| | | 17 | | /// <param name="baseProxy">The base proxy.</param> |
| | | 18 | | /// <param name="decodeFunc">The decode function for the payload arguments or return value.</param> |
| | | 19 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 20 | | /// <returns>The decode value.</returns> |
| | | 21 | | internal static ValueTask<T> DecodeValueAsync<T>( |
| | | 22 | | this IncomingFrame frame, |
| | | 23 | | ISliceFeature feature, |
| | | 24 | | ISliceProxy? baseProxy, |
| | | 25 | | DecodeFunc<T> decodeFunc, |
| | | 26 | | CancellationToken cancellationToken) |
| | 131 | 27 | | { |
| | 131 | 28 | | return frame.Payload.TryReadSliceSegment(feature.MaxSegmentSize, out ReadResult readResult) ? |
| | 131 | 29 | | new(DecodeSegment(readResult)) : |
| | 131 | 30 | | PerformDecodeAsync(); |
| | | 31 | | |
| | | 32 | | // All the logic is in this local function. |
| | | 33 | | T DecodeSegment(ReadResult readResult) |
| | 131 | 34 | | { |
| | | 35 | | // We never call CancelPendingRead; an interceptor or middleware can but it's not correct. |
| | 131 | 36 | | if (readResult.IsCanceled) |
| | 0 | 37 | | { |
| | 0 | 38 | | throw new InvalidOperationException("Unexpected call to CancelPendingRead."); |
| | | 39 | | } |
| | | 40 | | |
| | 131 | 41 | | var decoder = new SliceDecoder(readResult.Buffer, baseProxy, feature.MaxCollectionAllocation); |
| | 131 | 42 | | T value = decodeFunc(ref decoder); |
| | 131 | 43 | | decoder.SkipTagged(); |
| | 131 | 44 | | decoder.CheckEndOfBuffer(); |
| | | 45 | | |
| | 131 | 46 | | frame.Payload.AdvanceTo(readResult.Buffer.End); |
| | 131 | 47 | | return value; |
| | 131 | 48 | | } |
| | | 49 | | |
| | | 50 | | async ValueTask<T> PerformDecodeAsync() => |
| | 4 | 51 | | DecodeSegment(await frame.Payload.ReadSliceSegmentAsync( |
| | 4 | 52 | | feature.MaxSegmentSize, |
| | 4 | 53 | | cancellationToken).ConfigureAwait(false)); |
| | 131 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary>Reads/decodes empty args or a void return value.</summary> |
| | | 57 | | /// <param name="frame">The incoming frame.</param> |
| | | 58 | | /// <param name="feature">The Slice feature.</param> |
| | | 59 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 60 | | internal static ValueTask DecodeVoidAsync( |
| | | 61 | | this IncomingFrame frame, |
| | | 62 | | ISliceFeature feature, |
| | | 63 | | CancellationToken cancellationToken) |
| | 37 | 64 | | { |
| | 37 | 65 | | if (frame.Payload.TryReadSliceSegment(feature.MaxSegmentSize, out ReadResult readResult)) |
| | 37 | 66 | | { |
| | 37 | 67 | | DecodeSegment(readResult); |
| | 37 | 68 | | return default; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return PerformDecodeAsync(); |
| | | 72 | | |
| | | 73 | | // All the logic is in this local function. |
| | | 74 | | void DecodeSegment(ReadResult readResult) |
| | 37 | 75 | | { |
| | | 76 | | // We never call CancelPendingRead; an interceptor or middleware can but it's not correct. |
| | 37 | 77 | | if (readResult.IsCanceled) |
| | 0 | 78 | | { |
| | 0 | 79 | | throw new InvalidOperationException("Unexpected call to CancelPendingRead."); |
| | | 80 | | } |
| | | 81 | | |
| | 37 | 82 | | if (!readResult.Buffer.IsEmpty) |
| | 12 | 83 | | { |
| | | 84 | | // No need to pass maxCollectionAllocation since the only thing this decoding does is skip unknown tags. |
| | 12 | 85 | | var decoder = new SliceDecoder(readResult.Buffer); |
| | 12 | 86 | | decoder.SkipTagged(); |
| | 12 | 87 | | decoder.CheckEndOfBuffer(); |
| | 12 | 88 | | } |
| | 37 | 89 | | frame.Payload.AdvanceTo(readResult.Buffer.End); |
| | 37 | 90 | | } |
| | | 91 | | |
| | | 92 | | async ValueTask PerformDecodeAsync() => |
| | 0 | 93 | | DecodeSegment(await frame.Payload.ReadSliceSegmentAsync( |
| | 0 | 94 | | feature.MaxSegmentSize, |
| | 0 | 95 | | cancellationToken).ConfigureAwait(false)); |
| | 37 | 96 | | } |
| | | 97 | | } |