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) {
142 ThreadPool.QueueUserWorkItem (CB, null);
146 void CB (object unused)
151 internal void WaitUntilComplete ()
156 AsyncWaitHandle.WaitOne ();
159 internal bool WaitUntilComplete (int timeout, bool exitContext)
164 return AsyncWaitHandle.WaitOne (timeout, exitContext);
167 public object AsyncState {
168 get { return state; }
171 public WaitHandle AsyncWaitHandle {
175 handle = new ManualResetEvent (isCompleted);
182 public bool CompletedSynchronously {
183 get { return synch; }
186 public bool IsCompleted {
194 internal bool GotException {
195 get { return (exc != null); }
198 internal Exception Exception {
202 internal int NBytes {
203 get { return nbytes; }
204 set { nbytes = value; }
207 internal IAsyncResult InnerAsyncResult {
208 get { return innerAsyncResult; }
209 set { innerAsyncResult = value; }
212 internal Stream WriteStream {
213 get { return writeStream; }
216 internal HttpWebResponse Response {
217 get { return response; }
220 internal byte [] Buffer {
221 get { return buffer; }
224 internal int Offset {
225 get { return offset; }