| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Ice.Codec; |
| | | 5 | | using IceRpc.Ice.Operations.Internal; |
| | | 6 | | using System.Buffers; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.IO.Pipelines; |
| | | 9 | | |
| | | 10 | | namespace IceRpc.Ice.Operations; |
| | | 11 | | |
| | | 12 | | /// <summary>Provides extension methods for <see cref="IncomingResponse" /> to decode its Ice-encoded payload. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class IncomingResponseExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary>Decodes a response payload.</summary> |
| | | 17 | | /// <typeparam name="T">The type of the return value.</typeparam> |
| | | 18 | | /// <param name="response">The incoming response.</param> |
| | | 19 | | /// <param name="request">The outgoing request.</param> |
| | | 20 | | /// <param name="sender">The proxy that sent the request.</param> |
| | | 21 | | /// <param name="decodeReturnValue">A function that decodes the return value.</param> |
| | | 22 | | /// <param name="defaultActivator">The activator to use when the activator provided by the request's |
| | | 23 | | /// <see cref="IIceFeature" /> is <see langword="null" />.</param> |
| | | 24 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 25 | | /// <returns>A value task that holds the operation's return value. This value task is faulted and holds a <see |
| | | 26 | | /// cref="IceException" /> when the status code is <see cref="StatusCode.ApplicationError"/>.</returns> |
| | | 27 | | /// <exception cref="DispatchException">Thrown when the status code of the response is greater than |
| | | 28 | | /// <see cref="StatusCode.ApplicationError"/>.</exception> |
| | | 29 | | public static ValueTask<T> DecodeReturnValueAsync<T>( |
| | | 30 | | this IncomingResponse response, |
| | | 31 | | OutgoingRequest request, |
| | | 32 | | IIceProxy sender, |
| | | 33 | | DecodeFunc<T> decodeReturnValue, |
| | | 34 | | IActivator defaultActivator, |
| | | 35 | | CancellationToken cancellationToken = default) |
| | 59 | 36 | | { |
| | 59 | 37 | | IIceFeature feature = request.Features.Get<IIceFeature>() ?? IceFeature.Default; |
| | 59 | 38 | | IActivator activator = feature.Activator ?? defaultActivator; |
| | | 39 | | |
| | 59 | 40 | | return response.StatusCode switch |
| | 59 | 41 | | { |
| | 58 | 42 | | StatusCode.Ok => response.DecodeValueAsync( |
| | 58 | 43 | | feature, |
| | 58 | 44 | | feature.BaseProxy ?? sender, |
| | 58 | 45 | | decodeReturnValue, |
| | 58 | 46 | | activator, |
| | 58 | 47 | | cancellationToken), |
| | 59 | 48 | | |
| | 0 | 49 | | StatusCode.ApplicationError => DecodeAndThrowExceptionAsync(), |
| | 59 | 50 | | |
| | 1 | 51 | | _ => throw new DispatchException(response.StatusCode, response.ErrorMessage) |
| | 1 | 52 | | { |
| | 1 | 53 | | ConvertToInternalError = true |
| | 1 | 54 | | } |
| | 59 | 55 | | }; |
| | | 56 | | |
| | | 57 | | async ValueTask<T> DecodeAndThrowExceptionAsync() => |
| | 0 | 58 | | throw await response.DecodeIceExceptionAsync(feature, sender, activator, cancellationToken) |
| | 0 | 59 | | .ConfigureAwait(false); |
| | 58 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary>Verifies that an Ice-encoded response payload carries no return value or only tagged return values. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="response">The incoming response.</param> |
| | | 65 | | /// <param name="request">The outgoing request.</param> |
| | | 66 | | /// <param name="sender">The proxy that sent the request.</param> |
| | | 67 | | /// <param name="defaultActivator">The activator to use when the activator provided by the request's |
| | | 68 | | /// <see cref="IIceFeature" /> is <see langword="null" />.</param> |
| | | 69 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 70 | | /// <returns>A value task representing the asynchronous completion of the operation. This value task is faulted and |
| | | 71 | | /// holds a <see cref="IceException" /> when the status code is <see cref="StatusCode.ApplicationError" />. |
| | | 72 | | /// </returns> |
| | | 73 | | /// <exception cref="DispatchException">Thrown when the status code of the response is greater than |
| | | 74 | | /// <see cref="StatusCode.ApplicationError"/>.</exception> |
| | | 75 | | public static ValueTask DecodeVoidReturnValueAsync( |
| | | 76 | | this IncomingResponse response, |
| | | 77 | | OutgoingRequest request, |
| | | 78 | | IIceProxy sender, |
| | | 79 | | IActivator defaultActivator, |
| | | 80 | | CancellationToken cancellationToken = default) |
| | 73 | 81 | | { |
| | 73 | 82 | | IIceFeature feature = request.Features.Get<IIceFeature>() ?? IceFeature.Default; |
| | 73 | 83 | | IActivator activator = feature.Activator ?? defaultActivator; |
| | | 84 | | |
| | 73 | 85 | | return response.StatusCode switch |
| | 73 | 86 | | { |
| | 43 | 87 | | StatusCode.Ok => response.DecodeVoidAsync(feature, cancellationToken), |
| | 16 | 88 | | StatusCode.ApplicationError => DecodeAndThrowExceptionAsync(), |
| | 14 | 89 | | _ => throw new DispatchException(response.StatusCode, response.ErrorMessage) |
| | 14 | 90 | | { |
| | 14 | 91 | | ConvertToInternalError = true |
| | 14 | 92 | | } |
| | 73 | 93 | | }; |
| | | 94 | | |
| | | 95 | | async ValueTask DecodeAndThrowExceptionAsync() => |
| | 16 | 96 | | throw await response.DecodeIceExceptionAsync(feature, sender, activator, cancellationToken) |
| | 16 | 97 | | .ConfigureAwait(false); |
| | 59 | 98 | | } |
| | | 99 | | |
| | | 100 | | private static async ValueTask<IceException> DecodeIceExceptionAsync( |
| | | 101 | | this IncomingResponse response, |
| | | 102 | | IIceFeature feature, |
| | | 103 | | IIceProxy sender, |
| | | 104 | | IActivator activator, |
| | | 105 | | CancellationToken cancellationToken) |
| | 16 | 106 | | { |
| | 16 | 107 | | Debug.Assert(response.StatusCode == StatusCode.ApplicationError); |
| | | 108 | | |
| | 16 | 109 | | ReadResult readResult = await response.Payload.ReadFullPayloadAsync( |
| | 16 | 110 | | feature.MaxPayloadSize, |
| | 16 | 111 | | cancellationToken).ConfigureAwait(false); |
| | | 112 | | |
| | | 113 | | // We never call CancelPendingRead on response.Payload; an interceptor can but it's not correct. |
| | 16 | 114 | | if (readResult.IsCanceled) |
| | 0 | 115 | | { |
| | 0 | 116 | | throw new InvalidOperationException("Unexpected call to CancelPendingRead."); |
| | | 117 | | } |
| | | 118 | | |
| | 16 | 119 | | IceException exception = DecodeBuffer(readResult.Buffer); |
| | 16 | 120 | | response.Payload.AdvanceTo(readResult.Buffer.End); |
| | 16 | 121 | | return exception; |
| | | 122 | | |
| | | 123 | | IceException DecodeBuffer(ReadOnlySequence<byte> buffer) |
| | 16 | 124 | | { |
| | | 125 | | // An Ice exception never sets Message, even when received over icerpc. |
| | | 126 | | |
| | 16 | 127 | | var decoder = new IceDecoder( |
| | 16 | 128 | | buffer, |
| | 16 | 129 | | feature.BaseProxy ?? sender, |
| | 16 | 130 | | maxCollectionAllocation: feature.MaxCollectionAllocation, |
| | 16 | 131 | | activator, |
| | 16 | 132 | | maxDepth: feature.MaxDepth); |
| | | 133 | | |
| | 16 | 134 | | IceException exception = decoder.DecodeException(response.ErrorMessage); |
| | 16 | 135 | | decoder.CheckEndOfBuffer(); |
| | 16 | 136 | | return exception; |
| | 16 | 137 | | } |
| | 16 | 138 | | } |
| | | 139 | | } |