Fix Close method in EndPointListener.cs because an exception (System.InvalidOperationException: Hashtable.Enumerator: snapshot out of sync.) has occured
This commit is contained in:
@@ -79,44 +79,6 @@ namespace WebSocketSharp.Net {
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Static Methods
|
||||
|
||||
static void OnAccept (object sender, EventArgs e)
|
||||
{
|
||||
SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
|
||||
EndPointListener epl = (EndPointListener) args.UserToken;
|
||||
Socket accepted = null;
|
||||
if (args.SocketError == SocketError.Success) {
|
||||
accepted = args.AcceptSocket;
|
||||
args.AcceptSocket = null;
|
||||
}
|
||||
|
||||
try {
|
||||
if (epl.sock != null)
|
||||
epl.sock.AcceptAsync (args);
|
||||
} catch {
|
||||
if (accepted != null) {
|
||||
try {
|
||||
accepted.Close ();
|
||||
} catch {}
|
||||
accepted = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (accepted == null)
|
||||
return;
|
||||
|
||||
if (epl.secure && (epl.cert == null || epl.key == null)) {
|
||||
accepted.Close ();
|
||||
return;
|
||||
}
|
||||
HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert, epl.key);
|
||||
epl.unregistered [conn] = conn;
|
||||
conn.BeginReadRequest ();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
void AddSpecial (List<ListenerPrefix> coll, ListenerPrefix prefix)
|
||||
@@ -204,6 +166,40 @@ namespace WebSocketSharp.Net {
|
||||
return best_match;
|
||||
}
|
||||
|
||||
static void OnAccept (object sender, EventArgs e)
|
||||
{
|
||||
SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
|
||||
EndPointListener epl = (EndPointListener) args.UserToken;
|
||||
Socket accepted = null;
|
||||
if (args.SocketError == SocketError.Success) {
|
||||
accepted = args.AcceptSocket;
|
||||
args.AcceptSocket = null;
|
||||
}
|
||||
|
||||
try {
|
||||
if (epl.sock != null)
|
||||
epl.sock.AcceptAsync (args);
|
||||
} catch {
|
||||
if (accepted != null) {
|
||||
try {
|
||||
accepted.Close ();
|
||||
} catch {}
|
||||
accepted = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (accepted == null)
|
||||
return;
|
||||
|
||||
if (epl.secure && (epl.cert == null || epl.key == null)) {
|
||||
accepted.Close ();
|
||||
return;
|
||||
}
|
||||
HttpConnection conn = new HttpConnection (accepted, epl, epl.secure, epl.cert, epl.key);
|
||||
epl.unregistered [conn] = conn;
|
||||
conn.BeginReadRequest ();
|
||||
}
|
||||
|
||||
bool RemoveSpecial (List<ListenerPrefix> coll, ListenerPrefix prefix)
|
||||
{
|
||||
if (coll == null)
|
||||
@@ -343,8 +339,10 @@ namespace WebSocketSharp.Net {
|
||||
{
|
||||
sock.Close ();
|
||||
lock (unregistered.SyncRoot) {
|
||||
foreach (HttpConnection c in unregistered.Keys)
|
||||
Hashtable copy = (Hashtable) unregistered.Clone ();
|
||||
foreach (HttpConnection c in copy.Keys)
|
||||
c.Close (true);
|
||||
copy.Clear ();
|
||||
unregistered.Clear ();
|
||||
}
|
||||
}
|
||||
|
@@ -37,12 +37,22 @@ namespace WebSocketSharp.Net {
|
||||
|
||||
sealed class EndPointManager {
|
||||
|
||||
#region Fields
|
||||
|
||||
static Dictionary<IPAddress, Dictionary<int, EndPointListener>> ip_to_endpoints = new Dictionary<IPAddress, Dictionary<int, EndPointListener>> ();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
private EndPointManager ()
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
static void AddPrefixInternal (string p, HttpListener listener)
|
||||
{
|
||||
ListenerPrefix lp = new ListenerPrefix (p);
|
||||
@@ -91,6 +101,10 @@ namespace WebSocketSharp.Net {
|
||||
epl.RemovePrefix (lp, listener);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public static void AddListener (HttpListener listener)
|
||||
{
|
||||
List<string> added = new List<string> ();
|
||||
@@ -144,5 +158,7 @@ namespace WebSocketSharp.Net {
|
||||
RemovePrefixInternal (prefix, listener);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user