AsyncUnaryCall.cs 5.4 KB

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