AsyncDuplexStreamingCall.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace Grpc.Core
  19. {
  20. /// <summary>
  21. /// Return type for bidirectional streaming calls.
  22. /// </summary>
  23. /// <typeparam name="TRequest">Request message type for this call.</typeparam>
  24. /// <typeparam name="TResponse">Response message type for this call.</typeparam>
  25. public sealed class AsyncDuplexStreamingCall<TRequest, TResponse> : IDisposable
  26. {
  27. readonly IClientStreamWriter<TRequest> requestStream;
  28. readonly IAsyncStreamReader<TResponse> responseStream;
  29. readonly Task<Metadata> responseHeadersAsync;
  30. readonly Func<Status> getStatusFunc;
  31. readonly Func<Metadata> getTrailersFunc;
  32. readonly Action disposeAction;
  33. /// <summary>
  34. /// Creates a new AsyncDuplexStreamingCall object with the specified properties.
  35. /// </summary>
  36. /// <param name="requestStream">Stream of request values.</param>
  37. /// <param name="responseStream">Stream of response values.</param>
  38. /// <param name="responseHeadersAsync">Response headers of the asynchronous call.</param>
  39. /// <param name="getStatusFunc">Delegate returning the status of the call.</param>
  40. /// <param name="getTrailersFunc">Delegate returning the trailing metadata of the call.</param>
  41. /// <param name="disposeAction">Delegate to invoke when Dispose is called on the call object.</param>
  42. public AsyncDuplexStreamingCall(IClientStreamWriter<TRequest> requestStream,
  43. IAsyncStreamReader<TResponse> responseStream,
  44. Task<Metadata> responseHeadersAsync,
  45. Func<Status> getStatusFunc,
  46. Func<Metadata> getTrailersFunc,
  47. Action disposeAction)
  48. {
  49. this.requestStream = requestStream;
  50. this.responseStream = responseStream;
  51. this.responseHeadersAsync = responseHeadersAsync;
  52. this.getStatusFunc = getStatusFunc;
  53. this.getTrailersFunc = getTrailersFunc;
  54. this.disposeAction = disposeAction;
  55. }
  56. /// <summary>
  57. /// Async stream to read streaming responses.
  58. /// </summary>
  59. public IAsyncStreamReader<TResponse> ResponseStream
  60. {
  61. get
  62. {
  63. return responseStream;
  64. }
  65. }
  66. /// <summary>
  67. /// Async stream to send streaming requests.
  68. /// </summary>
  69. public IClientStreamWriter<TRequest> RequestStream
  70. {
  71. get
  72. {
  73. return requestStream;
  74. }
  75. }
  76. /// <summary>
  77. /// Asynchronous access to response headers.
  78. /// </summary>
  79. public Task<Metadata> ResponseHeadersAsync
  80. {
  81. get
  82. {
  83. return this.responseHeadersAsync;
  84. }
  85. }
  86. /// <summary>
  87. /// Gets the call status if the call has already finished.
  88. /// Throws InvalidOperationException otherwise.
  89. /// </summary>
  90. public Status GetStatus()
  91. {
  92. return getStatusFunc();
  93. }
  94. /// <summary>
  95. /// Gets the call trailing metadata if the call has already finished.
  96. /// Throws InvalidOperationException otherwise.
  97. /// </summary>
  98. public Metadata GetTrailers()
  99. {
  100. return getTrailersFunc();
  101. }
  102. /// <summary>
  103. /// Provides means to cleanup after the call.
  104. /// If the call has already finished normally (request stream has been completed and response stream has been fully read), doesn't do anything.
  105. /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call.
  106. /// As a result, all resources being used by the call should be released eventually.
  107. /// </summary>
  108. /// <remarks>
  109. /// Normally, there is no need for you to dispose the call unless you want to utilize the
  110. /// "Cancel" semantics of invoking <c>Dispose</c>.
  111. /// </remarks>
  112. public void Dispose()
  113. {
  114. disposeAction.Invoke();
  115. }
  116. }
  117. }