diff --git a/QuicNet.Infrastructure/Connections/ConnectionPool.cs b/QuicNet.Infrastructure/Connections/ConnectionPool.cs index e7c5580..5b6ba20 100644 --- a/QuicNet.Infrastructure/Connections/ConnectionPool.cs +++ b/QuicNet.Infrastructure/Connections/ConnectionPool.cs @@ -1,4 +1,5 @@ -using System; +using QuicNet.Infrastructure.Settings; +using System; using System.Collections.Generic; using System.Text; @@ -8,7 +9,7 @@ namespace QuicNet.Infrastructure.Connections /// Since UDP is a stateless protocol, the ConnectionPool is used as a Conenction Manager to /// route packets to the right "Connection". /// - public class ConnectionPool + public static class ConnectionPool { /// /// Starting point for connection identifiers. @@ -16,11 +17,19 @@ public class ConnectionPool /// private static UInt32 _connectionIdIterator = 0; - private Dictionary _pool = new Dictionary(); + private static Dictionary _pool = new Dictionary(); - public ConnectionPool() + public static bool AddConnection(UInt32 id) { + if (_pool.ContainsKey(id)) + return false; + if (_pool.Count > QuicSettings.MaximumConnectionIds) + return false; + + _pool.Add(id, new QuicConnection(id)); + + return true; } } } diff --git a/QuicNet.Infrastructure/Packets/Unpacker.cs b/QuicNet.Infrastructure/Packets/Unpacker.cs index 5b3e396..0ffcd6e 100644 --- a/QuicNet.Infrastructure/Packets/Unpacker.cs +++ b/QuicNet.Infrastructure/Packets/Unpacker.cs @@ -40,7 +40,7 @@ public Packet Unpack(byte[] data) return result; } - private PacketType GetPacketType(byte[] data) + public PacketType GetPacketType(byte[] data) { if (data == null || data.Length <= 0) return PacketType.BrokenPacket; diff --git a/QuicNet/QuicListener.cs b/QuicNet/QuicListener.cs index 0f20403..8668285 100644 --- a/QuicNet/QuicListener.cs +++ b/QuicNet/QuicListener.cs @@ -1,4 +1,5 @@ -using QuicNet.Infrastructure.Connections; +using QuicNet.Infrastructure; +using QuicNet.Infrastructure.Connections; using QuicNet.Infrastructure.Packets; using System; using System.Collections.Generic; @@ -15,19 +16,43 @@ public class QuicListener private UdpClient _client; private IPEndPoint _endPoint; - private Unpacker _packetDispatcher; - - public event Action OnClientConnected; + private Unpacker _unpacker; public QuicListener() { _endPoint = new IPEndPoint(IPAddress.Any, 11000); _client = new UdpClient(11000); + _unpacker = new Unpacker(); } public void Start() + { + + } + + public byte[] Receive() { byte[] data = _client.Receive(ref _endPoint); + + PacketType type = _unpacker.GetPacketType(data); + switch(type) + { + case PacketType.InitialPacket: + { + InitialPacket packet = new InitialPacket(); + packet.Decode(data); + + ConnectionPool.AddConnection(packet.SourceConnectionId); + break; + } + case PacketType.BrokenPacket: + { + // Discard packet + break; + } + } + + return data; } } }