From c3fe2a21c15d047c3924cd53f8d4413c2d3fd4c4 Mon Sep 17 00:00:00 2001 From: dooly Date: Tue, 7 Jan 2025 00:58:20 +1000 Subject: [PATCH] early work on client console for connecting to servers --- .../BasisNetworkClient/BasisNetworkClient.cs | 3 - .../BasisNetworkClientConsole.csproj | 16 +++ .../BasisNetworkClientConsole/Program.cs | 107 ++++++++++++++++++ Basis Server/BasisNetworkServer.sln | 10 ++ .../BasisNetworkServer/BasisNetworkServer.cs | 4 + .../BasisNetworkConsole.csproj | 2 +- 6 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/BasisNetworkClientConsole.csproj create mode 100644 Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs diff --git a/Basis Server/BasisNetworkClient/BasisNetworkClient.cs b/Basis Server/BasisNetworkClient/BasisNetworkClient.cs index 941bba049..753b9203a 100644 --- a/Basis Server/BasisNetworkClient/BasisNetworkClient.cs +++ b/Basis Server/BasisNetworkClient/BasisNetworkClient.cs @@ -1,9 +1,6 @@ using Basis.Network.Core; using LiteNetLib; using LiteNetLib.Utils; -using System; -using System.Threading; -using System.Threading.Tasks; using static Basis.Network.Core.Serializable.SerializableBasis; using static SerializableBasis; diff --git a/Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/BasisNetworkClientConsole.csproj b/Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/BasisNetworkClientConsole.csproj new file mode 100644 index 000000000..e8736091f --- /dev/null +++ b/Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/BasisNetworkClientConsole.csproj @@ -0,0 +1,16 @@ + + + + Exe + net9.0 + enable + enable + Basis.Program + + + true + + + + + diff --git a/Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs b/Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs new file mode 100644 index 000000000..f526b6ffa --- /dev/null +++ b/Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs @@ -0,0 +1,107 @@ +using System.Text; +using static Basis.Network.Core.Serializable.SerializableBasis; +using static SerializableBasis; + +namespace Basis +{ + class Program + { + public static void Main(string[] args) + { + // Set up global exception handlers + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + + // Get the path to the config.xml file in the application's directory + string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.xml"); + + + // Create a cancellation token source + var cancellationTokenSource = new CancellationTokenSource(); + var cancellationToken = cancellationTokenSource.Token; + + // Start the server in a background task and prevent it from exiting + Task serverTask = Task.Run(() => + { + try + { + ReadyMessage RM = new ReadyMessage + { + playerMetaDataMessage = new PlayerMetaDataMessage() + }; + RM.playerMetaDataMessage.playerDisplayName = "Fake User"; + RM.playerMetaDataMessage.playerUUID = "UUID Test"; + RM.clientAvatarChangeMessage = new ClientAvatarChangeMessage + { + byteArray = new byte[13] + }; + RM.localAvatarSyncMessage = new LocalAvatarSyncMessage + { + array = new byte[LocalAvatarSyncMessage.AvatarSyncSize] + }; + AuthenticationMessage Authmessage = new AuthenticationMessage + { + bytes = Encoding.UTF8.GetBytes("default_password") + }; + BasisNetworkClient.AuthenticationMessage = Authmessage; + BasisNetworkClient.StartClient("localhost", 4296, RM,true); + BNL.Log("Connecting!"); + } + catch (Exception ex) + { + BNL.LogError($"Server encountered an error: {ex.Message} {ex.StackTrace}"); + // Optionally, handle server restart or log critical errors + } + }, cancellationToken); + + // Register a shutdown hook to clean up resources when the application is terminated + AppDomain.CurrentDomain.ProcessExit += async (sender, eventArgs) => + { + BNL.Log("Shutting down server..."); + + // Perform graceful shutdown of the server and logging + cancellationTokenSource.Cancel(); + + try + { + await serverTask; // Wait for the server to finish + } + catch (Exception ex) + { + BNL.LogError($"Error during server shutdown: {ex.Message}"); + } + BNL.Log("Server shut down successfully."); + }; + + // Keep the application running + while (true) + { + Thread.Sleep(15000); + } + } + + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + var exception = e.ExceptionObject as Exception; + if (exception != null) + { + BNL.LogError($"Fatal exception: {exception.Message}"); + BNL.LogError($"Stack trace: {exception.StackTrace}"); + } + else + { + BNL.LogError("An unknown fatal exception occurred."); + } + } + + private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + foreach (var exception in e.Exception.InnerExceptions) + { + BNL.LogError($"Unobserved task exception: {exception.Message}"); + BNL.LogError($"Stack trace: {exception.StackTrace}"); + } + e.SetObserved(); // Prevents the application from crashing + } + } +} diff --git a/Basis Server/BasisNetworkServer.sln b/Basis Server/BasisNetworkServer.sln index 311e79fc1..258a11ced 100644 --- a/Basis Server/BasisNetworkServer.sln +++ b/Basis Server/BasisNetworkServer.sln @@ -19,6 +19,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasisNetworkCore", "BasisNe EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasisNetworkServer", "BasisNetworkServer\BasisNetworkServer.csproj", "{51F765C5-646F-4F9E-B212-416E93E2E562}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasisNetworkClientConsole", "BasisNetworkClientConsole\BasisNetworkClientConsole\BasisNetworkClientConsole.csproj", "{B806E008-766C-42FD-BD05-BD486B23CA76}" + ProjectSection(ProjectDependencies) = postProject + {6972BF72-BE14-4C71-ABF3-6EF914FADEC8} = {6972BF72-BE14-4C71-ABF3-6EF914FADEC8} + {7828CBD0-B57F-4C02-AD19-A0E015A5DD70} = {7828CBD0-B57F-4C02-AD19-A0E015A5DD70} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +51,10 @@ Global {51F765C5-646F-4F9E-B212-416E93E2E562}.Debug|Any CPU.Build.0 = Debug|Any CPU {51F765C5-646F-4F9E-B212-416E93E2E562}.Release|Any CPU.ActiveCfg = Release|Any CPU {51F765C5-646F-4F9E-B212-416E93E2E562}.Release|Any CPU.Build.0 = Release|Any CPU + {B806E008-766C-42FD-BD05-BD486B23CA76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B806E008-766C-42FD-BD05-BD486B23CA76}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B806E008-766C-42FD-BD05-BD486B23CA76}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B806E008-766C-42FD-BD05-BD486B23CA76}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Basis Server/BasisNetworkServer/BasisNetworkServer.cs b/Basis Server/BasisNetworkServer/BasisNetworkServer.cs index 181e43318..66d46a169 100644 --- a/Basis Server/BasisNetworkServer/BasisNetworkServer.cs +++ b/Basis Server/BasisNetworkServer/BasisNetworkServer.cs @@ -306,6 +306,10 @@ private static void HandleNetworkReceiveEvent(NetPeer peer, NetPacketReader read catch (Exception e) { BNL.LogError($"{e.Message} : {e.StackTrace}"); + if (reader != null) + { + reader.Recycle(); + } } } diff --git a/Basis Server/BasisServerConsole/BasisNetworkConsole.csproj b/Basis Server/BasisServerConsole/BasisNetworkConsole.csproj index 4ba5d8ddf..560bdc4b4 100644 --- a/Basis Server/BasisServerConsole/BasisNetworkConsole.csproj +++ b/Basis Server/BasisServerConsole/BasisNetworkConsole.csproj @@ -1,4 +1,4 @@ - + Exe