From 67e12b95ea7b68f4904a7484d77ecfd787d16d0c Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Tue, 30 Oct 2007 09:05:31 +0000 Subject: * Optimized usings * Shortened type references * Removed redundant 'this' qualifier --- .../RestClient/GenericAsyncResult.cs | 37 ++++++------- .../Communications/RestClient/RestClient.cs | 64 ++++++++++++---------- 2 files changed, 52 insertions(+), 49 deletions(-) (limited to 'OpenSim/Framework/Communications/RestClient') diff --git a/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs b/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs index c821fa4..72d8f65 100644 --- a/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs +++ b/OpenSim/Framework/Communications/RestClient/GenericAsyncResult.cs @@ -1,13 +1,10 @@ using System; -using System.Collections.Generic; -using System.Text; using System.Threading; namespace OpenSim.Framework.Communications { internal class SimpleAsyncResult : IAsyncResult { - private readonly AsyncCallback m_callback; /// @@ -36,7 +33,6 @@ namespace OpenSim.Framework.Communications m_completedSynchronously = 1; } - #region IAsyncResult Members public object AsyncState @@ -45,7 +41,6 @@ namespace OpenSim.Framework.Communications } - public WaitHandle AsyncWaitHandle { get @@ -82,19 +77,18 @@ namespace OpenSim.Framework.Communications get { return Thread.VolatileRead(ref m_completed) == 1; } } - #endregion - #region class Methods + internal void SetAsCompleted(bool completedSynchronously) { m_completed = 1; - if(completedSynchronously) + if (completedSynchronously) m_completedSynchronously = 1; else m_completedSynchronously = 0; - + SignalCompletion(); } @@ -112,9 +106,9 @@ namespace OpenSim.Framework.Communications private void SignalCompletion() { - if(m_waitHandle != null) m_waitHandle.Set(); + if (m_waitHandle != null) m_waitHandle.Set(); - if(m_callback != null) m_callback(this); + if (m_callback != null) m_callback(this); } public void EndInvoke() @@ -125,14 +119,14 @@ namespace OpenSim.Framework.Communications // If the operation isn't done, wait for it AsyncWaitHandle.WaitOne(); AsyncWaitHandle.Close(); - m_waitHandle = null; // Allow early GC + m_waitHandle = null; // Allow early GC } // Operation is done: if an exception occured, throw it if (m_exception != null) throw m_exception; - } + } - #endregion + #endregion } internal class AsyncResult : SimpleAsyncResult @@ -140,10 +134,12 @@ namespace OpenSim.Framework.Communications private T m_result = default(T); public AsyncResult(AsyncCallback asyncCallback, Object state) : - base(asyncCallback, state) { } + base(asyncCallback, state) + { + } - public void SetAsCompleted(T result, bool completedSynchronously) + public void SetAsCompleted(T result, bool completedSynchronously) { // Save the asynchronous operation's result m_result = result; @@ -153,11 +149,10 @@ namespace OpenSim.Framework.Communications base.SetAsCompleted(completedSynchronously); } - new public T EndInvoke() + public new T EndInvoke() { - base.EndInvoke(); - return m_result; + base.EndInvoke(); + return m_result; } - } -} +} \ No newline at end of file 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 @@ using System; +using System.Collections.Generic; using System.IO; using System.Net; -using System.Web; using System.Text; -using System.Collections.Generic; using System.Threading; - -using OpenSim.Framework.Console; +using System.Web; namespace OpenSim.Framework.Communications { @@ -29,9 +27,10 @@ namespace OpenSim.Framework.Communications /// public class RestClient { + private string realuri; - string realuri; #region member variables + /// /// The base Uri of the web-service e.g. http://www.google.com /// @@ -60,7 +59,7 @@ namespace OpenSim.Framework.Communications /// /// MemoryStream representing the resultiong resource /// - Stream _resource; + private Stream _resource; /// /// WebRequest object, held as a member variable @@ -80,12 +79,12 @@ namespace OpenSim.Framework.Communications /// /// Default time out period /// - const int DefaultTimeout = 10 * 1000; // 10 seconds timeout + private const int DefaultTimeout = 10*1000; // 10 seconds timeout /// /// Default Buffer size of a block requested from the web-server /// - const int BufferSize = 4096; // Read blocks of 4 KB. + private const int BufferSize = 4096; // Read blocks of 4 KB. /// @@ -97,6 +96,7 @@ namespace OpenSim.Framework.Communications #endregion member variables #region constructors + /// /// Instantiate a new RestClient /// @@ -111,7 +111,8 @@ namespace OpenSim.Framework.Communications _lock = new object(); } - object _lock; + private object _lock; + #endregion constructors /// @@ -120,8 +121,8 @@ namespace OpenSim.Framework.Communications /// path entry public void AddResourcePath(string element) { - if(isSlashed(element)) - _pathElements.Add(element.Substring(0, element.Length-1)); + if (isSlashed(element)) + _pathElements.Add(element.Substring(0, element.Length - 1)); else _pathElements.Add(element); } @@ -178,7 +179,7 @@ namespace OpenSim.Framework.Communications /// Build a Uri based on the intial Url, path elements and parameters /// /// fully constructed Uri - Uri buildUri() + private Uri buildUri() { StringBuilder sb = new StringBuilder(); sb.Append(_url); @@ -196,7 +197,8 @@ namespace OpenSim.Framework.Communications { sb.Append("?"); firstElement = false; - } else + } + else sb.Append("&"); sb.Append(kv.Key); @@ -209,7 +211,9 @@ namespace OpenSim.Framework.Communications realuri = sb.ToString(); return new Uri(sb.ToString()); } + #region Async communications with server + /// /// Async method, invoked when a block of data has been received from the service /// @@ -218,13 +222,14 @@ namespace OpenSim.Framework.Communications { try { - Stream s = (Stream)ar.AsyncState; + Stream s = (Stream) ar.AsyncState; int read = s.EndRead(ar); if (read > 0) { _resource.Write(_readbuf, 0, read); - IAsyncResult asynchronousResult = s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); + IAsyncResult asynchronousResult = + s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); // TODO! Implement timeout, without killing the server //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); @@ -251,12 +256,13 @@ namespace OpenSim.Framework.Communications try { // grab response - WebRequest wr = (WebRequest)ar.AsyncState; - _response = (HttpWebResponse)wr.EndGetResponse(ar); + WebRequest wr = (WebRequest) ar.AsyncState; + _response = (HttpWebResponse) wr.EndGetResponse(ar); // get response stream, and setup async reading Stream s = _response.GetResponseStream(); - IAsyncResult asynchronousResult = s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); + IAsyncResult asynchronousResult = + s.BeginRead(_readbuf, 0, BufferSize, new AsyncCallback(StreamIsReadyDelegate), s); // TODO! Implement timeout, without killing the server // wait until completed, or we timed out @@ -281,6 +287,7 @@ namespace OpenSim.Framework.Communications } } } + #endregion Async communications with server /// @@ -290,17 +297,17 @@ namespace OpenSim.Framework.Communications { lock (_lock) { - _request = (HttpWebRequest)WebRequest.Create(buildUri()); + _request = (HttpWebRequest) WebRequest.Create(buildUri()); _request.KeepAlive = false; _request.ContentType = "application/xml"; _request.Timeout = 200000; _asyncException = null; // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); - _response = (HttpWebResponse)_request.GetResponse(); + _response = (HttpWebResponse) _request.GetResponse(); Stream src = _response.GetResponseStream(); int length = src.Read(_readbuf, 0, BufferSize); - while(length > 0) + while (length > 0) { _resource.Write(_readbuf, 0, length); length = src.Read(_readbuf, 0, BufferSize); @@ -329,7 +336,7 @@ namespace OpenSim.Framework.Communications public Stream Request(Stream src) { - _request = (HttpWebRequest)WebRequest.Create(buildUri()); + _request = (HttpWebRequest) WebRequest.Create(buildUri()); _request.KeepAlive = false; _request.ContentType = "application/xml"; _request.Timeout = 900000; @@ -340,13 +347,13 @@ namespace OpenSim.Framework.Communications src.Seek(0, SeekOrigin.Begin); Stream dst = _request.GetRequestStream(); byte[] buf = new byte[1024]; - int length = src.Read(buf,0, 1024); + int length = src.Read(buf, 0, 1024); while (length > 0) { dst.Write(buf, 0, length); length = src.Read(buf, 0, 1024); } - _response = (HttpWebResponse)_request.GetResponse(); + _response = (HttpWebResponse) _request.GetResponse(); // IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); @@ -357,8 +364,8 @@ namespace OpenSim.Framework.Communications return null; } - #region Async Invocation + public IAsyncResult BeginRequest(AsyncCallback callback, object state) { /// @@ -371,7 +378,7 @@ namespace OpenSim.Framework.Communications public Stream EndRequest(IAsyncResult asyncResult) { - AsyncResult ar = (AsyncResult)asyncResult; + AsyncResult ar = (AsyncResult) asyncResult; // Wait for operation to complete, then return result or // throw exception @@ -381,7 +388,7 @@ namespace OpenSim.Framework.Communications private void RequestHelper(Object asyncResult) { // We know that it's really an AsyncResult object - AsyncResult ar = (AsyncResult)asyncResult; + AsyncResult ar = (AsyncResult) asyncResult; try { // Perform the operation; if sucessful set the result @@ -394,6 +401,7 @@ namespace OpenSim.Framework.Communications ar.HandleException(e, false); } } + #endregion Async Invocation } -} +} \ No newline at end of file -- cgit v1.1