WebAsyncResult.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.Reset ();
  67. }
  68. internal void SetCompleted (bool synch, int nbytes)
  69. {
  70. isCompleted = true;
  71. this.synch = synch;
  72. this.nbytes = nbytes;
  73. exc = null;
  74. ((ManualResetEvent) AsyncWaitHandle).Set ();
  75. }
  76. internal void SetCompleted (bool synch, Stream writeStream)
  77. {
  78. isCompleted = true;
  79. this.synch = synch;
  80. this.writeStream = writeStream;
  81. exc = null;
  82. ((ManualResetEvent) AsyncWaitHandle).Set ();
  83. }
  84. internal void SetCompleted (bool synch, HttpWebResponse response)
  85. {
  86. isCompleted = true;
  87. this.synch = synch;
  88. this.response = response;
  89. exc = null;
  90. ((ManualResetEvent) AsyncWaitHandle).Set ();
  91. }
  92. internal void DoCallback ()
  93. {
  94. if (!callbackDone && cb != null) {
  95. callbackDone = true;
  96. cb (this);
  97. }
  98. }
  99. internal void WaitUntilComplete ()
  100. {
  101. if (isCompleted)
  102. return;
  103. AsyncWaitHandle.WaitOne ();
  104. }
  105. internal bool WaitUntilComplete (int timeout, bool exitContext)
  106. {
  107. if (isCompleted)
  108. return true;
  109. return AsyncWaitHandle.WaitOne (timeout, exitContext);
  110. }
  111. public object AsyncState {
  112. get { return state; }
  113. }
  114. public WaitHandle AsyncWaitHandle {
  115. get {
  116. if (handle == null) {
  117. lock (this) {
  118. if (handle == null)
  119. handle = new ManualResetEvent (isCompleted);
  120. }
  121. }
  122. return handle;
  123. }
  124. }
  125. public bool CompletedSynchronously {
  126. get { return synch; }
  127. }
  128. public bool IsCompleted {
  129. get { return isCompleted; }
  130. }
  131. internal bool GotException {
  132. get { return (exc != null); }
  133. }
  134. internal Exception Exception {
  135. get { return exc; }
  136. }
  137. internal int NBytes {
  138. get { return nbytes; }
  139. set { nbytes = value; }
  140. }
  141. internal IAsyncResult InnerAsyncResult {
  142. get { return innerAsyncResult; }
  143. set { innerAsyncResult = value; }
  144. }
  145. internal Stream WriteStream {
  146. get { return writeStream; }
  147. }
  148. internal HttpWebResponse Response {
  149. get { return response; }
  150. }
  151. internal byte [] Buffer {
  152. get { return buffer; }
  153. }
  154. internal int Offset {
  155. get { return offset; }
  156. }
  157. internal int Size {
  158. get { return size; }
  159. }
  160. }
  161. }