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