WebAsyncResult.cs 4.6 KB

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