Setup user auth packets

This commit is contained in:
derek.holloway committed 2026-08-03 13:43:44 -07:00
1 parent 827b1285a9
commit a78c040b09
3 files changed
+45 -3

No files matched your search

+27 -2
View File
@@ -2,6 +2,7 @@ using System;
using System.Collections.Concurrent;
using System.Net;
using System.Threading.Tasks;
using Mistox.ResevedPackets;
namespace Mistox.Server {
@@ -12,6 +13,7 @@ namespace Mistox.Server {
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
public string ConnectionKey { get; set; } = ""; // The connection key for the server
public int ConnectionTimeoutSeconds { get; set; } = 10; // If no reply in time kill the connection
public bool Alive { get; set; } = true; // Is the server running
public ConcurrentDictionary<string, ServerConnection> Connections; // All the active connections. key is username
@@ -25,7 +27,16 @@ namespace Mistox.Server {
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port );
}
// Functions
async Task ProcessAuthRequest(UserAuthPacket packet) {
// Need to set the connection userName via this packet
string user = packet.UserName;
packet.ServerName = ServerName;
packet.AuthSuccessful = packet.ConnectionKey == ConnectionKey ? 1 : 0;
await SendClient(packet.UserName, packet);
}
// Functions
public event Action<ServerConnection> onUserConnected;
public void InvokeUserConnected( ServerConnection user ){
@@ -34,7 +45,21 @@ namespace Mistox.Server {
public event Action<dynamic> onDataReceived;
public void InvokeDataReceived( dynamic data ){
onDataReceived?.Invoke( data );
switch (data as object) {
case UserAuthPacket packet:
Task.Run(async () => {
try {
await ProcessAuthRequest(packet);
} catch {}
});
break;
case null:
Console.WriteLine("Received a null packet.");
break;
default:
onDataReceived?.Invoke(data);
break;
}
}
public event Action<ServerConnection> onUserDisconnected;