1 #region Copyright notice and license 2 3 // Copyright 2015-2016 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.Threading.Tasks; 20 using Grpc.Core.Internal; 21 using Grpc.Core.Utils; 22 23 namespace Grpc.Core 24 { 25 /// <summary> 26 /// Invokes client RPCs using <see cref="Calls"/>. 27 /// </summary> 28 public class DefaultCallInvoker : CallInvoker 29 { 30 readonly Channel channel; 31 32 /// <summary> 33 /// Initializes a new instance of the <see cref="Grpc.Core.DefaultCallInvoker"/> class. 34 /// </summary> 35 /// <param name="channel">Channel to use.</param> DefaultCallInvoker(Channel channel)36 public DefaultCallInvoker(Channel channel) 37 { 38 this.channel = GrpcPreconditions.CheckNotNull(channel); 39 } 40 41 /// <summary> 42 /// Invokes a simple remote call in a blocking fashion. 43 /// </summary> BlockingUnaryCall(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)44 public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) 45 { 46 var call = CreateCall(method, host, options); 47 return Calls.BlockingUnaryCall(call, request); 48 } 49 50 /// <summary> 51 /// Invokes a simple remote call asynchronously. 52 /// </summary> AsyncUnaryCall(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)53 public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) 54 { 55 var call = CreateCall(method, host, options); 56 return Calls.AsyncUnaryCall(call, request); 57 } 58 59 /// <summary> 60 /// Invokes a server streaming call asynchronously. 61 /// In server streaming scenario, client sends on request and server responds with a stream of responses. 62 /// </summary> AsyncServerStreamingCall(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request)63 public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) 64 { 65 var call = CreateCall(method, host, options); 66 return Calls.AsyncServerStreamingCall(call, request); 67 } 68 69 /// <summary> 70 /// Invokes a client streaming call asynchronously. 71 /// In client streaming scenario, client sends a stream of requests and server responds with a single response. 72 /// </summary> AsyncClientStreamingCall(Method<TRequest, TResponse> method, string host, CallOptions options)73 public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) 74 { 75 var call = CreateCall(method, host, options); 76 return Calls.AsyncClientStreamingCall(call); 77 } 78 79 /// <summary> 80 /// Invokes a duplex streaming call asynchronously. 81 /// In duplex streaming scenario, client sends a stream of requests and server responds with a stream of responses. 82 /// The response stream is completely independent and both side can be sending messages at the same time. 83 /// </summary> AsyncDuplexStreamingCall(Method<TRequest, TResponse> method, string host, CallOptions options)84 public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) 85 { 86 var call = CreateCall(method, host, options); 87 return Calls.AsyncDuplexStreamingCall(call); 88 } 89 90 /// <summary>Creates call invocation details for given method.</summary> 91 protected virtual CallInvocationDetails<TRequest, TResponse> CreateCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) 92 where TRequest : class 93 where TResponse : class 94 { 95 return new CallInvocationDetails<TRequest, TResponse>(channel, method, host, options); 96 } 97 } 98 } 99