diff options
author | lbsa71 | 2007-10-30 09:05:31 +0000 |
---|---|---|
committer | lbsa71 | 2007-10-30 09:05:31 +0000 |
commit | 67e12b95ea7b68f4904a7484d77ecfd787d16d0c (patch) | |
tree | 20b00d24c8a7617017960432ec044852e3ad5fa9 /OpenSim/Framework/Communications/RestClient/RestClient.cs | |
parent | * Deleted .user file (diff) | |
download | opensim-SC_OLD-67e12b95ea7b68f4904a7484d77ecfd787d16d0c.zip opensim-SC_OLD-67e12b95ea7b68f4904a7484d77ecfd787d16d0c.tar.gz opensim-SC_OLD-67e12b95ea7b68f4904a7484d77ecfd787d16d0c.tar.bz2 opensim-SC_OLD-67e12b95ea7b68f4904a7484d77ecfd787d16d0c.tar.xz |
* Optimized usings
* Shortened type references
* Removed redundant 'this' qualifier
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Communications/RestClient/RestClient.cs | 64 |
1 files changed, 36 insertions, 28 deletions
diff --git a/OpenSim/Framework/Communications/RestClient/RestClient.cs b/OpenSim/Framework/Communications/RestClient/RestClient.cs index 392669f..ac3a287 100644 --- a/OpenSim/Framework/Communications/RestClient/RestClient.cs +++ b/OpenSim/Framework/Communications/RestClient/RestClient.cs | |||
@@ -1,12 +1,10 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.Collections.Generic; | ||
2 | using System.IO; | 3 | using System.IO; |
3 | using System.Net; | 4 | using System.Net; |
4 | using System.Web; | ||
5 | using System.Text; | 5 | using System.Text; |
6 | using System.Collections.Generic; | ||
7 | using System.Threading; | 6 | using System.Threading; |
8 | 7 | using System.Web; | |
9 | using OpenSim.Framework.Console; | ||
10 | 8 | ||
11 | namespace OpenSim.Framework.Communications | 9 | namespace OpenSim.Framework.Communications |
12 | { | 10 | { |
@@ -29,9 +27,10 @@ namespace OpenSim.Framework.Communications | |||
29 | /// </remarks> | 27 | /// </remarks> |
30 | public class RestClient | 28 | public class RestClient |
31 | { | 29 | { |
30 | private string realuri; | ||
32 | 31 | ||
33 | string realuri; | ||
34 | #region member variables | 32 | #region member variables |
33 | |||
35 | /// <summary> | 34 | /// <summary> |
36 | /// The base Uri of the web-service e.g. http://www.google.com | 35 | /// The base Uri of the web-service e.g. http://www.google.com |
37 | /// </summary> | 36 | /// </summary> |
@@ -60,7 +59,7 @@ namespace OpenSim.Framework.Communications | |||
60 | /// <summary> | 59 | /// <summary> |
61 | /// MemoryStream representing the resultiong resource | 60 | /// MemoryStream representing the resultiong resource |
62 | /// </summary> | 61 | /// </summary> |
63 | Stream _resource; | 62 | private Stream _resource; |
64 | 63 | ||
65 | /// <summary> | 64 | /// <summary> |
66 | /// WebRequest object, held as a member variable | 65 | /// WebRequest object, held as a member variable |
@@ -80,12 +79,12 @@ namespace OpenSim.Framework.Communications | |||
80 | /// <summary> | 79 | /// <summary> |
81 | /// Default time out period | 80 | /// Default time out period |
82 | /// </summary> | 81 | /// </summary> |
83 | const int DefaultTimeout = 10 * 1000; // 10 seconds timeout | 82 | private const int DefaultTimeout = 10*1000; // 10 seconds timeout |
84 | 83 | ||
85 | /// <summary> | 84 | /// <summary> |
86 | /// Default Buffer size of a block requested from the web-server | 85 | /// Default Buffer size of a block requested from the web-server |
87 | /// </summary> | 86 | /// </summary> |
88 | const int BufferSize = 4096; // Read blocks of 4 KB. | 87 | private const int BufferSize = 4096; // Read blocks of 4 KB. |
89 | 88 | ||
90 | 89 | ||
91 | /// <summary> | 90 | /// <summary> |
@@ -97,6 +96,7 @@ namespace OpenSim.Framework.Communications | |||
97 | #endregion member variables | 96 | #endregion member variables |
98 | 97 | ||
99 | #region constructors | 98 | #region constructors |
99 | |||
100 | /// <summary> | 100 | /// <summary> |
101 | /// Instantiate a new RestClient | 101 | /// Instantiate a new RestClient |
102 | /// </summary> | 102 | /// </summary> |
@@ -111,7 +111,8 @@ namespace OpenSim.Framework.Communications | |||
111 | _lock = new object(); | 111 | _lock = new object(); |
112 | } | 112 | } |
113 | 113 | ||
114 | object _lock; | 114 | private object _lock; |
115 | |||
115 | #endregion constructors | 116 | #endregion constructors |
116 | 117 | ||
117 | /// <summary> | 118 | /// <summary> |
@@ -120,8 +121,8 @@ namespace OpenSim.Framework.Communications | |||
120 | /// <param name="element">path entry</param> | 121 | /// <param name="element">path entry</param> |
121 | public void AddResourcePath(string element) | 122 | public void AddResourcePath(string element) |
122 | { | 123 | { |
123 | if(isSlashed(element)) | 124 | if (isSlashed(element)) |
124 | _pathElements.Add(element.Substring(0, element.Length-1)); | 125 | _pathElements.Add(element.Substring(0, element.Length - 1)); |
125 | else | 126 | else |
126 | _pathElements.Add(element); | 127 | _pathElements.Add(element); |
127 | } | 128 | } |
@@ -178,7 +179,7 @@ namespace OpenSim.Framework.Communications | |||
178 | /// Build a Uri based on the intial Url, path elements and parameters | 179 | /// Build a Uri based on the intial Url, path elements and parameters |
179 | /// </summary> | 180 | /// </summary> |
180 | /// <returns>fully constructed Uri</returns> | 181 | /// <returns>fully constructed Uri</returns> |
181 | Uri buildUri() | 182 | private Uri buildUri() |
182 | { | 183 | { |
183 | StringBuilder sb = new StringBuilder(); | 184 | StringBuilder sb = new StringBuilder(); |
184 | sb.Append(_url); | 185 | sb.Append(_url); |
@@ -196,7 +197,8 @@ namespace OpenSim.Framework.Communications | |||
196 | { | 197 | { |
197 | sb.Append("?"); | 198 | sb.Append("?"); |
198 | firstElement = false; | 199 | firstElement = false; |
199 | } else | 200 | } |
201 | else | ||
200 | sb.Append("&"); | 202 | sb.Append("&"); |
201 | 203 | ||
202 | sb.Append(kv.Key); | 204 | sb.Append(kv.Key); |
@@ -209,7 +211,9 @@ namespace OpenSim.Framework.Communications | |||
209 | realuri = sb.ToString(); | 211 | realuri = sb.ToString(); |
210 | return new Uri(sb.ToString()); | 212 | return new Uri(sb.ToString()); |
211 | } | 213 | } |
214 | |||
212 | #region Async communications with server | 215 | #region Async communications with server |
216 | |||
213 | /// <summary> | 217 | /// <summary> |
214 | /// Async method, invoked when a block of data has been received from the service | 218 | /// Async method, invoked when a block of data has been received from the service |
215 | /// </summary> | 219 | /// </summary> |
@@ -218,13 +222,14 @@ namespace OpenSim.Framework.Communications | |||
218 | { | 222 | { |
219 | try | 223 | try |
220 | { | 224 | { |
221 | Stream s = (Stream)ar.AsyncState; | 225 | Stream s = (Stream) ar.AsyncState; |
222 | int read = s.EndRead(ar); | 226 | int read = s.EndRead(ar); |
223 | 227 | ||
224 | if (read > 0) | 228 | if (read > 0) |
225 | { | 229 | { |
226 | _resource.Write(_readbuf, 0, read); | 230 | _resource.Write(_readbuf, 0, read); |
227 | IAsyncResult asynchronousResult = s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); | 231 | IAsyncResult asynchronousResult = |
232 | s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); | ||
228 | 233 | ||
229 | // TODO! Implement timeout, without killing the server | 234 | // TODO! Implement timeout, without killing the server |
230 | //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); | 235 | //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); |
@@ -251,12 +256,13 @@ namespace OpenSim.Framework.Communications | |||
251 | try | 256 | try |
252 | { | 257 | { |
253 | // grab response | 258 | // grab response |
254 | WebRequest wr = (WebRequest)ar.AsyncState; | 259 | WebRequest wr = (WebRequest) ar.AsyncState; |
255 | _response = (HttpWebResponse)wr.EndGetResponse(ar); | 260 | _response = (HttpWebResponse) wr.EndGetResponse(ar); |
256 | 261 | ||
257 | // get response stream, and setup async reading | 262 | // get response stream, and setup async reading |
258 | Stream s = _response.GetResponseStream(); | 263 | Stream s = _response.GetResponseStream(); |
259 | IAsyncResult asynchronousResult = s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); | 264 | IAsyncResult asynchronousResult = |
265 | s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); | ||
260 | 266 | ||
261 | // TODO! Implement timeout, without killing the server | 267 | // TODO! Implement timeout, without killing the server |
262 | // wait until completed, or we timed out | 268 | // wait until completed, or we timed out |
@@ -281,6 +287,7 @@ namespace OpenSim.Framework.Communications | |||
281 | } | 287 | } |
282 | } | 288 | } |
283 | } | 289 | } |
290 | |||
284 | #endregion Async communications with server | 291 | #endregion Async communications with server |
285 | 292 | ||
286 | /// <summary> | 293 | /// <summary> |
@@ -290,17 +297,17 @@ namespace OpenSim.Framework.Communications | |||
290 | { | 297 | { |
291 | lock (_lock) | 298 | lock (_lock) |
292 | { | 299 | { |
293 | _request = (HttpWebRequest)WebRequest.Create(buildUri()); | 300 | _request = (HttpWebRequest) WebRequest.Create(buildUri()); |
294 | _request.KeepAlive = false; | 301 | _request.KeepAlive = false; |
295 | _request.ContentType = "application/xml"; | 302 | _request.ContentType = "application/xml"; |
296 | _request.Timeout = 200000; | 303 | _request.Timeout = 200000; |
297 | _asyncException = null; | 304 | _asyncException = null; |
298 | 305 | ||
299 | // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); | 306 | // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); |
300 | _response = (HttpWebResponse)_request.GetResponse(); | 307 | _response = (HttpWebResponse) _request.GetResponse(); |
301 | Stream src = _response.GetResponseStream(); | 308 | Stream src = _response.GetResponseStream(); |
302 | int length = src.Read(_readbuf, 0, BufferSize); | 309 | int length = src.Read(_readbuf, 0, BufferSize); |
303 | while(length > 0) | 310 | while (length > 0) |
304 | { | 311 | { |
305 | _resource.Write(_readbuf, 0, length); | 312 | _resource.Write(_readbuf, 0, length); |
306 | length = src.Read(_readbuf, 0, BufferSize); | 313 | length = src.Read(_readbuf, 0, BufferSize); |
@@ -329,7 +336,7 @@ namespace OpenSim.Framework.Communications | |||
329 | 336 | ||
330 | public Stream Request(Stream src) | 337 | public Stream Request(Stream src) |
331 | { | 338 | { |
332 | _request = (HttpWebRequest)WebRequest.Create(buildUri()); | 339 | _request = (HttpWebRequest) WebRequest.Create(buildUri()); |
333 | _request.KeepAlive = false; | 340 | _request.KeepAlive = false; |
334 | _request.ContentType = "application/xml"; | 341 | _request.ContentType = "application/xml"; |
335 | _request.Timeout = 900000; | 342 | _request.Timeout = 900000; |
@@ -340,13 +347,13 @@ namespace OpenSim.Framework.Communications | |||
340 | src.Seek(0, SeekOrigin.Begin); | 347 | src.Seek(0, SeekOrigin.Begin); |
341 | Stream dst = _request.GetRequestStream(); | 348 | Stream dst = _request.GetRequestStream(); |
342 | byte[] buf = new byte[1024]; | 349 | byte[] buf = new byte[1024]; |
343 | int length = src.Read(buf,0, 1024); | 350 | int length = src.Read(buf, 0, 1024); |
344 | while (length > 0) | 351 | while (length > 0) |
345 | { | 352 | { |
346 | dst.Write(buf, 0, length); | 353 | dst.Write(buf, 0, length); |
347 | length = src.Read(buf, 0, 1024); | 354 | length = src.Read(buf, 0, 1024); |
348 | } | 355 | } |
349 | _response = (HttpWebResponse)_request.GetResponse(); | 356 | _response = (HttpWebResponse) _request.GetResponse(); |
350 | 357 | ||
351 | // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); | 358 | // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); |
352 | 359 | ||
@@ -357,8 +364,8 @@ namespace OpenSim.Framework.Communications | |||
357 | return null; | 364 | return null; |
358 | } | 365 | } |
359 | 366 | ||
360 | |||
361 | #region Async Invocation | 367 | #region Async Invocation |
368 | |||
362 | public IAsyncResult BeginRequest(AsyncCallback callback, object state) | 369 | public IAsyncResult BeginRequest(AsyncCallback callback, object state) |
363 | { | 370 | { |
364 | /// <summary> | 371 | /// <summary> |
@@ -371,7 +378,7 @@ namespace OpenSim.Framework.Communications | |||
371 | 378 | ||
372 | public Stream EndRequest(IAsyncResult asyncResult) | 379 | public Stream EndRequest(IAsyncResult asyncResult) |
373 | { | 380 | { |
374 | AsyncResult<Stream> ar = (AsyncResult<Stream>)asyncResult; | 381 | AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult; |
375 | 382 | ||
376 | // Wait for operation to complete, then return result or | 383 | // Wait for operation to complete, then return result or |
377 | // throw exception | 384 | // throw exception |
@@ -381,7 +388,7 @@ namespace OpenSim.Framework.Communications | |||
381 | private void RequestHelper(Object asyncResult) | 388 | private void RequestHelper(Object asyncResult) |
382 | { | 389 | { |
383 | // We know that it's really an AsyncResult<DateTime> object | 390 | // We know that it's really an AsyncResult<DateTime> object |
384 | AsyncResult<Stream> ar = (AsyncResult<Stream>)asyncResult; | 391 | AsyncResult<Stream> ar = (AsyncResult<Stream>) asyncResult; |
385 | try | 392 | try |
386 | { | 393 | { |
387 | // Perform the operation; if sucessful set the result | 394 | // Perform the operation; if sucessful set the result |
@@ -394,6 +401,7 @@ namespace OpenSim.Framework.Communications | |||
394 | ar.HandleException(e, false); | 401 | ar.HandleException(e, false); |
395 | } | 402 | } |
396 | } | 403 | } |
404 | |||
397 | #endregion Async Invocation | 405 | #endregion Async Invocation |
398 | } | 406 | } |
399 | } | 407 | } \ No newline at end of file |