2004-05-13 Gonzalo Paniagua Javier <gonzalo@ximian.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 using System.IO;
11 using System.Threading;
12
13 namespace System.Net
14 {
15         class WebAsyncResult : IAsyncResult
16         {
17                 ManualResetEvent handle;
18                 bool synch;
19                 bool isCompleted;
20                 AsyncCallback cb;
21                 object state;
22                 int nbytes;
23                 IAsyncResult innerAsyncResult;
24                 bool callbackDone;
25                 Exception exc;
26                 HttpWebRequest request;
27                 HttpWebResponse response;
28                 Stream writeStream;
29                 byte [] buffer;
30                 int offset;
31                 int size;
32                 object locker = new object ();
33
34                 public WebAsyncResult (AsyncCallback cb, object state)
35                 {
36                         this.cb = cb;
37                         this.state = state;
38                 }
39
40                 public WebAsyncResult (HttpWebRequest request, AsyncCallback cb, object state)
41                 {
42                         this.request = request;
43                         this.cb = cb;
44                         this.state = state;
45                 }
46
47                 public WebAsyncResult (AsyncCallback cb, object state, byte [] buffer, int offset, int size)
48                 {
49                         this.cb = cb;
50                         this.state = state;
51                         this.buffer = buffer;
52                         this.offset = offset;
53                         this.size = size;
54                 }
55
56                 internal void SetCompleted (bool synch, Exception e)
57                 {
58                         this.synch = synch;
59                         exc = e;
60                         lock (locker) {
61                                 isCompleted = true;
62                                 if (handle != null)
63                                         handle.Set ();
64                         }
65                 }
66                 
67                 internal void Reset ()
68                 {
69                         callbackDone = false;
70                         exc = null;
71                         request = null;
72                         response = null;
73                         writeStream = null;
74                         exc = null;
75                         lock (locker) {
76                                 isCompleted = false;
77                                 if (handle != null)
78                                         handle.Reset ();
79                         }
80                 }
81
82                 internal void SetCompleted (bool synch, int nbytes)
83                 {
84                         this.synch = synch;
85                         this.nbytes = nbytes;
86                         exc = null;
87                         lock (locker) {
88                                 isCompleted = true;
89                                 if (handle != null)
90                                         handle.Set ();
91                         }
92                 }
93                 
94                 internal void SetCompleted (bool synch, Stream writeStream)
95                 {
96                         this.synch = synch;
97                         this.writeStream = writeStream;
98                         exc = null;
99                         lock (locker) {
100                                 isCompleted = true;
101                                 if (handle != null)
102                                         handle.Set ();
103                         }
104                 }
105                 
106                 internal void SetCompleted (bool synch, HttpWebResponse response)
107                 {
108                         this.synch = synch;
109                         this.response = response;
110                         exc = null;
111                         lock (locker) {
112                                 isCompleted = true;
113                                 if (handle != null)
114                                         handle.Set ();
115                         }
116                 }
117                 
118                 internal void DoCallback ()
119                 {
120                         if (!callbackDone && cb != null) {
121                                 callbackDone = true;
122                                 cb (this);
123                         }
124                 }
125                 
126                 internal void WaitUntilComplete ()
127                 {
128                         if (IsCompleted)
129                                 return;
130
131                         AsyncWaitHandle.WaitOne ();
132                 }
133
134                 internal bool WaitUntilComplete (int timeout, bool exitContext)
135                 {
136                         if (IsCompleted)
137                                 return true;
138
139                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
140                 }
141
142                 public object AsyncState {
143                         get { return state; }
144                 }
145
146                 public WaitHandle AsyncWaitHandle {
147                         get {
148                                 lock (locker) {
149                                         if (handle == null)
150                                                 handle = new ManualResetEvent (isCompleted);
151                                 }
152                                 
153                                 return handle;
154                         }
155                 }
156
157                 public bool CompletedSynchronously {
158                         get { return synch; }
159                 }
160
161                 public bool IsCompleted {
162                         get {
163                                 lock (locker) {
164                                         return isCompleted;
165                                 }
166                         }
167                 }
168
169                 internal bool GotException {
170                         get { return (exc != null); }
171                 }
172                 
173                 internal Exception Exception {
174                         get { return exc; }
175                 }
176                 
177                 internal int NBytes {
178                         get { return nbytes; }
179                         set { nbytes = value; }
180                 }
181
182                 internal IAsyncResult InnerAsyncResult {
183                         get { return innerAsyncResult; }
184                         set { innerAsyncResult = value; }
185                 }
186
187                 internal Stream WriteStream {
188                         get { return writeStream; }
189                 }
190
191                 internal HttpWebResponse Response {
192                         get { return response; }
193                 }
194
195                 internal byte [] Buffer {
196                         get { return buffer; }
197                 }
198
199                 internal int Offset {
200                         get { return offset; }
201                 }
202
203                 internal int Size {
204                         get { return size; }
205                 }
206         }
207 }
208