Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | // All rights reserved. |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 5 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 9 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 10 | // * 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 19 | // |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 20 | // 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 Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 34 | using System.Threading.Tasks; |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 35 | using Grpc.Core.Internal; |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 36 | |
Jan Tattermusch | 3086862 | 2015-02-19 09:22:33 -0800 | [diff] [blame] | 37 | namespace Grpc.Core |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 38 | { |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 39 | /// <summary> |
Jan Tattermusch | e5c9b80 | 2015-07-14 17:46:58 -0700 | [diff] [blame] | 40 | /// Helper methods for generated clients to make RPC calls. |
Jan Tattermusch | 1b926ee | 2015-08-10 22:51:29 -0700 | [diff] [blame] | 41 | /// 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 Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 44 | /// </summary> |
| 45 | public static class Calls |
| 46 | { |
Jan Tattermusch | 1b926ee | 2015-08-10 22:51:29 -0700 | [diff] [blame] | 47 | /// <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 Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 55 | public static TResponse BlockingUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 56 | where TRequest : class |
| 57 | where TResponse : class |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 58 | { |
Jan Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 59 | var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
Jan Tattermusch | 542e21c | 2015-08-05 02:25:33 -0700 | [diff] [blame] | 60 | return asyncCall.UnaryCall(req); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Jan Tattermusch | 1b926ee | 2015-08-10 22:51:29 -0700 | [diff] [blame] | 63 | /// <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 Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 71 | public static AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 72 | where TRequest : class |
| 73 | where TResponse : class |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 74 | { |
Jan Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 75 | var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
Jan Tattermusch | 542e21c | 2015-08-05 02:25:33 -0700 | [diff] [blame] | 76 | var asyncResult = asyncCall.UnaryCallAsync(req); |
Jan Tattermusch | fb34a99 | 2015-08-21 10:45:39 -0700 | [diff] [blame] | 77 | return new AsyncUnaryCall<TResponse>(asyncResult, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Jan Tattermusch | 1b926ee | 2015-08-10 22:51:29 -0700 | [diff] [blame] | 80 | /// <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 Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 89 | public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 90 | where TRequest : class |
| 91 | where TResponse : class |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 92 | { |
Jan Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 93 | var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
Jan Tattermusch | 542e21c | 2015-08-05 02:25:33 -0700 | [diff] [blame] | 94 | asyncCall.StartServerStreamingCall(req); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 95 | var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall); |
Jan Tattermusch | 4c25efa | 2015-08-21 16:07:57 -0700 | [diff] [blame^] | 96 | return new AsyncServerStreamingCall<TResponse>(responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Jan Tattermusch | 1b926ee | 2015-08-10 22:51:29 -0700 | [diff] [blame] | 99 | /// <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 Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 106 | public static AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 107 | where TRequest : class |
| 108 | where TResponse : class |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 109 | { |
Jan Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 110 | var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
Jan Tattermusch | 542e21c | 2015-08-05 02:25:33 -0700 | [diff] [blame] | 111 | var resultTask = asyncCall.ClientStreamingCallAsync(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 112 | var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall); |
Jan Tattermusch | fb34a99 | 2015-08-21 10:45:39 -0700 | [diff] [blame] | 113 | return new AsyncClientStreamingCall<TRequest, TResponse>(requestStream, resultTask, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jan Tattermusch | 1b926ee | 2015-08-10 22:51:29 -0700 | [diff] [blame] | 116 | /// <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 Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 125 | public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call) |
Jan Tattermusch | bdb1b48 | 2015-05-06 14:46:25 -0700 | [diff] [blame] | 126 | where TRequest : class |
| 127 | where TResponse : class |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 128 | { |
Jan Tattermusch | 5cb5ced | 2015-08-05 04:05:57 -0700 | [diff] [blame] | 129 | var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
Jan Tattermusch | 542e21c | 2015-08-05 02:25:33 -0700 | [diff] [blame] | 130 | asyncCall.StartDuplexStreamingCall(); |
Jan Tattermusch | a5272b6 | 2015-04-30 11:56:46 -0700 | [diff] [blame] | 131 | var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall); |
| 132 | var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall); |
Jan Tattermusch | 4c25efa | 2015-08-21 16:07:57 -0700 | [diff] [blame^] | 133 | return new AsyncDuplexStreamingCall<TRequest, TResponse>(requestStream, responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 134 | } |
Jan Tattermusch | a7608b0 | 2015-02-03 17:54:38 -0800 | [diff] [blame] | 135 | } |
| 136 | } |