Cleanup async and respond with user list
This commit is contained in:
1 parent
41805d7de9
commit
258b019f1a
1 file changed
+31
-10
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Mistox.ResevedPackets;
|
||||
@@ -27,7 +28,6 @@ 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) {
|
||||
@@ -39,7 +39,27 @@ namespace Mistox.Server {
|
||||
} else {
|
||||
packet.AuthSuccessful = -1;
|
||||
}
|
||||
await SendClient(packet.UserName, packet);
|
||||
await user.Send(packet);
|
||||
}
|
||||
|
||||
async Task BuildServerList(ServerConnection user, HeartbeatPacket packet) {
|
||||
packet.ServerList = new List<UserTableObject>();
|
||||
foreach( ServerConnection cur in Connections.Values) {
|
||||
packet.ServerList.Add( new UserTableObject() {
|
||||
UserName = cur.UserName,
|
||||
Position = cur.Position,
|
||||
IpAddress = cur.getRemote()
|
||||
} );
|
||||
}
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
// Functions
|
||||
@@ -49,17 +69,18 @@ namespace Mistox.Server {
|
||||
onUserConnected?.Invoke( user );
|
||||
}
|
||||
|
||||
public event Action<ServerConnection, dynamic> onDataReceived;
|
||||
public event Action<ServerConnection, object> onDataReceived;
|
||||
public void InvokeDataReceived( ServerConnection user, dynamic data ){
|
||||
switch (data as object) {
|
||||
case UserAuthPacket packet:
|
||||
if (!user.IsAuthenticated) {
|
||||
Task.Run(async () => {
|
||||
try {
|
||||
switch (data) {
|
||||
case UserAuthPacket packet when !user.IsAuthenticated:
|
||||
_ = RunAsync(async () => {
|
||||
await ProcessAuthRequest(user, packet);
|
||||
} catch {}
|
||||
});
|
||||
}
|
||||
break;
|
||||
case HeartbeatPacket packet when user.IsAuthenticated:
|
||||
_ = RunAsync(async () => {
|
||||
await BuildServerList(user, packet);
|
||||
});
|
||||
break;
|
||||
case null:
|
||||
Console.WriteLine("Received a null packet.");
|
||||
|
||||
Reference in new issue
Block a user