1 #region Copyright notice and license 2 3 // Copyright 2018 gRPC authors. 4 // 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 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 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. 16 17 #endregion 18 19 using System; 20 using Grpc.Core.Utils; 21 22 namespace Grpc.Core.Interceptors 23 { 24 /// <summary> 25 /// Decorates an underlying <see cref="Grpc.Core.CallInvoker" /> to 26 /// intercept calls through a given interceptor. 27 /// </summary> 28 internal class InterceptingCallInvoker : CallInvoker 29 { 30 readonly CallInvoker invoker; 31 readonly Interceptor interceptor; 32 33 /// <summary> 34 /// Creates a new instance of <see cref="Grpc.Core.Interceptors.InterceptingCallInvoker" /> 35 /// with the given underlying invoker and interceptor instances. 36 /// </summary> InterceptingCallInvoker(CallInvoker invoker, Interceptor interceptor)37 public InterceptingCallInvoker(CallInvoker invoker, Interceptor interceptor) 38 { 39 this.invoker = GrpcPreconditions.CheckNotNull(invoker, nameof(invoker)); 40 this.interceptor = GrpcPreconditions.CheckNotNull(interceptor, nameof(interceptor)); 41 } 42 43 /// <summary> 44 /// Intercepts a simple blocking call with the registered interceptor. 45 /// </summary> BlockingUnaryCall(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)46 public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) 47 { 48 return interceptor.BlockingUnaryCall( 49 request, 50 new ClientInterceptorContext<TRequest, TResponse>(method, host, options), 51 (req, ctx) => invoker.BlockingUnaryCall(ctx.Method, ctx.Host, ctx.Options, req)); 52 } 53 54 /// <summary> 55 /// Intercepts a simple asynchronous call with the registered interceptor. 56 /// </summary> AsyncUnaryCall(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)57 public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) 58 { 59 return interceptor.AsyncUnaryCall( 60 request, 61 new ClientInterceptorContext<TRequest, TResponse>(method, host, options), 62 (req, ctx) => invoker.AsyncUnaryCall(ctx.Method, ctx.Host, ctx.Options, req)); 63 } 64 65 /// <summary> 66 /// Intercepts an asynchronous server streaming call with the registered interceptor. 67 /// </summary> AsyncServerStreamingCall(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)68 public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) 69 { 70 return interceptor.AsyncServerStreamingCall( 71 request, 72 new ClientInterceptorContext<TRequest, TResponse>(method, host, options), 73 (req, ctx) => invoker.AsyncServerStreamingCall(ctx.Method, ctx.Host, ctx.Options, req)); 74 } 75 76 /// <summary> 77 /// Intercepts an asynchronous client streaming call with the registered interceptor. 78 /// </summary> AsyncClientStreamingCall(Method<TRequest, TResponse> method, string host, CallOptions options)79 public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) 80 { 81 return interceptor.AsyncClientStreamingCall( 82 new ClientInterceptorContext<TRequest, TResponse>(method, host, options), 83 ctx => invoker.AsyncClientStreamingCall(ctx.Method, ctx.Host, ctx.Options)); 84 } 85 86 /// <summary> 87 /// Intercepts an asynchronous duplex streaming call with the registered interceptor. 88 /// </summary> AsyncDuplexStreamingCall(Method<TRequest, TResponse> method, string host, CallOptions options)89 public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) 90 { 91 return interceptor.AsyncDuplexStreamingCall( 92 new ClientInterceptorContext<TRequest, TResponse>(method, host, options), 93 ctx => invoker.AsyncDuplexStreamingCall(ctx.Method, ctx.Host, ctx.Options)); 94 } 95 } 96 } 97