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