| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using Google.Protobuf; |
| | 4 | | using IceRpc.Features; |
| | 5 | | using IceRpc.Protobuf.Internal; |
| | 6 | | using System.Collections.Immutable; |
| | 7 | | using System.IO.Pipelines; |
| | 8 | |
|
| | 9 | | namespace IceRpc.Protobuf; |
| | 10 | |
|
| | 11 | | /// <summary>Provides extension methods for <see cref="IInvoker" />.</summary> |
| | 12 | | public static class InvokerExtensions |
| | 13 | | { |
| 1 | 14 | | private static readonly IDictionary<RequestFieldKey, OutgoingFieldValue> _idempotentFields = |
| 1 | 15 | | new Dictionary<RequestFieldKey, OutgoingFieldValue> |
| 1 | 16 | | { |
| 1 | 17 | | [RequestFieldKey.Idempotent] = default |
| 1 | 18 | | }.ToImmutableDictionary(); |
| | 19 | |
|
| | 20 | | /// <summary>Sends a request to a service and decodes the response. This method is for Protobuf unary RPCs. |
| | 21 | | /// </summary> |
| | 22 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 23 | | /// <param name="invoker">The invoker used to send the request.</param> |
| | 24 | | /// <param name="serviceAddress">The address of the target service.</param> |
| | 25 | | /// <param name="operation">The name of the operation, as specified in Protobuf.</param> |
| | 26 | | /// <param name="inputMessage">The input message to encode in the request payload.</param> |
| | 27 | | /// <param name="messageParser">The <see cref="MessageParser{T}"/> used to decode the response payload.</param> |
| | 28 | | /// <param name="encodeOptions">The options to customize the encoding of the request payload.</param> |
| | 29 | | /// <param name="features">The invocation features.</param> |
| | 30 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | 31 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 32 | | /// <returns>The operation's return value.</returns> |
| | 33 | | public static Task<TOutput> InvokeUnaryAsync<TOutput>( |
| | 34 | | this IInvoker invoker, |
| | 35 | | ServiceAddress serviceAddress, |
| | 36 | | string operation, |
| | 37 | | IMessage inputMessage, |
| | 38 | | MessageParser<TOutput> messageParser, |
| | 39 | | ProtobufEncodeOptions? encodeOptions = null, |
| | 40 | | IFeatureCollection? features = null, |
| | 41 | | bool idempotent = false, |
| | 42 | | CancellationToken cancellationToken = default) where TOutput : IMessage<TOutput> |
| 12 | 43 | | { |
| 12 | 44 | | var request = new OutgoingRequest(serviceAddress) |
| 12 | 45 | | { |
| 12 | 46 | | Features = features ?? FeatureCollection.Empty, |
| 12 | 47 | | Fields = idempotent ? |
| 12 | 48 | | _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| 12 | 49 | | Operation = operation, |
| 12 | 50 | | Payload = inputMessage.EncodeAsLengthPrefixedMessage( |
| 12 | 51 | | encodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions), |
| 12 | 52 | | }; |
| | 53 | |
|
| | 54 | | Task<IncomingResponse> responseTask; |
| | 55 | | try |
| 12 | 56 | | { |
| 12 | 57 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| 12 | 58 | | } |
| 0 | 59 | | catch |
| 0 | 60 | | { |
| 0 | 61 | | request.Dispose(); |
| 0 | 62 | | throw; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // ReceiveResponseAsync is responsible for disposing the request |
| 12 | 66 | | return ReceiveResponseAsync(messageParser, responseTask, request, cancellationToken); |
| 12 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary>Sends a request to a service and decodes the response. This method is for Protobuf client-streaming |
| | 70 | | /// RPCs.</summary> |
| | 71 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | 72 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 73 | | /// <param name="invoker">The invoker used to send the request.</param> |
| | 74 | | /// <param name="serviceAddress">The address of the target service.</param> |
| | 75 | | /// <param name="operation">The name of the operation, as specified in Protobuf.</param> |
| | 76 | | /// <param name="stream">The stream of input message to encode in the request payload continuation.</param> |
| | 77 | | /// <param name="messageParser">The <see cref="MessageParser{T}"/> used to decode the response payload.</param> |
| | 78 | | /// <param name="encodeOptions">The options to customize the encoding of the request payload continuation.</param> |
| | 79 | | /// <param name="features">The invocation features.</param> |
| | 80 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | 81 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 82 | | /// <returns>The operation's return value.</returns> |
| | 83 | | public static Task<TOutput> InvokeClientStreamingAsync<TInput, TOutput>( |
| | 84 | | this IInvoker invoker, |
| | 85 | | ServiceAddress serviceAddress, |
| | 86 | | string operation, |
| | 87 | | IAsyncEnumerable<TInput> stream, |
| | 88 | | MessageParser<TOutput> messageParser, |
| | 89 | | ProtobufEncodeOptions? encodeOptions = null, |
| | 90 | | IFeatureCollection? features = null, |
| | 91 | | bool idempotent = false, |
| | 92 | | CancellationToken cancellationToken = default) where TInput : IMessage<TInput> |
| | 93 | | where TOutput : IMessage<TOutput> |
| 4 | 94 | | { |
| 4 | 95 | | var request = new OutgoingRequest(serviceAddress) |
| 4 | 96 | | { |
| 4 | 97 | | Features = features ?? FeatureCollection.Empty, |
| 4 | 98 | | Fields = idempotent ? |
| 4 | 99 | | _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| 4 | 100 | | Operation = operation, |
| 4 | 101 | | PayloadContinuation = stream.ToPipeReader(encodeOptions), |
| 4 | 102 | | }; |
| | 103 | |
|
| | 104 | | Task<IncomingResponse> responseTask; |
| | 105 | | try |
| 4 | 106 | | { |
| 4 | 107 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| 4 | 108 | | } |
| 0 | 109 | | catch |
| 0 | 110 | | { |
| 0 | 111 | | request.Dispose(); |
| 0 | 112 | | throw; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | // ReceiveResponseAsync is responsible for disposing the request |
| 4 | 116 | | return ReceiveResponseAsync(messageParser, responseTask, request, cancellationToken); |
| 4 | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary>Sends a request to a service and decodes the response. This method is for Protobuf server-streaming |
| | 120 | | /// RPCs.</summary> |
| | 121 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 122 | | /// <param name="invoker">The invoker used to send the request.</param> |
| | 123 | | /// <param name="serviceAddress">The address of the target service.</param> |
| | 124 | | /// <param name="operation">The name of the operation, as specified in Protobuf.</param> |
| | 125 | | /// <param name="inputMessage">The input message to encode in the request payload.</param> |
| | 126 | | /// <param name="messageParser">The <see cref="MessageParser{T}"/> used to decode the response payload.</param> |
| | 127 | | /// <param name="encodeOptions">The options to customize the encoding of the request payload.</param> |
| | 128 | | /// <param name="features">The invocation features.</param> |
| | 129 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | 130 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 131 | | /// <returns>The operation's return value.</returns> |
| | 132 | | public static Task<IAsyncEnumerable<TOutput>> InvokeServerStreamingAsync<TOutput>( |
| | 133 | | this IInvoker invoker, |
| | 134 | | ServiceAddress serviceAddress, |
| | 135 | | string operation, |
| | 136 | | IMessage inputMessage, |
| | 137 | | MessageParser<TOutput> messageParser, |
| | 138 | | ProtobufEncodeOptions? encodeOptions = null, |
| | 139 | | IFeatureCollection? features = null, |
| | 140 | | bool idempotent = false, |
| | 141 | | CancellationToken cancellationToken = default) where TOutput : IMessage<TOutput> |
| 5 | 142 | | { |
| 5 | 143 | | var request = new OutgoingRequest(serviceAddress) |
| 5 | 144 | | { |
| 5 | 145 | | Features = features ?? FeatureCollection.Empty, |
| 5 | 146 | | Fields = idempotent ? |
| 5 | 147 | | _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| 5 | 148 | | Operation = operation, |
| 5 | 149 | | Payload = inputMessage.EncodeAsLengthPrefixedMessage( |
| 5 | 150 | | encodeOptions?.PipeOptions ?? ProtobufEncodeOptions.Default.PipeOptions), |
| 5 | 151 | | }; |
| | 152 | |
|
| | 153 | | Task<IncomingResponse> responseTask; |
| | 154 | | try |
| 5 | 155 | | { |
| 5 | 156 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| 5 | 157 | | } |
| 0 | 158 | | catch |
| 0 | 159 | | { |
| 0 | 160 | | request.Dispose(); |
| 0 | 161 | | throw; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // ReceiveStreamingResponseAsync is responsible for disposing the request |
| 5 | 165 | | return ReceiveStreamingResponseAsync(messageParser, responseTask, request); |
| 5 | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary>Sends a request to a service and decodes the response. This method is for Protobuf bidi-streaming |
| | 169 | | /// RPCs.</summary> |
| | 170 | | /// <typeparam name="TInput">The type of the input message.</typeparam> |
| | 171 | | /// <typeparam name="TOutput">The type of the output message.</typeparam> |
| | 172 | | /// <param name="invoker">The invoker used to send the request.</param> |
| | 173 | | /// <param name="serviceAddress">The address of the target service.</param> |
| | 174 | | /// <param name="operation">The name of the operation, as specified in Protobuf.</param> |
| | 175 | | /// <param name="stream">The stream of input message to encode in the request payload continuation.</param> |
| | 176 | | /// <param name="messageParser">The <see cref="MessageParser{T}"/> used to decode the response payload.</param> |
| | 177 | | /// <param name="encodeOptions">The options to customize the encoding of the request payload continuation.</param>// |
| | 178 | | /// <param name="features">The invocation features.</param> |
| | 179 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | 180 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 181 | | /// <returns>The operation's return value.</returns> |
| | 182 | | public static Task<IAsyncEnumerable<TOutput>> InvokeBidiStreamingAsync<TInput, TOutput>( |
| | 183 | | this IInvoker invoker, |
| | 184 | | ServiceAddress serviceAddress, |
| | 185 | | string operation, |
| | 186 | | IAsyncEnumerable<TInput> stream, |
| | 187 | | MessageParser<TOutput> messageParser, |
| | 188 | | ProtobufEncodeOptions? encodeOptions = null, |
| | 189 | | IFeatureCollection? features = null, |
| | 190 | | bool idempotent = false, |
| | 191 | | CancellationToken cancellationToken = default) where TInput : IMessage<TInput> |
| | 192 | | where TOutput : IMessage<TOutput> |
| | 193 | |
|
| 4 | 194 | | { |
| 4 | 195 | | var request = new OutgoingRequest(serviceAddress) |
| 4 | 196 | | { |
| 4 | 197 | | Features = features ?? FeatureCollection.Empty, |
| 4 | 198 | | Fields = idempotent ? |
| 4 | 199 | | _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| 4 | 200 | | Operation = operation, |
| 4 | 201 | | PayloadContinuation = stream.ToPipeReader(encodeOptions), |
| 4 | 202 | | }; |
| | 203 | |
|
| | 204 | | Task<IncomingResponse> responseTask; |
| | 205 | | try |
| 4 | 206 | | { |
| 4 | 207 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| 4 | 208 | | } |
| 0 | 209 | | catch |
| 0 | 210 | | { |
| 0 | 211 | | request.Dispose(); |
| 0 | 212 | | throw; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | // ReceiveStreamingResponseAsync is responsible for disposing the request |
| 4 | 216 | | return ReceiveStreamingResponseAsync(messageParser, responseTask, request); |
| 4 | 217 | | } |
| | 218 | |
|
| | 219 | | private static async Task<TOutput> ReceiveResponseAsync<TOutput>( |
| | 220 | | MessageParser<TOutput> messageParser, |
| | 221 | | Task<IncomingResponse> responseTask, |
| | 222 | | OutgoingRequest request, |
| | 223 | | CancellationToken cancellationToken) where TOutput : IMessage<TOutput> |
| 16 | 224 | | { |
| | 225 | | try |
| 16 | 226 | | { |
| 16 | 227 | | IncomingResponse response = await responseTask.ConfigureAwait(false); |
| 16 | 228 | | if (response.StatusCode == StatusCode.Ok) |
| 13 | 229 | | { |
| 13 | 230 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| 13 | 231 | | return await response.Payload.DecodeProtobufMessageAsync( |
| 13 | 232 | | messageParser, |
| 13 | 233 | | protobufFeature.MaxMessageLength, |
| 13 | 234 | | cancellationToken).ConfigureAwait(false); |
| | 235 | | } |
| | 236 | | else |
| 3 | 237 | | { |
| | 238 | | // IceRPC guarantees the error message is non-null when StatusCode > Ok. |
| 3 | 239 | | throw new DispatchException(response.StatusCode, response.ErrorMessage!) |
| 3 | 240 | | { |
| 3 | 241 | | ConvertToInternalError = true |
| 3 | 242 | | }; |
| | 243 | | } |
| | 244 | | } |
| | 245 | | finally |
| 16 | 246 | | { |
| 16 | 247 | | request.Dispose(); |
| 16 | 248 | | } |
| 13 | 249 | | } |
| | 250 | |
|
| | 251 | | private static async Task<IAsyncEnumerable<TOutput>> ReceiveStreamingResponseAsync<TOutput>( |
| | 252 | | MessageParser<TOutput> messageParser, |
| | 253 | | Task<IncomingResponse> responseTask, |
| | 254 | | OutgoingRequest request) where TOutput : IMessage<TOutput> |
| 9 | 255 | | { |
| | 256 | | try |
| 9 | 257 | | { |
| 9 | 258 | | IncomingResponse response = await responseTask.ConfigureAwait(false); |
| 9 | 259 | | if (response.StatusCode == StatusCode.Ok) |
| 6 | 260 | | { |
| 6 | 261 | | IProtobufFeature protobufFeature = request.Features.Get<IProtobufFeature>() ?? ProtobufFeature.Default; |
| 6 | 262 | | PipeReader payload = response.DetachPayload(); |
| 6 | 263 | | return payload.ToAsyncEnumerable( |
| 6 | 264 | | messageParser, |
| 6 | 265 | | protobufFeature.MaxMessageLength, |
| 6 | 266 | | CancellationToken.None); |
| | 267 | | } |
| | 268 | | else |
| 3 | 269 | | { |
| | 270 | | // IceRPC guarantees the error message is non-null when StatusCode > Ok. |
| 3 | 271 | | throw new DispatchException(response.StatusCode, response.ErrorMessage!) |
| 3 | 272 | | { |
| 3 | 273 | | ConvertToInternalError = true |
| 3 | 274 | | }; |
| | 275 | | } |
| | 276 | | } |
| | 277 | | finally |
| 9 | 278 | | { |
| 9 | 279 | | request.Dispose(); |
| 9 | 280 | | } |
| 6 | 281 | | } |
| | 282 | | } |