Build heartbeat chain
This commit is contained in:
1 parent
909718d8d5
commit
f946ebef1e
5 files changed
+173
-122
No files matched your search
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user