Refactored EndPointManager.cs

This commit is contained in:
sta 2014-10-15 13:15:30 +09:00
parent 85dd00626b
commit 3dee61f5f8

View File

@ -46,10 +46,18 @@ namespace WebSocketSharp.Net
{ {
internal sealed class EndPointManager internal sealed class EndPointManager
{ {
#region Private Static Fields #region Private Fields
private static Dictionary<IPAddress, Dictionary<int, EndPointListener>> _ipToEndpoints = private static Dictionary<IPAddress, Dictionary<int, EndPointListener>> _ipToEndpoints;
new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
#endregion
#region Static Constructor
static EndPointManager ()
{
_ipToEndpoints = new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
}
#endregion #endregion
@ -65,63 +73,59 @@ namespace WebSocketSharp.Net
private static void addPrefix (string uriPrefix, HttpListener httpListener) private static void addPrefix (string uriPrefix, HttpListener httpListener)
{ {
var prefix = new ListenerPrefix (uriPrefix); var pref = new ListenerPrefix (uriPrefix);
if (prefix.Path.IndexOf ('%') != -1) if (pref.Path.IndexOf ('%') != -1)
throw new HttpListenerException (400, "Invalid path."); // TODO: Code? throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1) if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
throw new HttpListenerException (400, "Invalid path."); // TODO: Code? throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
// Always listens on all the interfaces, no matter the host name/ip used. // Always listens on all the interfaces, no matter the host name/ip used.
var epListener = getEndPointListener ( var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.Secure, httpListener);
IPAddress.Any, prefix.Port, httpListener, prefix.Secure); epl.AddPrefix (pref, httpListener);
epListener.AddPrefix (prefix, httpListener);
} }
private static EndPointListener getEndPointListener ( private static EndPointListener getEndPointListener (
IPAddress address, int port, HttpListener httpListener, bool secure) IPAddress address, int port, bool secure, HttpListener httpListener)
{ {
Dictionary<int, EndPointListener> endpoints = null; Dictionary<int, EndPointListener> eps = null;
if (_ipToEndpoints.ContainsKey (address)) { if (_ipToEndpoints.ContainsKey (address)) {
endpoints = _ipToEndpoints [address]; eps = _ipToEndpoints[address];
} }
else { else {
endpoints = new Dictionary<int, EndPointListener> (); eps = new Dictionary<int, EndPointListener> ();
_ipToEndpoints [address] = endpoints; _ipToEndpoints[address] = eps;
} }
EndPointListener epListener = null; EndPointListener epl = null;
if (endpoints.ContainsKey (port)) { if (eps.ContainsKey (port)) {
epListener = endpoints [port]; epl = eps[port];
} }
else { else {
epListener = new EndPointListener ( epl = new EndPointListener (
address, address,
port, port,
secure, secure,
httpListener.CertificateFolderPath, httpListener.CertificateFolderPath,
httpListener.DefaultCertificate); httpListener.DefaultCertificate);
endpoints [port] = epListener; eps[port] = epl;
} }
return epListener; return epl;
} }
private static void removePrefix (string uriPrefix, HttpListener httpListener) private static void removePrefix (string uriPrefix, HttpListener httpListener)
{ {
var prefix = new ListenerPrefix (uriPrefix); var pref = new ListenerPrefix (uriPrefix);
if (prefix.Path.IndexOf ('%') != -1) if (pref.Path.IndexOf ('%') != -1)
return; return;
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1) if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
return; return;
var epListener = getEndPointListener ( var epl = getEndPointListener (IPAddress.Any, pref.Port, pref.Secure, httpListener);
IPAddress.Any, prefix.Port, httpListener, prefix.Secure); epl.RemovePrefix (pref, httpListener);
epListener.RemovePrefix (prefix, httpListener);
} }
#endregion #endregion
@ -133,14 +137,14 @@ namespace WebSocketSharp.Net
var added = new List<string> (); var added = new List<string> ();
lock (((ICollection) _ipToEndpoints).SyncRoot) { lock (((ICollection) _ipToEndpoints).SyncRoot) {
try { try {
foreach (var prefix in httpListener.Prefixes) { foreach (var pref in httpListener.Prefixes) {
addPrefix (prefix, httpListener); addPrefix (pref, httpListener);
added.Add (prefix); added.Add (pref);
} }
} }
catch { catch {
foreach (var prefix in added) foreach (var pref in added)
removePrefix (prefix, httpListener); removePrefix (pref, httpListener);
throw; throw;
} }
@ -156,9 +160,9 @@ namespace WebSocketSharp.Net
public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint) public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint)
{ {
lock (((ICollection) _ipToEndpoints).SyncRoot) { lock (((ICollection) _ipToEndpoints).SyncRoot) {
var endpoints = _ipToEndpoints [endpoint.Address]; var eps = _ipToEndpoints[endpoint.Address];
endpoints.Remove (endpoint.Port); eps.Remove (endpoint.Port);
if (endpoints.Count == 0) if (eps.Count == 0)
_ipToEndpoints.Remove (endpoint.Address); _ipToEndpoints.Remove (endpoint.Address);
epListener.Close (); epListener.Close ();
@ -168,8 +172,8 @@ namespace WebSocketSharp.Net
public static void RemoveListener (HttpListener httpListener) public static void RemoveListener (HttpListener httpListener)
{ {
lock (((ICollection) _ipToEndpoints).SyncRoot) lock (((ICollection) _ipToEndpoints).SyncRoot)
foreach (var prefix in httpListener.Prefixes) foreach (var pref in httpListener.Prefixes)
removePrefix (prefix, httpListener); removePrefix (pref, httpListener);
} }
public static void RemovePrefix (string uriPrefix, HttpListener httpListener) public static void RemovePrefix (string uriPrefix, HttpListener httpListener)