adjust socket listener

This commit is contained in:
Luke Pulverenti 2014-09-13 20:37:04 -04:00
parent 8d20621e19
commit 4f3ea6c6c3

View file

@ -15,7 +15,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
public class WebSocketSharpListener : IHttpListener public class WebSocketSharpListener : IHttpListener
{ {
private HttpListener _listener; private HttpListener _listener;
private readonly ManualResetEvent _listenForNextRequest = new ManualResetEvent(false); private readonly ManualResetEventSlim _listenForNextRequest = new ManualResetEventSlim(false);
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Action<string> _endpointListener; private readonly Action<string> _endpointListener;
@ -64,7 +64,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
try try
{ {
_listener.BeginGetContext(ListenerCallback, _listener); _listener.BeginGetContext(ListenerCallback, _listener);
_listenForNextRequest.WaitOne(); _listenForNextRequest.Wait();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -78,8 +78,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
// Handle the processing of a request in here. // Handle the processing of a request in here.
private void ListenerCallback(IAsyncResult asyncResult) private void ListenerCallback(IAsyncResult asyncResult)
{ {
_listenForNextRequest.Set();
var listener = asyncResult.AsyncState as HttpListener; var listener = asyncResult.AsyncState as HttpListener;
HttpListenerContext context; HttpListenerContext context;
@ -90,7 +88,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{ {
if (!isListening) if (!isListening)
{ {
_logger.Debug("Ignoring ListenerCallback() as HttpListener is no longer listening"); return; _logger.Debug("Ignoring ListenerCallback() as HttpListener is no longer listening");
return;
} }
// The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework, // The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework,
// blocks until there is a request to be processed or some type of data is available. // blocks until there is a request to be processed or some type of data is available.
@ -106,6 +105,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
_logger.Warn(errMsg); _logger.Warn(errMsg);
return; return;
} }
finally
{
_listenForNextRequest.Set();
}
Task.Factory.StartNew(() => InitTask(context)); Task.Factory.StartNew(() => InitTask(context));
} }