WebAsyncResult.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 WebAsyncResult (AsyncCallback cb, object state)
  52. {
  53. this.cb = cb;
  54. this.state = state;
  55. }
  56. public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
  57. {
  58. this.request = request;
  59. this.cb = cb;
  60. this.state = state;
  61. }
  62. public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
  63. {
  64. this.cb = cb;
  65. this.state = state;
  66. this.buffer = buffer;
  67. this.offset = offset;
  68. this.size = size;
  69. }
  70. internal void SetCompleted (bool synch, Exception e)
  71. {
  72. this.synch = synch;
  73. exc = e;
  74. lock (locker) {
  75. isCompleted = true;
  76. if (handle != null)
  77. handle.Set ();
  78. }
  79. }
  80. internal void Reset ()
  81. {
  82. callbackDone = false;
  83. exc = null;
  84. request = null;
  85. response = null;
  86. writeStream = null;
  87. exc = null;
  88. lock (locker) {
  89. isCompleted = false;
  90. if (handle != null)
  91. handle.Reset ();
  92. }
  93. }
  94. internal void SetCompleted (bool synch, int nbytes)
  95. {
  96. this.synch = synch;
  97. this.nbytes = nbytes;
  98. exc = null;
  99. lock (locker) {
  100. isCompleted = true;
  101. if (handle != null)
  102. handle.Set ();
  103. }
  104. }
  105. internal void SetCompleted (bool synch, Stream writeStream)
  106. {
  107. this.synch = synch;
  108. this.writeStream = writeStream;
  109. exc = null;
  110. lock (locker) {
  111. isCompleted = true;
  112. if (handle != null)
  113. handle.Set ();
  114. }
  115. }
  116. internal void SetCompleted (bool synch, HttpWebResponse response)
  117. {
  118. this.synch = synch;
  119. this.response = response;
  120. exc = null;
  121. lock (locker) {
  122. isCompleted = true;
  123. if (handle != null)
  124. handle.Set ();
  125. }
  126. }
  127. internal void DoCallback ()
  128. {
  129. if (!callbackDone && cb != null) {
  130. callbackDone = true;
  131. cb (this);
  132. }
  133. }
  134. internal void WaitUntilComplete ()
  135. {
  136. if (IsCompleted)
  137. return;
  138. AsyncWaitHandle.WaitOne ();
  139. }
  140. internal bool WaitUntilComplete (int timeout, bool exitContext)
  141. {
  142. if (IsCompleted)
  143. return true;
  144. return AsyncWaitHandle.WaitOne (timeout, exitContext);
  145. }
  146. public object AsyncState {
  147. get { return state; }
  148. }
  149. public WaitHandle AsyncWaitHandle {
  150. get {
  151. lock (locker) {
  152. if (handle == null)
  153. handle = new ManualResetEvent (isCompleted);
  154. }
  155. return handle;
  156. }
  157. }
  158. public bool CompletedSynchronously {
  159. get { return synch; }
  160. }
  161. public bool IsCompleted {
  162. get {
  163. lock (locker) {
  164. return isCompleted;
  165. }
  166. }
  167. }
  168. internal bool GotException {
  169. get { return (exc != null); }
  170. }
  171. internal Exception Exception {
  172. get { return exc; }
  173. }
  174. internal int NBytes {
  175. get { return nbytes; }
  176. set { nbytes = value; }
  177. }
  178. internal IAsyncResult InnerAsyncResult {
  179. get { return innerAsyncResult; }
  180. set { innerAsyncResult = value; }
  181. }
  182. internal Stream WriteStream {
  183. get { return writeStream; }
  184. }
  185. internal HttpWebResponse Response {
  186. get { return response; }
  187. }
  188. internal byte [] Buffer {
  189. get { return buffer; }
  190. }
  191. internal int Offset {
  192. get { return offset; }
  193. }
  194. internal int Size {
  195. get { return size; }
  196. }
  197. }
  198. }