AsyncUnaryCall.cs 3.6 KB

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