| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using Google.Protobuf; |
| | 4 | | using IceRpc.Features; |
| | 5 | | using IceRpc.Protobuf.Internal; |
| | 6 | |
|
| | 7 | | namespace IceRpc.Protobuf; |
| | 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 : IMessage<TInput> |
| | 30 | | where TOutput : IMessage<TOutput> |
| | 31 | | where TService : class |
| 10 | 32 | | { |
| 10 | 33 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | 34 | |
|
| 10 | 35 | | TInput input = await request.Payload.DecodeProtobufMessageAsync( |
| 10 | 36 | | inputParser, |
| 10 | 37 | | protobufFeature.MaxMessageLength, |
| 10 | 38 | | cancellationToken).ConfigureAwait(false); |
| | 39 | |
|
| 10 | 40 | | TOutput output = await method(service, input, request.Features, cancellationToken).ConfigureAwait(false); |
| | 41 | |
|
| 10 | 42 | | return new OutgoingResponse(request) |
| 10 | 43 | | { |
| 10 | 44 | | Payload = output.EncodeAsLengthPrefixedMessage( |
| 10 | 45 | | protobufFeature.EncodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions) |
| 10 | 46 | | }; |
| 10 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary>Dispatches a client-streaming RPC method.</summary> |
| | 50 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | 51 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 52 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | 53 | | /// <param name="request">The incoming request.</param> |
| | 54 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | 55 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | 56 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | 57 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | 58 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | 59 | | public static async ValueTask<OutgoingResponse> DispatchClientStreamingAsync<TInput, TOutput, TService>( |
| | 60 | | this IncomingRequest request, |
| | 61 | | MessageParser<TInput> inputParser, |
| | 62 | | TService service, |
| | 63 | | Func<TService, IAsyncEnumerable<TInput>, IFeatureCollection, CancellationToken, ValueTask<TOutput>> method, |
| | 64 | | CancellationToken cancellationToken) where TInput : IMessage<TInput> |
| | 65 | | where TOutput : IMessage<TOutput> |
| | 66 | | where TService : class |
| 3 | 67 | | { |
| 3 | 68 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | 69 | |
|
| 3 | 70 | | IAsyncEnumerable<TInput> input = request.DetachPayload().ToAsyncEnumerable( |
| 3 | 71 | | inputParser, |
| 3 | 72 | | protobufFeature.MaxMessageLength, |
| 3 | 73 | | CancellationToken.None); |
| | 74 | |
|
| 3 | 75 | | TOutput output = await method(service, input, request.Features, cancellationToken).ConfigureAwait(false); |
| | 76 | |
|
| 3 | 77 | | return new OutgoingResponse(request) |
| 3 | 78 | | { |
| 3 | 79 | | Payload = output.EncodeAsLengthPrefixedMessage( |
| 3 | 80 | | protobufFeature.EncodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions) |
| 3 | 81 | | }; |
| 3 | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary>Dispatches a server-streaming RPC method.</summary> |
| | 85 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | 86 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 87 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | 88 | | /// <param name="request">The incoming request.</param> |
| | 89 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | 90 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | 91 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | 92 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | 93 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | 94 | | public static async ValueTask<OutgoingResponse> DispatchServerStreamingAsync<TInput, TOutput, TService>( |
| | 95 | | this IncomingRequest request, |
| | 96 | | MessageParser<TInput> inputParser, |
| | 97 | | TService service, |
| | 98 | | Func<TService, TInput, IFeatureCollection, CancellationToken, ValueTask<IAsyncEnumerable<TOutput>>> method, |
| | 99 | | CancellationToken cancellationToken) where TInput : IMessage<TInput> |
| | 100 | | where TOutput : IMessage<TOutput> |
| | 101 | | where TService : class |
| 3 | 102 | | { |
| 3 | 103 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | 104 | |
|
| 3 | 105 | | TInput input = await request.Payload.DecodeProtobufMessageAsync( |
| 3 | 106 | | inputParser, |
| 3 | 107 | | protobufFeature.MaxMessageLength, |
| 3 | 108 | | cancellationToken).ConfigureAwait(false); |
| | 109 | |
|
| 3 | 110 | | IAsyncEnumerable<TOutput> output = await method(service, input, request.Features, cancellationToken) |
| 3 | 111 | | .ConfigureAwait(false); |
| | 112 | |
|
| 3 | 113 | | return new OutgoingResponse(request) |
| 3 | 114 | | { |
| 3 | 115 | | PayloadContinuation = output.ToPipeReader(protobufFeature.EncodeOptions) |
| 3 | 116 | | }; |
| 3 | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary>Dispatches a bidi-streaming RPC method.</summary> |
| | 120 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | 121 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 122 | | /// <typeparam name="TService">The generated INameService interface.</typeparam> |
| | 123 | | /// <param name="request">The incoming request.</param> |
| | 124 | | /// <param name="inputParser">A message parser used to decode the request payload.</param> |
| | 125 | | /// <param name="service">The instance of <typeparamref name="TService"/> that implement the RPC.</param> |
| | 126 | | /// <param name="method">The user-provided implementation of the RPC method.</param> |
| | 127 | | /// <param name="cancellationToken">The cancellation token that accepts cancellation requests.</param> |
| | 128 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | 129 | | public static async ValueTask<OutgoingResponse> DispatchBidiStreamingAsync<TInput, TOutput, TService>( |
| | 130 | | this IncomingRequest request, |
| | 131 | | MessageParser<TInput> inputParser, |
| | 132 | | TService service, |
| | 133 | | Func<TService, IAsyncEnumerable<TInput>, IFeatureCollection, CancellationToken, ValueTask<IAsyncEnumerable<TOutp |
| | 134 | | CancellationToken cancellationToken) where TInput : IMessage<TInput> |
| | 135 | | where TOutput : IMessage<TOutput> |
| | 136 | | where TService : class |
| 3 | 137 | | { |
| 3 | 138 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| | 139 | |
|
| 3 | 140 | | IAsyncEnumerable<TInput> input = request.DetachPayload().ToAsyncEnumerable( |
| 3 | 141 | | inputParser, |
| 3 | 142 | | protobufFeature.MaxMessageLength, |
| 3 | 143 | | CancellationToken.None); |
| | 144 | |
|
| 3 | 145 | | IAsyncEnumerable<TOutput> output = await method(service, input, request.Features, cancellationToken) |
| 3 | 146 | | .ConfigureAwait(false); |
| | 147 | |
|
| 3 | 148 | | return new OutgoingResponse(request) |
| 3 | 149 | | { |
| 3 | 150 | | PayloadContinuation = output.ToPipeReader(protobufFeature.EncodeOptions) |
| 3 | 151 | | }; |
| 3 | 152 | | } |
| | 153 | | } |