| | | 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.</param> |
| | | 54 | | /// <param name="payloadContinuation">The optional payload continuation of the request.</param> |
| | | 55 | | /// <param name="responseDecodeFunc">The decode function for the response payload. It decodes and throws an |
| | | 56 | | /// exception when the status code of the response is <see cref="StatusCode.ApplicationError" />.</param> |
| | | 57 | | /// <param name="features">The invocation features.</param> |
| | | 58 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | | 59 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 60 | | /// <returns>The operation's return value.</returns> |
| | | 61 | | /// <exception cref="SliceException">Thrown if the response carries a Slice exception.</exception> |
| | | 62 | | public static Task<T> InvokeOperationAsync<TProxy, T>( |
| | | 63 | | this TProxy proxy, |
| | | 64 | | string operation, |
| | | 65 | | PipeReader payload, |
| | | 66 | | PipeReader? payloadContinuation, |
| | | 67 | | ResponseDecodeFunc<T> responseDecodeFunc, |
| | | 68 | | IFeatureCollection? features = null, |
| | | 69 | | bool idempotent = false, |
| | | 70 | | CancellationToken cancellationToken = default) where TProxy : struct, IProxy |
| | 48 | 71 | | { |
| | 48 | 72 | | if (proxy.Invoker is not IInvoker invoker) |
| | 0 | 73 | | { |
| | 0 | 74 | | throw new InvalidOperationException("Cannot send requests using a proxy with a null invoker."); |
| | | 75 | | } |
| | | 76 | | |
| | 48 | 77 | | var request = new OutgoingRequest(proxy.ServiceAddress) |
| | 48 | 78 | | { |
| | 48 | 79 | | Features = features ?? FeatureCollection.Empty, |
| | 48 | 80 | | Fields = idempotent ? _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| | 48 | 81 | | Operation = operation, |
| | 48 | 82 | | Payload = payload, |
| | 48 | 83 | | PayloadContinuation = payloadContinuation |
| | 48 | 84 | | }; |
| | | 85 | | |
| | | 86 | | Task<IncomingResponse> responseTask; |
| | | 87 | | try |
| | 48 | 88 | | { |
| | 48 | 89 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| | 48 | 90 | | } |
| | 0 | 91 | | catch |
| | 0 | 92 | | { |
| | 0 | 93 | | request.Dispose(); |
| | 0 | 94 | | throw; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // ReadResponseAsync is responsible for disposing the request |
| | 48 | 98 | | return ReadResponseAsync(responseTask, request); |
| | | 99 | | |
| | | 100 | | async Task<T> ReadResponseAsync(Task<IncomingResponse> responseTask, OutgoingRequest request) |
| | 48 | 101 | | { |
| | | 102 | | try |
| | 48 | 103 | | { |
| | 48 | 104 | | IncomingResponse response = await responseTask.ConfigureAwait(false); |
| | 48 | 105 | | return await responseDecodeFunc( |
| | 48 | 106 | | response, |
| | 48 | 107 | | request, |
| | 48 | 108 | | proxy, |
| | 48 | 109 | | cancellationToken).ConfigureAwait(false); |
| | | 110 | | } |
| | | 111 | | finally |
| | 48 | 112 | | { |
| | 48 | 113 | | request.Dispose(); |
| | 48 | 114 | | } |
| | 48 | 115 | | } |
| | 48 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary>Sends a request to a service and decodes the "void" response.</summary> |
| | | 119 | | /// <typeparam name="TProxy">The type of the proxy struct.</typeparam> |
| | | 120 | | /// <param name="proxy">A proxy for the remote service.</param> |
| | | 121 | | /// <param name="operation">The name of the operation, as specified in Slice.</param> |
| | | 122 | | /// <param name="payload">The payload of the request.</param> |
| | | 123 | | /// <param name="payloadContinuation">The payload continuation of the request.</param> |
| | | 124 | | /// <param name="responseDecodeFunc">The decode function for the response payload. It decodes and throws an |
| | | 125 | | /// exception when the status code of the response is <see cref="StatusCode.ApplicationError" />.</param> |
| | | 126 | | /// <param name="features">The invocation features.</param> |
| | | 127 | | /// <param name="idempotent">When <see langword="true" />, the request is idempotent.</param> |
| | | 128 | | /// <param name="oneway">When <see langword="true" />, the request is sent one-way and an empty response is returned |
| | | 129 | | /// immediately after sending the request.</param> |
| | | 130 | | /// <param name="cancellationToken">A cancellation token that receives the cancellation requests.</param> |
| | | 131 | | /// <returns>A task that completes when the void response is returned.</returns> |
| | | 132 | | /// <exception cref="SliceException">Thrown if the response carries a failure.</exception> |
| | | 133 | | public static Task InvokeOperationAsync<TProxy>( |
| | | 134 | | this TProxy proxy, |
| | | 135 | | string operation, |
| | | 136 | | PipeReader payload, |
| | | 137 | | PipeReader? payloadContinuation, |
| | | 138 | | ResponseDecodeFunc responseDecodeFunc, |
| | | 139 | | IFeatureCollection? features = null, |
| | | 140 | | bool idempotent = false, |
| | | 141 | | bool oneway = false, |
| | | 142 | | CancellationToken cancellationToken = default) where TProxy : struct, IProxy |
| | 70 | 143 | | { |
| | 70 | 144 | | if (proxy.Invoker is not IInvoker invoker) |
| | 0 | 145 | | { |
| | 0 | 146 | | throw new InvalidOperationException("Cannot send requests using a proxy with a null invoker."); |
| | | 147 | | } |
| | | 148 | | |
| | 70 | 149 | | var request = new OutgoingRequest(proxy.ServiceAddress) |
| | 70 | 150 | | { |
| | 70 | 151 | | Features = features ?? FeatureCollection.Empty, |
| | 70 | 152 | | Fields = idempotent ? _idempotentFields : ImmutableDictionary<RequestFieldKey, OutgoingFieldValue>.Empty, |
| | 70 | 153 | | IsOneway = oneway, |
| | 70 | 154 | | Operation = operation, |
| | 70 | 155 | | Payload = payload, |
| | 70 | 156 | | PayloadContinuation = payloadContinuation |
| | 70 | 157 | | }; |
| | | 158 | | |
| | | 159 | | Task<IncomingResponse> responseTask; |
| | | 160 | | try |
| | 70 | 161 | | { |
| | 70 | 162 | | responseTask = invoker.InvokeAsync(request, cancellationToken); |
| | 69 | 163 | | } |
| | 1 | 164 | | catch |
| | 1 | 165 | | { |
| | 1 | 166 | | request.Dispose(); |
| | 1 | 167 | | throw; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | // ReadResponseAsync is responsible for disposing the request |
| | 69 | 171 | | return ReadResponseAsync(responseTask, request); |
| | | 172 | | |
| | | 173 | | async Task ReadResponseAsync(Task<IncomingResponse> responseTask, OutgoingRequest request) |
| | 69 | 174 | | { |
| | | 175 | | try |
| | 69 | 176 | | { |
| | 69 | 177 | | IncomingResponse response = await responseTask.ConfigureAwait(false); |
| | | 178 | | |
| | 69 | 179 | | await responseDecodeFunc( |
| | 69 | 180 | | response, |
| | 69 | 181 | | request, |
| | 69 | 182 | | proxy, |
| | 69 | 183 | | cancellationToken).ConfigureAwait(false); |
| | 40 | 184 | | } |
| | | 185 | | finally |
| | 69 | 186 | | { |
| | 69 | 187 | | request.Dispose(); |
| | 69 | 188 | | } |
| | 40 | 189 | | } |
| | 69 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary>Converts a proxy into a proxy struct. This conversion always succeeds.</summary> |
| | | 193 | | /// <typeparam name="TProxy">The type of the target proxy struct.</typeparam> |
| | | 194 | | /// <param name="proxy">The source proxy.</param> |
| | | 195 | | /// <returns>A new instance of <typeparamref name="TProxy" />.</returns> |
| | | 196 | | public static TProxy ToProxy<TProxy>(this IProxy proxy) where TProxy : struct, IProxy => |
| | 3 | 197 | | new() { EncodeOptions = proxy.EncodeOptions, Invoker = proxy.Invoker, ServiceAddress = proxy.ServiceAddress }; |
| | | 198 | | } |