AsyncServerStreamingCall.cs 4.2 KB

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