AsyncServerStreamingCall.cs 3.4 KB

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