wasSharpNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 19  →  ?path2? @ 20
/Network/HTTP/HTTPServer.cs
@@ -15,7 +15,7 @@
{
private int activeRequests;
 
private HttpListener HTTPListener = null;
private HttpListener HTTPListener;
 
private int processedRequests;
 
@@ -24,9 +24,11 @@
 
public bool IsRunning => HTTPListener != null && HTTPListener.IsListening;
 
public bool Start(IEnumerable<string> prefixes)
private readonly ManualResetEventSlim stopListen = new ManualResetEventSlim(false);
 
public bool Start(List<string> prefixes)
{
HTTPListener = new HttpListener()
HTTPListener = new HttpListener
{
AuthenticationSchemes = AuthenticationSchemes
};
@@ -41,23 +43,27 @@
// Do not bomb if the client disconnects.
HTTPListener.IgnoreWriteExceptions = true;
HTTPListener.Start();
ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener));
return true;
return ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener));
}
 
public void Stop()
{
HTTPListener.Stop();
stopListen.Set();
}
 
private void Listen(object state)
{
var callbackState = (HTTPServerCallbackState)state;
HTTPServerCallbackState callbackState = (HTTPServerCallbackState)state;
 
while (callbackState.Listener.IsListening)
{
callbackState.Listener.BeginGetContext(ContextCallback, callbackState);
callbackState.ContextRetrieved.WaitOne();
 
if (WaitHandle.WaitAny(new[] { callbackState.ContextRetrieved, stopListen.WaitHandle }) != 1)
continue;
 
callbackState.Listener.Stop();
break;
}
}
 
@@ -66,7 +72,7 @@
private void ContextCallback(IAsyncResult ar)
{
var callbackState = (HTTPServerCallbackState)ar.AsyncState;
HttpListenerContext httpContext = null;
HttpListenerContext httpContext;
 
Interlocked.Increment(ref processedRequests);
Interlocked.Increment(ref activeRequests);
@@ -85,9 +91,6 @@
callbackState.ContextRetrieved.Set();
}
 
if (httpContext == null)
return;
 
try
{
ProcessHTTPContext(httpContext);
@@ -101,7 +104,6 @@
public void Dispose()
{
Stop();
HTTPListener = null;
}
 
private class HTTPServerCallbackState
@@ -109,7 +111,7 @@
public HTTPServerCallbackState(HttpListener listener)
{
if (listener == null)
throw new ArgumentNullException("listener");
throw new ArgumentNullException(nameof(listener));
Listener = listener;
ContextRetrieved = new AutoResetEvent(false);
}