2using System.Collections.Generic;
5using System.Threading.Tasks;
7using Microsoft.EntityFrameworkCore;
8using Microsoft.Extensions.Logging;
9using Microsoft.Extensions.Options;
34 readonly ILogger<PortAllocator>
logger;
51 IOptions<SwarmConfiguration> swarmConfigurationOptions,
52 ILogger<PortAllocator>
logger)
56 swarmConfiguration = swarmConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(swarmConfigurationOptions));
57 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
61 public async Task<ushort?>
GetAvailablePort(ushort basePort,
bool checkOne, CancellationToken cancellationToken)
63 logger.LogTrace(
"Port allocation >= {basePort} requested...", basePort);
70 .ToListAsync(cancellationToken);
76 .Select(x => x.ApiValidationPort)
77 .ToListAsync(cancellationToken);
79 var exceptions =
new List<Exception>();
83 for (port = basePort; port < ushort.MaxValue; ++port)
85 if (checkOne && port != basePort)
89 || ddPorts.Contains(port)
90 || dmPorts.Contains(port))
103 logger.LogInformation(
"Allocated port {port}", port);
107 logger.LogWarning(
"Unable to allocate port >= {basePort}!", basePort);
112 if (port != basePort)
115 exceptions.Count == 1
117 :
new AggregateException(exceptions),
118 "Failed to allocate ports {basePort}-{lastCheckedPort}!",
string? Identifier
The server's identifier.
Configuration for the server swarm system.
Extension methods for the Socket class.
static void BindTest(ushort port, bool includeIPv6)
Attempt to exclusively bind to a given port .
readonly ILogger< PortAllocator > logger
The ILogger for the PortAllocator.
async Task< ushort?> GetAvailablePort(ushort basePort, bool checkOne, CancellationToken cancellationToken)
Gets a port not currently in use by TGS. A Task<TResult> resulting in the first available port on suc...
readonly SwarmConfiguration swarmConfiguration
The SwarmConfiguration for the PortAllocator.
readonly IServerPortProvider serverPortProvider
The IServerPortProvider for the PortAllocator.
readonly IDatabaseContext databaseContext
The IDatabaseContext for the PortAllocator.
PortAllocator(IServerPortProvider serverPortProvider, IDatabaseContext databaseContext, IOptions< SwarmConfiguration > swarmConfigurationOptions, ILogger< PortAllocator > logger)
Initializes a new instance of the PortAllocator class.
Provides access to the server's HttpApiPort.
ushort HttpApiPort
The port the server listens on.
IDatabaseCollection< DreamDaemonSettings > DreamDaemonSettings
The Models.DreamDaemonSettings in the IDatabaseContext.
IDatabaseCollection< DreamMakerSettings > DreamMakerSettings
The Models.DreamMakerSettings in the IDatabaseContext.
Gets unassigned ports for use by TGS.