Build heartbeat chain

This commit is contained in:
derek.holloway committed 2026-08-06 09:36:18 -07:00
1 parent 909718d8d5
commit f946ebef1e
5 files changed
+173 -122

No files matched your search

+35 -19
View File
@@ -9,8 +9,6 @@ namespace Mistox.Server {
public class MistoxServer {
// Class Definitions
public string ServerName { get; private set; } = ""; // The name of the server
public int MaxClients { get; set; } = 10; // The max players per server
public float ConnectionRange { get; set; } = 40; // InGame distance for valid conections
@@ -28,20 +26,18 @@ namespace Mistox.Server {
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port );
}
async Task ProcessAuthRequest(ServerConnection user, UserAuthPacket packet) {
packet.ServerName = ServerName;
if (packet.ConnectionKey == ConnectionKey) {
packet.AuthSuccessful = 1;
Connections.TryRemove(user.UserName, out _);
user.UserName = packet.UserName;
Connections[packet.UserName] = user;
user.IsAuthenticated = true;
} else {
packet.AuthSuccessful = -1;
async Task RunAsync(Func<Task> work) {
try{
await work();
} catch (Exception ex) {
Console.WriteLine($"Error during background packet processing: {ex.Message}");
}
await user.Send(packet);
}
/////////////////////////////
// Heartbeat
/////////////////////////////
async Task BuildServerList(ServerConnection user, HeartbeatPacket packet) {
packet.ServerList = new List<UserTableObject>();
foreach( ServerConnection cur in Connections.Values) {
@@ -54,18 +50,32 @@ namespace Mistox.Server {
await user.Send( packet );
}
async Task RunAsync(Func<Task> work) {
try{
await work();
} catch (Exception ex) {
Console.WriteLine($"Error during background packet processing: {ex.Message}");
/////////////////////////////
// Auth
/////////////////////////////
async Task ProcessAuthRequest(ServerConnection user, UserAuthPacket packet) {
packet.ServerName = ServerName;
if (packet.ConnectionKey == ConnectionKey) {
packet.AuthSuccessful = 1;
Connections.TryRemove(user.UserName, out _);
user.UserName = packet.UserName;
Connections[packet.UserName] = user;
user.IsAuthenticated = true;
InvokeUserConnected(user);
} else {
packet.AuthSuccessful = -1;
}
await user.Send(packet);
}
// Functions
/////////////////////////////
// Events
/////////////////////////////
public event Action<ServerConnection> onUserConnected;
public void InvokeUserConnected( ServerConnection user ){
Connections[user.UserName] = user;
onUserConnected?.Invoke( user );
}
@@ -78,6 +88,7 @@ namespace Mistox.Server {
});
break;
case HeartbeatPacket packet when user.IsAuthenticated:
user.Position = packet.Position;
_ = RunAsync(async () => {
await BuildServerList(user, packet);
});
@@ -93,9 +104,14 @@ namespace Mistox.Server {
public event Action<ServerConnection> onUserDisconnected;
public void InvokeUserDisconnected( ServerConnection user ){
Connections.Remove(user.UserName, out _);
onUserDisconnected?.Invoke( user );
}
/////////////////////////////
// Send Methods
/////////////////////////////
public async Task SendAllClients<Packet>(Packet data) {
foreach( ServerConnection cur in Connections.Values ) {
await cur.Send(data);
-2
View File
@@ -26,7 +26,6 @@ namespace Mistox.Server {
_client.NoDelay = true;
_client.ReceiveTimeout = _parent.ConnectionTimeoutSeconds * 1000;
_parent.InvokeUserConnected( this );
// Start the receive thread
Task.Run(ReceiveThread);
}
@@ -93,7 +92,6 @@ namespace Mistox.Server {
_sendLock.Dispose();
_cts.Dispose();
_parent.Connections.TryRemove(UserName, out _);
_parent.InvokeUserDisconnected(this);
// Remove this item from the Dictionary on the Server for garbage collection
}