Commit 6a9937d2166023c370489d8e6654d01f6ec52f44 is not correct. WebClient.Proxy shoul...
[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                                 ThreadPool.QueueUserWorkItem (CB, null);
143                         }
144                 }
145
146                 void CB (object unused)
147                 {
148                         cb (this);
149                 }
150                 
151                 internal void WaitUntilComplete ()
152                 {
153                         if (IsCompleted)
154                                 return;
155
156                         AsyncWaitHandle.WaitOne ();
157                 }
158
159                 internal bool WaitUntilComplete (int timeout, bool exitContext)
160                 {
161                         if (IsCompleted)
162                                 return true;
163
164                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
165                 }
166
167                 public object AsyncState {
168                         get { return state; }
169                 }
170
171                 public WaitHandle AsyncWaitHandle {
172                         get {
173                                 lock (locker) {
174                                         if (handle == null)
175                                                 handle = new ManualResetEvent (isCompleted);
176                                 }
177                                 
178                                 return handle;
179                         }
180                 }
181
182                 public bool CompletedSynchronously {
183                         get { return synch; }
184                 }
185
186                 public bool IsCompleted {
187                         get {
188                                 lock (locker) {
189                                         return isCompleted;
190                                 }
191                         }
192                 }
193
194                 internal bool GotException {
195                         get { return (exc != null); }
196                 }
197                 
198                 internal Exception Exception {
199                         get { return exc; }
200                 }
201                 
202                 internal int NBytes {
203                         get { return nbytes; }
204                         set { nbytes = value; }
205                 }
206
207                 internal IAsyncResult InnerAsyncResult {
208                         get { return innerAsyncResult; }
209                         set { innerAsyncResult = value; }
210                 }
211
212                 internal Stream WriteStream {
213                         get { return writeStream; }
214                 }
215
216                 internal HttpWebResponse Response {
217                         get { return response; }
218                 }
219
220                 internal byte [] Buffer {
221                         get { return buffer; }
222                 }
223
224                 internal int Offset {
225                         get { return offset; }
226                 }
227
228                 internal int Size {
229                         get { return size; }
230                 }
231         }
232 }
233