Skip to content

Commit 3e017d8

Browse files
committed
Added connection close error handling
1 parent 1823c2d commit 3e017d8

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

QuicNet.Tests.ConsoleClient/Program.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ static void Main(string[] args)
1919
QuicConnection connection = client.Connect("127.0.0.1", 11000); // Connect to peer (Server)
2020
Console.WriteLine("Connected");
2121

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

2525
Console.WriteLine("Send 'Hello From Client!'");
2626
stream.Send(Encoding.UTF8.GetBytes("Hello from Client!")); // Send Data
2727
Console.WriteLine("Waiting for message from the server");
28-
// byte[] data = stream.Receive(); // Receive from server
29-
// Console.WriteLine("Received: " + Encoding.UTF8.GetString(data));
28+
try
29+
{
30+
byte[] data = stream.Receive(); // Receive from server
31+
Console.WriteLine("Received: " + Encoding.UTF8.GetString(data));
32+
}
33+
catch (Exception e)
34+
{
35+
Console.WriteLine(e.Message);
36+
}
37+
3038
Console.ReadKey();
3139
}
3240
}

QuicNet/Connections/QuicConnection.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using QuickNet.Utilities;
22
using QuicNet.Context;
3+
using QuicNet.Exceptions;
34
using QuicNet.Infrastructure.Frames;
45
using QuicNet.Infrastructure.PacketProcessing;
56
using QuicNet.Infrastructure.Packets;
@@ -17,6 +18,7 @@ public class QuicConnection
1718
{
1819
private UInt64 _currentTransferRate;
1920
private ConnectionState _state;
21+
private string _lastError;
2022
private Dictionary<UInt64, QuicStream> _streams;
2123

2224
private PacketWireTransfer _pwt;
@@ -52,8 +54,6 @@ public void ProcessFrames(List<Frame> frames)
5254
{
5355
if (frame.Type == 0x01)
5456
OnRstStreamFrame(frame);
55-
if (frame.Type == 0x02)
56-
OnConnectionCloseFrame(frame);
5757
if (frame.Type == 0x04)
5858
OnRstStreamFrame(frame);
5959
if (frame.Type >= 0x08 && frame.Type <= 0x0f)
@@ -66,6 +66,8 @@ public void ProcessFrames(List<Frame> frames)
6666
OnMaxStreamFrame(frame);
6767
if (frame.Type == 0x14)
6868
OnDataBlockedFrame(frame);
69+
if (frame.Type == 0x1c)
70+
OnConnectionCloseFrame(frame);
6971
}
7072
}
7173

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

8587
private void OnConnectionCloseFrame(Frame frame)
8688
{
89+
ConnectionCloseFrame ccf = (ConnectionCloseFrame)frame;
8790
_state = ConnectionState.Draining;
91+
_lastError = ccf.ReasonPhrase;
8892
}
8993

9094
private void OnRstStreamFrame(Frame frame)
@@ -175,6 +179,7 @@ internal QuicConnection(ConnectionData connection)
175179
{
176180
_currentTransferRate = 0;
177181
_state = ConnectionState.Open;
182+
_lastError = string.Empty;
178183
_streams = new Dictionary<UInt64, QuicStream>();
179184
_pwt = connection.PWT;
180185

@@ -199,6 +204,18 @@ internal void ReceivePacket()
199204
ShortHeaderPacket shp = (ShortHeaderPacket)packet;
200205
ProcessFrames(shp.GetFrames());
201206
}
207+
208+
// If the connection has been closed
209+
if (_state == ConnectionState.Draining)
210+
{
211+
if (string.IsNullOrWhiteSpace(_lastError))
212+
_lastError = "Protocol error";
213+
214+
TerminateConnection();
215+
216+
throw new QuicConnectivityException(_lastError);
217+
}
218+
202219
}
203220

204221
internal bool SendData(Packet packet)

QuicNet/Streams/QuicStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void ProcessData(StreamFrame frame)
127127
// Terminate connection if maximum stream data is reached
128128
if (_currentTransferRate >= _maximumStreamData)
129129
{
130-
ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, "Maximum stream data reached.");
130+
ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, "Maximum stream data transfer reached.");
131131
_connection.SendData(errorPacket);
132132
_connection.TerminateConnection();
133133

0 commit comments

Comments
 (0)