| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Ice.Codec; |
| | | 5 | | using IceRpc.Ice.Operations.Internal; |
| | | 6 | | using System.IO.Pipelines; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Ice.Operations; |
| | | 9 | | |
| | | 10 | | /// <summary>Provides extension methods for <see cref="IncomingRequest" /> to decode its Ice-encoded payload. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class IncomingRequestExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary>Creates an outgoing response with status code <see cref="StatusCode.ApplicationError" /> with an Ice |
| | | 15 | | /// exception payload.</summary> |
| | | 16 | | /// <param name="request">The incoming request.</param> |
| | | 17 | | /// <param name="iceException">The Ice exception to encode in the payload.</param> |
| | | 18 | | /// <returns>The new outgoing response.</returns> |
| | | 19 | | public static OutgoingResponse CreateIceExceptionResponse( |
| | | 20 | | this IncomingRequest request, |
| | | 21 | | IceException iceException) |
| | 16 | 22 | | { |
| | 16 | 23 | | IceEncodeOptions encodeOptions = |
| | 16 | 24 | | request.Features.Get<IIceFeature>()?.EncodeOptions ?? IceEncodeOptions.Default; |
| | | 25 | | |
| | 16 | 26 | | var pipe = new Pipe(encodeOptions.PipeOptions); |
| | | 27 | | |
| | | 28 | | try |
| | 16 | 29 | | { |
| | 16 | 30 | | var encoder = new IceEncoder(pipe.Writer); |
| | 16 | 31 | | iceException.Encode(ref encoder); |
| | 16 | 32 | | pipe.Writer.Complete(); |
| | | 33 | | |
| | | 34 | | // By default, the generated Ice exceptions don't set a custom message and don't support setting an inner |
| | | 35 | | // exception. However, Message can still be overridden, so the value transmitted over icerpc is whatever |
| | | 36 | | // iceException.Message returns. |
| | | 37 | | // The icerpc client uses this message when it can't decode the Ice exception. See |
| | | 38 | | // IceDecoder.DecodeException for more details. |
| | 16 | 39 | | return new OutgoingResponse(request, StatusCode.ApplicationError, iceException.Message) |
| | 16 | 40 | | { |
| | 16 | 41 | | Payload = pipe.Reader |
| | 16 | 42 | | }; |
| | | 43 | | } |
| | 0 | 44 | | catch |
| | 0 | 45 | | { |
| | 0 | 46 | | pipe.Reader.Complete(); |
| | 0 | 47 | | pipe.Writer.Complete(); |
| | 0 | 48 | | throw; |
| | | 49 | | } |
| | 16 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Decodes a request payload into a list of arguments.</summary> |
| | | 53 | | /// <typeparam name="T">The type of the request parameters.</typeparam> |
| | | 54 | | /// <param name="request">The incoming request.</param> |
| | | 55 | | /// <param name="decodeFunc">The decode function for the arguments from the payload.</param> |
| | | 56 | | /// <param name="defaultActivator">The activator to use when the activator provided by the request's |
| | | 57 | | /// <see cref="IIceFeature" /> is <see langword="null" />.</param> |
| | | 58 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 59 | | /// <returns>The request arguments.</returns> |
| | | 60 | | public static ValueTask<T> DecodeArgsAsync<T>( |
| | | 61 | | this IncomingRequest request, |
| | | 62 | | DecodeFunc<T> decodeFunc, |
| | | 63 | | IActivator defaultActivator, |
| | | 64 | | CancellationToken cancellationToken = default) |
| | 55 | 65 | | { |
| | 55 | 66 | | IIceFeature feature = request.Features.Get<IIceFeature>() ?? IceFeature.Default; |
| | | 67 | | |
| | 55 | 68 | | return request.DecodeValueAsync( |
| | 55 | 69 | | feature, |
| | 55 | 70 | | feature.BaseProxy, |
| | 55 | 71 | | decodeFunc, |
| | 55 | 72 | | feature.Activator ?? defaultActivator, |
| | 55 | 73 | | cancellationToken); |
| | 55 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary>Verifies that a request payload carries no argument or only unknown tagged arguments.</summary> |
| | | 77 | | /// <param name="request">The incoming request.</param> |
| | | 78 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 79 | | /// <returns>A value task that completes when the checking is complete.</returns> |
| | | 80 | | public static ValueTask DecodeEmptyArgsAsync( |
| | | 81 | | this IncomingRequest request, |
| | | 82 | | CancellationToken cancellationToken = default) => |
| | 69 | 83 | | request.DecodeVoidAsync( |
| | 69 | 84 | | request.Features.Get<IIceFeature>() ?? IceFeature.Default, |
| | 69 | 85 | | cancellationToken); |
| | | 86 | | |
| | | 87 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name.</summary> |
| | | 88 | | /// <typeparam name="TArgs">The type of the operation arguments.</typeparam> |
| | | 89 | | /// <typeparam name="TReturnValue">The type of the operation return value.</typeparam> |
| | | 90 | | /// <param name="request">The incoming request.</param> |
| | | 91 | | /// <param name="decodeArgs">A function that decodes the arguments from the request payload.</param> |
| | | 92 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 93 | | /// <param name="encodeReturnValue">A function that encodes the return value into a PipeReader.</param> |
| | | 94 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Ice |
| | | 95 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 96 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 97 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 98 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TArgs, TReturnValue>( |
| | | 99 | | this IncomingRequest request, |
| | | 100 | | Func<IncomingRequest, CancellationToken, ValueTask<TArgs>> decodeArgs, |
| | | 101 | | Func<TArgs, IFeatureCollection, CancellationToken, ValueTask<TReturnValue>> method, |
| | | 102 | | Func<TReturnValue, IceEncodeOptions?, PipeReader> encodeReturnValue, |
| | | 103 | | Func<IceException, bool>? inExceptionSpecification = null, |
| | | 104 | | CancellationToken cancellationToken = default) |
| | 22 | 105 | | { |
| | 22 | 106 | | TArgs args = await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 107 | | try |
| | 22 | 108 | | { |
| | 22 | 109 | | TReturnValue returnValue = await method(args, request.Features, cancellationToken).ConfigureAwait(false); |
| | 22 | 110 | | return new OutgoingResponse(request) |
| | 22 | 111 | | { |
| | 22 | 112 | | Payload = encodeReturnValue(returnValue, request.Features.Get<IIceFeature>()?.EncodeOptions), |
| | 22 | 113 | | }; |
| | | 114 | | } |
| | 0 | 115 | | catch (IceException iceException) when (inExceptionSpecification?.Invoke(iceException) ?? false) |
| | 0 | 116 | | { |
| | 0 | 117 | | return request.CreateIceExceptionResponse(iceException); |
| | | 118 | | } |
| | 22 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 122 | | /// does not accept any arguments.</summary> |
| | | 123 | | /// <typeparam name="TReturnValue">The type of the operation return value.</typeparam> |
| | | 124 | | /// <param name="request">The incoming request.</param> |
| | | 125 | | /// <param name="decodeArgs">A function that decodes the empty arguments from the request payload.</param> |
| | | 126 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 127 | | /// <param name="encodeReturnValue">A function that encodes the return value into a PipeReader.</param> |
| | | 128 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Ice |
| | | 129 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 130 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 131 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 132 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TReturnValue>( |
| | | 133 | | this IncomingRequest request, |
| | | 134 | | Func<IncomingRequest, CancellationToken, ValueTask> decodeArgs, |
| | | 135 | | Func<IFeatureCollection, CancellationToken, ValueTask<TReturnValue>> method, |
| | | 136 | | Func<TReturnValue, IceEncodeOptions?, PipeReader> encodeReturnValue, |
| | | 137 | | Func<IceException, bool>? inExceptionSpecification = null, |
| | | 138 | | CancellationToken cancellationToken = default) |
| | 14 | 139 | | { |
| | 14 | 140 | | await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 141 | | try |
| | 14 | 142 | | { |
| | 14 | 143 | | TReturnValue returnValue = await method(request.Features, cancellationToken).ConfigureAwait(false); |
| | 14 | 144 | | return new OutgoingResponse(request) |
| | 14 | 145 | | { |
| | 14 | 146 | | Payload = encodeReturnValue(returnValue, request.Features.Get<IIceFeature>()?.EncodeOptions) |
| | 14 | 147 | | }; |
| | | 148 | | } |
| | 0 | 149 | | catch (IceException iceException) when (inExceptionSpecification?.Invoke(iceException) ?? false) |
| | 0 | 150 | | { |
| | 0 | 151 | | return request.CreateIceExceptionResponse(iceException); |
| | | 152 | | } |
| | 14 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 156 | | /// does not return anything.</summary> |
| | | 157 | | /// <typeparam name="TArgs">The type of the operation arguments.</typeparam> |
| | | 158 | | /// <param name="request">The incoming request.</param> |
| | | 159 | | /// <param name="decodeArgs">A function that decodes the arguments from the request payload.</param> |
| | | 160 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 161 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Ice |
| | | 162 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 163 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 164 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 165 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TArgs>( |
| | | 166 | | this IncomingRequest request, |
| | | 167 | | Func<IncomingRequest, CancellationToken, ValueTask<TArgs>> decodeArgs, |
| | | 168 | | Func<TArgs, IFeatureCollection, CancellationToken, ValueTask> method, |
| | | 169 | | Func<IceException, bool>? inExceptionSpecification = null, |
| | | 170 | | CancellationToken cancellationToken = default) |
| | 16 | 171 | | { |
| | 16 | 172 | | TArgs args = await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 173 | | try |
| | 16 | 174 | | { |
| | 16 | 175 | | await method(args, request.Features, cancellationToken).ConfigureAwait(false); |
| | 16 | 176 | | return new OutgoingResponse(request); |
| | | 177 | | } |
| | 0 | 178 | | catch (IceException iceException) when (inExceptionSpecification?.Invoke(iceException) ?? false) |
| | 0 | 179 | | { |
| | 0 | 180 | | return request.CreateIceExceptionResponse(iceException); |
| | | 181 | | } |
| | 16 | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 185 | | /// does not accept any arguments and does not return anything.</summary> |
| | | 186 | | /// <param name="request">The incoming request.</param> |
| | | 187 | | /// <param name="decodeArgs">A function that decodes the empty arguments from the request payload.</param> |
| | | 188 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 189 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Ice |
| | | 190 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 191 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 192 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 193 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync( |
| | | 194 | | this IncomingRequest request, |
| | | 195 | | Func<IncomingRequest, CancellationToken, ValueTask> decodeArgs, |
| | | 196 | | Func<IFeatureCollection, CancellationToken, ValueTask> method, |
| | | 197 | | Func<IceException, bool>? inExceptionSpecification = null, |
| | | 198 | | CancellationToken cancellationToken = default) |
| | 55 | 199 | | { |
| | 55 | 200 | | await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 201 | | try |
| | 55 | 202 | | { |
| | 55 | 203 | | await method(request.Features, cancellationToken).ConfigureAwait(false); |
| | 26 | 204 | | return new OutgoingResponse(request); |
| | | 205 | | } |
| | 21 | 206 | | catch (IceException iceException) when (inExceptionSpecification?.Invoke(iceException) ?? false) |
| | 16 | 207 | | { |
| | 16 | 208 | | return request.CreateIceExceptionResponse(iceException); |
| | | 209 | | } |
| | 42 | 210 | | } |
| | | 211 | | } |