| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Features; |
| | | 4 | | using IceRpc.Slice.Operations.Internal; |
| | | 5 | | using System.IO.Pipelines; |
| | | 6 | | using ZeroC.Slice.Codec; |
| | | 7 | | |
| | | 8 | | namespace IceRpc.Slice.Operations; |
| | | 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>Decodes a request payload into a list of arguments.</summary> |
| | | 15 | | /// <typeparam name="T">The type of the request parameters.</typeparam> |
| | | 16 | | /// <param name="request">The incoming request.</param> |
| | | 17 | | /// <param name="decodeFunc">The decode function for the arguments from the payload.</param> |
| | | 18 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 19 | | /// <returns>The request arguments.</returns> |
| | | 20 | | public static ValueTask<T> DecodeArgsAsync<T>( |
| | | 21 | | this IncomingRequest request, |
| | | 22 | | DecodeFunc<T> decodeFunc, |
| | | 23 | | CancellationToken cancellationToken = default) |
| | 64 | 24 | | { |
| | 64 | 25 | | ISliceFeature feature = request.Features.Get<ISliceFeature>() ?? SliceFeature.Default; |
| | | 26 | | |
| | 64 | 27 | | return request.DecodeValueAsync( |
| | 64 | 28 | | feature, |
| | 64 | 29 | | feature.BaseProxy, |
| | 64 | 30 | | decodeFunc, |
| | 64 | 31 | | cancellationToken); |
| | 64 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Verifies that a request payload carries no argument or only unknown tagged arguments.</summary> |
| | | 35 | | /// <param name="request">The incoming request.</param> |
| | | 36 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 37 | | /// <returns>A value task that completes when the checking is complete.</returns> |
| | | 38 | | public static ValueTask DecodeEmptyArgsAsync( |
| | | 39 | | this IncomingRequest request, |
| | | 40 | | CancellationToken cancellationToken = default) => |
| | 17 | 41 | | request.DecodeVoidAsync( |
| | 17 | 42 | | request.Features.Get<ISliceFeature>() ?? SliceFeature.Default, |
| | 17 | 43 | | cancellationToken); |
| | | 44 | | |
| | | 45 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name.</summary> |
| | | 46 | | /// <typeparam name="TArgs">The type of the operation arguments.</typeparam> |
| | | 47 | | /// <typeparam name="TReturnValue">The type of the operation return value.</typeparam> |
| | | 48 | | /// <param name="request">The incoming request.</param> |
| | | 49 | | /// <param name="decodeArgs">A function that decodes the arguments from the request payload.</param> |
| | | 50 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 51 | | /// <param name="encodeReturnValue">A function that encodes the return value into a PipeReader.</param> |
| | | 52 | | /// <param name="encodeReturnValueStream">A function that encodes the stream portion of the return value.</param> |
| | | 53 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 54 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 55 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TArgs, TReturnValue>( |
| | | 56 | | this IncomingRequest request, |
| | | 57 | | Func<IncomingRequest, CancellationToken, ValueTask<TArgs>> decodeArgs, |
| | | 58 | | Func<TArgs, IFeatureCollection, CancellationToken, ValueTask<TReturnValue>> method, |
| | | 59 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader> encodeReturnValue, |
| | | 60 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader>? encodeReturnValueStream = null, |
| | | 61 | | CancellationToken cancellationToken = default) |
| | 17 | 62 | | { |
| | 17 | 63 | | TArgs args = await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | 17 | 64 | | TReturnValue returnValue = await method(args, request.Features, cancellationToken).ConfigureAwait(false); |
| | | 65 | | |
| | 17 | 66 | | return new OutgoingResponse(request) |
| | 17 | 67 | | { |
| | 17 | 68 | | Payload = encodeReturnValue(returnValue, request.Features.Get<ISliceFeature>()?.EncodeOptions), |
| | 17 | 69 | | PayloadContinuation = |
| | 17 | 70 | | encodeReturnValueStream?.Invoke(returnValue, request.Features.Get<ISliceFeature>()?.EncodeOptions) |
| | 17 | 71 | | }; |
| | 17 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 75 | | /// does not accept any arguments.</summary> |
| | | 76 | | /// <typeparam name="TReturnValue">The type of the operation return value.</typeparam> |
| | | 77 | | /// <param name="request">The incoming request.</param> |
| | | 78 | | /// <param name="decodeArgs">A function that decodes the empty arguments from the request payload.</param> |
| | | 79 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 80 | | /// <param name="encodeReturnValue">A function that encodes the return value into a PipeReader.</param> |
| | | 81 | | /// <param name="encodeReturnValueStream">A function that encodes the stream portion of the return value.</param> |
| | | 82 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 83 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 84 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TReturnValue>( |
| | | 85 | | this IncomingRequest request, |
| | | 86 | | Func<IncomingRequest, CancellationToken, ValueTask> decodeArgs, |
| | | 87 | | Func<IFeatureCollection, CancellationToken, ValueTask<TReturnValue>> method, |
| | | 88 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader> encodeReturnValue, |
| | | 89 | | Func<TReturnValue, SliceEncodeOptions?, PipeReader>? encodeReturnValueStream = null, |
| | | 90 | | CancellationToken cancellationToken = default) |
| | 5 | 91 | | { |
| | 5 | 92 | | await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | 5 | 93 | | TReturnValue returnValue = await method(request.Features, cancellationToken).ConfigureAwait(false); |
| | 5 | 94 | | return new OutgoingResponse(request) |
| | 5 | 95 | | { |
| | 5 | 96 | | Payload = encodeReturnValue(returnValue, request.Features.Get<ISliceFeature>()?.EncodeOptions), |
| | 5 | 97 | | PayloadContinuation = encodeReturnValueStream?.Invoke(returnValue, request.Features.Get<ISliceFeature>()?.En |
| | 5 | 98 | | }; |
| | 5 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 102 | | /// does not return anything.</summary> |
| | | 103 | | /// <typeparam name="TArgs">The type of the operation arguments.</typeparam> |
| | | 104 | | /// <param name="request">The incoming request.</param> |
| | | 105 | | /// <param name="decodeArgs">A function that decodes the arguments from the request payload.</param> |
| | | 106 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 107 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 108 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 109 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync<TArgs>( |
| | | 110 | | this IncomingRequest request, |
| | | 111 | | Func<IncomingRequest, CancellationToken, ValueTask<TArgs>> decodeArgs, |
| | | 112 | | Func<TArgs, IFeatureCollection, CancellationToken, ValueTask> method, |
| | | 113 | | CancellationToken cancellationToken = default) |
| | 7 | 114 | | { |
| | 7 | 115 | | TArgs args = await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | 7 | 116 | | await method(args, request.Features, cancellationToken).ConfigureAwait(false); |
| | 7 | 117 | | return new OutgoingResponse(request); |
| | 7 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary>Dispatches an incoming request to a method that matches the request's operation name. The operation |
| | | 121 | | /// does not accept any arguments and does not return anything.</summary> |
| | | 122 | | /// <param name="request">The incoming request.</param> |
| | | 123 | | /// <param name="decodeArgs">A function that decodes the empty arguments from the request payload.</param> |
| | | 124 | | /// <param name="method">The user-provided implementation of the operation.</param> |
| | | 125 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 126 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 127 | | public static async ValueTask<OutgoingResponse> DispatchOperationAsync( |
| | | 128 | | this IncomingRequest request, |
| | | 129 | | Func<IncomingRequest, CancellationToken, ValueTask> decodeArgs, |
| | | 130 | | Func<IFeatureCollection, CancellationToken, ValueTask> method, |
| | | 131 | | CancellationToken cancellationToken = default) |
| | 6 | 132 | | { |
| | 6 | 133 | | await decodeArgs(request, cancellationToken).ConfigureAwait(false); |
| | 6 | 134 | | await method(request.Features, cancellationToken).ConfigureAwait(false); |
| | 6 | 135 | | return new OutgoingResponse(request); |
| | 6 | 136 | | } |
| | | 137 | | } |