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;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Mistox.ResevedPackets;
|
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 );
|
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async Task ProcessAuthRequest(ServerConnection user, UserAuthPacket packet) {
|
async Task ProcessAuthRequest(ServerConnection user, UserAuthPacket packet) {
|
||||||
packet.ServerName = ServerName;
|
packet.ServerName = ServerName;
|
||||||
if (packet.ConnectionKey == ConnectionKey) {
|
if (packet.ConnectionKey == ConnectionKey) {
|
||||||
@@ -39,7 +39,27 @@ namespace Mistox.Server {
|
|||||||
} else {
|
} else {
|
||||||
packet.AuthSuccessful = -1;
|
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
|
// Functions
|
||||||
@@ -49,17 +69,18 @@ namespace Mistox.Server {
|
|||||||
onUserConnected?.Invoke( user );
|
onUserConnected?.Invoke( user );
|
||||||
}
|
}
|
||||||
|
|
||||||
public event Action<ServerConnection, dynamic> onDataReceived;
|
public event Action<ServerConnection, object> onDataReceived;
|
||||||
public void InvokeDataReceived( ServerConnection user, dynamic data ){
|
public void InvokeDataReceived( ServerConnection user, dynamic data ){
|
||||||
switch (data as object) {
|
switch (data) {
|
||||||
case UserAuthPacket packet:
|
case UserAuthPacket packet when !user.IsAuthenticated:
|
||||||
if (!user.IsAuthenticated) {
|
_ = RunAsync(async () => {
|
||||||
Task.Run(async () => {
|
|
||||||
try {
|
|
||||||
await ProcessAuthRequest(user, packet);
|
await ProcessAuthRequest(user, packet);
|
||||||
} catch {}
|
|
||||||
});
|
});
|
||||||
}
|
break;
|
||||||
|
case HeartbeatPacket packet when user.IsAuthenticated:
|
||||||
|
_ = RunAsync(async () => {
|
||||||
|
await BuildServerList(user, packet);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case null:
|
case null:
|
||||||
Console.WriteLine("Received a null packet.");
|
Console.WriteLine("Received a null packet.");
|
||||||
|
|||||||
Reference in new issue
Block a user