AsyncDuplexStreamingCall.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. internal AsyncDuplexStreamingCall(IClientStreamWriter<TRequest> requestStream, IAsyncStreamReader<TResponse> responseStream, Task<Metadata> responseHeadersAsync, Func<Status> getStatusFunc, Func<Metadata> getTrailersFunc, Action disposeAction)
  34. {
  35. this.requestStream = requestStream;
  36. this.responseStream = responseStream;
  37. this.responseHeadersAsync = responseHeadersAsync;
  38. this.getStatusFunc = getStatusFunc;
  39. this.getTrailersFunc = getTrailersFunc;
  40. this.disposeAction = disposeAction;
  41. }
  42. /// <summary>
  43. /// Async stream to read streaming responses.
  44. /// </summary>
  45. public IAsyncStreamReader<TResponse> ResponseStream
  46. {
  47. get
  48. {
  49. return responseStream;
  50. }
  51. }
  52. /// <summary>
  53. /// Async stream to send streaming requests.
  54. /// </summary>
  55. public IClientStreamWriter<TRequest> RequestStream
  56. {
  57. get
  58. {
  59. return requestStream;
  60. }
  61. }
  62. /// <summary>
  63. /// Asynchronous access to response headers.
  64. /// </summary>
  65. public Task<Metadata> ResponseHeadersAsync
  66. {
  67. get
  68. {
  69. return this.responseHeadersAsync;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the call status if the call has already finished.
  74. /// Throws InvalidOperationException otherwise.
  75. /// </summary>
  76. public Status GetStatus()
  77. {
  78. return getStatusFunc();
  79. }
  80. /// <summary>
  81. /// Gets the call trailing metadata if the call has already finished.
  82. /// Throws InvalidOperationException otherwise.
  83. /// </summary>
  84. public Metadata GetTrailers()
  85. {
  86. return getTrailersFunc();
  87. }
  88. /// <summary>
  89. /// Provides means to cleanup after the call.
  90. /// If the call has already finished normally (request stream has been completed and response stream has been fully read), doesn't do anything.
  91. /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call.
  92. /// As a result, all resources being used by the call should be released eventually.
  93. /// </summary>
  94. /// <remarks>
  95. /// Normally, there is no need for you to dispose the call unless you want to utilize the
  96. /// "Cancel" semantics of invoking <c>Dispose</c>.
  97. /// </remarks>
  98. public void Dispose()
  99. {
  100. disposeAction.Invoke();
  101. }
  102. }
  103. }