Skip to content

Commit

Permalink
Merge pull request #5 from Vect0rZ/new-api
Browse files Browse the repository at this point in the history
Refactor and resolve #4
  • Loading branch information
Vect0rZ authored Jan 6, 2019
2 parents 1409322 + 772d936 commit dc55618
Show file tree
Hide file tree
Showing 17 changed files with 423 additions and 233 deletions.
2 changes: 1 addition & 1 deletion QuicNet.Infrastructure/Frames/StreamFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override void Decode(ByteArray array)
EndOfStream = true;

StreamData = array.ReadBytes((int)Length.Value);
ConvertedStreamId = QuickNet.Utilities.StreamId.Decode(ByteUtilities.GetBytes(Length.Value));
ConvertedStreamId = QuickNet.Utilities.StreamId.Decode(ByteUtilities.GetBytes(StreamId.Value));
}

public override byte[] Encode()
Expand Down
19 changes: 15 additions & 4 deletions QuicNet.Tests.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using QuicNet.Connections;
using QuicNet.Context;
using QuicNet.Streams;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,12 +13,21 @@ class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting client.");
QuicClient client = new QuicClient();
QuicContext context = client.Connect("127.0.0.1", 11000); // Connect to peer (Server)
QuicStreamContext sc = client.CreateStream(); // Create a data stream
sc.Send(Encoding.UTF8.GetBytes("Hello from Client!")); // Send Data
Console.WriteLine("Connecting to server.");
QuicConnection connection = client.Connect("127.0.0.1", 11000); // Connect to peer (Server)
Console.WriteLine("Connected");

QuicStream stream = connection.CreateStream(QuickNet.Utilities.StreamType.ClientUnidirectional); // Create a data stream
Console.WriteLine("Create stream with id: " + stream.StreamId.IntegerValue.ToString());

sc.Close(); // Close the stream after processing
Console.WriteLine("Send 'Hello From Client!'");
stream.Send(Encoding.UTF8.GetBytes("Hello from Client!")); // Send Data
Console.WriteLine("Waiting for message from the server");
// byte[] data = stream.Receive(); // Receive from server
// Console.WriteLine("Received: " + Encoding.UTF8.GetString(data));
Console.ReadKey();
}
}
}
20 changes: 6 additions & 14 deletions QuicNet/Connections/ConnectionPool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using QuicNet.Context;
using QuicNet.Infrastructure;
using QuicNet.Infrastructure.Settings;
using QuicNet.InternalInfrastructure;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -11,7 +12,7 @@ namespace QuicNet.Connections
/// Since UDP is a stateless protocol, the ConnectionPool is used as a Conenction Manager to
/// route packets to the right "Connection".
/// </summary>
public static class ConnectionPool
internal static class ConnectionPool
{
/// <summary>
/// Starting point for connection identifiers.
Expand All @@ -30,19 +31,20 @@ public static class ConnectionPool
/// </summary>
/// <param name="id">Connection Id</param>
/// <returns></returns>
public static bool AddConnection(UInt32 id, out UInt32 availableConnectionId)
public static bool AddConnection(ConnectionData connection, out UInt32 availableConnectionId)
{
availableConnectionId = 0;

if (_pool.ContainsKey(id))
if (_pool.ContainsKey(connection.ConnectionId))
return false;

if (_pool.Count > QuicSettings.MaximumConnectionIds)
return false;

availableConnectionId = _ns.Get();

_pool.Add(availableConnectionId, new QuicConnection(availableConnectionId, availableConnectionId));
connection.PeerConnectionId = connection.ConnectionId;
_pool.Add(availableConnectionId, new QuicConnection(connection));

return true;
}
Expand All @@ -54,15 +56,5 @@ public static QuicConnection Find(UInt32 id)

return _pool[id];
}

