From fdf1a1880918f3f3aee15c210e0d716e8b5b6028 Mon Sep 17 00:00:00 2001 From: sta Date: Sat, 2 Apr 2016 11:14:50 +0900 Subject: [PATCH] [Fix] Check if can register a context --- websocket-sharp/Net/HttpConnection.cs | 7 ++++++- websocket-sharp/Net/HttpListener.cs | 30 +++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/websocket-sharp/Net/HttpConnection.cs b/websocket-sharp/Net/HttpConnection.cs index 095f454e..d941278a 100644 --- a/websocket-sharp/Net/HttpConnection.cs +++ b/websocket-sharp/Net/HttpConnection.cs @@ -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; } diff --git a/websocket-sharp/Net/HttpListener.cs b/websocket-sharp/Net/HttpListener.cs index 924e3cb9..cf1f3499 100644 --- a/websocket-sharp/Net/HttpListener.cs +++ b/websocket-sharp/Net/HttpListener.cs @@ -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)