Skip to content

Commit

Permalink
Added connection close error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Vect0rZ committed Jan 7, 2019
1 parent 1823c2d commit 3e017d8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
14 changes: 11 additions & 3 deletions QuicNet.Tests.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ static void Main(string[] args)
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
QuicStream stream = connection.CreateStream(QuickNet.Utilities.StreamType.ClientBidirectional); // Create a data stream
Console.WriteLine("Create stream with id: " + stream.StreamId.IntegerValue.ToString());

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));
try
{
byte[] data = stream.Receive(); // Receive from server
Console.WriteLine("Received: " + Encoding.UTF8.GetString(data));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadKey();
}
}
Expand Down
21 changes: 19 additions & 2 deletions QuicNet/Connections/QuicConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using QuickNet.Utilities;
using QuicNet.Context;
using QuicNet.Exceptions;
using QuicNet.Infrastructure.Frames;
using QuicNet.Infrastructure.PacketProcessing;
using QuicNet.Infrastructure.Packets;
Expand All @@ -17,6 +18,7 @@ public class QuicConnection
{
private UInt64 _currentTransferRate;
private ConnectionState _state;
private string _lastError;
private Dictionary<UInt64, QuicStream> _streams;

private PacketWireTransfer _pwt;
Expand Down Expand Up @@ -52,8 +54,6 @@ public void ProcessFrames(List<Frame> frames)
{
if (frame.Type == 0x01)
OnRstStreamFrame(frame);
if (frame.Type == 0x02)
OnConnectionCloseFrame(frame);
if (frame.Type == 0x04)
OnRstStreamFrame(frame);
if (frame.Type >= 0x08 && frame.Type <= 0x0f)
Expand All @@ -66,6 +66,8 @@ public void ProcessFrames(List<Frame> frames)
OnMaxStreamFrame(frame);
if (frame.Type == 0x14)
OnDataBlockedFrame(frame);
if (frame.Type == 0x1c)
OnConnectionCloseFrame(frame);
}
}

Expand All @@ -84,7 +86,9 @@ public bool MaximumReached()

private void OnConnectionCloseFrame(Frame frame)
{
ConnectionCloseFrame ccf = (ConnectionCloseFrame)frame;
_state = ConnectionState.Draining;
_lastError = ccf.ReasonPhrase;
}

private void OnRstStreamFrame(Frame frame)
Expand Down Expand Up @@ -175,6 +179,7 @@ internal QuicConnection(ConnectionData connection)
{
_currentTransferRate = 0;
_state = ConnectionState.Open;
_lastError = string.Empty;
_streams = new Dictionary<UInt64, QuicStream>();
_pwt = connection.PWT;

Expand All @@ -199,6 +204,18 @@ internal void ReceivePacket()
ShortHeaderPacket shp = (ShortHeaderPacket)packet;
ProcessFrames(shp.GetFrames());
}

// If the connection has been closed
if (_state == ConnectionState.Draining)
{
if (string.IsNullOrWhiteSpace(_lastError))
_lastError = "Protocol error";

TerminateConnection();

throw new QuicConnectivityException(_lastError);
}

}

internal bool SendData(Packet packet)
Expand Down
2 changes: 1 addition & 1 deletion QuicNet/Streams/QuicStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void ProcessData(StreamFrame frame)
// Terminate connection if maximum stream data is reached
if (_currentTransferRate >= _maximumStreamData)
{
ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, "Maximum stream data reached.");
ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, "Maximum stream data transfer reached.");
_connection.SendData(errorPacket);
_connection.TerminateConnection();

Expand Down

0 comments on commit 3e017d8

Please sign in to comment.