Setup user auth packets
This commit is contained in:
1 parent
827b1285a9
commit
a78c040b09
3 files changed
+43
-1
No files matched your search
@@ -12,7 +12,7 @@ namespace Mistox.Client {
|
||||
public string ServerName { get; private set; } = ""; // The Name of the server
|
||||
public string UserName { get; private set; } = ""; // My UserName
|
||||
public bool Alive { get; set; } = true; // Is the server running
|
||||
public bool isAuthenticated { get; set; } = false; // Is the server authenticated
|
||||
public bool isAuthenticated { get; private set; } = false; // Is the server authenticated
|
||||
public ConcurrentDictionary<IPEndPoint, mUDPClient> clientConnections { get; private set; } // UserName, Connection
|
||||
|
||||
public mTCPClient ServerConnection {get; private set; } // Global TCP Server Connection
|
||||
@@ -49,6 +49,13 @@ namespace Mistox.Client {
|
||||
});
|
||||
}
|
||||
|
||||
public async Task CheckAuthStatus(UserAuthPacket packet) {
|
||||
if (packet.AuthSuccessful == 1) {
|
||||
isAuthenticated = true;
|
||||
}
|
||||
ServerName = packet.ServerName;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Receive Methods
|
||||
/////////////////////////////
|
||||
@@ -91,6 +98,13 @@ namespace Mistox.Client {
|
||||
case UserDisconnectedPacket packet:
|
||||
UserDisconnected(packet);
|
||||
break;
|
||||
case UserAuthPacket packet:
|
||||
Task.Run(async () => {
|
||||
try {
|
||||
await CheckAuthStatus(packet);
|
||||
} catch {}
|
||||
});
|
||||
break;
|
||||
case null:
|
||||
Console.WriteLine("Received a null packet.");
|
||||
break;
|
||||
|
||||
@@ -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,6 +27,15 @@ namespace Mistox.Server {
|
||||
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port );
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
@@ -34,7 +45,21 @@ namespace Mistox.Server {
|
||||
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public void InvokeDataReceived( dynamic 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;
|
||||
|
||||
@@ -13,8 +13,11 @@ namespace Mistox.ResevedPackets {
|
||||
|
||||
[mPacket]
|
||||
public class UserAuthPacket {
|
||||
// Sent to the server
|
||||
public string UserName { get; set; } = "";
|
||||
public string ConnectionKey { get; set; } = "";
|
||||
// Response from the server
|
||||
public string ServerName { get; set; } = "";
|
||||
public int AuthSuccessful { get; set; } = 0; // 0 is standard, -1 is auth failure, 1 is auth success
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user