mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
29b3c57207
Ports are now static per SharedDevice where possible, falling back to random ports if an error occurs.
30 lines
782 B
C#
30 lines
782 B
C#
using EmbedIO;
|
|
|
|
namespace NAPS2.Escl.Server;
|
|
|
|
internal static class WebServerExtensions
|
|
{
|
|
public static async Task StartAsync(this WebServer server, CancellationToken cancelToken = default)
|
|
{
|
|
var startedTcs = new TaskCompletionSource<bool>();
|
|
server.StateChanged += (_, args) =>
|
|
{
|
|
if (args.NewState == WebServerState.Listening)
|
|
{
|
|
startedTcs.TrySetResult(true);
|
|
}
|
|
};
|
|
_ = server.RunAsync(cancelToken).ContinueWith(t =>
|
|
{
|
|
if (t.IsFaulted)
|
|
{
|
|
startedTcs.TrySetException(t.Exception!);
|
|
}
|
|
else
|
|
{
|
|
startedTcs.TrySetCanceled();
|
|
}
|
|
});
|
|
await startedTcs.Task;
|
|
}
|
|
} |