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