Skip to content

Commit

Permalink
Bump dedi interface version
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicgraphics committed Apr 5, 2024
1 parent fa8cfdf commit 6e2d6c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Authors>BeatTogether Team</Authors>
<Company>BeatTogether</Company>
<RepositoryUrl>https://github.com/beattogether/BeatTogether.DedicatedServer</RepositoryUrl>
<Version>1.7.1</Version>
<Version>1.7.2</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
37 changes: 23 additions & 14 deletions BeatTogether.DedicatedServer.Node/PortAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public sealed class PortAllocator : IPortAllocator
{
private readonly NodeConfiguration _configuration;

private readonly object _lock = new object();

private readonly HashSet<int> _acquiredPorts = new();
private readonly HashSet<int> _releasedPorts = new();

Expand All @@ -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;
}
}
}

0 comments on commit 6e2d6c7

Please sign in to comment.