blob: e57ac89db37c59aa57f71a231a2196b5cff3866a [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#endregion
33
Jan Tattermuscha7608b02015-02-03 17:54:38 -080034using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080035using Grpc.Core.Internal;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080036
Jan Tattermusch30868622015-02-19 09:22:33 -080037namespace Grpc.Core
Jan Tattermuscha7608b02015-02-03 17:54:38 -080038{
Jan Tattermuscha7608b02015-02-03 17:54:38 -080039 /// <summary>
Jan Tattermusche5c9b802015-07-14 17:46:58 -070040 /// Helper methods for generated clients to make RPC calls.
Jan Tattermusch1b926ee2015-08-10 22:51:29 -070041 /// Most users will use this class only indirectly and will be
42 /// making calls using client object generated from protocol
43 /// buffer definition files.
Jan Tattermuscha7608b02015-02-03 17:54:38 -080044 /// </summary>
45 public static class Calls
46 {
Jan Tattermusch1b926ee2015-08-10 22:51:29 -070047 /// <summary>
48 /// Invokes a simple remote call in a blocking fashion.
49 /// </summary>
50 /// <returns>The response.</returns>
51 /// <param name="call">The call defintion.</param>
52 /// <param name="req">Request message.</param>
53 /// <typeparam name="TRequest">Type of request message.</typeparam>
54 /// <typeparam name="TResponse">The of response message.</typeparam>
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -070055 public static TResponse BlockingUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
Jan Tattermuschbdb1b482015-05-06 14:46:25 -070056 where TRequest : class
57 where TResponse : class
Jan Tattermuscha7608b02015-02-03 17:54:38 -080058 {
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -070059 var asyncCall = new AsyncCall<TRequest, TResponse>(call);
Jan Tattermusch542e21c2015-08-05 02:25:33 -070060 return asyncCall.UnaryCall(req);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080061 }
62
Jan Tattermusch1b926ee2015-08-10 22:51:29 -070063 /// <summary>
64 /// Invokes a simple remote call asynchronously.
65 /// </summary>
66 /// <returns>An awaitable call object providing access to the response.</returns>
67 /// <param name="call">The call defintion.</param>
68 /// <param name="req">Request message.</param>
69 /// <typeparam name="TRequest">Type of request message.</typeparam>
70 /// <typeparam name="TResponse">The of response message.</typeparam>
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -070071 public static AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
Jan Tattermuschbdb1b482015-05-06 14:46:25 -070072 where TRequest : class
73 where TResponse : class
Jan Tattermuscha7608b02015-02-03 17:54:38 -080074 {
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -070075 var asyncCall = new AsyncCall<TRequest, TResponse>(call);
Jan Tattermusch542e21c2015-08-05 02:25:33 -070076 var asyncResult = asyncCall.UnaryCallAsync(req);
Jan Tattermuschfb34a992015-08-21 10:45:39 -070077 return new AsyncUnaryCall<TResponse>(asyncResult, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080078 }
79
Jan Tattermusch1b926ee2015-08-10 22:51:29 -070080 /// <summary>
81 /// Invokes a server streaming call asynchronously.
82 /// In server streaming scenario, client sends on request and server responds with a stream of responses.
83 /// </summary>
84 /// <returns>A call object providing access to the asynchronous response stream.</returns>
85 /// <param name="call">The call defintion.</param>
86 /// <param name="req">Request message.</param>
87 /// <typeparam name="TRequest">Type of request message.</typeparam>
88 /// <typeparam name="TResponse">The of response messages.</typeparam>
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -070089 public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
Jan Tattermuschbdb1b482015-05-06 14:46:25 -070090 where TRequest : class
91 where TResponse : class
Jan Tattermuscha7608b02015-02-03 17:54:38 -080092 {
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -070093 var asyncCall = new AsyncCall<TRequest, TResponse>(call);
Jan Tattermusch542e21c2015-08-05 02:25:33 -070094 asyncCall.StartServerStreamingCall(req);
Jan Tattermuscha5272b62015-04-30 11:56:46 -070095 var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
Jan Tattermusch4c25efa2015-08-21 16:07:57 -070096 return new AsyncServerStreamingCall<TResponse>(responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
Jan Tattermuscha7608b02015-02-03 17:54:38 -080097 }
98
Jan Tattermusch1b926ee2015-08-10 22:51:29 -070099 /// <summary>
100 /// Invokes a client streaming call asynchronously.
101 /// In client streaming scenario, client sends a stream of requests and server responds with a single response.
102 /// </summary>
103 /// <returns>An awaitable call object providing access to the response.</returns>
104 /// <typeparam name="TRequest">Type of request messages.</typeparam>
105 /// <typeparam name="TResponse">The of response message.</typeparam>
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -0700106 public static AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call)
Jan Tattermuschbdb1b482015-05-06 14:46:25 -0700107 where TRequest : class
108 where TResponse : class
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800109 {
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -0700110 var asyncCall = new AsyncCall<TRequest, TResponse>(call);
Jan Tattermusch542e21c2015-08-05 02:25:33 -0700111 var resultTask = asyncCall.ClientStreamingCallAsync();
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700112 var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
Jan Tattermuschfb34a992015-08-21 10:45:39 -0700113 return new AsyncClientStreamingCall<TRequest, TResponse>(requestStream, resultTask, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800114 }
115
Jan Tattermusch1b926ee2015-08-10 22:51:29 -0700116 /// <summary>
117 /// Invokes a duplex streaming call asynchronously.
118 /// In duplex streaming scenario, client sends a stream of requests and server responds with a stream of responses.
119 /// The response stream is completely independent and both side can be sending messages at the same time.
120 /// </summary>
121 /// <returns>A call object providing access to the asynchronous request and response streams.</returns>
122 /// <param name="call">The call definition.</param>
123 /// <typeparam name="TRequest">Type of request messages.</typeparam>
124 /// <typeparam name="TResponse">Type of reponse messages.</typeparam>
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -0700125 public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call)
Jan Tattermuschbdb1b482015-05-06 14:46:25 -0700126 where TRequest : class
127 where TResponse : class
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800128 {
Jan Tattermusch5cb5ced2015-08-05 04:05:57 -0700129 var asyncCall = new AsyncCall<TRequest, TResponse>(call);
Jan Tattermusch542e21c2015-08-05 02:25:33 -0700130 asyncCall.StartDuplexStreamingCall();
Jan Tattermuscha5272b62015-04-30 11:56:46 -0700131 var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
132 var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
Jan Tattermusch4c25efa2015-08-21 16:07:57 -0700133 return new AsyncDuplexStreamingCall<TRequest, TResponse>(requestStream, responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel);
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800134 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800135 }
136}