Started on Events & Connection Handling
Events - Created OnClientConnected Connection Handling - Started on async client connection handling, firing event
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Articulate_Network.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -13,10 +14,26 @@ namespace Articulate_Network.Managers
|
||||
public class PacketManager : IManager
|
||||
{
|
||||
Dictionary<Type, List<PropertyInfo>> packetInfo = new Dictionary<Type, List<PropertyInfo>>();
|
||||
Dictionary<Type, IPacket> packetInstances = new Dictionary<Type, IPacket>();
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
||||
var @interface = typeof(IPacket);
|
||||
|
||||
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
var managersFound = assembly.GetTypes().Where(t => @interface.IsAssignableFrom(t) && !t.IsAbstract);
|
||||
|
||||
foreach (var p in managersFound)
|
||||
{
|
||||
var packet = (IPacket)Activator.CreateInstance(p);
|
||||
|
||||
packetInstances.Add(p, packet);
|
||||
LoadPacketInfo(packet);
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Registered {packetInstances.Count} packet{(packetInstances.Count == 1 ? "" : "s")}");
|
||||
}
|
||||
|
||||
public void LoadPacketInfo(IPacket packet)
|
||||
@@ -26,9 +43,15 @@ namespace Articulate_Network.Managers
|
||||
|
||||
public T Send<T>(IPacket packet)
|
||||
{
|
||||
Stopwatch watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
if (!packetInfo.ContainsKey(packet.GetType()))
|
||||
LoadPacketInfo(packet);
|
||||
|
||||
watch.Stop();
|
||||
|
||||
Console.WriteLine($"Reflection time: {watch.ElapsedTicks / 10} microseconds");
|
||||
|
||||
//send packet
|
||||
|
||||
@@ -38,9 +61,11 @@ namespace Articulate_Network.Managers
|
||||
|
||||
return (T)deserialized;
|
||||
}
|
||||
|
||||
|
||||
public MemoryStream SerializePacket(IPacket packet)
|
||||
{
|
||||
Stopwatch watch = new Stopwatch();
|
||||
watch.Start();
|
||||
var fields = packetInfo[packet.GetType()];
|
||||
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
@@ -58,12 +83,19 @@ namespace Articulate_Network.Managers
|
||||
binaryWriter.Write((string)field.GetValue(packet));
|
||||
}
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
watch.Stop();
|
||||
|
||||
Console.WriteLine($"Serialize time: {watch.ElapsedTicks / 10} microseconds");
|
||||
return memoryStream;
|
||||
}
|
||||
|
||||
public T DeserializePacket<T>(Stream stream)
|
||||
{
|
||||
IPacket packet = (IPacket)Activator.CreateInstance(typeof(T));
|
||||
Stopwatch watch = new Stopwatch();
|
||||
watch.Start();
|
||||
IPacket packet = /*packetInstances[typeof(T)];*/(IPacket)Activator.CreateInstance(typeof(T));
|
||||
|
||||
var fields = packetInfo[typeof(T)];
|
||||
BinaryReader reader = new BinaryReader(stream);
|
||||
@@ -80,6 +112,10 @@ namespace Articulate_Network.Managers
|
||||
field.SetValue(packet, reader.ReadString());
|
||||
}
|
||||
|
||||
watch.Stop();
|
||||
|
||||
Console.WriteLine($"Deserialize time: {watch.ElapsedTicks / 10} microseconds");
|
||||
|
||||
return (T)packet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user