Reworked HttpWebRequest and related classes.
[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.Close ();
74                                 handle = null;
75                         }
76                 }
77
78                 internal void SetCompleted (bool synch, int nbytes)
79                 {
80                         isCompleted = true;
81                         this.synch = synch;
82                         this.nbytes = nbytes;
83                         exc = null;
84                         ((ManualResetEvent) AsyncWaitHandle).Set ();
85                 }
86                 
87                 internal void SetCompleted (bool synch, Stream writeStream)
88                 {
89                         isCompleted = true;
90                         this.synch = synch;
91                         this.writeStream = writeStream;
92                         exc = null;
93                         ((ManualResetEvent) AsyncWaitHandle).Set ();
94                 }
95                 
96                 internal void SetCompleted (bool synch, HttpWebResponse response)
97                 {
98                         isCompleted = true;
99                         this.synch = synch;
100                         this.response = response;
101                         exc = null;
102                         ((ManualResetEvent) AsyncWaitHandle).Set ();
103                 }
104                 
105                 internal void DoCallback ()
106                 {
107                         if (!callbackDone && cb != null) {
108                                 callbackDone = true;
109                                 cb (this);
110                         }
111                 }
112                 
113                 internal void WaitUntilComplete ()
114                 {
115                         if (isCompleted)
116                                 return;
117
118                         AsyncWaitHandle.WaitOne ();
119                 }
120
121                 internal bool WaitUntilComplete (int timeout, bool exitContext)
122                 {
123                         if (isCompleted)
124                                 return true;
125
126                         return AsyncWaitHandle.WaitOne (timeout, exitContext);
127                 }
128
129                 public object AsyncState {
130                         get { return state; }
131                 }
132
133                 public WaitHandle AsyncWaitHandle {
134                         get {
135                                 if (handle == null) {
136                                         lock (this) {
137                                                 if (handle == null)
138                                                         handle = new ManualResetEvent (isCompleted);
139                                         }
140                                 }
141                                 
142                                 return handle;
143                         }
144                 }
145
146                 public bool CompletedSynchronously {
147                         get { return synch; }
148                 }
149
150                 public bool IsCompleted {
151                         get { return isCompleted; }
152                 }
153
154                 internal bool GotException {
155                         get { return (exc != null); }
156                 }
157                 
158                 internal Exception Exception {
159                         get { return exc; }
160                 }
161                 
162                 internal int NBytes {
163                         get { return nbytes; }
164                         set { nbytes = value; }
165                 }
166
167                 internal IAsyncResult InnerAsyncResult {
168                         get { return innerAsyncResult; }
169                         set { innerAsyncResult = value; }
170                 }
171
172                 internal Stream WriteStream {
173                         get { return writeStream; }
174                 }
175
176                 internal HttpWebResponse Response {
177                         get { return response; }
178                 }
179
180                 internal byte [] Buffer {
181                         get { return buffer; }
182                 }
183
184                 internal int Offset {
185                         get { return offset; }
186                 }
187
188                 internal int Size {
189                         get { return size; }
190                 }
191         }
192 }
193