/Network/HTTP/HTTPServer.cs |
@@ -13,6 +13,9 @@ |
{ |
public abstract class HTTPServer : IDisposable |
{ |
private readonly ManualResetEventSlim listenStop = new ManualResetEventSlim(false); |
|
private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false); |
private int activeRequests; |
|
private HttpListener HTTPListener; |
@@ -24,8 +27,10 @@ |
|
public bool IsRunning => HTTPListener != null && HTTPListener.IsListening; |
|
private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false); |
private readonly ManualResetEventSlim listenStop = new ManualResetEventSlim(false); |
public void Dispose() |
{ |
stopListen.Set(); |
} |
|
public bool Start(List<string> prefixes) |
{ |
@@ -37,9 +42,7 @@ |
// Add all prefixes. |
HTTPListener.Prefixes.Clear(); |
foreach (var prefix in prefixes) |
{ |
HTTPListener.Prefixes.Add(prefix); |
} |
|
// Do not bomb if the client disconnects. |
HTTPListener.IgnoreWriteExceptions = true; |
@@ -55,13 +58,13 @@ |
|
private void Listen(object state) |
{ |
HTTPServerCallbackState callbackState = (HTTPServerCallbackState)state; |
var callbackState = (HTTPServerCallbackState) state; |
|
while (callbackState.Listener.IsListening) |
{ |
callbackState.Listener?.BeginGetContext(ContextCallback, callbackState); |
|
if (WaitHandle.WaitAny(new[] { callbackState.ContextRetrieved, stopListen.WaitHandle }) != 1) |
if (WaitHandle.WaitAny(new[] {callbackState.ContextRetrieved, stopListen.WaitHandle}) != 1) |
continue; |
|
try |
@@ -81,7 +84,7 @@ |
|
private void ContextCallback(IAsyncResult ar) |
{ |
var callbackState = (HTTPServerCallbackState)ar.AsyncState; |
var callbackState = (HTTPServerCallbackState) ar.AsyncState; |
HttpListenerContext httpContext; |
|
Interlocked.Increment(ref processedRequests); |
@@ -111,11 +114,6 @@ |
} |
} |
|
public void Dispose() |
{ |
stopListen.Set(); |
} |
|
private class HTTPServerCallbackState |
{ |
public HTTPServerCallbackState(HttpListener listener) |
@@ -131,4 +129,4 @@ |
public AutoResetEvent ContextRetrieved { get; } |
} |
} |
} |
} |