aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/Grpc.Core/Interceptors/Interceptor.cs
blob: ab708034f3c5e7ba328e479e7edaa828bab54863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#region Copyright notice and license

// Copyright 2018 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System;
using System.Reflection;
using System.Threading.Tasks;
using Grpc.Core.Internal;

namespace Grpc.Core.Interceptors
{
    /// <summary>
    /// Serves as the base class for gRPC interceptors.
    /// </summary>
    public abstract class Interceptor
    {
        /// <summary>
        /// Represents a continuation for intercepting simple blocking invocations.
        /// A delegate of this type is passed to the BlockingUnaryCall method
        /// when an outgoing invocation is being intercepted and calling the
        /// delegate will invoke the next interceptor in the chain, or the underlying
        /// call invoker if called from the last interceptor. The interceptor is
        /// allowed to call it zero, one, or multiple times, passing it the appropriate
        /// context and request values as it sees fit.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this invocation.</typeparam>
        /// <typeparam name="TResponse">Response message type for this invocation.</typeparam>
        /// <param name="request">The request value to continue the invocation with.</param>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// instance to pass to the next step in the invocation process.
        /// </param>
        /// <returns>
        /// The response value of the invocation to return to the caller.
        /// The interceptor can choose to return the return value of the
        /// continuation delegate or an arbitrary value as it sees fit.
        /// </returns>
        public delegate TResponse BlockingUnaryCallContinuation<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context)
            where TRequest : class
            where TResponse : class;

