Skip to content

Commit

Permalink
early work on client console for connecting to servers
Browse files Browse the repository at this point in the history
  • Loading branch information
dooly123 committed Jan 6, 2025
1 parent 8acaf87 commit c3fe2a2
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 4 deletions.
3 changes: 0 additions & 3 deletions Basis Server/BasisNetworkClient/BasisNetworkClient.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<StartupObject>Basis.Program</StartupObject>
</PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\BasisNetworkClient\BasisNetworkClient.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 13 in Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs

View workflow job for this annotation

GitHub Actions / Build server on Ubuntu (Basis Server, linux)

Nullability of reference types in type of parameter 'sender' of 'void Program.TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)' doesn't match the target delegate 'EventHandler<UnobservedTaskExceptionEventArgs>' (possibly because of nullability attributes).

Check warning on line 13 in Basis Server/BasisNetworkClientConsole/BasisNetworkClientConsole/Program.cs

View workflow job for this annotation

GitHub Actions / Build server on Ubuntu (Basis Server, win)

Nullability of reference types in type of parameter 'sender' of 'void Program.TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)' doesn't match the target delegate 'EventHandler<UnobservedTaskExceptionEventArgs>' (possibly because of nullability attributes).

// 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
}
}
}
10 changes: 10 additions & 0 deletions Basis Server/BasisNetworkServer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Basis Server/BasisNetworkServer/BasisNetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Basis Server/BasisServerConsole/BasisNetworkConsole.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down

0 comments on commit c3fe2a2

Please sign in to comment.