mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
Ports are now static per SharedDevice where possible, falling back to random ports if an error occurs.
43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
namespace NAPS2.Escl.Server;
|
|
|
|
internal static class PortFinder
|
|
{
|
|
private const int MAX_PORT_TRIES = 5;
|
|
private const int RANDOM_PORT_MIN = 10001;
|
|
private const int RANDOM_PORT_MAX = 19999;
|
|
|
|
public static async Task RunWithSpecifiedOrRandomPort(int defaultPort, Func<int, Task> portTaskFunc,
|
|
CancellationToken cancelToken)
|
|
{
|
|
int port = defaultPort;
|
|
int retries = 0;
|
|
var random = new Random();
|
|
if (port == 0)
|
|
{
|
|
port = RandomPort(random);
|
|
}
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
await portTaskFunc(port);
|
|
break;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
if (cancelToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
retries++;
|
|
port = RandomPort(random);
|
|
if (retries > MAX_PORT_TRIES)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static int RandomPort(Random random) => random.Next(RANDOM_PORT_MIN, RANDOM_PORT_MAX + 1);
|
|
} |