WebAsyncResult.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.IO;
  30. using System.Threading;
  31. namespace System.Net
  32. {
  33. class WebAsyncResult : IAsyncResult
  34. {
  35. ManualResetEvent handle;
  36. bool synch;
  37. bool isCompleted;
  38. AsyncCallback cb;
  39. object state;
  40. int nbytes;
  41. IAsyncResult innerAsyncResult;
  42. bool callbackDone;
  43. Exception exc;
  44. HttpWebRequest request;
  45. HttpWebResponse response;
  46. Stream writeStream;
  47. byte [] buffer;
  48. int offset;
  49. int size;
  50. object locker = new object ();
  51. public bool EndCalled;
  52. public WebAsyncResult (AsyncCallback cb, object state)
  53. {
  54. this.cb = cb;
  55. this.state = state;
  56. }
  57. public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
  58. {
  59. this.request = request;
  60. this.cb = cb;
  61. this.state = state;
  62. }
  63. public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
  64. {
  65. this.cb = cb;
  66. this.state = state;
  67. this.buffer = buffer;
  68. this.offset = offset;
  69. this.size = size;
  70. }
  71. internal void SetCompleted (bool synch, Exception e)
  72. {
  73. this.synch = synch;
  74. exc = e;
  75. lock (locker) {
  76. isCompleted = true;
  77. if (handle != null)
  78. handle.Set ();
  79. }
  80. }
  81. internal void Reset ()
  82. {
  83. callbackDone = false;
  84. exc = null;
  85. request = null;
  86. response = null;
  87. writeStream = null;
  88. exc = null;
  89. lock (locker) {
  90. isCompleted = false;
  91. if (handle != null)
  92. handle.Reset ();
  93. }
  94. }
  95. internal void SetCompleted (bool synch, int nbytes)
  96. {
  97. this.synch = synch;
  98. this.nbytes = nbytes;
  99. exc = null;
  100. lock (locker) {
  101. isCompleted = true;
  102. if (handle != null)
  103. handle.Set ();
  104. }
  105. }
  106. internal void SetCompleted (bool synch, Stream writeStream)
  107. {
  108. this.synch = synch;
  109. this.writeStream = writeStream;
  110. exc = null;
  111. lock (locker) {
  112. isCompleted = true;
  113. if (handle != null)
  114. handle.Set ();
  115. }
  116. }
  117. internal void SetCompleted (bool synch, HttpWebResponse response)
  118. {
  119. this.synch = synch;
  120. this.response = response;
  121. exc = null;
  122. lock (locker) {
  123. isCompleted = true;
  124. if (handle != null)
  125. handle.Set ();
  126. }
  127. }
  128. internal void DoCallback ()
  129. {
  130. if (!callbackDone && cb != null) {
  131. callbackDone = true;
  132. cb (this);
  133. }
  134. }
  135. internal void WaitUntilComplete ()
  136. {
  137. if (IsCompleted)
  138. return;
  139. AsyncWaitHandle.WaitOne ();
  140. }
  141. internal bool WaitUntilComplete (int timeout, bool exitContext)
  142. {
  143. if (IsCompleted)
  144. return true;
  145. return AsyncWaitHandle.WaitOne (timeout, exitContext);
  146. }
  147. public object AsyncState {
  148. get { return state; }
  149. }
  150. public WaitHandle AsyncWaitHandle {
  151. get {
  152. lock (locker) {
  153. if (handle == null)
  154. handle = new ManualResetEvent (isCompleted);
  155. }
  156. return handle;
  157. }
  158. }
  159. public bool CompletedSynchronously {
  160. get { return synch; }
  161. }
  162. public bool IsCompleted {
  163. get {
  164. lock (locker) {
  165. return isCompleted;
  166. }
  167. }
  168. }
  169. internal bool GotException {
  170. get { return (exc != null); }
  171. }
  172. internal Exception Exception {
  173. get { return exc; }
  174. }
  175. internal int NBytes {
  176. get { return nbytes; }
  177. set { nbytes = value; }
  178. }
  179. internal IAsyncResult InnerAsyncResult {
  180. get { return innerAsyncResult; }
  181. set { innerAsyncResult = value; }
  182. }
  183. internal Stream WriteStream {
  184. get { return writeStream; }
  185. }
  186. internal HttpWebResponse Response {
  187. get { return response; }
  188. }
  189. internal byte [] Buffer {
  190. get { return buffer; }
  191. }
  192. internal int Offset {
  193. get { return offset; }
  194. }
  195. internal int Size {
  196. get { return size; }
  197. }
  198. }
  199. }