feat(server): add packet handler registry and auth request handler
This commit is contained in:
parent
be00b3542e
commit
b0197489b1
16 changed files with 125 additions and 12 deletions
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Playvoi.Server.Shared.Attributes;
|
||||
using Playvoi.Server.Shared.Interface;
|
||||
|
||||
namespace Playvoi.Server.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddPacketHandlers(this IServiceCollection services)
|
||||
{
|
||||
var handlers = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.DefinedTypes)
|
||||
.Where(x => !x.IsAbstract && !x.IsInterface && !x.IsGenericTypeDefinition)
|
||||
.Where(x => typeof(IPacketHandler).IsAssignableFrom(x))
|
||||
.Select(t => (type: t, attr: t.GetCustomAttribute<HandlerRegisterAttribute>())).Where(x => x.attr != null);
|
||||
|
||||
foreach (var (type, attr) in handlers)
|
||||
{
|
||||
services.AddScoped(type);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Playvoi.Server.Extensions;
|
||||
using Playvoi.Server.Shared.Registry;
|
||||
|
||||
namespace Playvoi.Server.Infrastructure;
|
||||
|
|
@ -20,5 +21,7 @@ public static class Container
|
|||
services.AddLogging(c => c.AddSimpleConsole());
|
||||
services.AddSingleton<NetworkServer>();
|
||||
services.AddSingleton<PacketRegistry>();
|
||||
services.AddSingleton<HandlerRegistry>();
|
||||
services.AddPacketHandlers();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ using LiteNetLib;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Playvoi.Server.Shared;
|
||||
using Playvoi.Server.Shared.Interface;
|
||||
using Playvoi.Server.Shared.Registry;
|
||||
|
||||
namespace Playvoi.Server;
|
||||
|
|
@ -51,8 +52,8 @@ public class NetworkServer : INetEventListener
|
|||
{
|
||||
var packetType = (PacketType)reader.GetByte();
|
||||
var packet = ResolvePacket(packetType, reader);
|
||||
// var handler = ResolveHandler(packetType);
|
||||
// handler.Handle(packet, peer.Id);
|
||||
var handler = ResolveHandler(packetType);
|
||||
handler.Handle(packet, peer.Id);
|
||||
reader.Recycle();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -60,13 +61,6 @@ public class NetworkServer : INetEventListener
|
|||
_logger.LogError(ex, "Error processing message of XX");
|
||||
}
|
||||
}
|
||||
|
||||
// var data = Encoding.UTF8.GetString(reader.RawData);
|
||||
// Console.WriteLine($"Data received from client : {data}");
|
||||
//
|
||||
// var replay = "Ehm Replay";
|
||||
// var bytes = Encoding.UTF8.GetBytes(replay);
|
||||
// peer.Send(bytes, DeliveryMethod.ReliableOrdered);
|
||||
}
|
||||
|
||||
public void OnPeerConnected(NetPeer peer)
|
||||
|
|
@ -100,6 +94,13 @@ public class NetworkServer : INetEventListener
|
|||
{
|
||||
}
|
||||
|
||||
public IPacketHandler ResolveHandler(PacketType packetType)
|
||||
{
|
||||
var registry = _serviceProvider.GetRequiredService<HandlerRegistry>();
|
||||
var type = registry.Handlers[packetType];
|
||||
return (IPacketHandler)_serviceProvider.GetRequiredService(type);
|
||||
}
|
||||
|
||||
private INetPacket ResolvePacket(PacketType packetType, NetPacketReader reader)
|
||||
{
|
||||
var registery = _serviceProvider.GetRequiredService<PacketRegistry>();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
using _Network.Shared;
|
||||
using _Network.Shared.Interface;
|
||||
using Playvoi.Server.Shared.Attributes;
|
||||
using Playvoi.Server.Shared.Interface;
|
||||
|
||||
namespace Playvoi.Server.PacketHandlers;
|
||||
[HandlerRegister(PacketType.AuthRequest)]
|
||||
public class AuthRequestHandler : IPacketHandler
|
||||
{
|
||||
public void Handle(INetPacket packet, int connectionId)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using _Network.Shared;
|
||||
|
||||
namespace Playvoi.Server.Shared.Attributes;
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class HandlerRegisterAttribute : Attribute
|
||||
{
|
||||
public HandlerRegisterAttribute(PacketType type)
|
||||
{
|
||||
PacketType = type;
|
||||
}
|
||||
|
||||
public PacketType PacketType { get; set ; }
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
using _Network.Shared.Interface;
|
||||
|
||||
namespace Playvoi.Server.Shared.Interface;
|
||||
|
||||
public interface IPacketHandler
|
||||
{
|
||||
void Handle(INetPacket packet, int connectionId);
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using _Network.Shared;
|
||||
using Playvoi.Server.Shared.Attributes;
|
||||
using Playvoi.Server.Shared.Interface;
|
||||
|
||||
namespace Playvoi.Server.Shared.Registry;
|
||||
|
||||
public class HandlerRegistry
|
||||
{
|
||||
private Dictionary<PacketType, Type> _handlers = new Dictionary<PacketType, Type>();
|
||||
|
||||
public Dictionary<PacketType, Type> Handlers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_handlers.Count == 0)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
return _handlers;
|
||||
}
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
var handlers = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.DefinedTypes)
|
||||
.Where(x => !x.IsAbstract && !x.IsInterface && !x.IsGenericTypeDefinition)
|
||||
.Where(x => typeof(IPacketHandler).IsAssignableFrom(x))
|
||||
.Select(t => (type: t, attr: t.GetCustomAttribute<HandlerRegisterAttribute>())).Where(x => x.attr != null);
|
||||
;
|
||||
foreach (var (type, attr) in handlers)
|
||||
{
|
||||
if (!_handlers.ContainsKey(attr.PacketType))
|
||||
{
|
||||
_handlers[attr.PacketType] = type;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Multiple handlers for {attr.PacketType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -13,7 +13,7 @@ using System.Reflection;
|
|||
[assembly: System.Reflection.AssemblyCompanyAttribute("Playvoi.Server")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+3589ac80fa6d7b979ad9f34d2b1288abade43732")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+be00b3542e14d49b00f7373a13e89fcb423c17b8")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Playvoi.Server")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Playvoi.Server")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
4255e08552961883e2b970260df1fb32d18ac55b11416091dcdcb95773630632
|
||||
54014dd7ac4359d492a4b0ddbbccc8d1396884df8feab61e3beddc83d95508b9
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
3b032047bbbb0308a07922c6422b23b81574c160df35960901446a4c6151da34
|
||||
452610c42f1fac1e20e1510ff73f6e49a68de5b7968e9eef0a9411f71e75fe52
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue