-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
145 additions
and
127 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...table/EntryCommands/PortGenerationMode.cs → ...lanetConsole.Common/PortGenerationMode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Net.Sockets; | ||
using LibplanetConsole.Common.DataAnnotations; | ||
|
||
namespace LibplanetConsole.Common; | ||
|
||
public sealed class PortGenerator | ||
{ | ||
public const int DefaultSpace = 10; | ||
private readonly List<int> _portList = []; | ||
|
||
public PortGenerator(int startingPort) | ||
{ | ||
_portList.Add(startingPort == 0 ? PortUtility.NextPort() : startingPort); | ||
Current = _portList[0]; | ||
} | ||
|
||
public int Space { get; init; } = DefaultSpace; | ||
|
||
public int Current { get; private set; } | ||
|
||
public PortGenerationMode Mode { get; init; } = PortGenerationMode.Random; | ||
|
||
public int Next() | ||
{ | ||
Current = GetPort(Current); | ||
_portList.Add(Current); | ||
_portList.Sort(); | ||
return Current; | ||
} | ||
|
||
private int GetPort(int nextPort) | ||
{ | ||
if (Mode == PortGenerationMode.Random) | ||
{ | ||
var port = PortUtility.NextPort(); | ||
while (IsValidRandomPort(port) is false) | ||
{ | ||
port = PortUtility.NextPort(); | ||
} | ||
|
||
return port; | ||
} | ||
|
||
return nextPort + Space; | ||
} | ||
|
||
private bool IsValidRandomPort(int randomPort) | ||
{ | ||
for (var i = 0; i < _portList.Count; i++) | ||
{ | ||
var port = _portList[i]; | ||
if (Math.Abs(port - randomPort) < Space) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.