public static bool AttachContext(UInt32 id, QuicContext context)
{
if (_pool.ContainsKey(id) == false)
return false;

_pool[id].AttachContext(context);

return true;
}
}
}
116 changes: 93 additions & 23 deletions QuicNet/Connections/QuicConnection.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
using QuicNet.Context;
using QuickNet.Utilities;
using QuicNet.Context;
using QuicNet.Infrastructure.Frames;
using QuicNet.Infrastructure.PacketProcessing;
using QuicNet.Infrastructure.Packets;
using QuicNet.Infrastructure.Settings;
using QuicNet.InternalInfrastructure;
using QuicNet.Streams;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace QuicNet.Connections
{
public class QuicConnection
{
private UInt64 _currentTransferRate;
private ConnectionState _state;
private Dictionary<UInt64, QuicStream> _streams;

private PacketWireTransfer _pwt;

public UInt32 ConnectionId { get; private set; }
public UInt32 PeerConnectionId { get; private set; }
public QuicContext Context { get; private set; }

public PacketCreator PacketCreator { get; private set; }
public UInt64 MaxData { get; private set; }
public UInt64 MaxStreams { get; private set; }

private UInt64 _currentTransferRate;
private ConnectionState _state;
private Dictionary<UInt64, QuicStream> _streams;

public QuicConnection(UInt32 id, UInt32 peerConnectionId)
{
_currentTransferRate = 0;
_state = ConnectionState.Open;
_streams = new Dictionary<UInt64, QuicStream>();

ConnectionId = id;
PeerConnectionId = peerConnectionId;
// Also creates a new number space
PacketCreator = new PacketCreator(ConnectionId, PeerConnectionId);
MaxData = QuicSettings.MaxData;
MaxStreams = QuicSettings.MaximumStreamId;
}
public event Action<QuicStream> OnDataReceived;

public void AttachContext(QuicContext context)
public QuicStream CreateStream(StreamType type)
{
Context = context;
Context.Connection = this;
QuicStream stream = new QuicStream(this, new QuickNet.Utilities.StreamId(0, type));
_streams.Add(0, stream);

return stream;
}

public void ProcessFrames(List<Frame> frames)
Expand Down Expand Up @@ -127,6 +123,48 @@ private void OnMaxStreamFrame(Frame frame)
MaxStreams = msf.MaximumStreams.Value;
}

#region Internal

internal QuicConnection(ConnectionData connection)
{
_currentTransferRate = 0;
_state = ConnectionState.Open;
_streams = new Dictionary<UInt64, QuicStream>();
_pwt = connection.PWT;

ConnectionId = connection.ConnectionId;
PeerConnectionId = connection.PeerConnectionId;
// Also creates a new number space
PacketCreator = new PacketCreator(ConnectionId, PeerConnectionId);
MaxData = QuicSettings.MaxData;
MaxStreams = QuicSettings.MaximumStreamId;
}

/// <summary>
/// Client only!
/// </summary>
/// <returns></returns>
internal void ReceivePacket()
{
Packet packet = _pwt.ReadPacket();

if (packet is ShortHeaderPacket)
{
ShortHeaderPacket shp = (ShortHeaderPacket)packet;
ProcessFrames(shp.GetFrames());
}
}

internal bool SendData(Packet packet)
{
return _pwt.SendPacket(packet);
}

internal void DataReceived(QuicStream context)
{
OnDataReceived?.Invoke(context);
}

internal void TerminateConnection()
{
_state = ConnectionState.Draining;
Expand All @@ -135,7 +173,39 @@ internal void TerminateConnection()
internal void SendMaximumStreamReachedError()
{
ShortHeaderPacket packet = PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.STREAM_LIMIT_ERROR, "Maximum number of streams reached.");
Context.Send(packet);
Send(packet);
}

/// <summary>
/// Used to send protocol packets to the peer.
/// </summary>
/// <param name="packet"></param>
/// <returns></returns>
internal bool Send(Packet packet)
{
// Encode the packet
byte[] data = packet.Encode();

// Increment the connection transfer rate
IncrementRate(data.Length);

// If the maximum transfer rate is reached, send FLOW_CONTROL_ERROR
if (MaximumReached())
{
packet = PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, "Maximum data transfer reached.");

TerminateConnection();
}

// Ignore empty packets
if (data == null || data.Length <= 0)
return true;

bool result = _pwt.SendPacket(packet);

return result;
}

#endregion
}
}
83 changes: 0 additions & 83 deletions QuicNet/Context/QuicContext.cs

This file was deleted.

13 changes: 7 additions & 6 deletions QuicNet/Context/QuicStreamContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class QuicStreamContext
/// <summary>
/// The connection's context.
/// </summary>
public QuicContext ConnectionContext { get; set; }
// public QuicContext ConnectionContext { get; set; }

/// <summary>
/// Data received
Expand All @@ -43,11 +43,13 @@ public bool Send(byte[] data)
if (data == null || data.Length <= 0)
return true;

Packet packet = ConnectionContext.Connection.PacketCreator.CreateDataPacket(StreamId, data);
// Packet packet = ConnectionContext.Connection.PacketCreator.CreateDataPacket(StreamId, data);

bool result = ConnectionContext.Send(packet);
// bool result = ConnectionContext.Send(packet);

return result;
//return result;

return false;
}

public void Close()
Expand All @@ -65,11 +67,10 @@ public void Close()
/// </summary>
/// <param name="data"></param>
/// <param name="streamId"></param>
internal QuicStreamContext(QuicStream stream, QuicContext context)
internal QuicStreamContext(QuicStream stream)
{
Stream = stream;
StreamId = stream.StreamId;
ConnectionContext = context;
}

internal void SetData(byte[] data)
Expand Down
4 changes: 1 addition & 3 deletions QuicNet/Exceptions/QuicListenerNotStartedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ namespace QuicNet.Exceptions
{
public class QuicListenerNotStartedException : Exception
{
public QuicListenerNotStartedException()
{
}
public QuicListenerNotStartedException() { }

public QuicListenerNotStartedException(string message) : base(message)
{
Expand Down
Loading

0 comments on commit dc55618

Please sign in to comment.