| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using IceRpc.Features; |
| | 4 | | using IceRpc.Internal; |
| | 5 | | using System.Collections.Immutable; |
| | 6 | | using System.IO.Pipelines; |
| | 7 | | using ZeroC.Slice; |
| | 8 | |
|
| | 9 | | namespace IceRpc.Slice; |
| | 10 | |
|
| | 11 | | /// <summary>Represents a delegate that decodes the return value from a Slice-encoded response.</summary> |
| | 12 | | /// <typeparam name="T">The type of the return value to read.</typeparam> |
| | 13 | | /// <param name="response">The incoming response.</param> |
| | 14 | | /// <param name="request">The outgoing request.</param> |
| | 15 | | /// <param name="sender">The proxy that sent the request.</param> |
| | 16 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 17 | | /// <returns>A value task that contains the return value or a <see cref="SliceException" /> when the status code of the |
| | 18 | | /// response is <see cref="StatusCode.ApplicationError" />.</returns> |
| | 19 | | public delegate ValueTask<T> ResponseDecodeFunc<T>( |
| | 20 | | IncomingResponse response, |
| | 21 | | OutgoingRequest request, |
| | 22 | | IProxy sender, |
| | 23 | | CancellationToken cancellationToken); |
| | 24 | |
|
| | 25 | | /// <summary>Represents a delegate that decodes the "void" return value from a Slice-encoded response.</summary> |
| | 26 | | /// <param name="response">The incoming response.</param> |
| | 27 | | /// <param name="request">The outgoing request.</param> |
| | 28 | | /// <param name="sender">The proxy that sent the request.</param> |
| | 29 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 30 | | /// <returns>A value task that contains a <see cref="SliceException" /> when the status code of the response is |
| | 31 | | /// <see cref="StatusCode.ApplicationError" />.</returns> |
| | 32 | | public delegate ValueTask ResponseDecodeFunc( |
| | 33 | | IncomingResponse response, |
| | 34 | | OutgoingRequest request, |
| | 35 | | IProxy sender, |
| | 36 | | CancellationToken cancellationToken); |
| | 37 | |
|
| | 38 | | /// <summary>Provides extension methods for <see cref="IProxy" /> and generated proxy structs that implement this |
| | 39 | | /// interface.</summary> |
| | 40 | | public static class ProxyExtensions |
| | 41 | | { |
| 2 | 42 | | private static readonly IDictionary<RequestFieldKey, OutgoingFieldValue> _idempotentFields = |
| 2 | 43 | | new Dictionary<RequestFieldKey, OutgoingFieldValue> |
| 2 | 44 | | { |
| 2 | 45 | | [RequestFieldKey.Idempotent] = default |
| 2 | 46 | | }.ToImmutableDictionary(); |
| | 47 | |
|
| | 48 | | /// <summary>Sends a request to a service and decodes the response.</summary> |
| | 49 | | /// <typeparam name="TProxy">The type of the proxy struct.</typeparam> |
| | 50 | | /// <typeparam name="T">The response type.</typeparam> |
| | 51 | | /// <param name="proxy">A proxy to the remote service.</param> |
| | 52 | | /// <param name="operation">The name of the operation, as specified in Slice.</param> |
| | 53 | | /// <param name="payload">The payload of the request. <see langword="null" /> is equivalent to an empty |
| | 54 | | /// payload.</param> |
| | 55 | | /// <param name="payloadContinuation">The optional payload continuation of the request.</param> |
| | 56 | | /// <param name="responseDecodeFunc">The decode function for the response payload. It decodes and throws an |
| | 57 | | /// exception when the status code of the response is <see cref="StatusCode.ApplicationError" />.</param> |
| | 58 | | /// <param name="features">The invocation features.</param> |
| | 59 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | 60 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 61 | | /// <returns>The operation's return value.</returns> |
| | 62 | | /// <exception cref="SliceException">Thrown if the response carries a Slice exception.</exception> |
| | 63 | | public static Task<T> InvokeAsync<TProxy, T>( |
| | 64 | | this TProxy proxy, |
| | 65 | | string operation, |
| | 66 | | PipeReader? payload, |
| | 67 | | PipeReader? payloadContinuation, |
| | 68 | | ResponseDecodeFunc<T> responseDecodeFunc, |
| | 69 | | IFeatureCollection? features = null, |
| | 70 | | bool idempotent = false, |
| | 71 | | CancellationToken cancellationToken = default) where TProxy : struct, IProxy |
| 48 | 72 | | { |
| 48 | 73 | | if (proxy.Invoker is not IInvoker invoker) |
| 0 | 74 | | { |
| 0 | 75 | | throw new InvalidOperationException("Cannot send requests using a proxy with a null invoker."); |
| | 76 | | } |
| | 77 | |
|
| 48 | 78 | | if (payload is null && payloadContinuation is not null) |
| 0 | 79 | | { |
| 0 | 80 | | throw new ArgumentNullException( |
| 0 | 81 | | nameof(payload), |
| 0 | 82 | | $"When {nameof(payloadContinuation)} is not null, {nameof(payload)} cannot be null."); |
| | 83 | | } |
| | 84 | |
|
| 48 | 85 | | var request = new OutgoingRequest(proxy.ServiceAddress) |
| 48 | 86 | | { |
| 48 | 87 | | Features = features ?? FeatureCollection.Empty, |
| 48 | 88 | | Fields = idempotent ? |
| 48 | 89 | | _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| 48 | 90 | | Operation = operation, |
| 48 | 91 | | Payload = payload ?? EmptyPipeReader.Instance, |
| 48 | 92 | | PayloadContinuation = payloadContinuation |
| 48 | 93 | | }; |
| | 94 | |
|
| | 95 | | Task<IncomingResponse> responseTask; |
| | 96 | | try |
| 48 | 97 | | { |
| 48 | 98 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| 48 | 99 | | } |
| 0 | 100 | | catch |
| 0 | 101 | | { |
| 0 | 102 | | request.Dispose(); |
| 0 | 103 | | throw; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | // ReadResponseAsync is responsible for disposing the request |
| 48 | 107 | | return ReadResponseAsync(responseTask, request); |
| | 108 | |
|
| | 109 | | async Task<T> ReadResponseAsync(Task<IncomingResponse> responseTask, OutgoingRequest request) |
| 48 | 110 | | { |
| | 111 | | try |
| 48 | 112 | | { |
| 48 | 113 | | IncomingResponse response = await responseTask.ConfigureAwait(false); |
| 48 | 114 | | return await responseDecodeFunc( |
| 48 | 115 | | response, |
| 48 | 116 | | request, |
| 48 | 117 | | proxy, |
| 48 | 118 | | cancellationToken).ConfigureAwait(false); |
| | 119 | | } |
| | 120 | | finally |
| 48 | 121 | | { |
| 48 | 122 | | request.Dispose(); |
| 48 | 123 | | } |
| 48 | 124 | | } |
| 48 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary>Sends a request to a service and decodes the "void" response.</summary> |
| | 128 | | /// <typeparam name="TProxy">The type of the proxy struct.</typeparam> |
| | 129 | | /// <param name="proxy">A proxy for the remote service.</param> |
| | 130 | | /// <param name="operation">The name of the operation, as specified in Slice.</param> |
| | 131 | | /// <param name="payload">The payload of the request. <see langword="null" /> is equivalent to an empty |
| | 132 | | /// payload.</param> |
| | 133 | | /// <param name="payloadContinuation">The payload continuation of the request.</param> |
| | 134 | | /// <param name="responseDecodeFunc">The decode function for the response payload. It decodes and throws an |
| | 135 | | /// exception when the status code of the response is <see cref="StatusCode.ApplicationError" />.</param> |
| | 136 | | /// <param name="features">The invocation features.</param> |
| | 137 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | 138 | | /// <param name="oneway">When <see langword="true" />, the request is sent one-way and an empty response is returned |
| | 139 | | /// immediately after sending the request.</param> |
| | 140 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | 141 | | /// <returns>A task that completes when the void response is returned.</returns> |
| | 142 | | /// <exception cref="SliceException">Thrown if the response carries a failure.</exception> |
| | 143 | | public static Task InvokeAsync<TProxy>( |
| | 144 | | this TProxy proxy, |
| | 145 | | string operation, |
| | 146 | | PipeReader? payload, |
| | 147 | | PipeReader? payloadContinuation, |
| | 148 | | ResponseDecodeFunc responseDecodeFunc, |
| | 149 | | IFeatureCollection? features = null, |
| | 150 | | bool idempotent = false, |
| | 151 | | bool oneway = false, |
| | 152 | | CancellationToken cancellationToken = default) where TProxy : struct, IProxy |
| 73 | 153 | | { |
| 73 | 154 | | if (proxy.Invoker is not IInvoker invoker) |
| 0 | 155 | | { |
| 0 | 156 | | throw new InvalidOperationException("Cannot send requests using a proxy with a null invoker."); |
| | 157 | | } |
| | 158 | |
|
| 73 | 159 | | if (payload is null && payloadContinuation is not null) |
| 0 | 160 | | { |
| 0 | 161 | | throw new ArgumentNullException( |
| 0 | 162 | | nameof(payload), |
| 0 | 163 | | $"When {nameof(payloadContinuation)} is not null, {nameof(payload)} cannot be null."); |
| | 164 | | } |
| | 165 | |
|
| 73 | 166 | | var request = new OutgoingRequest(proxy.ServiceAddress) |
| 73 | 167 | | { |
| 73 | 168 | | Features = features ?? FeatureCollection.Empty, |
| 73 | 169 | | Fields = idempotent ? _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| 73 | 170 | | IsOneway = oneway, |
| 73 | 171 | | Operation = operation, |
| 73 | 172 | | Payload = payload ?? EmptyPipeReader.Instance, |
| 73 | 173 | | PayloadContinuation = payloadContinuation |
| 73 | 174 | | }; |
| | 175 | |
|
| | 176 | | Task<IncomingResponse> responseTask; |
| | 177 | | try |
| 73 | 178 | | { |
| 73 | 179 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| 72 | 180 | | } |
| 1 | 181 | | catch |
| 1 | 182 | | { |
| 1 | 183 | | request.Dispose(); |
| 1 | 184 | | throw; |
| | 185 | | } |
| | 186 | |
|
| | 187 | | // ReadResponseAsync is responsible for disposing the request |
| 72 | 188 | | return ReadResponseAsync(responseTask, request); |
| | 189 | |
|
| | 190 | | async Task ReadResponseAsync(Task<IncomingResponse> responseTask, OutgoingRequest request) |
| 72 | 191 | | { |
| | 192 | | try |
| 72 | 193 | | { |
| 72 | 194 | | IncomingResponse response = await responseTask.ConfigureAwait(false); |
| | 195 | |
|
| 72 | 196 | | await responseDecodeFunc( |
| 72 | 197 | | response, |
| 72 | 198 | | request, |
| 72 | 199 | | proxy, |
| 72 | 200 | | cancellationToken).ConfigureAwait(false); |
| 43 | 201 | | } |
| | 202 | | finally |
| 72 | 203 | | { |
| 72 | 204 | | request.Dispose(); |
| 72 | 205 | | } |
| 43 | 206 | | } |
| 72 | 207 | | } |
| | 208 | |
|
| | 209 | | /// <summary>Converts a proxy into a proxy struct. This conversion always succeeds.</summary> |
| | 210 | | /// <typeparam name="TProxy">The type of the target proxy struct.</typeparam> |
| | 211 | | /// <param name="proxy">The source proxy.</param> |
| | 212 | | /// <returns>A new instance of <typeparamref name="TProxy" />.</returns> |
| | 213 | | public static TProxy ToProxy<TProxy>(this IProxy proxy) where TProxy : struct, IProxy => |
| 3 | 214 | | new() { EncodeOptions = proxy.EncodeOptions, Invoker = proxy.Invoker, ServiceAddress = proxy.ServiceAddress }; |
| | 215 | | } |