Refactored EndPointManager.cs
This commit is contained in:
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;
|
||||||
@ -37,9 +46,10 @@ 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
|
||||||
|
|
||||||
@ -59,11 +69,13 @@ namespace WebSocketSharp.Net
|
|||||||
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 (
|
||||||
|
IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
|
||||||
|
|
||||||
epListener.AddPrefix (prefix, httpListener);
|
epListener.AddPrefix (prefix, httpListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,25 +83,26 @@ namespace WebSocketSharp.Net
|
|||||||
IPAddress address, int port, HttpListener httpListener, bool secure)
|
IPAddress address, int port, HttpListener httpListener, bool secure)
|
||||||
{
|
{
|
||||||
Dictionary<int, EndPointListener> endpoints = null;
|
Dictionary<int, EndPointListener> endpoints = null;
|
||||||
if (_ipToEndpoints.ContainsKey (address))
|
if (_ipToEndpoints.ContainsKey (address)) {
|
||||||
{
|
|
||||||
endpoints = _ipToEndpoints [address];
|
endpoints = _ipToEndpoints [address];
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
endpoints = new Dictionary<int, EndPointListener> ();
|
endpoints = new Dictionary<int, EndPointListener> ();
|
||||||
_ipToEndpoints [address] = endpoints;
|
_ipToEndpoints [address] = endpoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
EndPointListener epListener = null;
|
EndPointListener epListener = null;
|
||||||
if (endpoints.ContainsKey (port))
|
if (endpoints.ContainsKey (port)) {
|
||||||
{
|
|
||||||
epListener = endpoints [port];
|
epListener = endpoints [port];
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
epListener = new EndPointListener (
|
epListener = new EndPointListener (
|
||||||
address, port, secure, httpListener.CertificateFolderPath, httpListener.DefaultCertificate);
|
address,
|
||||||
|
port,
|
||||||
|
secure,
|
||||||
|
httpListener.CertificateFolderPath,
|
||||||
|
httpListener.DefaultCertificate);
|
||||||
|
|
||||||
endpoints [port] = epListener;
|
endpoints [port] = epListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +118,9 @@ namespace WebSocketSharp.Net
|
|||||||
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
if (prefix.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var epListener = getEndPointListener (IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
|
var epListener = getEndPointListener (
|
||||||
|
IPAddress.Any, prefix.Port, httpListener, prefix.Secure);
|
||||||
|
|
||||||
epListener.RemovePrefix (prefix, httpListener);
|
epListener.RemovePrefix (prefix, httpListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,11 +131,9 @@ namespace WebSocketSharp.Net
|
|||||||
public static void AddListener (HttpListener httpListener)
|
public static void AddListener (HttpListener httpListener)
|
||||||
{
|
{
|
||||||
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 prefix in httpListener.Prefixes) {
|
||||||
{
|
|
||||||
addPrefix (prefix, httpListener);
|
addPrefix (prefix, httpListener);
|
||||||
added.Add (prefix);
|
added.Add (prefix);
|
||||||
}
|
}
|
||||||
@ -136,16 +149,14 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
public static void AddPrefix (string uriPrefix, HttpListener httpListener)
|
public static void AddPrefix (string uriPrefix, HttpListener httpListener)
|
||||||
{
|
{
|
||||||
lock (((ICollection) _ipToEndpoints).SyncRoot)
|
lock (((ICollection) _ipToEndpoints).SyncRoot) {
|
||||||
{
|
|
||||||
addPrefix (uriPrefix, httpListener);
|
addPrefix (uriPrefix, httpListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 endpoints = _ipToEndpoints [endpoint.Address];
|
||||||
endpoints.Remove (endpoint.Port);
|
endpoints.Remove (endpoint.Port);
|
||||||
if (endpoints.Count == 0)
|
if (endpoints.Count == 0)
|
||||||
@ -157,8 +168,7 @@ 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 prefix in httpListener.Prefixes)
|
||||||
removePrefix (prefix, httpListener);
|
removePrefix (prefix, httpListener);
|
||||||
}
|
}
|
||||||
@ -166,8 +176,7 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user