From 8e02a1caa509c012a59a394f4175203e23f87b4e Mon Sep 17 00:00:00 2001 From: im-apbecker <165831479+im-apbecker@users.noreply.github.com> Date: Wed, 10 Apr 2024 08:55:07 -0600 Subject: [PATCH] Simplify port processing at the top level (#102) --- HKMPServer/HkmpServer.cs | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/HKMPServer/HkmpServer.cs b/HKMPServer/HkmpServer.cs index 55790e4..20d42fd 100644 --- a/HKMPServer/HkmpServer.cs +++ b/HKMPServer/HkmpServer.cs @@ -26,7 +26,9 @@ public void Initialize(string[] args) { return; } - if (string.IsNullOrEmpty(args[0]) || !ParsePort(args[0], out var port)) { + var portArg = args[0]; + + if (string.IsNullOrEmpty(portArg) || !ushort.TryParse(portArg, out var port)) { Logger.Info("Invalid port, should be an integer between 0 and 65535"); return; } @@ -47,7 +49,7 @@ public void Initialize(string[] args) { /// The input manager for command-line input. /// The logging class for logging to console. private void StartServer( - int port, + ushort port, ServerSettings serverSettings, ConsoleInputManager consoleInputManager, ConsoleLogger consoleLogger @@ -60,7 +62,7 @@ ConsoleLogger consoleLogger var serverManager = new ConsoleServerManager(netServer, serverSettings, packetManager, consoleLogger); serverManager.Initialize(); - serverManager.Start(port); + serverManager.Start((int)port); // TODO: make an event in ServerManager that we can register for so we know when the server shuts down consoleInputManager.ConsoleInputEvent += input => { @@ -71,33 +73,5 @@ ConsoleLogger consoleLogger }; consoleInputManager.Start(); } - - /// - /// Try to parse the given input as a networking port. - /// - /// The string to parse. - /// Will be set to the parsed port if this method returns true, or 0 if the method - /// returns false. - /// True if the given input was parsed as a valid port, false otherwise. - private static bool ParsePort(string input, out int port) { - if (!int.TryParse(input, out port)) { - return false; - } - - if (!IsValidPort(port)) { - return false; - } - - return true; - } - - /// - /// Returns true if the given port is a valid networking port. - /// - /// The port to check. - /// True if the port is valid, false otherwise. - private static bool IsValidPort(int port) { - return port >= 0 && port <= 65535; - } } }