Refactored EndPointManager.cs

master
sta 12 years ago
parent dae4e957cd
commit 8ec0711bde

@ -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); {
if (prefix.Path.IndexOf ('%') != -1) var prefix = new ListenerPrefix (uriPrefix);
throw new HttpListenerException (400, "Invalid path."); if (prefix.Path.IndexOf ('%') != -1)
throw new HttpListenerException (400, "Invalid path.");
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1) // TODO: Code?
throw new HttpListenerException (400, "Invalid path."); if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
// Always listens on all the interfaces, no matter the host name/ip used.
var epListener = getEndPointListener (IPAddress.Any, prefix.Port, httpListener, prefix.Secure); // Always listens on all the interfaces, no matter the host name/ip used.
epListener.AddPrefix (prefix, httpListener); var epListener = getEndPointListener (
} 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; private static EndPointListener getEndPointListener (
if (_ipToEndpoints.ContainsKey (address)) IPAddress address, int port, HttpListener httpListener, bool secure)
{ {
endpoints = _ipToEndpoints [address]; Dictionary<int, EndPointListener> endpoints = null;
} if (_ipToEndpoints.ContainsKey (address)) {
else endpoints = _ipToEndpoints [address];
{ }
endpoints = new Dictionary<int, EndPointListener> (); else {
_ipToEndpoints [address] = endpoints; endpoints = new Dictionary<int, EndPointListener> ();
} _ipToEndpoints [address] = endpoints;
}
EndPointListener epListener = null;
if (endpoints.ContainsKey (port)) EndPointListener epListener = null;
{ if (endpoints.ContainsKey (port)) {
epListener = endpoints [port]; epListener = endpoints [port];
} }
else else {
{ epListener = new EndPointListener (
epListener = new EndPointListener ( address,
address, port, secure, httpListener.CertificateFolderPath, httpListener.DefaultCertificate); port,
endpoints [port] = epListener; secure,
} httpListener.CertificateFolderPath,
httpListener.DefaultCertificate);
return epListener;
} endpoints [port] = epListener;
}
private static void removePrefix (string uriPrefix, HttpListener httpListener)
{ return epListener;
var prefix = new ListenerPrefix (uriPrefix); }
if (prefix.Path.IndexOf ('%') != -1)
return; private static void removePrefix (string uriPrefix, HttpListener httpListener)
{
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1) var prefix = new ListenerPrefix (uriPrefix);
return; if (prefix.Path.IndexOf ('%') != -1)
return;
var epListener = getEndPointListener (IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
epListener.RemovePrefix (prefix, httpListener); if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
} return;
#endregion var epListener = getEndPointListener (
IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
#region Public Methods
epListener.RemovePrefix (prefix, httpListener);
public static void AddListener (HttpListener httpListener) }
{
var added = new List<string> (); #endregion
lock (((ICollection) _ipToEndpoints).SyncRoot)
{ #region Public Methods
try {
foreach (var prefix in httpListener.Prefixes) public static void AddListener (HttpListener httpListener)
{ {
addPrefix (prefix, httpListener); var added = new List<string> ();
added.Add (prefix); lock (((ICollection) _ipToEndpoints).SyncRoot) {
} try {
} foreach (var prefix in httpListener.Prefixes) {
catch { addPrefix (prefix, httpListener);
foreach (var prefix in added) added.Add (prefix);
removePrefix (prefix, httpListener); }
}
throw; catch {
} foreach (var prefix in added)
} removePrefix (prefix, httpListener);
}
throw;
public static void AddPrefix (string uriPrefix, HttpListener httpListener) }
{ }
lock (((ICollection) _ipToEndpoints).SyncRoot) }
{
addPrefix (uriPrefix, httpListener); public static void AddPrefix (string uriPrefix, HttpListener httpListener)
} {
} lock (((ICollection) _ipToEndpoints).SyncRoot) {
addPrefix (uriPrefix, httpListener);
public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint) }
{ }
lock (((ICollection) _ipToEndpoints).SyncRoot)
{ public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint)
var endpoints = _ipToEndpoints [endpoint.Address]; {
endpoints.Remove (endpoint.Port); lock (((ICollection) _ipToEndpoints).SyncRoot) {
if (endpoints.Count == 0) var endpoints = _ipToEndpoints [endpoint.Address];
_ipToEndpoints.Remove (endpoint.Address); endpoints.Remove (endpoint.Port);
if (endpoints.Count == 0)
epListener.Close (); _ipToEndpoints.Remove (endpoint.Address);
}
} epListener.Close ();
}
public static void RemoveListener (HttpListener httpListener) }
{
lock (((ICollection) _ipToEndpoints).SyncRoot) public static void RemoveListener (HttpListener httpListener)
{ {
foreach (var prefix in httpListener.Prefixes) lock (((ICollection) _ipToEndpoints).SyncRoot) {
removePrefix (prefix, httpListener); foreach (var prefix in httpListener.Prefixes)
} removePrefix (prefix, httpListener);
} }
}
public static void RemovePrefix (string uriPrefix, HttpListener httpListener)
{ public static void RemovePrefix (string uriPrefix, HttpListener httpListener)
lock (((ICollection) _ipToEndpoints).SyncRoot) {
{ lock (((ICollection) _ipToEndpoints).SyncRoot) {
removePrefix (uriPrefix, httpListener); removePrefix (uriPrefix, httpListener);
} }
} }
#endregion #endregion
} }
} }

Loading…
Cancel
Save