Started on Events & Connection Handling
Events - Created OnClientConnected Connection Handling - Started on async client connection handling, firing event
This commit is contained in:
@@ -43,6 +43,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Attributes\PacketIgnore.cs" />
|
<Compile Include="Attributes\PacketIgnore.cs" />
|
||||||
<Compile Include="Client.cs" />
|
<Compile Include="Client.cs" />
|
||||||
|
<Compile Include="Events\ClientConnectedEventArgs.cs" />
|
||||||
<Compile Include="Interfaces\IManager.cs" />
|
<Compile Include="Interfaces\IManager.cs" />
|
||||||
<Compile Include="Interfaces\IPacket.cs" />
|
<Compile Include="Interfaces\IPacket.cs" />
|
||||||
<Compile Include="Managers\PacketManager.cs" />
|
<Compile Include="Managers\PacketManager.cs" />
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Articulate_Network.Events
|
||||||
|
{
|
||||||
|
public class ClientConnectedEventArgs
|
||||||
|
{
|
||||||
|
public TcpClient TcpClient { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using Articulate_Network.Interfaces;
|
using Articulate_Network.Interfaces;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -13,10 +14,26 @@ namespace Articulate_Network.Managers
|
|||||||
public class PacketManager : IManager
|
public class PacketManager : IManager
|
||||||
{
|
{
|
||||||
Dictionary<Type, List<PropertyInfo>> packetInfo = new Dictionary<Type, List<PropertyInfo>>();
|
Dictionary<Type, List<PropertyInfo>> packetInfo = new Dictionary<Type, List<PropertyInfo>>();
|
||||||
|
Dictionary<Type, IPacket> packetInstances = new Dictionary<Type, IPacket>();
|
||||||
|
|
||||||
public void Initialize()
|
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)
|
public void LoadPacketInfo(IPacket packet)
|
||||||
@@ -26,9 +43,15 @@ namespace Articulate_Network.Managers
|
|||||||
|
|
||||||
public T Send<T>(IPacket packet)
|
public T Send<T>(IPacket packet)
|
||||||
{
|
{
|
||||||
|
Stopwatch watch = new Stopwatch();
|
||||||
|
watch.Start();
|
||||||
|
|
||||||
if (!packetInfo.ContainsKey(packet.GetType()))
|
if (!packetInfo.ContainsKey(packet.GetType()))
|
||||||
LoadPacketInfo(packet);
|
LoadPacketInfo(packet);
|
||||||
|
|
||||||
|
watch.Stop();
|
||||||
|
|
||||||
|
Console.WriteLine($"Reflection time: {watch.ElapsedTicks / 10} microseconds");
|
||||||
|
|
||||||
//send packet
|
//send packet
|
||||||
|
|
||||||
@@ -38,9 +61,11 @@ namespace Articulate_Network.Managers
|
|||||||
|
|
||||||
return (T)deserialized;
|
return (T)deserialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MemoryStream SerializePacket(IPacket packet)
|
public MemoryStream SerializePacket(IPacket packet)
|
||||||
{
|
{
|
||||||
|
Stopwatch watch = new Stopwatch();
|
||||||
|
watch.Start();
|
||||||
var fields = packetInfo[packet.GetType()];
|
var fields = packetInfo[packet.GetType()];
|
||||||
|
|
||||||
MemoryStream memoryStream = new MemoryStream();
|
MemoryStream memoryStream = new MemoryStream();
|
||||||
@@ -58,12 +83,19 @@ namespace Articulate_Network.Managers
|
|||||||
binaryWriter.Write((string)field.GetValue(packet));
|
binaryWriter.Write((string)field.GetValue(packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memoryStream.Position = 0;
|
||||||
|
|
||||||
|
watch.Stop();
|
||||||
|
|
||||||
|
Console.WriteLine($"Serialize time: {watch.ElapsedTicks / 10} microseconds");
|
||||||
return memoryStream;
|
return memoryStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T DeserializePacket<T>(Stream stream)
|
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)];
|
var fields = packetInfo[typeof(T)];
|
||||||
BinaryReader reader = new BinaryReader(stream);
|
BinaryReader reader = new BinaryReader(stream);
|
||||||
@@ -80,6 +112,10 @@ namespace Articulate_Network.Managers
|
|||||||
field.SetValue(packet, reader.ReadString());
|
field.SetValue(packet, reader.ReadString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
watch.Stop();
|
||||||
|
|
||||||
|
Console.WriteLine($"Deserialize time: {watch.ElapsedTicks / 10} microseconds");
|
||||||
|
|
||||||
return (T)packet;
|
return (T)packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Articulate_Network.Interfaces;
|
using Articulate_Network.Events;
|
||||||
|
using Articulate_Network.Interfaces;
|
||||||
using Articulate_Network.Managers;
|
using Articulate_Network.Managers;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -14,8 +15,9 @@ namespace Articulate_Network
|
|||||||
public class Server
|
public class Server
|
||||||
{
|
{
|
||||||
List<IManager> managers = new List<IManager>();
|
List<IManager> managers = new List<IManager>();
|
||||||
|
List<TcpClient> clients = new List<TcpClient>();
|
||||||
|
|
||||||
TcpListener tcp;
|
TcpListener tcpListener;
|
||||||
|
|
||||||
public Server()
|
public Server()
|
||||||
{
|
{
|
||||||
@@ -26,13 +28,15 @@ namespace Articulate_Network
|
|||||||
|
|
||||||
public async Task<bool> Start(string ip, int port)
|
public async Task<bool> Start(string ip, int port)
|
||||||
{
|
{
|
||||||
tcp = new TcpListener(IPAddress.Parse(ip), port);
|
tcpListener = new TcpListener(IPAddress.Parse(ip), port);
|
||||||
|
|
||||||
bool connected = await Task.Run(async () =>
|
bool connected = await Task.Run(async () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
tcp.Start();
|
tcpListener.Start();
|
||||||
|
|
||||||
|
_ = HandleConnections();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -47,6 +51,20 @@ namespace Articulate_Network
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task HandleConnections()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var client = await tcpListener.AcceptTcpClientAsync();
|
||||||
|
|
||||||
|
clients.Add(client);
|
||||||
|
|
||||||
|
OnClientConnected(new ClientConnectedEventArgs() { TcpClient = client });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public async Task<bool> SendPacket(IPacket packet)
|
public async Task<bool> SendPacket(IPacket packet)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -75,5 +93,15 @@ namespace Articulate_Network
|
|||||||
{
|
{
|
||||||
return (T)managers.SingleOrDefault(t => typeof(T) == t.GetType());
|
return (T)managers.SingleOrDefault(t => typeof(T) == t.GetType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void OnClientConnected(ClientConnectedEventArgs e)
|
||||||
|
{
|
||||||
|
EventHandler<ClientConnectedEventArgs> handler = ClientConnected;
|
||||||
|
|
||||||
|
if (handler != null)
|
||||||
|
handler(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public event EventHandler<ClientConnectedEventArgs> ClientConnected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user