feat: change PacketHandler to conform to async
This commit is contained in:
parent
59770e3369
commit
dd855675f9
3 changed files with 11 additions and 8 deletions
|
@ -1,8 +1,9 @@
|
||||||
using Server.Packets;
|
using NetCoreServer;
|
||||||
|
using Server.Packets;
|
||||||
|
|
||||||
namespace Server.PacketHandlers;
|
namespace Server.PacketHandlers;
|
||||||
|
|
||||||
public interface IPacketHandler<in T> where T : IPacket
|
public interface IPacketHandler<in T> where T : IPacket
|
||||||
{
|
{
|
||||||
public void Handle(T packet);
|
public Task HandleAsync(T packet, TcpSession session);
|
||||||
}
|
}
|
|
@ -20,7 +20,7 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(LoginInfoPacket packet)
|
public async Task HandleAsync(LoginInfoPacket packet, TcpSession session)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Login data: Username {Username} & Password {Password}", packet.Username,
|
_logger.LogInformation("Login data: Username {Username} & Password {Password}", packet.Username,
|
||||||
packet.Password);
|
packet.Password);
|
||||||
|
@ -29,7 +29,8 @@ public class LoginHandler : IPacketHandler<LoginInfoPacket>
|
||||||
{
|
{
|
||||||
if (_configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin"))
|
if (_configuration.GetSection("Testing").GetValue<bool>("CreateAccountOnLogin"))
|
||||||
{
|
{
|
||||||
_wonderkingContext.Accounts.AddAsync(new Account(packet.Username, packet.Password, "", 0));
|
var result = _wonderkingContext.Accounts.AddAsync(new Account(packet.Username, packet.Password, "", 0));
|
||||||
|
await result;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -80,10 +80,10 @@ public class PacketDistributorService : IHostedService
|
||||||
_logger.LogInformation("Packet with ID: {MessageOperationCode} has been received",
|
_logger.LogInformation("Packet with ID: {MessageOperationCode} has been received",
|
||||||
rawPacket.OperationCode);
|
rawPacket.OperationCode);
|
||||||
_concurrentQueue.Enqueue(rawPacket);
|
_concurrentQueue.Enqueue(rawPacket);
|
||||||
Parallel.Invoke(DequeueAndProcessAsync);
|
Parallel.Invoke(() => DequeueAndProcessAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void DequeueAndProcessAsync()
|
private async Task DequeueAndProcessAsync()
|
||||||
{
|
{
|
||||||
if (_concurrentQueue.TryDequeue(out var item))
|
if (_concurrentQueue.TryDequeue(out var item))
|
||||||
{
|
{
|
||||||
|
@ -97,7 +97,8 @@ public class PacketDistributorService : IHostedService
|
||||||
var packetHandler =
|
var packetHandler =
|
||||||
ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider,
|
ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider,
|
||||||
_packetHandlers[item.OperationCode]);
|
_packetHandlers[item.OperationCode]);
|
||||||
packetHandler.GetType().GetMethod("Handle")?.Invoke(packetHandler, new[] { packet });
|
packetHandler.GetType().GetMethod("HandleAsync")
|
||||||
|
?.Invoke(packetHandler, new Object[] { packet, item.Session });
|
||||||
_logger.LogDebug("Packet data {PacketData}", JsonConvert.SerializeObject(packet));
|
_logger.LogDebug("Packet data {PacketData}", JsonConvert.SerializeObject(packet));
|
||||||
_logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} has finished",
|
_logger.LogTrace("[{TempId}] Packet with ID: {MessageOperationCode} has finished",
|
||||||
item.Session.Id,
|
item.Session.Id,
|
||||||
|
|
Loading…
Add table
Reference in a new issue