7cc9d5e1d8bfca35b1ff1e4cc3357efcde210204
[mono.git] / mcs / class / System / System.Net / WebAsyncResult.cs
1 //
2 // System.Net.WebAsyncResult
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.IO;
32 using System.Threading;
33
34 namespace System.Net
35 {
36         class WebAsyncResult : IAsyncResult
37         {
38                 ManualResetEvent handle;
39                 bool synch;
40                 bool isCompleted;
41                 AsyncCallback cb;
42                 object state;
43                 int nbytes;
44                 IAsyncResult innerAsyncResult;
45                 bool callbackDone;
46                 Exception exc;
47                 HttpWebResponse response;
48                 Stream writeStream;
49                 byte [] buffer;
50                 int offset;
51                 int size;
52                 object locker = new object ();
53                 public bool EndCalled;
54                 public bool AsyncWriteAll;
55
56                 public WebAsyncResult (AsyncCallback cb, object state)
57                 {
58                         this.cb = cb;
59                         this.state = state;
60                 }
61
62                 public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
63                 {
64                         this.cb = cb;
65                         this.state = state;
66                 }
67
68                 public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
69                 {
70                         this.cb = cb;
71                         this.state = state;
72                         this.buffer = buffer;
73                         this.offset = offset;
74                         this.size = size;
75                 }
76
77                 internal void SetCompleted (bool synch, Exception e)
78                 {
79                         this.synch = synch;
80                         exc = e;
81                         lock (locker) {
82                                 isCompleted = true;
83                                 if (handle != null)
84                                         handle.Set ();
85                         }
86                 }
87                 
88                 internal void Reset ()
89                 {
90                         callbackDone = false;
91                         exc = null;
92                         response = null;
93                         writeStream = null;
94                         exc = null;
95                         lock (locker) {
96                                 isCompleted = false;
97                                 if (handle != null)
98                                         handle.Reset ();
99                         }
100                 }
101
102                 internal void SetCompleted (bool synch, int nbytes)
103                 {
104                         this.synch = synch;
105                         this.nbytes = nbytes;
106                         exc = null;
107                         lock (locker) {
108                                 isCompleted = true;
109                                 if (handle != null)
110                                         handle.Set ();
111                         }
112                 }
113                 
114                 internal void SetCompleted (bool synch, Stream writeStream)
115                 {
116                         this.synch = synch;
117                         this.writeStream = writeStream;
118                         exc = null;
119                         lock (locker) {
120                                 isCompleted = true;
121                                 if (handle != null)
122                                         handle.Set ();
123                         }
124                 }
125                 
126                 internal void SetCompleted (bool synch, HttpWebResponse response)
127                 {
128                         this.synch = synch;
129                         this.response = response;
130                         exc = null;
131                         lock (locker) {
132                                 isCompleted = true;
133                                 if (handle != null)
134                                         handle.Set ();
135                         }
136                 }
137                 
138                 internal void DoCallback ()
139                 {
140                         if (!callbackDone && cb != null) {
141                                 callbackDone = true;
142                                 if (synch)
143                                         cb (this);
144                                 else
145                                         ThreadPool.QueueUserWorkItem (CB, null);
146                         }
147                 }
148
149                 void CB (object unused)
150                 {
151                         cb (this);
152                 }
153                 
154                 internal void WaitUntilComplete ()
155                 {
156                         if (IsCompleted)
157                                 return;
158
159                         AsyncWaitHandle.WaitOne ();
160                 }
161
162                 internal bool WaitUntilComplete (int timeout, bool exitContext)
163                 {
164                         if (IsCompleted)
165                                 return true;
166
167                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
168                 }
169
170                 public object AsyncState {
171                         get { return state; }
172                 }
173
174                 public WaitHandle AsyncWaitHandle {
175                         get {
176                                 lock (locker) {
177                                         if (handle == null)
178                                                 handle = new ManualResetEvent (isCompleted);
179                                 }
180                                 
181                                 return handle;
182                         }
183                 }
184
185                 public bool CompletedSynchronously {
186                         get { return synch; }
187                 }
188
189                 public bool IsCompleted {
190                         get {
191                                 lock (locker) {
192                                         return isCompleted;
193                                 }
194                         }
195                 }
196
197                 internal bool GotException {
198                         get { return (exc != null); }
199                 }
200                 
201                 internal Exception Exception {
202                         get { return exc; }
203                 }
204                 
205                 internal int NBytes {
206                         get { return nbytes; }
207                         set { nbytes = value; }
208                 }
209
210                 internal IAsyncResult InnerAsyncResult {
211                         get { return innerAsyncResult; }
212                         set { innerAsyncResult = value; }
213                 }
214
215                 internal Stream WriteStream {
216                         get { return writeStream; }
217                 }
218
219                 internal HttpWebResponse Response {
220                         get { return response; }
221                 }
222
223                 internal byte [] Buffer {
224                         get { return buffer; }
225                 }
226
227                 internal int Offset {
228                         get { return offset; }
229                 }
230
231                 internal int Size {
232                         get { return size; }
233                 }
234         }
235 }
236