[Fix] Check if can register a context

This commit is contained in:
sta 2016-04-02 11:14:50 +09:00
parent c9511823b8
commit fdf1a18809
2 changed files with 25 additions and 12 deletions

View File

@ -303,7 +303,12 @@ namespace WebSocketSharp.Net
}
conn._contextBound = true;
lsnr.RegisterContext (conn._context);
if (!lsnr.RegisterContext (conn._context)) {
conn._contextBound = false;
conn.close ();
return;
}
return;
}

View File

@ -581,25 +581,33 @@ namespace WebSocketSharp.Net
throw new ObjectDisposedException (GetType ().ToString ());
}
internal void RegisterContext (HttpListenerContext context)
internal bool RegisterContext (HttpListenerContext context)
{
lock (_ctxRegistrySync)
_ctxRegistry[context] = context;
if (!_listening)
return false;
HttpListenerAsyncResult ares = null;
lock (_waitQueueSync) {
if (_waitQueue.Count == 0) {
lock (_ctxQueueSync)
_ctxQueue.Add (context);
}
else {
ares = _waitQueue[0];
_waitQueue.RemoveAt (0);
lock (_ctxRegistrySync) {
if (!_listening)
return false;
_ctxRegistry[context] = context;
lock (_waitQueueSync) {
if (_waitQueue.Count == 0) {
lock (_ctxQueueSync)
_ctxQueue.Add (context);
}
else {
ares = _waitQueue[0];
_waitQueue.RemoveAt (0);
}
}
}
if (ares != null)
ares.Complete (context);
return true;
}
internal void RemoveConnection (HttpConnection connection)