wasSharpNET
/Network/HTTP/HTTPServer.cs |
@@ -25,6 +25,7 @@ |
public bool IsRunning => HTTPListener != null && HTTPListener.IsListening; |
|
private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false); |
private readonly ManualResetEventSlim listenStop = new ManualResetEventSlim(false); |
|
public bool Start(List<string> prefixes) |
{ |
@@ -46,9 +47,10 @@ |
return ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
} |
|
public void Stop() |
public bool Stop(int timeout) |
{ |
stopListen.Set(); |
return listenStop.Wait(timeout); |
} |
|
private void Listen(object state) |
@@ -57,12 +59,20 @@ |
|
while (callbackState.Listener.IsListening) |
{ |
callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
callbackState.Listener?.BeginGetContext(ContextCallback, callbackState); |
|
if (WaitHandle.WaitAny(new[] { callbackState.ContextRetrieved, stopListen.WaitHandle }) != 1) |
continue; |
|
callbackState.Listener.Stop(); |
try |
{ |
callbackState.Listener.Stop(); |
callbackState.Listener.Close(); |
} |
finally |
{ |
listenStop.Set(); |
} |
break; |
} |
} |
@@ -103,7 +113,7 @@ |
|
public void Dispose() |
{ |
Stop(); |
stopListen.Set(); |
} |
|
private class HTTPServerCallbackState |