2 // System.Net.WebAsyncResult
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Threading;
36 class WebAsyncResult : IAsyncResult
38 ManualResetEvent handle;
44 IAsyncResult innerAsyncResult;
47 HttpWebResponse response;
52 object locker = new object ();
53 public bool EndCalled;
54 public bool AsyncWriteAll;
56 public WebAsyncResult (AsyncCallback cb, object state)
62 public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
68 public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
77 internal void SetCompleted (bool synch, Exception e)
88 internal void Reset ()
102 internal void SetCompleted (bool synch, int nbytes)
105 this.nbytes = nbytes;
114 internal void SetCompleted (bool synch, Stream writeStream)
117 this.writeStream = writeStream;
126 internal void SetCompleted (bool synch, HttpWebResponse response)
129 this.response = response;
138 internal void DoCallback ()
140 if (!callbackDone && cb != null) {
145 ThreadPool.QueueUserWorkItem (CB, null);
149 void CB (object unused)
154 internal void WaitUntilComplete ()
159 AsyncWaitHandle.WaitOne ();
162 internal bool WaitUntilComplete (int timeout, bool exitContext)
167 return AsyncWaitHandle.WaitOne (timeout, exitContext);
170 public object AsyncState {
171 get { return state; }
174 public WaitHandle AsyncWaitHandle {
178 handle = new ManualResetEvent (isCompleted);
185 public bool CompletedSynchronously {
186 get { return synch; }
189 public bool IsCompleted {
197 internal bool GotException {
198 get { return (exc != null); }
201 internal Exception Exception {
205 internal int NBytes {
206 get { return nbytes; }
207 set { nbytes = value; }
210 internal IAsyncResult InnerAsyncResult {
211 get { return innerAsyncResult; }
212 set { innerAsyncResult = value; }
215 internal Stream WriteStream {
216 get { return writeStream; }
219 internal HttpWebResponse Response {
220 get { return response; }
223 internal byte [] Buffer {
224 get { return buffer; }
227 internal int Offset {
228 get { return offset; }