Refactored EndPointManager.cs

This commit is contained in:
sta 2014-04-10 14:42:51 +09:00
parent dae4e957cd
commit 8ec0711bde

View File

@ -1,32 +1,41 @@
// #region License
// EndPointManager.cs /*
// Copied from System.Net.EndPointManager.cs * EndPointManager.cs
// *
// Author: * This code is derived from System.Net.EndPointManager.cs of Mono
// Gonzalo Paniagua Javier (gonzalo@ximian.com) * (http://www.mono-project.com).
// *
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com) * The MIT License
// Copyright (c) 2012-2013 sta.blockhead *
// * Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
// Permission is hereby granted, free of charge, to any person obtaining * Copyright (c) 2012-2014 sta.blockhead
// a copy of this software and associated documentation files (the *
// "Software"), to deal in the Software without restriction, including * Permission is hereby granted, free of charge, to any person obtaining a copy
// without limitation the rights to use, copy, modify, merge, publish, * of this software and associated documentation files (the "Software"), to deal
// distribute, sublicense, and/or sell copies of the Software, and to * in the Software without restriction, including without limitation the rights
// permit persons to whom the Software is furnished to do so, subject to * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// the following conditions: * copies of the Software, and to permit persons to whom the Software is
// * furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be *
// included in all copies or substantial portions of the Software. * The above copyright notice and this permission notice shall be included in
// * all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// * THE SOFTWARE.
*/
#endregion
#region Authors
/*
* Authors:
* - Gonzalo Paniagua Javier <gonzalo@ximian.com>
*/
#endregion
using System; using System;
using System.Collections; using System.Collections;
@ -35,143 +44,143 @@ using System.Net;
namespace WebSocketSharp.Net namespace WebSocketSharp.Net
{ {
internal sealed class EndPointManager internal sealed class EndPointManager
{ {
#region Private Fields #region Private Static Fields
private static Dictionary<IPAddress, Dictionary<int, EndPointListener>> _ipToEndpoints = new Dictionary<IPAddress, Dictionary<int, EndPointListener>> (); private static Dictionary<IPAddress, Dictionary<int, EndPointListener>> _ipToEndpoints =
new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
#endregion #endregion
#region Private Constructors #region Private Constructors
private EndPointManager () private EndPointManager ()
{ {
} }
#endregion #endregion
#region Private Methods #region Private Methods
private static void addPrefix (string uriPrefix, HttpListener httpListener) private static void addPrefix (string uriPrefix, HttpListener httpListener)
{ {
var prefix = new ListenerPrefix (uriPrefix); var prefix = new ListenerPrefix (uriPrefix);
if (prefix.Path.IndexOf ('%') != -1) if (prefix.Path.IndexOf ('%') != -1)
throw new HttpListenerException (400, "Invalid path."); throw new HttpListenerException (400, "Invalid path.");
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code? if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
throw new HttpListenerException (400, "Invalid path."); 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 (IPAddress.Any, prefix.Port, httpListener, prefix.Secure); var epListener = getEndPointListener (
epListener.AddPrefix (prefix, httpListener); IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
}
private static EndPointListener getEndPointListener ( epListener.AddPrefix (prefix, httpListener);
IPAddress address, int port, HttpListener httpListener, bool secure) }
{
Dictionary<int, EndPointListener> endpoints = null;
if (_ipToEndpoints.ContainsKey (address))
{
endpoints = _ipToEndpoints [address];
}
else
{
endpoints = new Dictionary<int, EndPointListener> ();
_ipToEndpoints [address] = endpoints;
}
EndPointListener epListener = null; private static EndPointListener getEndPointListener (
if (endpoints.ContainsKey (port)) IPAddress address, int port, HttpListener httpListener, bool secure)
{ {
epListener = endpoints [port]; Dictionary<int, EndPointListener> endpoints = null;
} if (_ipToEndpoints.ContainsKey (address)) {
else endpoints = _ipToEndpoints [address];
{ }
epListener = new EndPointListener ( else {
address, port, secure, httpListener.CertificateFolderPath, httpListener.DefaultCertificate); endpoints = new Dictionary<int, EndPointListener> ();
endpoints [port] = epListener; _ipToEndpoints [address] = endpoints;
} }
return epListener; EndPointListener epListener = null;
} if (endpoints.ContainsKey (port)) {
epListener = endpoints [port];
}
else {
epListener = new EndPointListener (
address,
port,
secure,
httpListener.CertificateFolderPath,
httpListener.DefaultCertificate);
private static void removePrefix (string uriPrefix, HttpListener httpListener) endpoints [port] = epListener;
{ }
var prefix = new ListenerPrefix (uriPrefix);
if (prefix.Path.IndexOf ('%') != -1)
return;
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1) return epListener;
return; }
var epListener = getEndPointListener (IPAddress.Any, prefix.Port, httpListener, prefix.Secure); private static void removePrefix (string uriPrefix, HttpListener httpListener)
epListener.RemovePrefix (prefix, httpListener); {
} var prefix = new ListenerPrefix (uriPrefix);
if (prefix.Path.IndexOf ('%') != -1)
return;
#endregion if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
return;
#region Public Methods var epListener = getEndPointListener (
IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
public static void AddListener (HttpListener httpListener) epListener.RemovePrefix (prefix, httpListener);
{ }
var added = new List<string> ();
lock (((ICollection) _ipToEndpoints).SyncRoot)
{
try {
foreach (var prefix in httpListener.Prefixes)
{
addPrefix (prefix, httpListener);
added.Add (prefix);
}
}
catch {
foreach (var prefix in added)
removePrefix (prefix, httpListener);
throw; #endregion
}
}
}
public static void AddPrefix (string uriPrefix, HttpListener httpListener) #region Public Methods
{
lock (((ICollection) _ipToEndpoints).SyncRoot)
{
addPrefix (uriPrefix, httpListener);
}
}
public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint) public static void AddListener (HttpListener httpListener)
{ {
lock (((ICollection) _ipToEndpoints).SyncRoot) var added = new List<string> ();
{ lock (((ICollection) _ipToEndpoints).SyncRoot) {
var endpoints = _ipToEndpoints [endpoint.Address]; try {
endpoints.Remove (endpoint.Port); foreach (var prefix in httpListener.Prefixes) {
if (endpoints.Count == 0) addPrefix (prefix, httpListener);
_ipToEndpoints.Remove (endpoint.Address); added.Add (prefix);
}
}
catch {
foreach (var prefix in added)
removePrefix (prefix, httpListener);
epListener.Close (); throw;
} }
} }
}
public static void RemoveListener (HttpListener httpListener) public static void AddPrefix (string uriPrefix, HttpListener httpListener)
{ {
lock (((ICollection) _ipToEndpoints).SyncRoot) lock (((ICollection) _ipToEndpoints).SyncRoot) {
{ addPrefix (uriPrefix, httpListener);
foreach (var prefix in httpListener.Prefixes) }
removePrefix (prefix, httpListener); }
}
}
public static void RemovePrefix (string uriPrefix, HttpListener httpListener) public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint)
{ {
lock (((ICollection) _ipToEndpoints).SyncRoot) lock (((ICollection) _ipToEndpoints).SyncRoot) {
{ var endpoints = _ipToEndpoints [endpoint.Address];
removePrefix (uriPrefix, httpListener); endpoints.Remove (endpoint.Port);
} if (endpoints.Count == 0)
} _ipToEndpoints.Remove (endpoint.Address);
#endregion epListener.Close ();
} }
}
public static void RemoveListener (HttpListener httpListener)
{
lock (((ICollection) _ipToEndpoints).SyncRoot) {
foreach (var prefix in httpListener.Prefixes)
removePrefix (prefix, httpListener);
}
}
public static void RemovePrefix (string uriPrefix, HttpListener httpListener)
{
lock (((ICollection) _ipToEndpoints).SyncRoot) {
removePrefix (uriPrefix, httpListener);
}
}
#endregion
}
} }