WebAsyncResult.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. IAsyncResult chunkAsyncResult;
  23. bool callbackDone;
  24. Exception exc;
  25. HttpWebRequest request;
  26. HttpWebResponse response;
  27. Stream writeStream;
  28. byte [] buffer;
  29. int offset;
  30. int size;
  31. public WebAsyncResult (AsyncCallback cb, object state)
  32. {
  33. this.cb = cb;
  34. this.state = state;
  35. }
  36. public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
  37. {
  38. this.request = request;
  39. this.cb = cb;
  40. this.state = state;
  41. }
  42. public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
  43. {
  44. this.cb = cb;
  45. this.state = state;
  46. this.buffer = buffer;
  47. this.offset = offset;
  48. this.size = size;
  49. }
  50. internal void SetCompleted (bool synch, Exception e)
  51. {
  52. isCompleted = true;
  53. this.synch = synch;
  54. exc = e;
  55. ((ManualResetEvent) AsyncWaitHandle).Set ();
  56. }
  57. internal void Reset ()
  58. {
  59. isCompleted = false;
  60. callbackDone = false;
  61. exc = null;
  62. request = null;
  63. response = null;
  64. writeStream = null;
  65. exc = null;
  66. if (handle != null)
  67. handle.Reset ();
  68. }
  69. internal void SetCompleted (bool synch, int nbytes)
  70. {
  71. isCompleted = true;
  72. this.synch = synch;
  73. this.nbytes = nbytes;
  74. exc = null;
  75. ((ManualResetEvent) AsyncWaitHandle).Set ();
  76. }
  77. internal void SetCompleted (bool synch, Stream writeStream)
  78. {
  79. isCompleted = true;
  80. this.synch = synch;
  81. this.writeStream = writeStream;
  82. exc = null;
  83. ((ManualResetEvent) AsyncWaitHandle).Set ();
  84. }
  85. internal void SetCompleted (bool synch, HttpWebResponse response)
  86. {
  87. isCompleted = true;
  88. this.synch = synch;
  89. this.response = response;
  90. exc = null;
  91. ((ManualResetEvent) AsyncWaitHandle).Set ();
  92. }
  93. internal void DoCallback ()
  94. {
  95. if (!callbackDone && cb != null) {
  96. callbackDone = true;
  97. cb (this);
  98. }
  99. }
  100. internal void WaitUntilComplete ()
  101. {
  102. if (isCompleted)
  103. return;
  104. AsyncWaitHandle.WaitOne ();
  105. }
  106. internal bool WaitUntilComplete (int timeout, bool exitContext)
  107. {
  108. if (isCompleted)
  109. return true;
  110. return AsyncWaitHandle.WaitOne (timeout, exitContext);
  111. }
  112. public object AsyncState {
  113. get { return state; }
  114. }
  115. public WaitHandle AsyncWaitHandle {
  116. get {
  117. if (handle == null) {
  118. lock (this) {
  119. if (handle == null)
  120. handle = new ManualResetEvent (isCompleted);
  121. }
  122. }
  123. return handle;
  124. }
  125. }
  126. public bool CompletedSynchronously {
  127. get { return synch; }
  128. }
  129. public bool IsCompleted {
  130. get { return isCompleted; }
  131. }
  132. internal bool GotException {
  133. get { return (exc != null); }
  134. }
  135. internal Exception Exception {
  136. get { return exc; }
  137. }
  138. internal int NBytes {
  139. get { return nbytes; }
  140. set { nbytes = value; }
  141. }
  142. internal IAsyncResult ChunkAsyncResult {
  143. get { return chunkAsyncResult; }
  144. set { chunkAsyncResult = value; }
  145. }
  146. internal IAsyncResult InnerAsyncResult {
  147. get { return innerAsyncResult; }
  148. set { innerAsyncResult = value; }
  149. }
  150. internal Stream WriteStream {
  151. get { return writeStream; }
  152. }
  153. internal HttpWebResponse Response {
  154. get { return response; }
  155. }
  156. internal byte [] Buffer {
  157. get { return buffer; }
  158. }
  159. internal int Offset {
  160. get { return offset; }
  161. }
  162. internal int Size {
  163. get { return size; }
  164. }
  165. }
  166. }