Created Manager Initializer & Started on Packet Serializing/Deserializing

Manager Initializer initializes all managers found inheriting the IManager interface
Packet Manager
- Created LoadPacketInfo which gets PropertyInfo using reflection, and caches the returned information
- Started on Send<Packet>(Packet)
- Started on serializing packets to stream
- Started on packet deserializer
Created PacketIgnore attribute to ignore properties in a packet when serializing
This commit is contained in:
2021-12-06 15:18:29 +10:00
parent 2e9a712e71
commit 87a569ee9e
5 changed files with 118 additions and 5 deletions

View File

@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\PacketIgnore.cs" />
<Compile Include="Client.cs" />
<Compile Include="Interfaces\IManager.cs" />
<Compile Include="Interfaces\IPacket.cs" />

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Articulate_Network.Attributes
{
public class PacketIgnore : System.Attribute
{
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Articulate_Network.Interfaces
{
interface IManager
public interface IManager
{
void Initialize();
}

View File

@ -1,6 +1,8 @@
using Articulate_Network.Interfaces;
using Articulate_Network.Attributes;
using Articulate_Network.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
@ -10,11 +12,75 @@ namespace Articulate_Network.Managers
{
public class PacketManager : IManager
{
List<IPacket> packets = new List<IPacket>();
Dictionary<Type, List<PropertyInfo>> packetInfo = new Dictionary<Type, List<PropertyInfo>>();
public void Initialize()
{
Console.WriteLine(Assembly.GetEntryAssembly());
}
public void LoadPacketInfo(IPacket packet)
{
packetInfo.Add(packet.GetType(), packet.GetType().GetProperties().Where(f => !Attribute.IsDefined(f, typeof(PacketIgnore))).OrderBy(f => f.Name).ToList());
}
public T Send<T>(IPacket packet)
{
if (!packetInfo.ContainsKey(packet.GetType()))
LoadPacketInfo(packet);
//send packet
var serialized = SerializePacket(packet);
var deserialized = DeserializePacket<T>(serialized);
return (T)deserialized;
}
public MemoryStream SerializePacket(IPacket packet)
{
var fields = packetInfo[packet.GetType()];
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
foreach (var field in fields)
{
if (field.PropertyType == typeof(bool))
binaryWriter.Write((bool)field.GetValue(packet));
if (field.PropertyType == typeof(int))
binaryWriter.Write((int)field.GetValue(packet));
if (field.PropertyType == typeof(string))
binaryWriter.Write((string)field.GetValue(packet));
}
return memoryStream;
}
public T DeserializePacket<T>(Stream stream)
{
IPacket packet = (IPacket)Activator.CreateInstance(typeof(T));
var fields = packetInfo[typeof(T)];
BinaryReader reader = new BinaryReader(stream);
foreach (var field in fields)
{
if (field.PropertyType == typeof(bool))
field.SetValue(packet, reader.ReadBoolean());
if (field.PropertyType == typeof(int))
field.SetValue(packet, reader.ReadInt32());
if (field.PropertyType == typeof(string))
field.SetValue(packet, reader.ReadString());
}
return (T)packet;
}
}
}

View File

@ -1,9 +1,11 @@
using Articulate_Network.Interfaces;
using Articulate_Network.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -15,11 +17,18 @@ namespace Articulate_Network
TcpListener tcp;
public Server()
{
InitializeManagers();
Console.WriteLine($"Initialized {managers.Count} manager{(managers.Count > 1 ? "s" : "")}");
}
public async Task<bool> Start(string ip, int port)
{
tcp = new TcpListener(IPAddress.Parse(ip), port);
bool connected = await Task.Run(() =>
bool connected = await Task.Run(async () =>
{
try
{
@ -42,5 +51,29 @@ namespace Articulate_Network
{
return false;
}
public void InitializeManagers()
{
var @interface = typeof(IManager);
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var managersFound = assembly.GetTypes().Where(t => @interface.IsAssignableFrom(t) && !t.IsAbstract);
foreach (var m in managersFound)
{
var manager = (IManager)Activator.CreateInstance(m);
manager.Initialize();
managers.Add(manager);
}
}
}
public T Get<T>()
{
return (T)managers.SingleOrDefault(t => typeof(T) == t.GetType());
}
}
}