2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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
55                 public WebAsyncResult (AsyncCallback cb, object state)
56                 {
57                         this.cb = cb;
58                         this.state = state;
59                 }
60
61                 public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
62                 {
63                         this.cb = cb;
64                         this.state = state;
65                 }
66
67                 public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
68                 {
69                         this.cb = cb;
70                         this.state = state;
71                         this.buffer = buffer;
72                         this.offset = offset;
73                         this.size = size;
74                 }
75
76                 internal void SetCompleted (bool synch, Exception e)
77                 {
78                         this.synch = synch;
79                         exc = e;
80                         lock (locker) {
81                                 isCompleted = true;
82                                 if (handle != null)
83                                         handle.Set ();
84                         }
85                 }
86                 
87                 internal void Reset ()
88                 {
89                         callbackDone = false;
90                         exc = null;
91                         response = null;
92                         writeStream = null;
93                         exc = null;
94                         lock (locker) {
95                                 isCompleted = false;
96                                 if (handle != null)
97                                         handle.Reset ();
98                         }
99                 }
100
101                 internal void SetCompleted (bool synch, int nbytes)
102                 {
103                         this.synch = synch;
104                         this.nbytes = nbytes;
105                         exc = null;
106                         lock (locker) {
107                                 isCompleted = true;
108                                 if (handle != null)
109                                         handle.Set ();
110                         }
111                 }
112                 
113                 internal void SetCompleted (bool synch, Stream writeStream)
114                 {
115                         this.synch = synch;
116                         this.writeStream = writeStream;
117                         exc = null;
118                         lock (locker) {
119                                 isCompleted = true;
120                                 if (handle != null)
121                                         handle.Set ();
122                         }
123                 }
124                 
125                 internal void SetCompleted (bool synch, HttpWebResponse response)
126                 {
127                         this.synch = synch;
128                         this.response = response;
129                         exc = null;
130                         lock (locker) {
131                                 isCompleted = true;
132                                 if (handle != null)
133                                         handle.Set ();
134                         }
135                 }
136                 
137                 internal void DoCallback ()
138                 {
139                         if (!callbackDone && cb != null) {
140                                 callbackDone = true;
141                                 cb (this);
142                         }
143                 }
144                 
145                 internal void WaitUntilComplete ()
146                 {
147                         if (IsCompleted)
148                                 return;
149
150                         AsyncWaitHandle.WaitOne ();
151                 }
152
153                 internal bool WaitUntilComplete (int timeout, bool exitContext)
154                 {
155                         if (IsCompleted)
156                                 return true;
157
158                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
159                 }
160
161                 public object AsyncState {
162                         get { return state; }
163                 }
164
165                 public WaitHandle AsyncWaitHandle {
166                         get {
167                                 lock (locker) {
168                                         if (handle == null)
169                                                 handle = new ManualResetEvent (isCompleted);
170                                 }
171                                 
172                                 return handle;
173                         }
174                 }
175
176                 public bool CompletedSynchronously {
177                         get { return synch; }
178                 }
179
180                 public bool IsCompleted {
181                         get {
182                                 lock (locker) {
183                                         return isCompleted;
184                                 }
185                         }
186                 }
187
188                 internal bool GotException {
189                         get { return (exc != null); }
190                 }
191                 
192                 internal Exception Exception {
193                         get { return exc; }
194                 }
195                 
196                 internal int NBytes {
197                         get { return nbytes; }
198                         set { nbytes = value; }
199                 }
200
201                 internal IAsyncResult InnerAsyncResult {
202                         get { return innerAsyncResult; }
203                         set { innerAsyncResult = value; }
204                 }
205
206                 internal Stream WriteStream {
207                         get { return writeStream; }
208                 }
209
210                 internal HttpWebResponse Response {
211                         get { return response; }
212                 }
213
214                 internal byte [] Buffer {
215                         get { return buffer; }
216                 }
217
218                 internal int Offset {
219                         get { return offset; }
220                 }
221
222                 internal int Size {
223                         get { return size; }
224                 }
225         }
226 }
227