diff --git a/BeatTogether.DedicatedServer.Interface/BeatTogether.DedicatedServer.Interface.csproj b/BeatTogether.DedicatedServer.Interface/BeatTogether.DedicatedServer.Interface.csproj index 2b41c9b3..ec558582 100644 --- a/BeatTogether.DedicatedServer.Interface/BeatTogether.DedicatedServer.Interface.csproj +++ b/BeatTogether.DedicatedServer.Interface/BeatTogether.DedicatedServer.Interface.csproj @@ -7,7 +7,7 @@ BeatTogether Team BeatTogether https://github.com/beattogether/BeatTogether.DedicatedServer - 1.7.1 + 1.7.2 enable diff --git a/BeatTogether.DedicatedServer.Node/PortAllocator.cs b/BeatTogether.DedicatedServer.Node/PortAllocator.cs index f3abc82d..67bb84b6 100644 --- a/BeatTogether.DedicatedServer.Node/PortAllocator.cs +++ b/BeatTogether.DedicatedServer.Node/PortAllocator.cs @@ -9,6 +9,8 @@ public sealed class PortAllocator : IPortAllocator { private readonly NodeConfiguration _configuration; + private readonly object _lock = new object(); + private readonly HashSet _acquiredPorts = new(); private readonly HashSet _releasedPorts = new(); @@ -24,27 +26,34 @@ public PortAllocator( public int? AcquirePort() { - if (_acquiredPorts.Count >= _configuration.MaximumSlots) - return null; - int port; - if (_releasedPorts.Any()) + lock (_lock) { - port = _releasedPorts.First(); - _releasedPorts.Remove(port); + if (_acquiredPorts.Count >= _configuration.MaximumSlots) + return null; + int port; + if (_releasedPorts.Any()) + { + port = _releasedPorts.First(); + _releasedPorts.Remove(port); + } + else + port = ++_lastPort; + _acquiredPorts.Add(port); + return port; } - else - port = ++_lastPort; - _acquiredPorts.Add(port); - return port; + } public bool ReleasePort(int port) { + lock (_lock) + { + if (!_acquiredPorts.Remove(port)) + return false; + _releasedPorts.Add(port); + return true; + } - if (!_acquiredPorts.Remove(port)) - return false; - _releasedPorts.Add(port); - return true; } } }