Refactored a few for EndPointManager.cs
This commit is contained in:
parent
d3b45eb6ec
commit
160aa923c2
@ -164,7 +164,7 @@ namespace WebSocketSharp.Net
|
|||||||
if (_all != null && _all.Count > 0)
|
if (_all != null && _all.Count > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
EndPointManager.RemoveEndPoint (this, _endpoint);
|
EndPointManager.RemoveEndPoint (_endpoint, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static RSACryptoServiceProvider createRSAFromFile (string filename)
|
private static RSACryptoServiceProvider createRSAFromFile (string filename)
|
||||||
|
@ -88,26 +88,32 @@ namespace WebSocketSharp.Net
|
|||||||
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
|
throw new HttpListenerException (400, "Invalid path."); // TODO: Code?
|
||||||
|
|
||||||
// Listens on all the interfaces if host name cannot be parsed by IPAddress.
|
// Listens on all the interfaces if host name cannot be parsed by IPAddress.
|
||||||
var epl = getEndPointListener (pref.Host, pref.Port, httpListener, pref.IsSecure);
|
var lsnr = getEndPointListener (pref.Host, pref.Port, httpListener, pref.IsSecure);
|
||||||
epl.AddPrefix (pref, httpListener);
|
lsnr.AddPrefix (pref, httpListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IPAddress convertToAddress (string hostName)
|
||||||
|
{
|
||||||
|
if (hostName == "*")
|
||||||
|
return IPAddress.Any;
|
||||||
|
|
||||||
|
IPAddress addr;
|
||||||
|
if (IPAddress.TryParse (hostName, out addr))
|
||||||
|
return addr;
|
||||||
|
|
||||||
|
try {
|
||||||
|
var host = Dns.GetHostEntry (hostName);
|
||||||
|
return host != null ? host.AddressList[0] : IPAddress.Any;
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return IPAddress.Any;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static EndPointListener getEndPointListener (
|
private static EndPointListener getEndPointListener (
|
||||||
string host, int port, HttpListener httpListener, bool secure)
|
string host, int port, HttpListener httpListener, bool secure)
|
||||||
{
|
{
|
||||||
IPAddress addr;
|
var addr = convertToAddress (host);
|
||||||
if (host == "*") {
|
|
||||||
addr = IPAddress.Any;
|
|
||||||
}
|
|
||||||
else if (!IPAddress.TryParse (host, out addr)) {
|
|
||||||
try {
|
|
||||||
var iphost = Dns.GetHostEntry (host);
|
|
||||||
addr = iphost != null ? iphost.AddressList[0] : IPAddress.Any;
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
addr = IPAddress.Any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Dictionary<int, EndPointListener> eps = null;
|
Dictionary<int, EndPointListener> eps = null;
|
||||||
if (_ipToEndpoints.ContainsKey (addr)) {
|
if (_ipToEndpoints.ContainsKey (addr)) {
|
||||||
@ -118,12 +124,12 @@ namespace WebSocketSharp.Net
|
|||||||
_ipToEndpoints[addr] = eps;
|
_ipToEndpoints[addr] = eps;
|
||||||
}
|
}
|
||||||
|
|
||||||
EndPointListener epl = null;
|
EndPointListener lsnr = null;
|
||||||
if (eps.ContainsKey (port)) {
|
if (eps.ContainsKey (port)) {
|
||||||
epl = eps[port];
|
lsnr = eps[port];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
epl = new EndPointListener (
|
lsnr = new EndPointListener (
|
||||||
addr,
|
addr,
|
||||||
port,
|
port,
|
||||||
secure,
|
secure,
|
||||||
@ -131,10 +137,10 @@ namespace WebSocketSharp.Net
|
|||||||
httpListener.SslConfiguration,
|
httpListener.SslConfiguration,
|
||||||
httpListener.ReuseAddress);
|
httpListener.ReuseAddress);
|
||||||
|
|
||||||
eps[port] = epl;
|
eps[port] = lsnr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return epl;
|
return lsnr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void removePrefix (string uriPrefix, HttpListener httpListener)
|
private static void removePrefix (string uriPrefix, HttpListener httpListener)
|
||||||
@ -146,8 +152,24 @@ namespace WebSocketSharp.Net
|
|||||||
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
if (pref.Path.IndexOf ("//", StringComparison.Ordinal) != -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var epl = getEndPointListener (pref.Host, pref.Port, httpListener, pref.IsSecure);
|
var lsnr = getEndPointListener (pref.Host, pref.Port, httpListener, pref.IsSecure);
|
||||||
epl.RemovePrefix (pref, httpListener);
|
lsnr.RemovePrefix (pref, httpListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Internal Methods
|
||||||
|
|
||||||
|
internal static void RemoveEndPoint (IPEndPoint endpoint, EndPointListener endpointListener)
|
||||||
|
{
|
||||||
|
lock (((ICollection) _ipToEndpoints).SyncRoot) {
|
||||||
|
var eps = _ipToEndpoints[endpoint.Address];
|
||||||
|
eps.Remove (endpoint.Port);
|
||||||
|
if (eps.Count == 0)
|
||||||
|
_ipToEndpoints.Remove (endpoint.Address);
|
||||||
|
|
||||||
|
endpointListener.Close ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -179,18 +201,6 @@ namespace WebSocketSharp.Net
|
|||||||
addPrefix (uriPrefix, httpListener);
|
addPrefix (uriPrefix, httpListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemoveEndPoint (EndPointListener epListener, IPEndPoint endpoint)
|
|
||||||
{
|
|
||||||
lock (((ICollection) _ipToEndpoints).SyncRoot) {
|
|
||||||
var eps = _ipToEndpoints[endpoint.Address];
|
|
||||||
eps.Remove (endpoint.Port);
|
|
||||||
if (eps.Count == 0)
|
|
||||||
_ipToEndpoints.Remove (endpoint.Address);
|
|
||||||
|
|
||||||
epListener.Close ();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void RemoveListener (HttpListener httpListener)
|
public static void RemoveListener (HttpListener httpListener)
|
||||||
{
|
{
|
||||||
lock (((ICollection) _ipToEndpoints).SyncRoot)
|
lock (((ICollection) _ipToEndpoints).SyncRoot)
|
||||||
|
Loading…
Reference in New Issue
Block a user