Changing port allocation tactics for the millionth time

This commit is contained in:
Jordan Dominion
2024-09-14 16:57:47 -04:00
parent 6e4aa507af
commit a131fea124
@@ -43,7 +43,6 @@ using Tgstation.Server.Host.Database;
using Tgstation.Server.Host.Extensions;
using Tgstation.Server.Host.Jobs;
using Tgstation.Server.Host.System;
using Tgstation.Server.Host.Utils;
using Tgstation.Server.Tests.Live.Instance;
namespace Tgstation.Server.Tests.Live
@@ -53,6 +52,7 @@ namespace Tgstation.Server.Tests.Live
[TestCategory("RequiresDatabase")]
public sealed class TestLiveServer
{
const ushort InitialPort = 42069;
public static readonly Version TestUpdateVersion = new(5, 11, 0);
static readonly Lazy<ushort> odDMPort = new(() => FreeTcpPort());
@@ -64,6 +64,7 @@ namespace Tgstation.Server.Tests.Live
static void InitializePorts()
{
tcpPortCounter = InitialPort;
_ = odDMPort.Value;
_ = odDDPort.Value;
_ = compatDMPort.Value;
@@ -156,38 +157,11 @@ namespace Tgstation.Server.Tests.Live
return result;
}
static ushort tcpPortCounter = InitialPort;
static ushort FreeTcpPort(params ushort[] usedPorts)
{
ushort result;
var listeners = new List<TcpListener>();
try
{
do
{
var l = new TcpListener(IPAddress.Any, 0);
l.Start();
try
{
listeners.Add(l);
}
catch
{
using (l)
l.Stop();
throw;
}
result = (ushort)((IPEndPoint)l.LocalEndpoint).Port;
}
while (usedPorts.Contains(result) || result < 20000);
}
finally
{
foreach (var l in listeners)
using (l)
l.Stop();
}
ushort result = tcpPortCounter++;
Console.WriteLine($"Allocated port: {result}");
return result;