#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 { /// /// Carries along the context associated with intercepted invocations on the client side. /// This is an EXPERIMENTAL API. /// public class ClientInterceptorContext where TRequest : class where TResponse : class { /// /// Creates a new instance of /// with the specified method, host, and call options. /// /// A object representing the method to be invoked. /// The host to dispatch the current call to. /// A instance containing the call options of the current call. public ClientInterceptorContext(Method method, string host, CallOptions options) { Method = method; Host = host; Options = options; } /// /// Gets the representing /// the method to be invoked. /// public Method Method { get; } /// /// Gets the host that the currect invocation will be dispatched to. /// public string Host { get; } /// /// Gets the structure representing the /// call options associated with the current invocation. /// public CallOptions Options { get; } } /// /// Serves as the base class for gRPC interceptors. /// This is an EXPERIMENTAL API. /// public abstract class Interceptor { /// /// Represents a continuation for intercepting simple blocking invocations. /// /// Request message type for this invocation. /// Response message type for this invocation. /// The request value to continue the invocation with. /// /// The /// instance to pass to the next step in the invocation process. /// public delegate TResponse BlockingUnaryCallContinuation(TRequest request, ClientInterceptorContext context) where TRequest : class where TResponse : class; /// /// Represents a continuation for intercepting simple asynchronous invocations. /// /// Request message type for this invocation. /// Response message type for this invocation. /// The request value to continue the invocation with. /// /// The /// instance to pass to the next step in the invocation process. /// public delegate AsyncUnaryCall AsyncUnaryCallContinuation(TRequest request, ClientInterceptorContext context) where TRequest : class where TResponse : class; /// /// Represents a continuation for intercepting asynchronous server-streaming invocations. /// /// Request message type for this invocation. /// Response message type for this invocation. /// The request value to continue the invocation with. /// /// The /// instance to pass to the next step in the invocation process. /// public delegate AsyncServerStreamingCall AsyncServerStreamingCallContinuation(TRequest request, ClientInterceptorContext context) where TRequest : class where TResponse : class; /// /// Represents a continuation for intercepting asynchronous client-streaming invocations. /// /// Request message type for this invocation. /// Response message type for this invocation. /// /// The /// instance to pass to the next step in the invocation process. /// public delegate AsyncClientStreamingCall AsyncClientStreamingCallContinuation(ClientInterceptorContext context) where TRequest : class where TResponse : class; /// /// Represents a continuation for intercepting asynchronous duplex invocations. /// /// /// The /// instance to pass to the next step in the invocation process. /// public delegate AsyncDuplexStreamingCall AsyncDuplexStreamingCallContinuation(ClientInterceptorContext context) where TRequest : class where TResponse : class; /// /// Intercepts a blocking invocation of a simple remote call. /// /// The request message of the invocation. /// /// The /// associated with the current invocation. /// /// /// The callback that continues the invocation process. /// This can be invoked zero or more times by the interceptor. /// /// The response message of the current invocation. public virtual TResponse BlockingUnaryCall(TRequest request, ClientInterceptorContext context, BlockingUnaryCallContinuation continuation) where TRequest : class where TResponse : class { return continuation(request, context); } /// /// Intercepts an asynchronous invocation of a simple remote call. /// /// The request message of the invocation. /// /// The /// associated with the current invocation. /// /// /// The callback that continues the invocation process. /// This can be invoked zero or more times by the interceptor. /// public virtual AsyncUnaryCall AsyncUnaryCall(TRequest request, ClientInterceptorContext context, AsyncUnaryCallContinuation continuation) where TRequest : class where TResponse : class { return continuation(request, context); } /// /// Intercepts an asynchronous invocation of a streaming remote call. /// /// The request message of the invocation. /// /// The /// associated with the current invocation. /// /// /// The callback that continues the invocation process. /// This can be invoked zero or more times by the interceptor. /// public virtual AsyncServerStreamingCall AsyncServerStreamingCall(TRequest request, ClientInterceptorContext context, AsyncServerStreamingCallContinuation continuation) where TRequest : class where TResponse : class { return continuation(request, context); } /// /// Intercepts an asynchronous invocation of a client streaming call. /// /// /// The /// associated with the current invocation. /// /// /// The callback that continues the invocation process. /// This can be invoked zero or more times by the interceptor. /// public virtual AsyncClientStreamingCall AsyncClientStreamingCall(ClientInterceptorContext context, AsyncClientStreamingCallContinuation continuation) where TRequest : class where TResponse : class { return continuation(context); } /// /// Intercepts an asynchronous invocation of a duplex streaming call. /// /// /// The /// associated with the current invocation. /// /// /// The callback that continues the invocation process. /// This can be invoked zero or more times by the interceptor. /// public virtual AsyncDuplexStreamingCall AsyncDuplexStreamingCall(ClientInterceptorContext context, AsyncDuplexStreamingCallContinuation continuation) where TRequest : class where TResponse : class { return continuation(context); } /// /// Server-side handler for intercepting unary calls. /// /// Request message type for this method. /// Response message type for this method. public virtual Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) where TRequest : class where TResponse : class { return continuation(request, context); } /// /// Server-side handler for intercepting client streaming call. /// /// Request message type for this method. /// Response message type for this method. public virtual Task ClientStreamingServerHandler(IAsyncStreamReader requestStream, ServerCallContext context, ClientStreamingServerMethod continuation) where TRequest : class where TResponse : class { return continuation(requestStream, context); } /// /// Server-side handler for intercepting server streaming calls. /// /// Request message type for this method. /// Response message type for this method. public virtual Task ServerStreamingServerHandler(TRequest request, IServerStreamWriter responseStream, ServerCallContext context, ServerStreamingServerMethod continuation) where TRequest : class where TResponse : class { return continuation(request, responseStream, context); } /// /// Server-side handler for intercepting bidi streaming calls. /// /// Request message type for this method. /// Response message type for this method. public virtual Task DuplexStreamingServerHandler(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context, DuplexStreamingServerMethod continuation) where TRequest : class where TResponse : class { return continuation(requestStream, responseStream, context); } } }