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 ServerName { get; private set; } = ""; // The Name of the server
|
||||||
public string UserName { get; private set; } = ""; // My UserName
|
public string UserName { get; private set; } = ""; // My UserName
|
||||||
public bool Alive { get; set; } = true; // Is the server running
|
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 ConcurrentDictionary<IPEndPoint, mUDPClient> clientConnections { get; private set; } // UserName, Connection
|
||||||
|
|
||||||
public mTCPClient ServerConnection {get; private set; } // Global TCP Server 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
|
// Receive Methods
|
||||||
/////////////////////////////
|
/////////////////////////////
|
||||||
@@ -91,6 +98,13 @@ namespace Mistox.Client {
|
|||||||
case UserDisconnectedPacket packet:
|
case UserDisconnectedPacket packet:
|
||||||
UserDisconnected(packet);
|
UserDisconnected(packet);
|
||||||
break;
|
break;
|
||||||
|
case UserAuthPacket packet:
|
||||||
|
Task.Run(async () => {
|
||||||
|
try {
|
||||||
|
await CheckAuthStatus(packet);
|
||||||
|
} catch {}
|
||||||
|
});
|
||||||
|
break;
|
||||||
case null:
|
case null:
|
||||||
Console.WriteLine("Received a null packet.");
|
Console.WriteLine("Received a null packet.");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using System;
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Mistox.ResevedPackets;
|
||||||
|
|
||||||
namespace Mistox.Server {
|
namespace Mistox.Server {
|
||||||
|
|
||||||
@@ -12,6 +13,7 @@ namespace Mistox.Server {
|
|||||||
public string ServerName { get; private set; } = ""; // The name of the server
|
public string ServerName { get; private set; } = ""; // The name of the server
|
||||||
public int MaxClients { get; set; } = 10; // The max players per server
|
public int MaxClients { get; set; } = 10; // The max players per server
|
||||||
public float ConnectionRange { get; set; } = 40; // InGame distance for valid conections
|
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 int ConnectionTimeoutSeconds { get; set; } = 10; // If no reply in time kill the connection
|
||||||
public bool Alive { get; set; } = true; // Is the server running
|
public bool Alive { get; set; } = true; // Is the server running
|
||||||
public ConcurrentDictionary<string, ServerConnection> Connections; // All the active connections. key is username
|
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 );
|
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
|
// Functions
|
||||||
|
|
||||||
public event Action<ServerConnection> onUserConnected;
|
public event Action<ServerConnection> onUserConnected;
|
||||||
@@ -34,7 +45,21 @@ namespace Mistox.Server {
|
|||||||
|
|
||||||
public event Action<dynamic> onDataReceived;
|
public event Action<dynamic> onDataReceived;
|
||||||
public void InvokeDataReceived( dynamic data ){
|
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);
|
onDataReceived?.Invoke(data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action<ServerConnection> onUserDisconnected;
|
public event Action<ServerConnection> onUserDisconnected;
|
||||||
|
|||||||
@@ -13,8 +13,11 @@ namespace Mistox.ResevedPackets {
|
|||||||
|
|
||||||
[mPacket]
|
[mPacket]
|
||||||
public class UserAuthPacket {
|
public class UserAuthPacket {
|
||||||
|
// Sent to the server
|
||||||
public string UserName { get; set; } = "";
|
public string UserName { get; set; } = "";
|
||||||
public string ConnectionKey { 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
|
public int AuthSuccessful { get; set; } = 0; // 0 is standard, -1 is auth failure, 1 is auth success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user