1 | /* |
---|
2 | XML-RPC.NET library |
---|
3 | Copyright (c) 2001-2006, Charles Cook <charlescook@cookcomputing.com> |
---|
4 | |
---|
5 | Permission is hereby granted, free of charge, to any person |
---|
6 | obtaining a copy of this software and associated documentation |
---|
7 | files (the "Software"), to deal in the Software without restriction, |
---|
8 | including without limitation the rights to use, copy, modify, merge, |
---|
9 | publish, distribute, sublicense, and/or sell copies of the Software, |
---|
10 | and to permit persons to whom the Software is furnished to do so, |
---|
11 | subject to the following conditions: |
---|
12 | |
---|
13 | The above copyright notice and this permission notice shall be |
---|
14 | included in all copies or substantial portions of the Software. |
---|
15 | |
---|
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
---|
18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
23 | DEALINGS IN THE SOFTWARE. |
---|
24 | */ |
---|
25 | |
---|
26 | namespace CookComputing.XmlRpc |
---|
27 | { |
---|
28 | using System; |
---|
29 | using System.IO; |
---|
30 | using System.Net; |
---|
31 | using System.Reflection; |
---|
32 | using System.Text; |
---|
33 | using System.Threading; |
---|
34 | |
---|
35 | public class XmlRpcAsyncResult : IAsyncResult |
---|
36 | { |
---|
37 | // IAsyncResult members |
---|
38 | public object AsyncState |
---|
39 | { |
---|
40 | get { return userAsyncState; } |
---|
41 | } |
---|
42 | |
---|
43 | public WaitHandle AsyncWaitHandle |
---|
44 | { |
---|
45 | get |
---|
46 | { |
---|
47 | bool completed = isCompleted; |
---|
48 | if (manualResetEvent == null) |
---|
49 | { |
---|
50 | lock(this) |
---|
51 | { |
---|
52 | if (manualResetEvent == null) |
---|
53 | manualResetEvent = new ManualResetEvent(completed); |
---|
54 | } |
---|
55 | } |
---|
56 | if (!completed && isCompleted) |
---|
57 | manualResetEvent.Set(); |
---|
58 | return manualResetEvent; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | public bool CompletedSynchronously |
---|
63 | { |
---|
64 | get { return completedSynchronously; } |
---|
65 | set |
---|
66 | { |
---|
67 | if (completedSynchronously) |
---|
68 | completedSynchronously = value; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | public bool IsCompleted |
---|
73 | { |
---|
74 | get { return isCompleted; } |
---|
75 | } |
---|
76 | |
---|
77 | #if (!COMPACT_FRAMEWORK) |
---|
78 | public CookieCollection ResponseCookies |
---|
79 | { |
---|
80 | get { return _responseCookies; } |
---|
81 | } |
---|
82 | #endif |
---|
83 | |
---|
84 | #if (!COMPACT_FRAMEWORK) |
---|
85 | public WebHeaderCollection ResponseHeaders |
---|
86 | { |
---|
87 | get { return _responseHeaders; } |
---|
88 | } |
---|
89 | #endif |
---|
90 | |
---|
91 | |
---|
92 | public bool UseEmptyParamsTag |
---|
93 | { |
---|
94 | get { return _useEmptyParamsTag; } |
---|
95 | } |
---|
96 | |
---|
97 | public bool UseIndentation |
---|
98 | { |
---|
99 | get { return _useIndentation; } |
---|
100 | } |
---|
101 | |
---|
102 | public int Indentation |
---|
103 | { |
---|
104 | get { return _indentation; } |
---|
105 | } |
---|
106 | |
---|
107 | public bool UseIntTag |
---|
108 | { |
---|
109 | get { return _useIntTag; } |
---|
110 | } |
---|
111 | |
---|
112 | public bool UseStringTag |
---|
113 | { |
---|
114 | get { return _useStringTag; } |
---|
115 | } |
---|
116 | |
---|
117 | // public members |
---|
118 | public void Abort() |
---|
119 | { |
---|
120 | if (request != null) |
---|
121 | request.Abort(); |
---|
122 | } |
---|
123 | |
---|
124 | public Exception Exception |
---|
125 | { |
---|
126 | get { return exception; } |
---|
127 | } |
---|
128 | |
---|
129 | public XmlRpcClientProtocol ClientProtocol |
---|
130 | { |
---|
131 | get { return clientProtocol; } |
---|
132 | } |
---|
133 | |
---|
134 | //internal members |
---|
135 | internal XmlRpcAsyncResult( |
---|
136 | XmlRpcClientProtocol ClientProtocol, |
---|
137 | XmlRpcRequest XmlRpcReq, |
---|
138 | Encoding XmlEncoding, |
---|
139 | bool useEmptyParamsTag, |
---|
140 | bool useIndentation, |
---|
141 | int indentation, |
---|
142 | bool UseIntTag, |
---|
143 | bool UseStringTag, |
---|
144 | WebRequest Request, |
---|
145 | AsyncCallback UserCallback, |
---|
146 | object UserAsyncState, |
---|
147 | int retryNumber) |
---|
148 | { |
---|
149 | xmlRpcRequest = XmlRpcReq; |
---|
150 | clientProtocol = ClientProtocol; |
---|
151 | request = Request; |
---|
152 | userAsyncState = UserAsyncState; |
---|
153 | userCallback = UserCallback; |
---|
154 | completedSynchronously = true; |
---|
155 | xmlEncoding = XmlEncoding; |
---|
156 | _useEmptyParamsTag = useEmptyParamsTag; |
---|
157 | _useIndentation = useIndentation; |
---|
158 | _indentation = indentation; |
---|
159 | _useIntTag = UseIntTag; |
---|
160 | _useStringTag = UseStringTag; |
---|
161 | } |
---|
162 | |
---|
163 | internal void Complete( |
---|
164 | Exception ex) |
---|
165 | { |
---|
166 | exception = ex; |
---|
167 | Complete(); |
---|
168 | } |
---|
169 | |
---|
170 | internal void Complete() |
---|
171 | { |
---|
172 | try |
---|
173 | { |
---|
174 | if (responseStream != null) |
---|
175 | { |
---|
176 | responseStream.Close(); |
---|
177 | responseStream = null; |
---|
178 | } |
---|
179 | if (responseBufferedStream != null) |
---|
180 | responseBufferedStream.Position = 0; |
---|
181 | } |
---|
182 | catch(Exception ex) |
---|
183 | { |
---|
184 | if (exception == null) |
---|
185 | exception = ex; |
---|
186 | } |
---|
187 | isCompleted = true; |
---|
188 | try |
---|
189 | { |
---|
190 | if (manualResetEvent != null) |
---|
191 | manualResetEvent.Set(); |
---|
192 | } |
---|
193 | catch(Exception ex) |
---|
194 | { |
---|
195 | if (exception == null) |
---|
196 | exception = ex; |
---|
197 | } |
---|
198 | if (userCallback != null) |
---|
199 | userCallback(this); |
---|
200 | } |
---|
201 | |
---|
202 | internal WebResponse WaitForResponse() |
---|
203 | { |
---|
204 | if (!isCompleted) |
---|
205 | AsyncWaitHandle.WaitOne(); |
---|
206 | if (exception != null) |
---|
207 | throw exception; |
---|
208 | return response; |
---|
209 | } |
---|
210 | |
---|
211 | internal bool EndSendCalled |
---|
212 | { |
---|
213 | get { return endSendCalled; } |
---|
214 | set { endSendCalled = value; } |
---|
215 | } |
---|
216 | |
---|
217 | internal byte[] Buffer |
---|
218 | { |
---|
219 | get { return buffer; } |
---|
220 | set { buffer = value; } |
---|
221 | } |
---|
222 | |
---|
223 | internal WebRequest Request { get { return request; } |
---|
224 | } |
---|
225 | |
---|
226 | internal WebResponse Response |
---|
227 | { |
---|
228 | get { return response; } |
---|
229 | set { response = value; } |
---|
230 | } |
---|
231 | |
---|
232 | internal Stream ResponseStream |
---|
233 | { |
---|
234 | get { return responseStream; } |
---|
235 | set { responseStream = value; } |
---|
236 | } |
---|
237 | |
---|
238 | internal XmlRpcRequest XmlRpcRequest |
---|
239 | { |
---|
240 | get { return xmlRpcRequest; } |
---|
241 | set { xmlRpcRequest = value; } |
---|
242 | } |
---|
243 | |
---|
244 | internal Stream ResponseBufferedStream |
---|
245 | { |
---|
246 | get { return responseBufferedStream; } |
---|
247 | set { responseBufferedStream = value; } |
---|
248 | } |
---|
249 | |
---|
250 | internal Encoding XmlEncoding |
---|
251 | { |
---|
252 | get { return xmlEncoding; } |
---|
253 | } |
---|
254 | |
---|
255 | XmlRpcClientProtocol clientProtocol; |
---|
256 | WebRequest request; |
---|
257 | AsyncCallback userCallback; |
---|
258 | object userAsyncState; |
---|
259 | bool completedSynchronously; |
---|
260 | bool isCompleted; |
---|
261 | bool endSendCalled = false; |
---|
262 | ManualResetEvent manualResetEvent; |
---|
263 | Exception exception; |
---|
264 | WebResponse response; |
---|
265 | Stream responseStream; |
---|
266 | Stream responseBufferedStream; |
---|
267 | byte[] buffer; |
---|
268 | XmlRpcRequest xmlRpcRequest; |
---|
269 | Encoding xmlEncoding; |
---|
270 | bool _useEmptyParamsTag; |
---|
271 | bool _useIndentation; |
---|
272 | int _indentation; |
---|
273 | bool _useIntTag; |
---|
274 | bool _useStringTag; |
---|
275 | #if (!COMPACT_FRAMEWORK) |
---|
276 | internal CookieCollection _responseCookies; |
---|
277 | internal WebHeaderCollection _responseHeaders; |
---|
278 | #endif |
---|
279 | } |
---|
280 | } |
---|