OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 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. |
| 19 // |
| 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 |
| 34 using System.Threading.Tasks; |
| 35 using Grpc.Core.Internal; |
| 36 |
| 37 namespace Grpc.Core |
| 38 { |
| 39 /// <summary> |
| 40 /// Helper methods for generated clients to make RPC calls. |
| 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. |
| 44 /// </summary> |
| 45 public static class Calls |
| 46 { |
| 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> |
| 55 public static TResponse BlockingUnaryCall<TRequest, TResponse>(CallInvoc
ationDetails<TRequest, TResponse> call, TRequest req) |
| 56 where TRequest : class |
| 57 where TResponse : class |
| 58 { |
| 59 var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
| 60 return asyncCall.UnaryCall(req); |
| 61 } |
| 62 |
| 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> |
| 71 public static AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TRespon
se>(CallInvocationDetails<TRequest, TResponse> call, TRequest req) |
| 72 where TRequest : class |
| 73 where TResponse : class |
| 74 { |
| 75 var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
| 76 var asyncResult = asyncCall.UnaryCallAsync(req); |
| 77 return new AsyncUnaryCall<TResponse>(asyncResult, asyncCall.Response
HeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); |
| 78 } |
| 79 |
| 80 /// <summary> |
| 81 /// Invokes a server streaming call asynchronously. |
| 82 /// In server streaming scenario, client sends on request and server res
ponds 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> |
| 89 public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCa
ll<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TReques
t req) |
| 90 where TRequest : class |
| 91 where TResponse : class |
| 92 { |
| 93 var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
| 94 asyncCall.StartServerStreamingCall(req); |
| 95 var responseStream = new ClientResponseStream<TRequest, TResponse>(a
syncCall); |
| 96 return new AsyncServerStreamingCall<TResponse>(responseStream, async
Call.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall
.Cancel); |
| 97 } |
| 98 |
| 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 /// <param name="call">The call defintion.</param> |
| 104 /// <returns>An awaitable call object providing access to the response.<
/returns> |
| 105 /// <typeparam name="TRequest">Type of request messages.</typeparam> |
| 106 /// <typeparam name="TResponse">The of response message.</typeparam> |
| 107 public static AsyncClientStreamingCall<TRequest, TResponse> AsyncClientS
treamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> cal
l) |
| 108 where TRequest : class |
| 109 where TResponse : class |
| 110 { |
| 111 var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
| 112 var resultTask = asyncCall.ClientStreamingCallAsync(); |
| 113 var requestStream = new ClientRequestStream<TRequest, TResponse>(asy
ncCall); |
| 114 return new AsyncClientStreamingCall<TRequest, TResponse>(requestStre
am, resultTask, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.G
etTrailers, asyncCall.Cancel); |
| 115 } |
| 116 |
| 117 /// <summary> |
| 118 /// Invokes a duplex streaming call asynchronously. |
| 119 /// In duplex streaming scenario, client sends a stream of requests and
server responds with a stream of responses. |
| 120 /// The response stream is completely independent and both side can be s
ending messages at the same time. |
| 121 /// </summary> |
| 122 /// <returns>A call object providing access to the asynchronous request
and response streams.</returns> |
| 123 /// <param name="call">The call definition.</param> |
| 124 /// <typeparam name="TRequest">Type of request messages.</typeparam> |
| 125 /// <typeparam name="TResponse">Type of reponse messages.</typeparam> |
| 126 public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexS
treamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> cal
l) |
| 127 where TRequest : class |
| 128 where TResponse : class |
| 129 { |
| 130 var asyncCall = new AsyncCall<TRequest, TResponse>(call); |
| 131 asyncCall.StartDuplexStreamingCall(); |
| 132 var requestStream = new ClientRequestStream<TRequest, TResponse>(asy
ncCall); |
| 133 var responseStream = new ClientResponseStream<TRequest, TResponse>(a
syncCall); |
| 134 return new AsyncDuplexStreamingCall<TRequest, TResponse>(requestStre
am, responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCa
ll.GetTrailers, asyncCall.Cancel); |
| 135 } |
| 136 } |
| 137 } |
OLD | NEW |