| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Slice.Internal; |
| | | 5 | | using System.IO.Pipelines; |
| | | 6 | | using ZeroC.Slice; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Slice; |
| | | 9 | | |
| | | 10 | | /// <summary>Provides extension methods for <see cref="IncomingRequest" /> to decode its Slice-encoded payload. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class IncomingRequestExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary>The generated code calls this method to ensure that when an operation is not declared idempotent, |
| | | 15 | | /// the request is not marked idempotent. If the request is marked idempotent, it means the caller incorrectly |
| | | 16 | | /// believes this operation is idempotent.</summary> |
| | | 17 | | /// <param name="request">The request to check.</param> |
| | | 18 | | /// <exception cref="InvalidDataException">Thrown if the request contains the <see cref="RequestFieldKey.Idempotent" |
| | | 19 | | /// field.</exception> |
| | | 20 | | public static void CheckNonIdempotent(this IncomingRequest request) |
| | 104 | 21 | | { |
| | 104 | 22 | | if (request.Fields.ContainsKey(RequestFieldKey.Idempotent)) |
| | 0 | 23 | | { |
| | 0 | 24 | | throw new InvalidDataException( |
| | 0 | 25 | | $"Invocation mode mismatch for operation '{request.Operation}': received idempotent field for an operati |
| | | 26 | | } |
| | 104 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary>Creates an outgoing response with status code <see cref="StatusCode.ApplicationError" /> with |
| | | 30 | | /// a Slice exception payload.</summary> |
| | | 31 | | /// <param name="request">The incoming request.</param> |
| | | 32 | | /// <param name="sliceException">The Slice exception to encode in the payload.</param> |
| | | 33 | | /// <param name="encoding">The encoding used for the request payload.</param> |
| | | 34 | | /// <returns>The new outgoing response.</returns> |
| | | 35 | | /// <exception cref="NotSupportedException">Thrown when <paramref name="sliceException" /> does not support encoding |
| | | 36 | | /// <paramref name="encoding" />.</exception> |
| | | 37 | | public static OutgoingResponse CreateSliceExceptionResponse( |
| | | 38 | | this IncomingRequest request, |
| | | 39 | | SliceException sliceException, |
| | | 40 | | SliceEncoding encoding) |
| | 16 | 41 | | { |
| | 16 | 42 | | SliceEncodeOptions encodeOptions = |
| | 16 | 43 | | request.Features.Get<ISliceFeature>()?.EncodeOptions ?? SliceEncodeOptions.Default; |
| | | 44 | | |
| | 16 | 45 | | var pipe = new Pipe(encodeOptions.PipeOptions); |
| | | 46 | | |
| | | 47 | | try |
| | 16 | 48 | | { |
| | 16 | 49 | | var encoder = new SliceEncoder(pipe.Writer, encoding); |
| | | 50 | | |
| | | 51 | | // sliceException.Encode can throw NotSupportedException |
| | 16 | 52 | | if (encoding == SliceEncoding.Slice1) |
| | 16 | 53 | | { |
| | 16 | 54 | | sliceException.Encode(ref encoder); |
| | 16 | 55 | | } |
| | | 56 | | else |
| | 0 | 57 | | { |
| | 0 | 58 | | Span<byte> sizePlaceholder = encoder.GetPlaceholderSpan(4); |
| | 0 | 59 | | int startPos = encoder.EncodedByteCount; |
| | 0 | 60 | | sliceException.Encode(ref encoder); |
| | 0 | 61 | | SliceEncoder.EncodeVarUInt62((ulong)(encoder.EncodedByteCount - startPos), sizePlaceholder); |
| | 0 | 62 | | } |
| | | 63 | | |
| | 16 | 64 | | pipe.Writer.Complete(); |
| | | 65 | | |
| | 16 | 66 | | return new OutgoingResponse(request, StatusCode.ApplicationError, GetErrorMessage(sliceException)) |
| | 16 | 67 | | { |
| | 16 | 68 | | Payload = pipe.Reader |
| | 16 | 69 | | }; |
| | | 70 | | } |
| | 0 | 71 | | catch |
| | 0 | 72 | | { |
| | 0 | 73 | | pipe.Reader.Complete(); |
| | 0 | 74 | | pipe.Writer.Complete(); |
| | 0 | 75 | | throw; |
| | | 76 | | } |
| | 16 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary>Decodes a request payload into a list of arguments.</summary> |
| | | 80 | | /// <typeparam name="T">The type of the request parameters.</typeparam> |
| | | 81 | | /// <param name="request">The incoming request.</param> |
| | | 82 | | /// <param name="encoding">The encoding of the request's payload.</param> |
| | | 83 | | /// <param name="decodeFunc">The decode function for the arguments from the payload.</param> |
| | | 84 | | /// <param name="defaultActivator">The activator to use when the activator provided by the request's <see |
| | | 85 | | /// cref="ISliceFeature" /> is <see langword="null" />. Used only when <paramref name="encoding" /> is <see |
| | | 86 | | /// cref="SliceEncoding.Slice1" />.</param> |
| | | 87 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 88 | | /// <returns>The request arguments.</returns> |
| | | 89 | | public static ValueTask<T> DecodeArgsAsync<T>( |
| | | 90 | | this IncomingRequest request, |
| | | 91 | | SliceEncoding encoding, |
| | | 92 | | DecodeFunc<T> decodeFunc, |
| | | 93 | | IActivator? defaultActivator = null, |
| | | 94 | | CancellationToken cancellationToken = default) |
| | 88 | 95 | | { |
| | 88 | 96 | | ISliceFeature feature = request.Features.Get<ISliceFeature>() ?? SliceFeature.Default; |
| | | 97 | | |
| | 88 | 98 | | return request.DecodeValueAsync( |
| | 88 | 99 | | encoding, |
| | 88 | 100 | | feature, |
| | 88 | 101 | | feature.BaseProxy, |
| | 88 | 102 | | decodeFunc, |
| | 88 | 103 | | feature.Activator ?? defaultActivator, |
| | 88 | 104 | | cancellationToken); |
| | 88 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary>Verifies that a request payload carries no argument or only unknown tagged arguments.</summary> |
| | | 108 | | /// <param name="request">The incoming request.</param> |
| | | 109 | | /// <param name="encoding">The encoding of the request payload.</param> |
| | | 110 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 111 | | /// <returns>A value task that completes when the checking is complete.</returns> |
| | | 112 | | public static ValueTask DecodeEmptyArgsAsync( |
| | | 113 | | this IncomingRequest request, |
| | | 114 | | SliceEncoding encoding, |
| | | 115 | | CancellationToken cancellationToken = default) => |
| | 75 | 116 | | request.DecodeVoidAsync( |
| | 75 | 117 | | encoding, |
| | 75 | 118 | | request.Features.Get<ISliceFeature>() ?? SliceFeature.Default, |
| | 75 | 119 | | cancellationToken); |
| | | 120 | | |
| | | 121 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name.</summary> |
| | | 122 | | /// <typeparam name="TArgs">The type of the operation arguments.</typeparam> |
| | | 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 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="encodeReturnValueStream">A function that encodes the stream portion of the return value.</param> |
| | | 129 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Slice |
| | | 130 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 131 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 132 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 133 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TArgs, TReturnValue>( |
| | | 134 | | this IncomingRequest request, |
| | | 135 | | Func<IncomingRequest, CancellationToken, ValueTask<TArgs>> decodeArgs, |
| | | 136 | | Func<TArgs, IFeatureCollection, CancellationToken, ValueTask<TReturnValue>> method, |
| | | 137 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader> encodeReturnValue, |
| | | 138 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader>? encodeReturnValueStream = null, |
| | | 139 | | Func<SliceException, bool>? inExceptionSpecification = null, |
| | | 140 | | CancellationToken cancellationToken = default) |
| | 33 | 141 | | { |
| | 33 | 142 | | TArgs args = await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 143 | | try |
| | 33 | 144 | | { |
| | 33 | 145 | | TReturnValue returnValue = await method(args, request.Features, cancellationToken).ConfigureAwait(false); |
| | 33 | 146 | | return new OutgoingResponse(request) |
| | 33 | 147 | | { |
| | 33 | 148 | | Payload = encodeReturnValue(returnValue, request.Features.Get<ISliceFeature>()?.EncodeOptions), |
| | 33 | 149 | | PayloadContinuation = |
| | 33 | 150 | | encodeReturnValueStream?.Invoke(returnValue, request.Features.Get<ISliceFeature>()?.EncodeOptions) |
| | 33 | 151 | | }; |
| | | 152 | | } |
| | 0 | 153 | | catch (SliceException sliceException) when (inExceptionSpecification?.Invoke(sliceException) ?? false) |
| | 0 | 154 | | { |
| | 0 | 155 | | return request.CreateSliceExceptionResponse(sliceException, SliceEncoding.Slice1); |
| | | 156 | | } |
| | 33 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 160 | | /// does not accept any arguments.</summary> |
| | | 161 | | /// <typeparam name="TReturnValue">The type of the operation return value.</typeparam> |
| | | 162 | | /// <param name="request">The incoming request.</param> |
| | | 163 | | /// <param name="decodeArgs">A function that decodes the empty arguments from the request payload.</param> |
| | | 164 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 165 | | /// <param name="encodeReturnValue">A function that encodes the return value into a PipeReader.</param> |
| | | 166 | | /// <param name="encodeReturnValueStream">A function that encodes the stream portion of the return value.</param> |
| | | 167 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Slice |
| | | 168 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 169 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 170 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 171 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TReturnValue>( |
| | | 172 | | this IncomingRequest request, |
| | | 173 | | Func<IncomingRequest, CancellationToken, ValueTask> decodeArgs, |
| | | 174 | | Func<IFeatureCollection, CancellationToken, ValueTask<TReturnValue>> method, |
| | | 175 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader> encodeReturnValue, |
| | | 176 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader>? encodeReturnValueStream = null, |
| | | 177 | | Func<SliceException, bool>? inExceptionSpecification = null, |
| | | 178 | | CancellationToken cancellationToken = default) |
| | 15 | 179 | | { |
| | 15 | 180 | | await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 181 | | try |
| | 15 | 182 | | { |
| | 15 | 183 | | TReturnValue returnValue = await method(request.Features, cancellationToken).ConfigureAwait(false); |
| | 15 | 184 | | return new OutgoingResponse(request) |
| | 15 | 185 | | { |
| | 15 | 186 | | Payload = encodeReturnValue(returnValue, request.Features.Get<ISliceFeature>()?.EncodeOptions), |
| | 15 | 187 | | PayloadContinuation = encodeReturnValueStream?.Invoke(returnValue, request.Features.Get<ISliceFeature>() |
| | 15 | 188 | | }; |
| | | 189 | | } |
| | 0 | 190 | | catch (SliceException sliceException) when (inExceptionSpecification?.Invoke(sliceException) ?? false) |
| | 0 | 191 | | { |
| | 0 | 192 | | return request.CreateSliceExceptionResponse(sliceException, SliceEncoding.Slice1); |
| | | 193 | | } |
| | 15 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 197 | | /// does not return anything.</summary> |
| | | 198 | | /// <typeparam name="TArgs">The type of the operation arguments.</typeparam> |
| | | 199 | | /// <param name="request">The incoming request.</param> |
| | | 200 | | /// <param name="decodeArgs">A function that decodes the arguments from the request payload.</param> |
| | | 201 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 202 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Slice |
| | | 203 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 204 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 205 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 206 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TArgs>( |
| | | 207 | | this IncomingRequest request, |
| | | 208 | | Func<IncomingRequest, CancellationToken, ValueTask<TArgs>> decodeArgs, |
| | | 209 | | Func<TArgs, IFeatureCollection, CancellationToken, ValueTask> method, |
| | | 210 | | Func<SliceException, bool>? inExceptionSpecification = null, |
| | | 211 | | CancellationToken cancellationToken = default) |
| | 14 | 212 | | { |
| | 14 | 213 | | TArgs args = await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 214 | | try |
| | 14 | 215 | | { |
| | 14 | 216 | | await method(args, request.Features, cancellationToken).ConfigureAwait(false); |
| | 14 | 217 | | return new OutgoingResponse(request); |
| | | 218 | | } |
| | 0 | 219 | | catch (SliceException sliceException) when (inExceptionSpecification?.Invoke(sliceException) ?? false) |
| | 0 | 220 | | { |
| | 0 | 221 | | return request.CreateSliceExceptionResponse(sliceException, SliceEncoding.Slice1); |
| | | 222 | | } |
| | 14 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 226 | | /// does not accept any arguments and does not return anything.</summary> |
| | | 227 | | /// <param name="request">The incoming request.</param> |
| | | 228 | | /// <param name="decodeArgs">A function that decodes the empty arguments from the request payload.</param> |
| | | 229 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 230 | | /// <param name="inExceptionSpecification">A function that returns <see langword="true" /> when the provided Slice |
| | | 231 | | /// exception conforms to the exception specification; otherwise, <see langword="false" />.</param> |
| | | 232 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 233 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 234 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync( |
| | | 235 | | this IncomingRequest request, |
| | | 236 | | Func<IncomingRequest, CancellationToken, ValueTask> decodeArgs, |
| | | 237 | | Func<IFeatureCollection, CancellationToken, ValueTask> method, |
| | | 238 | | Func<SliceException, bool>? inExceptionSpecification = null, |
| | | 239 | | CancellationToken cancellationToken = default) |
| | 54 | 240 | | { |
| | 54 | 241 | | await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | | 242 | | try |
| | 54 | 243 | | { |
| | 54 | 244 | | await method(request.Features, cancellationToken).ConfigureAwait(false); |
| | 25 | 245 | | return new OutgoingResponse(request); |
| | | 246 | | } |
| | 21 | 247 | | catch (SliceException sliceException) when (inExceptionSpecification?.Invoke(sliceException) ?? false) |
| | 16 | 248 | | { |
| | 16 | 249 | | return request.CreateSliceExceptionResponse(sliceException, SliceEncoding.Slice1); |
| | | 250 | | } |
| | 41 | 251 | | } |
| | | 252 | | |
| | | 253 | | // The error message includes the inner exception type and message because we don't transmit this inner exception |
| | | 254 | | // with the response. |
| | | 255 | | private static string GetErrorMessage(SliceException exception) => |
| | 16 | 256 | | exception.InnerException is Exception innerException ? |
| | 16 | 257 | | $"{exception.Message} This exception was caused by an exception of type '{innerException.GetType()}' with me |
| | 16 | 258 | | exception.Message; |
| | | 259 | | } |