Skip to content

Commit 03c1f9b

Browse files
committed
QuicListener can now receive stream data.
1 parent 60197c0 commit 03c1f9b

7 files changed

Lines changed: 40 additions & 9 deletions

File tree

QuicNet.Infrastructure/FrameParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public Frame GetFrame()
2626
case 0x00: result = new PaddingFrame(); break;
2727
case 0x02: result = new ConnectionCloseFrame(); break;
2828
case 0x06: result = new MaxStreamIdFrame(); break;
29+
case 0x10: result = new StreamFrame(); break;
2930
default: result = null; break;
3031
}
3132

QuicNet.Infrastructure/Packets/ShortHeaderPacket.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public override void Decode(byte[] packet)
1515
{
1616
ByteArray array = new ByteArray(packet);
1717
byte type = array.ReadByte();
18+
DestinationConnectionId = array.ReadByte();
1819
PacketNumber = array.ReadUInt32();
1920

2021
DecodeFrames(array);
@@ -26,6 +27,7 @@ public override byte[] Encode()
2627

2728
List<byte> result = new List<byte>();
2829
result.Add(Type);
30+
result.Add(DestinationConnectionId);
2931
result.AddRange(ByteUtilities.GetBytes(PacketNumber));
3032
result.AddRange(frames);
3133

QuicNet/Connections/QuicConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class QuicConnection
1212
{
1313
private ConnectionState _state;
1414
private Dictionary<UInt64, QuicStream> _streams;
15-
private QuicContext _context;
15+
public QuicContext Context { get; private set; }
1616

1717
public QuicConnection(UInt32 id)
1818
{
@@ -22,7 +22,7 @@ public QuicConnection(UInt32 id)
2222

2323
public void AttachContext(QuicContext context)
2424
{
25-
_context = context;
25+
Context = context;
2626
}
2727

2828
public void ProcessFrames(List<Frame> frames)

QuicNet/Context/QuicContext.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using QuicNet.Infrastructure.Packets;
1+
using QuickNet.Utilities;
2+
using QuicNet.Infrastructure.Packets;
3+
using QuicNet.Streams;
24
using System;
35
using System.Collections.Generic;
46
using System.Linq;
@@ -18,6 +20,7 @@ public class QuicContext
1820

1921
public IPEndPoint Endpoint { get; }
2022
public bool IsClosed { get; private set; }
23+
public event Action<byte[]> OnDataReceived;
2124

2225
/// <summary>
2326
/// Internal constructor to prevent createing the context outside the scope of Quic.
@@ -30,6 +33,11 @@ internal QuicContext(UdpClient client, IPEndPoint endpoint)
3033
Endpoint = endpoint;
3134
}
3235

36+
internal void DataReceived(byte[] data, StreamId id)
37+
{
38+
OnDataReceived?.Invoke(data);
39+
}
40+
3341
/// <summary>
3442
/// Send data to the client.
3543
/// </summary>

QuicNet/QuicListener.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ public void Start()
4141
{
4242
_client = new UdpClient(_port);
4343
_started = true;
44+
Receive();
4445
}
4546

4647
public void Close()
4748
{
4849
_client.Close();
4950
}
5051

51-
public void Receive()
52+
private void Receive()
5253
{
5354
if (!_started)
5455
throw new QuicListenerNotStartedException("Please call the Start() method before receving data.");

QuicNet/Streams/QuicStream.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public QuicStream(QuicConnection connection, StreamId streamId)
2727

2828
public void ProcessData(StreamFrame frame)
2929
{
30-
30+
byte[] data = frame.StreamData;
31+
_connection.Context.DataReceived(data, StreamId);
3132
}
3233
}
3334
}

QuickNet.Console/Program.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,25 @@ static void Main(string[] args)
3131
};
3232
ConnectionCloseFrame frame = new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, "The server is too busy to process your request.");
3333
MaxStreamIdFrame msidframe = new MaxStreamIdFrame(144123, StreamType.ClientUnidirectional);
34-
packet.AttachFrame(frame);
34+
//packet.AttachFrame(frame);
3535
packet.AttachFrame(msidframe);
3636

3737
byte[] data = packet.Encode();
38+
string b64 = ToBase64(data);
39+
40+
byte[] shpdata = new byte[] { 1, 1, 2, 3, 5, 8, 13, 21 };
41+
ShortHeaderPacket shp = new ShortHeaderPacket();
42+
shp.DestinationConnectionId = 124;
43+
shp.PacketNumber = 2;
44+
shp.AttachFrame(new StreamFrame() { StreamId = 1, Length = new VariableInteger((UInt64)shpdata.Length), StreamData = shpdata, Offset = 0 });
45+
46+
string shpb64 = ToBase64(shp.Encode());
47+
3848
packet.Decode(data);
3949

4050
byte[] ccfData = frame.Encode();
4151
frame.Decode(new ByteArray(ccfData));
4252

43-
string b64 = ToBase64(data);
44-
4553
byte[] streamIdData = new StreamId(123, StreamType.ClientUnidirectional);
4654
StreamId streamId = streamIdData;
4755

@@ -52,7 +60,17 @@ static void Main(string[] args)
5260

5361
private static void Listener_OnClientConnected(QuicContext obj)
5462
{
55-
throw new NotImplementedException();
63+
System.Console.WriteLine("Client connected.");
64+
obj.OnDataReceived += Obj_OnDataReceived;
65+
}
66+
67+
private static void Obj_OnDataReceived(byte[] obj)
68+
{
69+
System.Console.WriteLine("Data received");
70+
foreach (byte b in obj)
71+
{
72+
System.Console.Write(string.Format("{0},", b));
73+
}
5674
}
5775

5876
static string ToBase64(byte[] data)

0 commit comments

Comments
 (0)