From bdafcf2d3934f6e9c8b38dc014a5927af7414b2d Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 27 Jun 2016 14:58:59 +0900 Subject: [PATCH] [Modify] Replace it --- websocket-sharp/Net/EndPointListener.cs | 2 +- websocket-sharp/Net/EndPointManager.cs | 67 +++++-------------------- 2 files changed, 13 insertions(+), 56 deletions(-) diff --git a/websocket-sharp/Net/EndPointListener.cs b/websocket-sharp/Net/EndPointListener.cs index 98643a0a..85eea886 100644 --- a/websocket-sharp/Net/EndPointListener.cs +++ b/websocket-sharp/Net/EndPointListener.cs @@ -210,7 +210,7 @@ namespace WebSocketSharp.Net if (prefs != null && prefs.Count > 0) return; - EndPointManager.RemoveEndPoint (_endpoint.Address, _endpoint.Port); + EndPointManager.RemoveEndPoint (_endpoint); } private static RSACryptoServiceProvider createRSAFromFile (string filename) diff --git a/websocket-sharp/Net/EndPointManager.cs b/websocket-sharp/Net/EndPointManager.cs index f2fe9ec8..ebecf459 100644 --- a/websocket-sharp/Net/EndPointManager.cs +++ b/websocket-sharp/Net/EndPointManager.cs @@ -55,9 +55,6 @@ namespace WebSocketSharp.Net { #region Private Fields - private static readonly Dictionary> - _addressToEndpoints; - private static readonly Dictionary _endpoints; #endregion @@ -66,7 +63,6 @@ namespace WebSocketSharp.Net static EndPointManager () { - _addressToEndpoints = new Dictionary> (); _endpoints = new Dictionary (); } @@ -82,17 +78,6 @@ namespace WebSocketSharp.Net #region Private Methods - private static void addEndPointListener (IPAddress address, int port, EndPointListener listener) - { - Dictionary endpoints; - if (!_addressToEndpoints.TryGetValue (address, out endpoints)) { - endpoints = new Dictionary (); - _addressToEndpoints.Add (address, endpoints); - } - - endpoints.Add (port, listener); - } - private static void addPrefix (string uriPrefix, HttpListener listener) { var pref = new HttpListenerPrefix (uriPrefix); @@ -115,23 +100,24 @@ namespace WebSocketSharp.Net if (path.IndexOf ("//", StringComparison.Ordinal) != -1) throw new HttpListenerException (87, "Includes an invalid path."); + var endpoint = new IPEndPoint (addr, port); + EndPointListener lsnr; - if (tryGetEndPointListener (addr, port, out lsnr)) { + if (_endpoints.TryGetValue (endpoint, out lsnr)) { if (lsnr.IsSecure ^ pref.IsSecure) throw new HttpListenerException (87, "Includes an invalid scheme."); } else { lsnr = new EndPointListener ( - addr, - port, + endpoint, pref.IsSecure, listener.CertificateFolderPath, listener.SslConfiguration, listener.ReuseAddress ); - addEndPointListener (addr, port, lsnr); + _endpoints.Add (endpoint, lsnr); } lsnr.AddPrefix (pref, listener); @@ -164,8 +150,10 @@ namespace WebSocketSharp.Net if (path.IndexOf ("//", StringComparison.Ordinal) != -1) return; + var endpoint = new IPEndPoint (addr, port); + EndPointListener lsnr; - if (!tryGetEndPointListener (addr, port, out lsnr)) + if (!_endpoints.TryGetValue (endpoint, out lsnr)) return; if (lsnr.IsSecure ^ pref.IsSecure) @@ -174,17 +162,6 @@ namespace WebSocketSharp.Net lsnr.RemovePrefix (pref, listener); } - private static bool tryGetEndPointListener ( - IPAddress address, int port, out EndPointListener listener - ) - { - listener = null; - - Dictionary endpoints; - return _addressToEndpoints.TryGetValue (address, out endpoints) - && endpoints.TryGetValue (port, out listener); - } - #endregion #region Internal Methods @@ -203,26 +180,6 @@ namespace WebSocketSharp.Net } } - internal static bool RemoveEndPoint (IPAddress address, int port) - { - lock (((ICollection) _addressToEndpoints).SyncRoot) { - Dictionary endpoints; - if (!_addressToEndpoints.TryGetValue (address, out endpoints)) - return false; - - EndPointListener lsnr; - if (!endpoints.TryGetValue (port, out lsnr)) - return false; - - endpoints.Remove (port); - if (endpoints.Count == 0) - _addressToEndpoints.Remove (address); - - lsnr.Close (); - return true; - } - } - #endregion #region Public Methods @@ -230,7 +187,7 @@ namespace WebSocketSharp.Net public static void AddListener (HttpListener listener) { var added = new List (); - lock (((ICollection) _addressToEndpoints).SyncRoot) { + lock (((ICollection) _endpoints).SyncRoot) { try { foreach (var pref in listener.Prefixes) { addPrefix (pref, listener); @@ -248,13 +205,13 @@ namespace WebSocketSharp.Net public static void AddPrefix (string uriPrefix, HttpListener listener) { - lock (((ICollection) _addressToEndpoints).SyncRoot) + lock (((ICollection) _endpoints).SyncRoot) addPrefix (uriPrefix, listener); } public static void RemoveListener (HttpListener listener) { - lock (((ICollection) _addressToEndpoints).SyncRoot) { + lock (((ICollection) _endpoints).SyncRoot) { foreach (var pref in listener.Prefixes) removePrefix (pref, listener); } @@ -262,7 +219,7 @@ namespace WebSocketSharp.Net public static void RemovePrefix (string uriPrefix, HttpListener listener) { - lock (((ICollection) _addressToEndpoints).SyncRoot) + lock (((ICollection) _endpoints).SyncRoot) removePrefix (uriPrefix, listener); }