Skip to content

Commit

Permalink
Ability to transfer more than 64kb of data
Browse files Browse the repository at this point in the history
  • Loading branch information
Vect0rZ committed Dec 21, 2020
1 parent fa75e2c commit 6e5c614
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
4 changes: 2 additions & 2 deletions QuicNet.Infrastructure/PacketProcessing/PacketCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public ShortHeaderPacket CreateConnectionClosePacket(ErrorCode code, byte frameT
return packet;
}

public ShortHeaderPacket CreateDataPacket(UInt64 streamId, byte[] data, UInt64 offset)
public ShortHeaderPacket CreateDataPacket(UInt64 streamId, byte[] data, UInt64 offset, bool eos)
{
ShortHeaderPacket packet = new ShortHeaderPacket(_peerConnectionId.Size);
packet.PacketNumber = _ns.Get();
packet.DestinationConnectionId = (byte)_peerConnectionId;
packet.AttachFrame(new StreamFrame(streamId, data, offset, true));
packet.AttachFrame(new StreamFrame(streamId, data, offset, eos));

return packet;
}
Expand Down
34 changes: 25 additions & 9 deletions QuicNet.Tests.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,38 @@ static void Main(string[] args)
// Create a data stream
QuicStream stream = connection.CreateStream(QuickNet.Utilities.StreamType.ClientBidirectional);
// Send Data
stream.Send(Encoding.UTF8.GetBytes("Hello from Client!"));
stream.Send(Encoding.UTF8.GetBytes(GenerateData(64)));
// Wait reponse back from the server
byte[] data = stream.Receive();

Console.WriteLine(Encoding.UTF8.GetString(data));

// Create a new data stream
stream = connection.CreateStream(QuickNet.Utilities.StreamType.ClientBidirectional);
// Send Data
stream.Send(Encoding.UTF8.GetBytes("Hello from Client2!"));
// Wait reponse back from the server
data = stream.Receive();
Console.ReadKey();
}

Console.WriteLine(Encoding.UTF8.GetString(data));
static string GenerateData(int kb)
{
string res = "";
for (int i = 0; i < kb; i++)
{
for (int j = 0; j < 100; j++)
{
res += "!!!!!!!!!!";
}
}

Console.ReadKey();
return res;
}

static string GenerateBytes(int bytes)
{
string result = string.Empty;
for (int i = 0; i < bytes; i++)
{
result += "!";
}

return result;
}
}
}
8 changes: 6 additions & 2 deletions QuicNet/Connections/QuicConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class QuicConnection
public UInt64 MaxStreams { get; private set; }

public StreamOpenedEvent OnStreamOpened { get; set; }
public ConnectionClosedEvent OnConnectionClosed { get; set; }

/// <summary>
/// Creates a new stream for sending/receiving data.
Expand Down Expand Up @@ -98,6 +99,8 @@ private void OnConnectionCloseFrame(Frame frame)
ConnectionCloseFrame ccf = (ConnectionCloseFrame)frame;
_state = ConnectionState.Draining;
_lastError = ccf.ReasonPhrase;

OnConnectionClosed?.Invoke(this);
}

private void OnRstStreamFrame(Frame frame)
Expand Down Expand Up @@ -152,10 +155,11 @@ private void OnMaxDataFrame(Frame frame)
private void OnMaxStreamDataFrame(Frame frame)
{
MaxStreamDataFrame msdf = (MaxStreamDataFrame)frame;
if (_streams.ContainsKey(msdf.StreamId))
StreamId streamId = msdf.StreamId;
if (_streams.ContainsKey(streamId.Id))
{
// Find and set the new maximum stream data on the stream
QuicStream stream = _streams[msdf.ConvertedStreamId.Id];
QuicStream stream = _streams[streamId.Id];
stream.SetMaximumStreamData(msdf.MaximumStreamData.Value);
}
}
Expand Down
1 change: 1 addition & 0 deletions QuicNet/Events/DelegateDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ namespace QuicNet.Events
public delegate void ClientConnectedEvent(QuicConnection connection);
public delegate void StreamOpenedEvent(QuicStream stream);
public delegate void StreamDataReceivedEvent(QuicStream stream, byte[] data);
public delegate void ConnectionClosedEvent(QuicConnection connection);
}
34 changes: 28 additions & 6 deletions QuicNet/Streams/QuicStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,35 @@ public bool Send(byte[] data)

_connection.IncrementRate(data.Length);

ShortHeaderPacket packet = _connection.PacketCreator.CreateDataPacket(this.StreamId.IntegerValue, data, _sendOffset);
if (_connection.MaximumReached())
packet.AttachFrame(new StreamDataBlockedFrame(StreamId.IntegerValue, (UInt64)data.Length));
int numberOfPackets = (data.Length / QuicSettings.PMTU) + 1;
int leftoverCarry = data.Length % QuicSettings.PMTU;

_sendOffset += (UInt64)data.Length;
for (int i = 0; i < numberOfPackets; i++)
{
bool eos = false;
int dataSize = QuicSettings.PMTU;
if (i == numberOfPackets - 1)
{
eos = true;
dataSize = leftoverCarry;
}

byte[] buffer = new byte[dataSize];
Buffer.BlockCopy(data, (Int32)_sendOffset, buffer, 0, dataSize);

ShortHeaderPacket packet = _connection.PacketCreator.CreateDataPacket(this.StreamId.IntegerValue, buffer, _sendOffset, eos);
if (i == 0 && data.Length >= QuicSettings.MaxStreamData)
packet.AttachFrame(new MaxStreamDataFrame(this.StreamId.IntegerValue, (UInt64)(data.Length + 1)));

if (_connection.MaximumReached())
packet.AttachFrame(new StreamDataBlockedFrame(StreamId.IntegerValue, (UInt64)data.Length));

return _connection.SendData(packet);
_sendOffset += (UInt64)buffer.Length;

_connection.SendData(packet);
}

return true;
}

/// <summary>
Expand Down Expand Up @@ -172,7 +194,7 @@ private bool IsStreamFull()
if (kvp.Key > 0 && kvp.Key != length)
return false;

length = (UInt64)kvp.Value.Length;
length += (UInt64)kvp.Value.Length;
}

return true;
Expand Down

0 comments on commit 6e5c614

Please sign in to comment.