AsyncResult.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. using Aliyun.OSS.Common.Communication;
  11. namespace Aliyun.OSS.Util
  12. {
  13. /// <summary>
  14. /// The implementation of <see cref="IAsyncResult"/>
  15. /// that represents the status of an async operation.
  16. /// </summary>
  17. internal abstract class AsyncResult : IAsyncResult, IDisposable
  18. {
  19. #region Fields
  20. private readonly object _asyncState;
  21. private bool _isCompleted;
  22. private readonly AsyncCallback _userCallback;
  23. private ManualResetEvent _asyncWaitEvent;
  24. private Exception _exception;
  25. #endregion
  26. #region IAsyncResult Members
  27. /// <summary>
  28. /// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
  29. /// </summary>
  30. public object AsyncState
  31. {
  32. get { return _asyncState; }
  33. }
  34. public AsyncCallback Callback
  35. {
  36. get { return _userCallback; }
  37. }
  38. /// <summary>
  39. /// Gets a <see cref="WaitHandle"/> that is used to wait for an asynchronous operation to complete.
  40. /// </summary>
  41. public WaitHandle AsyncWaitHandle
  42. {
  43. get
  44. {
  45. if (_asyncWaitEvent != null)
  46. return _asyncWaitEvent;
  47. _asyncWaitEvent = new ManualResetEvent(false);
  48. if (IsCompleted)
  49. _asyncWaitEvent.Set();
  50. return _asyncWaitEvent;
  51. }
  52. }
  53. /// <summary>
  54. /// Gets a value that indicates whether the asynchronous operation completed synchronously.
  55. /// </summary>
  56. public bool CompletedSynchronously { get; protected set; }
  57. /// <summary>
  58. /// Gets a value that indicates whether the asynchronous operation has completed.
  59. /// </summary>
  60. [DebuggerNonUserCode]
  61. public bool IsCompleted
  62. {
  63. get { return _isCompleted; }
  64. }
  65. #endregion
  66. /// <summary>
  67. /// Initializes an instance of <see cref="AsyncResult"/>.
  68. /// </summary>
  69. /// <param name="callback">The callback method when the async operation completes.</param>
  70. /// <param name="state">A user-defined object that qualifies or contains information about an asynchronous operation.</param>
  71. protected AsyncResult(AsyncCallback callback, object state)
  72. {
  73. _userCallback = callback;
  74. _asyncState = state;
  75. }
  76. /// <summary>
  77. /// Completes the async operation with an exception.
  78. /// </summary>
  79. /// <param name="ex">Exception from the async operation.</param>
  80. public void Complete(Exception ex)
  81. {
  82. _exception = ex;
  83. NotifyCompletion();
  84. }
  85. /// <summary>
  86. /// When called in the dervied classes, wait for completion.
  87. /// It throws exception if the async operation ends with an exception.
  88. /// </summary>
  89. protected void WaitForCompletion()
  90. {
  91. if (!IsCompleted)
  92. AsyncWaitHandle.WaitOne();
  93. if (_exception != null)
  94. throw _exception;
  95. }
  96. /// <summary>
  97. /// When called in the derived classes, notify operation completion
  98. /// by setting <see cref="P:AsyncWaitHandle"/> and calling the user callback.
  99. /// </summary>
  100. protected void NotifyCompletion()
  101. {
  102. _isCompleted = true;
  103. if (_asyncWaitEvent != null)
  104. _asyncWaitEvent.Set();
  105. if (_userCallback != null)
  106. {
  107. var httpAsyncResult = this as ServiceClientImpl.HttpAsyncResult;
  108. Debug.Assert(httpAsyncResult != null);
  109. _userCallback(httpAsyncResult.AsyncState as RetryableAsyncResult);
  110. }
  111. }
  112. #region IDisposable Members
  113. /// <summary>
  114. /// Disposes the object and release resource.
  115. /// </summary>
  116. public void Dispose()
  117. {
  118. Dispose(true);
  119. GC.SuppressFinalize(this);
  120. }
  121. /// <summary>
  122. /// When overrided in the derived classes, release resources.
  123. /// </summary>
  124. /// <param name="disposing">Whether the method is called <see cref="M:Dispose"/></param>
  125. protected virtual void Dispose(bool disposing)
  126. {
  127. if (disposing && _asyncWaitEvent != null)
  128. {
  129. _asyncWaitEvent.Close();
  130. _asyncWaitEvent = null;
  131. }
  132. }
  133. #endregion
  134. }
  135. /// <summary>
  136. /// Represents the status of an async operation.
  137. /// It also holds the result of the operation.
  138. /// </summary>
  139. /// <typeparam name="T">Type of the operation result.</typeparam>
  140. internal class AsyncResult<T> : AsyncResult
  141. {
  142. /// <summary>
  143. /// The result of the async operation.
  144. /// </summary>
  145. private T _result;
  146. /// <summary>
  147. /// Initializes an instance of <see cref="AsyncResult&lt;T&gt;"/>.
  148. /// </summary>
  149. /// <param name="callback">The callback method when the async operation completes.</param>
  150. /// <param name="state">A user-defined object that qualifies or contains information about an asynchronous operation.</param>
  151. public AsyncResult(AsyncCallback callback, object state)
  152. : base(callback, state)
  153. { }
  154. /// <summary>
  155. /// Gets result and release resources.
  156. /// </summary>
  157. /// <returns>The instance of result.</returns>
  158. public T GetResult()
  159. {
  160. base.WaitForCompletion();
  161. return _result;
  162. }
  163. /// <summary>
  164. /// Sets result and notify completion.
  165. /// </summary>
  166. /// <param name="result">The instance of result.</param>
  167. public void Complete(T result)
  168. {
  169. // Complete should not throw if disposed.
  170. _result = result;
  171. base.NotifyCompletion();
  172. }
  173. }
  174. }