Fix Close method in EndPointListener.cs because an exception (System.InvalidOperationException: Hashtable.Enumerator: snapshot out of sync.) has occured

This commit is contained in:
sta
2012-09-27 21:47:08 +09:00
parent fa7aae6ae2
commit b4d8689bb3
57 changed files with 53 additions and 39 deletions

View File

@@ -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 ();
}
}

View File

@@ -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
}
}