1 #region Copyright notice and license
2 
3 // Copyright 2015 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 
22 namespace Grpc.Core
23 {
24     /// <summary>
25     /// Helper methods for generated clients to make RPC calls.
26     /// Most users will use this class only indirectly and will be
27     /// making calls using client object generated from protocol
28     /// buffer definition files.
29     /// </summary>
30     public static class Calls
31     {
32         /// <summary>
33         /// Invokes a simple remote call in a blocking fashion.
34         /// </summary>
35         /// <returns>The response.</returns>
36         /// <param name="call">The call defintion.</param>
37         /// <param name="req">Request message.</param>
38         /// <typeparam name="TRequest">Type of request message.</typeparam>
39         /// <typeparam name="TResponse">The of response message.</typeparam>
40         public static TResponse BlockingUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
41             where TRequest : class
42             where TResponse : class
43         {
44             var asyncCall = new AsyncCall<TRequest, TResponse>(call);
45             return asyncCall.UnaryCall(req);
46         }
47 
48         /// <summary>
49         /// Invokes a simple remote call asynchronously.
50         /// </summary>
51         /// <returns>An awaitable call object providing access to the response.</returns>
52         /// <param name="call">The call defintion.</param>
53         /// <param name="req">Request message.</param>
54         /// <typeparam name="TRequest">Type of request message.</typeparam>
55         /// <typeparam name="TResponse">The of response message.</typeparam>
56         public static AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
57             where TRequest : class
58             where TResponse : class
59         {
60             var asyncCall = new AsyncCall<TRequest, TResponse>(call);
61             var asyncResult = asyncCall.UnaryCallAsync(req);
62             return new AsyncUnaryCall<TResponse>(asyncResult, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
63         }
64 
65         /// <summary>
66         /// Invokes a server streaming call asynchronously.
67         /// In server streaming scenario, client sends on request and server responds with a stream of responses.
68         /// </summary>
69         /// <returns>A call object providing access to the asynchronous response stream.</returns>
70         /// <param name="call">The call defintion.</param>
71         /// <param name="req">Request message.</param>
72         /// <typeparam name="TRequest">Type of request message.</typeparam>
73         /// <typeparam name="TResponse">The of response messages.</typeparam>
74         public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
75             where TRequest : class
76             where TResponse : class
77         {
78             var asyncCall = new AsyncCall<TRequest, TResponse>(call);
asyncCall.StartServerStreamingCall(req)79             asyncCall.StartServerStreamingCall(req);
80             var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
81             return new AsyncServerStreamingCall<TResponse>(responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
82         }
83 
84         /// <summary>
85         /// Invokes a client streaming call asynchronously.
86         /// In client streaming scenario, client sends a stream of requests and server responds with a single response.
87         /// </summary>
88         /// <param name="call">The call defintion.</param>
89         /// <returns>An awaitable call object providing access to the response.</returns>
90         /// <typeparam name="TRequest">Type of request messages.</typeparam>
91         /// <typeparam name="TResponse">The of response message.</typeparam>
92         public static AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call)
93             where TRequest : class
94             where TResponse : class
95         {
96             var asyncCall = new AsyncCall<TRequest, TResponse>(call);
97             var resultTask = asyncCall.ClientStreamingCallAsync();
98             var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
99             return new AsyncClientStreamingCall<TRequest, TResponse>(requestStream, resultTask, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
100         }
101 
102         /// <summary>
103         /// Invokes a duplex streaming call asynchronously.
104         /// In duplex streaming scenario, client sends a stream of requests and server responds with a stream of responses.
105         /// The response stream is completely independent and both side can be sending messages at the same time.
106         /// </summary>
107         /// <returns>A call object providing access to the asynchronous request and response streams.</returns>
108         /// <param name="call">The call definition.</param>
109         /// <typeparam name="TRequest">Type of request messages.</typeparam>
110         /// <typeparam name="TResponse">Type of reponse messages.</typeparam>
111         public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call)
112             where TRequest : class
113             where TResponse : class
114         {
115             var asyncCall = new AsyncCall<TRequest, TResponse>(call);
asyncCall.StartDuplexStreamingCall()116             asyncCall.StartDuplexStreamingCall();
117             var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
118             var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
119             return new AsyncDuplexStreamingCall<TRequest, TResponse>(requestStream, responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
120         }
121     }
122 }
123