Started on Events & Connection Handling

Events
- Created OnClientConnected
Connection Handling
- Started on async client connection handling, firing event
This commit is contained in:
2021-12-13 15:43:28 +10:00
parent 87a569ee9e
commit fc1195c446
4 changed files with 87 additions and 7 deletions

View File

@ -43,6 +43,7 @@
<ItemGroup>
<Compile Include="Attributes\PacketIgnore.cs" />
<Compile Include="Client.cs" />
<Compile Include="Events\ClientConnectedEventArgs.cs" />
<Compile Include="Interfaces\IManager.cs" />
<Compile Include="Interfaces\IPacket.cs" />
<Compile Include="Managers\PacketManager.cs" />

View File

@ -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; }
}
}

View File

@ -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;
}
}

View File

@ -1,4 +1,5 @@
using Articulate_Network.Interfaces;
using Articulate_Network.Events;
using Articulate_Network.Interfaces;
using Articulate_Network.Managers;
using System;
using System.Collections.Generic;
@ -14,8 +15,9 @@ namespace Articulate_Network
public class Server
{
List<IManager> managers = new List<IManager>();
List<TcpClient> clients = new List<TcpClient>();
TcpListener tcp;
TcpListener tcpListener;
public Server()
{
@ -26,13 +28,15 @@ namespace Articulate_Network
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 () =>
{
try
{
tcp.Start();
tcpListener.Start();
_ = HandleConnections();
return true;
}
@ -47,6 +51,20 @@ namespace Articulate_Network
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)
{
return false;
@ -75,5 +93,15 @@ namespace Articulate_Network
{
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;
}
}