Implement base level packet interface
This commit is contained in:
parent
9e448f8aea
commit
efda5ec265
4 changed files with 65 additions and 0 deletions
7
Server.Packets/IPacket.cs
Normal file
7
Server.Packets/IPacket.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Server.Packets;
|
||||
|
||||
public interface IPacket
|
||||
{
|
||||
Span<Byte> Serialize();
|
||||
void Deserialize(Span<Byte> bytes);
|
||||
}
|
40
Server.Packets/PacketForwardingService.cs
Normal file
40
Server.Packets/PacketForwardingService.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Server.Packets;
|
||||
|
||||
public class PacketForwardingService
|
||||
{
|
||||
private ImmutableDictionary<PacketId, System.Reflection.ConstructorInfo> _packets = null!;
|
||||
|
||||
public PacketForwardingService()
|
||||
{
|
||||
InitializePackets(typeof(IPacket).Assembly);
|
||||
}
|
||||
|
||||
private void InitializePackets(Assembly assembly)
|
||||
{
|
||||
// Get all types that implement IPacket and have a PacketIdAttribute
|
||||
_packets = assembly.GetTypes()
|
||||
.Select(x => (attr: x.GetCustomAttribute<PacketIdAttribute>(), ctor: x.GetConstructor(Type.EmptyTypes)))
|
||||
.Where(x => x.attr != null && x.ctor != null && x.GetType().IsAssignableFrom(typeof(IPacket)))
|
||||
.ToImmutableDictionary(x => x.attr!.PacketId, x => x.ctor)!;
|
||||
}
|
||||
|
||||
public IPacket Dispatch(Span<Byte> bytes)
|
||||
{
|
||||
// Get constructor according to packet id
|
||||
IPacket packet = _packets[GetPacketId(bytes)].Invoke(Array.Empty<object>()) as IPacket ??
|
||||
throw new InvalidOperationException();
|
||||
// Slice off packet id
|
||||
packet.Deserialize(bytes.Slice(2));
|
||||
return packet;
|
||||
}
|
||||
|
||||
private PacketId GetPacketId(Span<Byte> bytes)
|
||||
{
|
||||
PacketId id = 0;
|
||||
id = (PacketId)BitConverter.ToUInt16(new[] { bytes[0], bytes[1] });
|
||||
return id;
|
||||
}
|
||||
}
|
6
Server.Packets/PacketId.cs
Normal file
6
Server.Packets/PacketId.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Server.Packets;
|
||||
|
||||
public enum PacketId : ushort
|
||||
{
|
||||
|
||||
}
|
12
Server.Packets/PacketIdAttribute.cs
Normal file
12
Server.Packets/PacketIdAttribute.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
namespace Server.Packets;
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||
[JetBrains.Annotations.BaseTypeRequiredAttribute(typeof(IPacket))]
|
||||
public class PacketIdAttribute : Attribute
|
||||
{
|
||||
public PacketIdAttribute(PacketId packetId)
|
||||
{
|
||||
PacketId = packetId;
|
||||
}
|
||||
|
||||
public PacketId PacketId { get; private set; }
|
||||
}
|
Loading…
Reference in a new issue