1 #region Copyright notice and license
3 // Copyright 2018 gRPC authors.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 using Grpc.Core.Utils;
22 namespace Grpc.Core.Interceptors
25 /// Decorates an underlying <see cref="Grpc.Core.CallInvoker" /> to
26 /// intercept calls through a given interceptor.
28 internal class InterceptingCallInvoker : CallInvoker
30 readonly CallInvoker invoker;
31 readonly Interceptor interceptor;
34 /// Creates a new instance of <see cref="Grpc.Core.Interceptors.InterceptingCallInvoker" />
35 /// with the given underlying invoker and interceptor instances.
37 public InterceptingCallInvoker(CallInvoker invoker, Interceptor interceptor)
39 this.invoker = GrpcPreconditions.CheckNotNull(invoker, nameof(invoker));
40 this.interceptor = GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor));
44 /// Intercepts a simple blocking call with the registered interceptor.
46 public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
48 return interceptor.BlockingUnaryCall(
50 new ClientInterceptorContext<TRequest, TResponse>(method, host, options),
51 (req, ctx) => invoker.BlockingUnaryCall(ctx.Method, ctx.Host, ctx.Options, req));
55 /// Intercepts a simple asynchronous call with the registered interceptor.
57 public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
59 return interceptor.AsyncUnaryCall(
61 new ClientInterceptorContext<TRequest, TResponse>(method, host, options),
62 (req, ctx) => invoker.AsyncUnaryCall(ctx.Method, ctx.Host, ctx.Options, req));
66 /// Intercepts an asynchronous server streaming call with the registered interceptor.
68 public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)
70 return interceptor.AsyncServerStreamingCall(
72 new ClientInterceptorContext<TRequest, TResponse>(method, host, options),
73 (req, ctx) => invoker.AsyncServerStreamingCall(ctx.Method, ctx.Host, ctx.Options, req));
77 /// Intercepts an asynchronous client streaming call with the registered interceptor.
79 public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options)
81 return interceptor.AsyncClientStreamingCall(
82 new ClientInterceptorContext<TRequest, TResponse>(method, host, options),
83 ctx => invoker.AsyncClientStreamingCall(ctx.Method, ctx.Host, ctx.Options));
87 /// Intercepts an asynchronous duplex streaming call with the registered interceptor.
89 public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options)
91 return interceptor.AsyncDuplexStreamingCall(
92 new ClientInterceptorContext<TRequest, TResponse>(method, host, options),
93 ctx => invoker.AsyncDuplexStreamingCall(ctx.Method, ctx.Host, ctx.Options));