Calls.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading.Tasks;
  18. using Grpc.Core.Internal;
  19. namespace Grpc.Core
  20. {
  21. /// <summary>
  22. /// Helper methods for generated clients to make RPC calls.
  23. /// Most users will use this class only indirectly and will be
  24. /// making calls using client object generated from protocol
  25. /// buffer definition files.
  26. /// </summary>
  27. public static class Calls
  28. {
  29. /// <summary>
  30. /// Invokes a simple remote call in a blocking fashion.
  31. /// </summary>
  32. /// <returns>The response.</returns>
  33. /// <param name="call">The call definition.</param>
  34. /// <param name="req">Request message.</param>
  35. /// <typeparam name="TRequest">Type of request message.</typeparam>
  36. /// <typeparam name="TResponse">The of response message.</typeparam>
  37. public static TResponse BlockingUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
  38. where TRequest : class
  39. where TResponse : class
  40. {
  41. var asyncCall = new AsyncCall<TRequest, TResponse>(call);
  42. return asyncCall.UnaryCall(req);
  43. }
  44. /// <summary>
  45. /// Invokes a simple remote call asynchronously.
  46. /// </summary>
  47. /// <returns>An awaitable call object providing access to the response.</returns>
  48. /// <param name="call">The call definition.</param>
  49. /// <param name="req">Request message.</param>
  50. /// <typeparam name="TRequest">Type of request message.</typeparam>
  51. /// <typeparam name="TResponse">The of response message.</typeparam>
  52. public static AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
  53. where TRequest : class
  54. where TResponse : class
  55. {
  56. var asyncCall = new AsyncCall<TRequest, TResponse>(call);
  57. var asyncResult = asyncCall.UnaryCallAsync(req);
  58. return new AsyncUnaryCall<TResponse>(asyncResult,
  59. Callbacks<TRequest, TResponse>.GetHeaders, Callbacks<TRequest, TResponse>.GetStatus,
  60. Callbacks<TRequest, TResponse>.GetTrailers, Callbacks<TRequest, TResponse>.Cancel,
  61. asyncCall);
  62. }
  63. /// <summary>
  64. /// Invokes a server streaming call asynchronously.
  65. /// In server streaming scenario, client sends on request and server responds with a stream of responses.
  66. /// </summary>
  67. /// <returns>A call object providing access to the asynchronous response stream.</returns>
  68. /// <param name="call">The call definition.</param>
  69. /// <param name="req">Request message.</param>
  70. /// <typeparam name="TRequest">Type of request message.</typeparam>
  71. /// <typeparam name="TResponse">The of response messages.</typeparam>
  72. public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call, TRequest req)
  73. where TRequest : class
  74. where TResponse : class
  75. {
  76. var asyncCall = new AsyncCall<TRequest, TResponse>(call);
  77. asyncCall.StartServerStreamingCall(req);
  78. var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
  79. return new AsyncServerStreamingCall<TResponse>(responseStream,
  80. Callbacks<TRequest, TResponse>.GetHeaders, Callbacks<TRequest, TResponse>.GetStatus,
  81. Callbacks<TRequest, TResponse>.GetTrailers, Callbacks<TRequest, TResponse>.Cancel,
  82. asyncCall);
  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 definition.</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,
  100. Callbacks<TRequest, TResponse>.GetHeaders, Callbacks<TRequest, TResponse>.GetStatus,
  101. Callbacks<TRequest, TResponse>.GetTrailers, Callbacks<TRequest, TResponse>.Cancel,
  102. asyncCall);
  103. }
  104. /// <summary>
  105. /// Invokes a duplex streaming call asynchronously.
  106. /// In duplex streaming scenario, client sends a stream of requests and server responds with a stream of responses.
  107. /// The response stream is completely independent and both side can be sending messages at the same time.
  108. /// </summary>
  109. /// <returns>A call object providing access to the asynchronous request and response streams.</returns>
  110. /// <param name="call">The call definition.</param>
  111. /// <typeparam name="TRequest">Type of request messages.</typeparam>
  112. /// <typeparam name="TResponse">Type of responsemessages.</typeparam>
  113. public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(CallInvocationDetails<TRequest, TResponse> call)
  114. where TRequest : class
  115. where TResponse : class
  116. {
  117. var asyncCall = new AsyncCall<TRequest, TResponse>(call);
  118. asyncCall.StartDuplexStreamingCall();
  119. var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
  120. var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
  121. return new AsyncDuplexStreamingCall<TRequest, TResponse>(requestStream, responseStream,
  122. Callbacks<TRequest, TResponse>.GetHeaders, Callbacks<TRequest, TResponse>.GetStatus,
  123. Callbacks<TRequest, TResponse>.GetTrailers, Callbacks<TRequest, TResponse>.Cancel,
  124. asyncCall);
  125. }
  126. private static class Callbacks<TRequest, TResponse>
  127. {
  128. internal static readonly Func<object, Task<Metadata>> GetHeaders = state => ((AsyncCall<TRequest, TResponse>)state).ResponseHeadersAsync;
  129. internal static readonly Func<object, Status> GetStatus = state => ((AsyncCall<TRequest, TResponse>)state).GetStatus();
  130. internal static readonly Func<object, Metadata> GetTrailers = state => ((AsyncCall<TRequest, TResponse>)state).GetTrailers();
  131. internal static readonly Action<object> Cancel = state => ((AsyncCall<TRequest, TResponse>)state).Cancel();
  132. }
  133. }
  134. }