WebAsyncResult.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // System.Net.WebAsyncResult
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System.IO;
  10. using System.Threading;
  11. namespace System.Net
  12. {
  13. class WebAsyncResult : IAsyncResult
  14. {
  15. ManualResetEvent handle;
  16. bool synch;
  17. bool isCompleted;
  18. AsyncCallback cb;
  19. object state;
  20. int nbytes;
  21. IAsyncResult innerAsyncResult;
  22. bool callbackDone;
  23. Exception exc;
  24. HttpWebRequest request;
  25. HttpWebResponse response;
  26. Stream writeStream;
  27. byte [] buffer;
  28. int offset;
  29. int size;
  30. public WebAsyncResult (AsyncCallback cb, object state)
  31. {
  32. this.cb = cb;
  33. this.state = state;
  34. }
  35. public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
  36. {
  37. this.request = request;
  38. this.cb = cb;
  39. this.state = state;
  40. }
  41. public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
  42. {
  43. this.cb = cb;
  44. this.state = state;
  45. this.buffer = buffer;
  46. this.offset = offset;
  47. this.size = size;
  48. }
  49. internal void SetCompleted (bool synch, Exception e)
  50. {
  51. isCompleted = true;
  52. this.synch = synch;
  53. exc = e;
  54. ((ManualResetEvent) AsyncWaitHandle).Set ();
  55. }
  56. internal void Reset ()
  57. {
  58. isCompleted = false;
  59. callbackDone = false;
  60. exc = null;
  61. request = null;
  62. response = null;
  63. writeStream = null;
  64. exc = null;
  65. if (handle != null) {
  66. handle.Close ();
  67. handle = null;
  68. }
  69. }
  70. internal void SetCompleted (bool synch, int nbytes)
  71. {
  72. isCompleted = true;
  73. this.synch = synch;
  74. this.nbytes = nbytes;
  75. exc = null;
  76. ((ManualResetEvent) AsyncWaitHandle).Set ();
  77. }
  78. internal void SetCompleted (bool synch, Stream writeStream)
  79. {
  80. isCompleted = true;
  81. this.synch = synch;
  82. this.writeStream = writeStream;
  83. exc = null;
  84. ((ManualResetEvent) AsyncWaitHandle).Set ();
  85. }
  86. internal void SetCompleted (bool synch, HttpWebResponse response)
  87. {
  88. isCompleted = true;
  89. this.synch = synch;
  90. this.response = response;
  91. exc = null;
  92. ((ManualResetEvent) AsyncWaitHandle).Set ();
  93. }
  94. internal void DoCallback ()
  95. {
  96. if (!callbackDone && cb != null) {
  97. callbackDone = true;
  98. cb (this);
  99. }
  100. }
  101. internal void WaitUntilComplete ()
  102. {
  103. if (isCompleted)
  104. return;
  105. AsyncWaitHandle.WaitOne ();
  106. }
  107. internal bool WaitUntilComplete (int timeout, bool exitContext)
  108. {
  109. if (isCompleted)
  110. return true;
  111. return AsyncWaitHandle.WaitOne (timeout, exitContext);
  112. }
  113. public object AsyncState {
  114. get { return state; }
  115. }
  116. public WaitHandle AsyncWaitHandle {
  117. get {
  118. if (handle == null) {
  119. lock (this) {
  120. if (handle == null)
  121. handle = new ManualResetEvent (isCompleted);
  122. }
  123. }
  124. return handle;
  125. }
  126. }
  127. public bool CompletedSynchronously {
  128. get { return synch; }
  129. }
  130. public bool IsCompleted {
  131. get { return isCompleted; }
  132. }
  133. internal bool GotException {
  134. get { return (exc != null); }
  135. }
  136. internal Exception Exception {
  137. get { return exc; }
  138. }
  139. internal int NBytes {
  140. get { return nbytes; }
  141. set { nbytes = value; }
  142. }
  143. internal IAsyncResult InnerAsyncResult {
  144. get { return innerAsyncResult; }
  145. set { innerAsyncResult = value; }
  146. }
  147. internal Stream WriteStream {
  148. get { return writeStream; }
  149. }
  150. internal HttpWebResponse Response {
  151. get { return response; }
  152. }
  153. internal byte [] Buffer {
  154. get { return buffer; }
  155. }
  156. internal int Offset {
  157. get { return offset; }
  158. }
  159. internal int Size {
  160. get { return size; }
  161. }
  162. }
  163. }