Skip to content

Commit

Permalink
Changes to ConnectionPool.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vect0rZ committed Nov 27, 2018
1 parent 8f9be1a commit 818e021
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
17 changes: 13 additions & 4 deletions QuicNet.Infrastructure/Connections/ConnectionPool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using QuicNet.Infrastructure.Settings;
using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -8,19 +9,27 @@ 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".
/// </summary>
public class ConnectionPool
public static class ConnectionPool
{
/// <summary>
/// Starting point for connection identifiers.
/// ConnectionId's are incremented sequentially by 1.
/// </summary>
private static UInt32 _connectionIdIterator = 0;

private Dictionary<UInt32, QuicConnection> _pool = new Dictionary<UInt32, QuicConnection>();
private static Dictionary<UInt32, QuicConnection> _pool = new Dictionary<UInt32, QuicConnection>();

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;
}
}
}
2 changes: 1 addition & 1 deletion QuicNet.Infrastructure/Packets/Unpacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 29 additions & 4 deletions QuicNet/QuicListener.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,19 +16,43 @@ public class QuicListener
private UdpClient _client;
private IPEndPoint _endPoint;

private Unpacker _packetDispatcher;

public event Action<QuicConnection> 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;
}
}
}

0 comments on commit 818e021

Please sign in to comment.