private static async void HandleRequest(IAsyncResult Sync)
{
bool Waiting = false;
try
{
// Finish accepting the client
HttpListenerContext Context = Listener.EndGetContext(Sync);
await Task.Run(async() =>
{
// Grab our connection
HttpClient Client = new HttpClient(Context);
// Wait for a connection slot to open up
await ConnectionPool.WaitAsync();
// Begin acceptinging another connection
if (!Waiting && IsRunning)
{
Listener.BeginGetContext(HandleRequest, Listener);
Waiting = true;
}
// Process the client connection
ProcessRequest(Client);
});
}
catch (HttpListenerException e)
{
// Thread abort, or application abort request... Ignore
if (e.ErrorCode == 995)
return;
// Log error
Program.ErrorLog.Write(
"ERROR: [HttpServer.HandleRequest] \r\n\t - {0}\r\n\t - ErrorCode: {1}",
e.Message,
e.ErrorCode
);
}
catch (Exception e)
{
ExceptionHandler.GenerateExceptionLog(e);
Program.ErrorLog.Write("ERROR: [HttpServer.HandleRequest] \r\n\t - {0}", e.Message);
}
finally
{
// Begin Listening again
if (!Waiting && IsRunning)
Listener.BeginGetContext(HandleRequest, Listener);
}
}