Skip to content

Commit

Permalink
make HTTP.Server be async instead of sync
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Jun 23, 2024
1 parent 46438cb commit bc60542
Showing 1 changed file with 24 additions and 39 deletions.
63 changes: 24 additions & 39 deletions src/http/partials/Server/HTTP.ServerTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Task Open(Uri host)

_server._serverOn.OnOpen?.Invoke(null, null);

ReceiveRequests();
InitAccept();
}
catch (Exception e)
{
Expand Down Expand Up @@ -150,50 +150,35 @@ public void WebsocketEventBroadcast(string name, string data, Encoding encoding)
}
}

private void ReceiveRequests()
private void InitAccept()
{
var thread = new Thread(() =>
_listener.BeginGetContext(AcceptCallback, null);

void AcceptCallback(IAsyncResult result)
{
while (IsOpened)
if (IsOpened)
{
HttpListenerContext context = null;
NetlyEnvironment.Logger.Create("Request entry.");
HttpListenerContext context = _listener.EndGetContext(result);

try
{
context = _listener.GetContext();
}
catch (Exception e)
Task.Run(async () =>
{
context = null;
NetlyEnvironment.Logger.Create($"Request fail: {e}");
}
finally
{
if (context != null)
Task.Run(() =>
{
NetlyEnvironment.Logger.Create("Task Init");

try
{
var task = HandleConnection(context);
Task.WaitAll(task);
}
catch (Exception e)
{
NetlyEnvironment.Logger.Create($"Task error: {e}");
}

NetlyEnvironment.Logger.Create("Task End");
});
}
try
{
await HandleConnection(context);
}
catch (Exception e)
{
NetlyEnvironment.Logger.Create($"{this}: {e}");
}
});

InitAccept();
}

Close();
}) { IsBackground = true };

thread.Start();
else
{
Close();
}
}
}


Expand Down

0 comments on commit bc60542

Please sign in to comment.