| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using Google.Protobuf; |
| | | 4 | | using IceRpc.Features; |
| | | 5 | | using IceRpc.Protobuf.RpcMethods.Internal; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Protobuf.RpcMethods; |
| | | 8 | | |
| | | 9 | | /// <summary>Provides extension methods for <see cref="IncomingRequest" />.</summary> |
| | | 10 | | /// <remarks>These methods are called by the <see cref="IDispatcher"/> implementation generated by the Protobuf service |
| | | 11 | | /// generator.</remarks> |
| | | 12 | | public static class IncomingRequestExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary>Dispatches a unary RPC method.</summary> |
| | | 15 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | | 16 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | | 17 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | | 18 | | /// <param name="request">The incoming request.</param> |
| | | 19 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | | 20 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | | 21 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | | 22 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | | 23 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 24 | | public static async ValueTask<OutgoingResponse> DispatchUnaryAsync<TInput, TOutput, TService>( |
| | | 25 | | this IncomingRequest request, |
| | | 26 | | MessageParser<TInput> inputParser, |
| | | 27 | | TService service, |
| | | 28 | | Func<TService, TInput, IFeatureCollection, CancellationToken, ValueTask<TOutput>> method, |
| | | 29 | | CancellationToken cancellationToken) where TInput : class, IMessage<TInput> |
| | | 30 | | where TOutput : class, IMessage<TOutput> |
| | | 31 | | where TService : class |
| | 14 | 32 | | { |
| | 14 | 33 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | | 34 | | |
| | 14 | 35 | | TInput input = await request.Payload.DecodeProtobufMessageAsync( |
| | 14 | 36 | | inputParser, |
| | 14 | 37 | | protobufFeature.MaxMessageLength, |
| | 14 | 38 | | acceptEmptyPayload: false, |
| | 14 | 39 | | cancellationToken).ConfigureAwait(false); |
| | | 40 | | |
| | 13 | 41 | | TOutput output = await method(service, input, request.Features, cancellationToken).ConfigureAwait(false); |
| | | 42 | | |
| | 13 | 43 | | return new OutgoingResponse(request) |
| | 13 | 44 | | { |
| | 13 | 45 | | Payload = output.EncodeAsLengthPrefixedMessage( |
| | 13 | 46 | | protobufFeature.EncodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions) |
| | 13 | 47 | | }; |
| | 13 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary>Dispatches a client-streaming RPC method.</summary> |
| | | 51 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | | 52 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | | 53 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | | 54 | | /// <param name="request">The incoming request.</param> |
| | | 55 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | | 56 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | | 57 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | | 58 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | | 59 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 60 | | public static async ValueTask<OutgoingResponse> DispatchClientStreamingAsync<TInput, TOutput, TService>( |
| | | 61 | | this IncomingRequest request, |
| | | 62 | | MessageParser<TInput> inputParser, |
| | | 63 | | TService service, |
| | | 64 | | Func<TService, IAsyncStream<TInput>, IFeatureCollection, CancellationToken, ValueTask<TOutput>> method, |
| | | 65 | | CancellationToken cancellationToken) where TInput : class, IMessage<TInput> |
| | | 66 | | where TOutput : class, IMessage<TOutput> |
| | | 67 | | where TService : class |
| | 3 | 68 | | { |
| | 3 | 69 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | | 70 | | |
| | | 71 | | // Ownership of the input stream is transferred to the user method. |
| | | 72 | | #pragma warning disable CA2000 // Dispose objects before losing scope |
| | 3 | 73 | | IAsyncStream<TInput> input = request.DetachPayload().ToAsyncStream( |
| | 3 | 74 | | inputParser, |
| | 3 | 75 | | protobufFeature.MaxMessageLength); |
| | | 76 | | #pragma warning restore CA2000 |
| | | 77 | | |
| | 3 | 78 | | TOutput output = await method(service, input, request.Features, cancellationToken).ConfigureAwait(false); |
| | | 79 | | |
| | 3 | 80 | | return new OutgoingResponse(request) |
| | 3 | 81 | | { |
| | 3 | 82 | | Payload = output.EncodeAsLengthPrefixedMessage( |
| | 3 | 83 | | protobufFeature.EncodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions) |
| | 3 | 84 | | }; |
| | 3 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary>Dispatches a server-streaming RPC method.</summary> |
| | | 88 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | | 89 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | | 90 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | | 91 | | /// <param name="request">The incoming request.</param> |
| | | 92 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | | 93 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | | 94 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | | 95 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | | 96 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 97 | | public static async ValueTask<OutgoingResponse> DispatchServerStreamingAsync<TInput, TOutput, TService>( |
| | | 98 | | this IncomingRequest request, |
| | | 99 | | MessageParser<TInput> inputParser, |
| | | 100 | | TService service, |
| | | 101 | | Func<TService, TInput, IFeatureCollection, CancellationToken, ValueTask<IAsyncEnumerable<TOutput>>> method, |
| | | 102 | | CancellationToken cancellationToken) where TInput : class, IMessage<TInput> |
| | | 103 | | where TOutput : class, IMessage<TOutput> |
| | | 104 | | where TService : class |
| | 3 | 105 | | { |
| | 3 | 106 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | | 107 | | |
| | 3 | 108 | | TInput input = await request.Payload.DecodeProtobufMessageAsync( |
| | 3 | 109 | | inputParser, |
| | 3 | 110 | | protobufFeature.MaxMessageLength, |
| | 3 | 111 | | acceptEmptyPayload: false, |
| | 3 | 112 | | cancellationToken).ConfigureAwait(false); |
| | | 113 | | |
| | 3 | 114 | | IAsyncEnumerable<TOutput> output = await method(service, input, request.Features, cancellationToken) |
| | 3 | 115 | | .ConfigureAwait(false); |
| | | 116 | | |
| | 3 | 117 | | return new OutgoingResponse(request) |
| | 3 | 118 | | { |
| | 3 | 119 | | PayloadContinuation = output.ToPipeReader(protobufFeature.EncodeOptions) |
| | 3 | 120 | | }; |
| | 3 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary>Dispatches a bidi-streaming RPC method.</summary> |
| | | 124 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | | 125 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | | 126 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | | 127 | | /// <param name="request">The incoming request.</param> |
| | | 128 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | | 129 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | | 130 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | | 131 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | | 132 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 133 | | public static async ValueTask<OutgoingResponse> DispatchBidiStreamingAsync<TInput, TOutput, TService>( |
| | | 134 | | this IncomingRequest request, |
| | | 135 | | MessageParser<TInput> inputParser, |
| | | 136 | | TService service, |
| | | 137 | | Func<TService, IAsyncStream<TInput>, IFeatureCollection, CancellationToken, ValueTask<IAsyncEnumerable<TOutput>> |
| | | 138 | | CancellationToken cancellationToken) where TInput : class, IMessage<TInput> |
| | | 139 | | where TOutput : class, IMessage<TOutput> |
| | | 140 | | where TService : class |
| | 3 | 141 | | { |
| | 3 | 142 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | | 143 | | |
| | | 144 | | // Ownership of the input stream is transferred to the user method. |
| | | 145 | | #pragma warning disable CA2000 // Dispose objects before losing scope |
| | 3 | 146 | | IAsyncStream<TInput> input = request.DetachPayload().ToAsyncStream( |
| | 3 | 147 | | inputParser, |
| | 3 | 148 | | protobufFeature.MaxMessageLength); |
| | | 149 | | #pragma warning restore CA2000 |
| | | 150 | | |
| | 3 | 151 | | IAsyncEnumerable<TOutput> output = |
| | 3 | 152 | | await method(service, input, request.Features, cancellationToken).ConfigureAwait(false); |
| | | 153 | | |
| | 3 | 154 | | return new OutgoingResponse(request) |
| | 3 | 155 | | { |
| | 3 | 156 | | PayloadContinuation = output.ToPipeReader(protobufFeature.EncodeOptions) |
| | 3 | 157 | | }; |
| | 3 | 158 | | } |
| | | 159 | | } |