mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-08-18 18:24:56 +02:00
.config
.run
.workdir
Agent
Common
Phantom.Common.Data
Agent
Backups
Instance
Java
Minecraft
Replies
AllowedPorts.cs
AuthToken.cs
ConnectionCommonKey.cs
Optional.cs
Phantom.Common.Data.csproj
PortRange.cs
RamAllocationUnits.cs
Result.cs
Phantom.Common.Data.Tests
Phantom.Common.Data.Web
Phantom.Common.Messages.Agent
Phantom.Common.Messages.Web
Controller
Docker
Utils
Web
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json
49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System.Text;
|
|
using MemoryPack;
|
|
|
|
namespace Phantom.Common.Data;
|
|
|
|
[MemoryPackable(GenerateType.VersionTolerant)]
|
|
readonly partial record struct PortRange(
|
|
[property: MemoryPackOrder(0)] ushort FirstPort,
|
|
[property: MemoryPackOrder(1)] ushort LastPort
|
|
) {
|
|
internal bool Contains(ushort port) {
|
|
return port >= FirstPort && port <= LastPort;
|
|
}
|
|
|
|
internal void ToString(StringBuilder builder) {
|
|
builder.Append(FirstPort);
|
|
|
|
if (LastPort != FirstPort) {
|
|
builder.Append('-');
|
|
builder.Append(LastPort);
|
|
}
|
|
}
|
|
|
|
internal static PortRange Parse(ReadOnlySpan<char> definition) {
|
|
int separatorIndex = definition.IndexOf('-');
|
|
if (separatorIndex == -1) {
|
|
var port = ParsePort(definition.Trim());
|
|
return new PortRange(port, port);
|
|
}
|
|
|
|
var firstPort = ParsePort(definition[..separatorIndex].Trim());
|
|
var lastPort = ParsePort(definition[(separatorIndex + 1)..].Trim());
|
|
if (lastPort < firstPort) {
|
|
throw new FormatException("Invalid port range '" + firstPort + "-" + lastPort + "'.");
|
|
}
|
|
else {
|
|
return new PortRange(firstPort, lastPort);
|
|
}
|
|
}
|
|
|
|
private static ushort ParsePort(ReadOnlySpan<char> port) {
|
|
try {
|
|
return ushort.Parse(port);
|
|
} catch (Exception) {
|
|
throw new FormatException("Invalid port '" + port.ToString() + "'.");
|
|
}
|
|
}
|
|
}
|