From f46953a5ad80fb618e7d156bb58a4d5bf045855b Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 13 Oct 2014 21:20:11 +0900 Subject: [PATCH] Refactored HttpListener.cs --- websocket-sharp/Net/HttpListener.cs | 75 ++++++++++++++--------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/websocket-sharp/Net/HttpListener.cs b/websocket-sharp/Net/HttpListener.cs index 1628e365..2fa8fa00 100644 --- a/websocket-sharp/Net/HttpListener.cs +++ b/websocket-sharp/Net/HttpListener.cs @@ -61,6 +61,8 @@ namespace WebSocketSharp.Net private object _connectionsSync; private List _ctxQueue; private object _ctxQueueSync; + private Dictionary _ctxRegistry; + private object _ctxRegistrySync; private Func _credFinder; private X509Certificate2 _defaultCert; private bool _disposed; @@ -68,8 +70,6 @@ namespace WebSocketSharp.Net private bool _listening; private HttpListenerPrefixCollection _prefixes; private string _realm; - private Dictionary _registry; - private object _registrySync; private List _waitQueue; private object _waitQueueSync; @@ -90,10 +90,10 @@ namespace WebSocketSharp.Net _ctxQueue = new List (); _ctxQueueSync = ((ICollection) _ctxQueue).SyncRoot; - _prefixes = new HttpListenerPrefixCollection (this); + _ctxRegistry = new Dictionary (); + _ctxRegistrySync = ((ICollection) _ctxRegistry).SyncRoot; - _registry = new Dictionary (); - _registrySync = ((ICollection) _registry).SyncRoot; + _prefixes = new HttpListenerPrefixCollection (this); _waitQueue = new List (); _waitQueueSync = ((ICollection) _waitQueue).SyncRoot; @@ -372,7 +372,7 @@ namespace WebSocketSharp.Net private void cleanup (bool force) { - lock (_registrySync) { + lock (_ctxRegistrySync) { if (!force) sendServiceUnavailable (); @@ -400,17 +400,17 @@ namespace WebSocketSharp.Net private void cleanupContextRegistry () { - lock (_registrySync) { - if (_registry.Count == 0) + lock (_ctxRegistrySync) { + if (_ctxRegistry.Count == 0) return; // Need to copy this since closing will call UnregisterContext. - var keys = _registry.Keys; - var all = new HttpListenerContext[keys.Count]; - keys.CopyTo (all, 0); - _registry.Clear (); - for (var i = all.Length - 1; i >= 0; i--) - all[i].Connection.Close (true); + var keys = _ctxRegistry.Keys; + var ctxs = new HttpListenerContext[keys.Count]; + keys.CopyTo (ctxs, 0); + _ctxRegistry.Clear (); + for (var i = ctxs.Length - 1; i >= 0; i--) + ctxs[i].Connection.Close (true); } } @@ -476,10 +476,10 @@ namespace WebSocketSharp.Net { CheckDisposed (); if (_prefixes.Count == 0) - throw new InvalidOperationException ("Please, call AddPrefix before using this method."); + throw new InvalidOperationException ("AddPrefix must be called before using this method."); if (!_listening) - throw new InvalidOperationException ("Please, call Start before using this method."); + throw new InvalidOperationException ("Start must be called before using this method."); // Lock _waitQueue early to avoid race conditions. lock (_waitQueueSync) { @@ -505,8 +505,8 @@ namespace WebSocketSharp.Net internal void RegisterContext (HttpListenerContext context) { - lock (_registrySync) - _registry[context] = context; + lock (_ctxRegistrySync) + _ctxRegistry[context] = context; ListenerAsyncResult ares = null; lock (_waitQueueSync) { @@ -539,8 +539,8 @@ namespace WebSocketSharp.Net internal void UnregisterContext (HttpListenerContext context) { - lock (_registrySync) - _registry.Remove (context); + lock (_ctxRegistrySync) + _ctxRegistry.Remove (context); lock (_ctxQueueSync) { var i = _ctxQueue.IndexOf (context); @@ -566,21 +566,21 @@ namespace WebSocketSharp.Net } /// - /// Begins getting an incoming request information asynchronously. + /// Begins getting an incoming request asynchronously. /// /// /// This asynchronous operation must be completed by calling the EndGetContext method. - /// Typically, that method is invoked by the delegate. + /// Typically, the method is invoked by the delegate. /// /// /// An that represents the status of the asynchronous operation. /// /// - /// An delegate that references the method(s) to invoke + /// An delegate that references the method to invoke /// when the asynchronous operation completes. /// /// - /// An that contains a user defined object to pass to + /// An that represents a user defined object to pass to /// the delegate. /// /// @@ -591,7 +591,7 @@ namespace WebSocketSharp.Net /// -or- /// /// - /// This listener hasn't been started or is stopped currently. + /// This listener hasn't been started, or is currently stopped. /// /// /// @@ -615,14 +615,14 @@ namespace WebSocketSharp.Net } /// - /// Ends an asynchronous operation to get an incoming request information. + /// Ends an asynchronous operation to get an incoming request. /// /// /// This method completes an asynchronous operation started by calling /// the BeginGetContext method. /// /// - /// A that contains a request information. + /// A that represents a request. /// /// /// An obtained by calling the BeginGetContext method. @@ -647,10 +647,10 @@ namespace WebSocketSharp.Net var ares = asyncResult as ListenerAsyncResult; if (ares == null) - throw new ArgumentException ("Wrong IAsyncResult.", "asyncResult"); + throw new ArgumentException ("A wrong IAsyncResult.", "asyncResult"); if (ares.EndCalled) - throw new InvalidOperationException ("Cannot reuse this IAsyncResult."); + throw new InvalidOperationException ("This IAsyncResult cannot be reused."); ares.EndCalled = true; if (!ares.IsCompleted) @@ -671,14 +671,13 @@ namespace WebSocketSharp.Net } /// - /// Gets an incoming request information. + /// Gets an incoming request. /// /// - /// This method waits for an incoming request and returns a request information - /// when the listener receives a request. + /// This method waits for an incoming request, and returns when a request is received. /// /// - /// A that contains a request information. + /// A that represents a request. /// /// /// @@ -688,7 +687,7 @@ namespace WebSocketSharp.Net /// -or- /// /// - /// This listener hasn't been started or is stopped currently. + /// This listener hasn't been started, or is currently stopped. /// /// /// @@ -703,7 +702,7 @@ namespace WebSocketSharp.Net } /// - /// Starts to receive incoming requests. + /// Starts receiving incoming requests. /// /// /// This listener has been closed. @@ -737,17 +736,17 @@ namespace WebSocketSharp.Net #endregion - #region Explicit Interface Implementation + #region Explicit Interface Implementations /// - /// Releases all resource used by the listener. + /// Releases all resources used by the listener. /// void IDisposable.Dispose () { if (_disposed) return; - close (true); // TODO: Should we force here or not? + close (true); _disposed = true; }