        /// <summary>
        /// Represents a continuation for intercepting simple asynchronous invocations.
        /// A delegate of this type is passed to the AsyncUnaryCall method
        /// when an outgoing invocation is being intercepted and calling the
        /// delegate will invoke the next interceptor in the chain, or the underlying
        /// call invoker if called from the last interceptor. The interceptor is
        /// allowed to call it zero, one, or multiple times, passing it the appropriate
        /// request value and context as it sees fit.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this invocation.</typeparam>
        /// <typeparam name="TResponse">Response message type for this invocation.</typeparam>
        /// <param name="request">The request value to continue the invocation with.</param>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// instance to pass to the next step in the invocation process.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncUnaryCall{TResponse}" />
        /// representing an asynchronous invocation of a unary RPC.
        /// The interceptor can choose to return the same object returned from
        /// the continuation delegate or an arbitrarily constructed instance as it sees fit.
        /// </returns>
        public delegate AsyncUnaryCall<TResponse> AsyncUnaryCallContinuation<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context)
            where TRequest : class
            where TResponse : class;

        /// <summary>
        /// Represents a continuation for intercepting asynchronous server-streaming invocations.
        /// A delegate of this type is passed to the AsyncServerStreamingCall method
        /// when an outgoing invocation is being intercepted and calling the
        /// delegate will invoke the next interceptor in the chain, or the underlying
        /// call invoker if called from the last interceptor. The interceptor is
        /// allowed to call it zero, one, or multiple times, passing it the appropriate
        /// request value and context as it sees fit.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this invocation.</typeparam>
        /// <typeparam name="TResponse">Response message type for this invocation.</typeparam>
        /// <param name="request">The request value to continue the invocation with.</param>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// instance to pass to the next step in the invocation process.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncServerStreamingCall{TResponse}" />
        /// representing an asynchronous invocation of a server-streaming RPC.
        /// The interceptor can choose to return the same object returned from
        /// the continuation delegate or an arbitrarily constructed instance as it sees fit.
        /// </returns>
        public delegate AsyncServerStreamingCall<TResponse> AsyncServerStreamingCallContinuation<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context)
            where TRequest : class
            where TResponse : class;

        /// <summary>
        /// Represents a continuation for intercepting asynchronous client-streaming invocations.
        /// A delegate of this type is passed to the AsyncClientStreamingCall method
        /// when an outgoing invocation is being intercepted and calling the
        /// delegate will invoke the next interceptor in the chain, or the underlying
        /// call invoker if called from the last interceptor. The interceptor is
        /// allowed to call it zero, one, or multiple times, passing it the appropriate
        /// request value and context as it sees fit.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this invocation.</typeparam>
        /// <typeparam name="TResponse">Response message type for this invocation.</typeparam>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// instance to pass to the next step in the invocation process.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncClientStreamingCall{TRequest, TResponse}" />
        /// representing an asynchronous invocation of a client-streaming RPC.
        /// The interceptor can choose to return the same object returned from
        /// the continuation delegate or an arbitrarily constructed instance as it sees fit.
        /// </returns>
        public delegate AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCallContinuation<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context)
            where TRequest : class
            where TResponse : class;

        /// <summary>
        /// Represents a continuation for intercepting asynchronous duplex invocations.
        /// A delegate of this type is passed to the AsyncDuplexStreamingCall method
        /// when an outgoing invocation is being intercepted and calling the
        /// delegate will invoke the next interceptor in the chain, or the underlying
        /// call invoker if called from the last interceptor. The interceptor is
        /// allowed to call it zero, one, or multiple times, passing it the appropriate
        /// request value and context as it sees fit.
        /// </summary>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// instance to pass to the next step in the invocation process.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncDuplexStreamingCall{TRequest, TResponse}" />
        /// representing an asynchronous invocation of a duplex-streaming RPC.
        /// The interceptor can choose to return the same object returned from
        /// the continuation delegate or an arbitrarily constructed instance as it sees fit.
        /// </returns>
        public delegate AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCallContinuation<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context)
            where TRequest : class
            where TResponse : class;

        /// <summary>
        /// Intercepts a blocking invocation of a simple remote call.
        /// </summary>
        /// <param name="request">The request message of the invocation.</param>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// associated with the current invocation.
        /// </param>
        /// <param name="continuation">
        /// The callback that continues the invocation process.
        /// This can be invoked zero or more times by the interceptor.
        /// The interceptor can invoke the continuation passing the given
        /// request value and context arguments, or substitute them as it sees fit.
        /// </param>
        /// <returns>
        /// The response message of the current invocation.
        /// The interceptor can simply return the return value of the
        /// continuation delegate passed to it intact, or an arbitrary
        /// value as it sees fit.
        /// </returns>
        public virtual TResponse BlockingUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(request, context);
        }

        /// <summary>
        /// Intercepts an asynchronous invocation of a simple remote call.
        /// </summary>
        /// <param name="request">The request message of the invocation.</param>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// associated with the current invocation.
        /// </param>
        /// <param name="continuation">
        /// The callback that continues the invocation process.
        /// This can be invoked zero or more times by the interceptor.
        /// The interceptor can invoke the continuation passing the given
        /// request value and context arguments, or substitute them as it sees fit.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncUnaryCall{TResponse}" />
        /// representing an asynchronous unary invocation.
        /// The interceptor can simply return the return value of the
        /// continuation delegate passed to it intact, or construct its
        /// own substitute as it sees fit.
        /// </returns>
        public virtual AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(request, context);
        }

        /// <summary>
        /// Intercepts an asynchronous invocation of a streaming remote call.
        /// </summary>
        /// <param name="request">The request message of the invocation.</param>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// associated with the current invocation.
        /// </param>
        /// <param name="continuation">
        /// The callback that continues the invocation process.
        /// This can be invoked zero or more times by the interceptor.
        /// The interceptor can invoke the continuation passing the given
        /// request value and context arguments, or substitute them as it sees fit.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncServerStreamingCall{TResponse}" />
        /// representing an asynchronous server-streaming invocation.
        /// The interceptor can simply return the return value of the
        /// continuation delegate passed to it intact, or construct its
        /// own substitute as it sees fit.
        /// </returns>
        public virtual AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(request, context);
        }

        /// <summary>
        /// Intercepts an asynchronous invocation of a client streaming call.
        /// </summary>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// associated with the current invocation.
        /// </param>
        /// <param name="continuation">
        /// The callback that continues the invocation process.
        /// This can be invoked zero or more times by the interceptor.
        /// The interceptor can invoke the continuation passing the given
        /// context argument, or substitute as it sees fit.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncClientStreamingCall{TRequest, TResponse}" />
        /// representing an asynchronous client-streaming invocation.
        /// The interceptor can simply return the return value of the
        /// continuation delegate passed to it intact, or construct its
        /// own substitute as it sees fit.
        /// </returns>
        public virtual AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(context);
        }

        /// <summary>
        /// Intercepts an asynchronous invocation of a duplex streaming call.
        /// </summary>
        /// <param name="context">
        /// The <see cref="Grpc.Core.Interceptors.ClientInterceptorContext{TRequest, TResponse}"/>
        /// associated with the current invocation.
        /// </param>
        /// <param name="continuation">
        /// The callback that continues the invocation process.
        /// This can be invoked zero or more times by the interceptor.
        /// The interceptor can invoke the continuation passing the given
        /// context argument, or substitute as it sees fit.
        /// </param>
        /// <returns>
        /// An instance of <see cref="Grpc.Core.AsyncDuplexStreamingCall{TRequest, TResponse}" />
        /// representing an asynchronous duplex-streaming invocation.
        /// The interceptor can simply return the return value of the
        /// continuation delegate passed to it intact, or construct its
        /// own substitute as it sees fit.
        /// </returns>
        public virtual AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(context);
        }

        /// <summary>
        /// Server-side handler for intercepting and incoming unary call.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this method.</typeparam>
        /// <typeparam name="TResponse">Response message type for this method.</typeparam>
        /// <param name="request">The request value of the incoming invocation.</param>
        /// <param name="context">
        /// An instance of <see cref="Grpc.Core.ServerCallContext" /> representing
        /// the context of the invocation.
        /// </param>
        /// <param name="continuation">
        /// A delegate that asynchronously proceeds with the invocation, calling
        /// the next interceptor in the chain, or the service request handler,
        /// in case of the last interceptor and return the response value of
        /// the RPC. The interceptor can choose to call it zero or more times
        /// at its discretion.
        /// </param>
        /// <returns>
        /// A future representing the response value of the RPC. The interceptor
        /// can simply return the return value from the continuation intact,
        /// or an arbitrary response value as it sees fit.
        /// </returns>
        public virtual Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(request, context);
        }

        /// <summary>
        /// Server-side handler for intercepting client streaming call.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this method.</typeparam>
        /// <typeparam name="TResponse">Response message type for this method.</typeparam>
        /// <param name="requestStream">The request stream of the incoming invocation.</param>
        /// <param name="context">
        /// An instance of <see cref="Grpc.Core.ServerCallContext" /> representing
        /// the context of the invocation.
        /// </param>
        /// <param name="continuation">
        /// A delegate that asynchronously proceeds with the invocation, calling
        /// the next interceptor in the chain, or the service request handler,
        /// in case of the last interceptor and return the response value of
        /// the RPC. The interceptor can choose to call it zero or more times
        /// at its discretion.
        /// </param>
        /// <returns>
        /// A future representing the response value of the RPC. The interceptor
        /// can simply return the return value from the continuation intact,
        /// or an arbitrary response value as it sees fit. The interceptor has
        /// the ability to wrap or substitute the request stream when calling
        /// the continuation.
        /// </returns>
        public virtual Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, ServerCallContext context, ClientStreamingServerMethod<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(requestStream, context);
        }

        /// <summary>
        /// Server-side handler for intercepting server streaming call.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this method.</typeparam>
        /// <typeparam name="TResponse">Response message type for this method.</typeparam>
        /// <param name="request">The request value of the incoming invocation.</param>
        /// <param name="responseStream">The response stream of the incoming invocation.</param>
        /// <param name="context">
        /// An instance of <see cref="Grpc.Core.ServerCallContext" /> representing
        /// the context of the invocation.
        /// </param>
        /// <param name="continuation">
        /// A delegate that asynchronously proceeds with the invocation, calling
        /// the next interceptor in the chain, or the service request handler,
        /// in case of the last interceptor and the interceptor can choose to
        /// call it zero or more times at its discretion. The interceptor has
        /// the ability to wrap or substitute the request value and the response stream
        /// when calling the continuation.
        /// </param>
        public virtual Task ServerStreamingServerHandler<TRequest, TResponse>(TRequest request, IServerStreamWriter<TResponse> responseStream, ServerCallContext context, ServerStreamingServerMethod<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(request, responseStream, context);
        }

        /// <summary>
        /// Server-side handler for intercepting bidirectional streaming calls.
        /// </summary>
        /// <typeparam name="TRequest">Request message type for this method.</typeparam>
        /// <typeparam name="TResponse">Response message type for this method.</typeparam>
        /// <param name="requestStream">The request stream of the incoming invocation.</param>
        /// <param name="responseStream">The response stream of the incoming invocation.</param>
        /// <param name="context">
        /// An instance of <see cref="Grpc.Core.ServerCallContext" /> representing
        /// the context of the invocation.
        /// </param>
        /// <param name="continuation">
        /// A delegate that asynchronously proceeds with the invocation, calling
        /// the next interceptor in the chain, or the service request handler,
        /// in case of the last interceptor and the interceptor can choose to
        /// call it zero or more times at its discretion. The interceptor has
        /// the ability to wrap or substitute the request and response streams
        /// when calling the continuation.
        /// </param>
        public virtual Task DuplexStreamingServerHandler<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, IServerStreamWriter<TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod<TRequest, TResponse> continuation)
            where TRequest : class
            where TResponse : class
        {
            return continuation(requestStream, responseStream, context);
        }
    }
}