diff --git a/QuicNet.Infrastructure/ErrorCodes.cs b/QuicNet.Infrastructure/ErrorCodes.cs index 163febe..429d6e0 100644 --- a/QuicNet.Infrastructure/ErrorCodes.cs +++ b/QuicNet.Infrastructure/ErrorCodes.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Infrastructure +namespace QuicNet.Infrastructure { - public enum ErrorCode : UInt16 + public enum ErrorCode : ushort { NO_ERROR = 0x0, INTERNAL_ERROR = 0x1, diff --git a/QuicNet.Infrastructure/Exceptions/ProtocolException.cs b/QuicNet.Infrastructure/Exceptions/ProtocolException.cs index 98f35f0..71ce9f2 100644 --- a/QuicNet.Infrastructure/Exceptions/ProtocolException.cs +++ b/QuicNet.Infrastructure/Exceptions/ProtocolException.cs @@ -1,17 +1,9 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Exceptions { public class ProtocolException : Exception { - public ProtocolException() - { - } - public ProtocolException(string message) : base(message) { } diff --git a/QuicNet.Infrastructure/FrameParser.cs b/QuicNet.Infrastructure/FrameParser.cs index ebb472c..59596e3 100644 --- a/QuicNet.Infrastructure/FrameParser.cs +++ b/QuicNet.Infrastructure/FrameParser.cs @@ -1,16 +1,11 @@ using QuickNet.Utilities; using QuicNet.Infrastructure.Frames; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure { public class FrameParser { - private ByteArray _array; + private readonly ByteArray _array; public FrameParser(ByteArray array) { @@ -20,40 +15,102 @@ public FrameParser(ByteArray array) public Frame GetFrame() { Frame result; - byte frameType = _array.PeekByte(); - switch(frameType) + var frameType = _array.PeekByte(); + switch (frameType) { - case 0x00: result = new PaddingFrame(); break; - case 0x01: result = new PingFrame(); break; - case 0x02: result = new AckFrame(); break; - case 0x03: result = new AckFrame(); break; - case 0x04: result = new ResetStreamFrame(); break; - case 0x05: result = new StopSendingFrame(); break; - case 0x06: result = new CryptoFrame(); break; - case 0x07: result = new NewTokenFrame(); break; - case 0x08: result = new StreamFrame(); break; - case 0x09: result = new StreamFrame(); break; - case 0x0a: result = new StreamFrame(); break; - case 0x0b: result = new StreamFrame(); break; - case 0x0c: result = new StreamFrame(); break; - case 0x0d: result = new StreamFrame(); break; - case 0x0e: result = new StreamFrame(); break; - case 0x0f: result = new StreamFrame(); break; - case 0x10: result = new MaxDataFrame(); break; - case 0x11: result = new MaxStreamDataFrame(); break; - case 0x12: result = new MaxStreamsFrame(); break; - case 0x13: result = new MaxStreamsFrame(); break; - case 0x14: result = new DataBlockedFrame(); break; - case 0x15: result = new StreamDataBlockedFrame(); break; - case 0x16: result = new StreamsBlockedFrame(); break; - case 0x17: result = new StreamsBlockedFrame(); break; - case 0x18: result = new NewConnectionIdFrame(); break; - case 0x19: result = new RetireConnectionIdFrame(); break; - case 0x1a: result = new PathChallengeFrame(); break; - case 0x1b: result = new PathResponseFrame(); break; - case 0x1c: result = new ConnectionCloseFrame(); break; - case 0x1d: result = new ConnectionCloseFrame(); break; - default: result = null; break; + case 0x00: + result = new PaddingFrame(); + break; + case 0x01: + result = new PingFrame(); + break; + case 0x02: + result = new AckFrame(); + break; + case 0x03: + result = new AckFrame(); + break; + case 0x04: + result = new ResetStreamFrame(); + break; + case 0x05: + result = new StopSendingFrame(); + break; + case 0x06: + result = new CryptoFrame(); + break; + case 0x07: + result = new NewTokenFrame(); + break; + case 0x08: + result = new StreamFrame(); + break; + case 0x09: + result = new StreamFrame(); + break; + case 0x0a: + result = new StreamFrame(); + break; + case 0x0b: + result = new StreamFrame(); + break; + case 0x0c: + result = new StreamFrame(); + break; + case 0x0d: + result = new StreamFrame(); + break; + case 0x0e: + result = new StreamFrame(); + break; + case 0x0f: + result = new StreamFrame(); + break; + case 0x10: + result = new MaxDataFrame(); + break; + case 0x11: + result = new MaxStreamDataFrame(); + break; + case 0x12: + result = new MaxStreamsFrame(); + break; + case 0x13: + result = new MaxStreamsFrame(); + break; + case 0x14: + result = new DataBlockedFrame(); + break; + case 0x15: + result = new StreamDataBlockedFrame(); + break; + case 0x16: + result = new StreamsBlockedFrame(); + break; + case 0x17: + result = new StreamsBlockedFrame(); + break; + case 0x18: + result = new NewConnectionIdFrame(); + break; + case 0x19: + result = new RetireConnectionIdFrame(); + break; + case 0x1a: + result = new PathChallengeFrame(); + break; + case 0x1b: + result = new PathResponseFrame(); + break; + case 0x1c: + result = new ConnectionCloseFrame(); + break; + case 0x1d: + result = new ConnectionCloseFrame(); + break; + default: + result = null; + break; } if (result != null) @@ -62,4 +119,4 @@ public Frame GetFrame() return result; } } -} +} \ No newline at end of file diff --git a/QuicNet.Infrastructure/Frames/AckFrame.cs b/QuicNet.Infrastructure/Frames/AckFrame.cs index 7f994d3..0ce659f 100644 --- a/QuicNet.Infrastructure/Frames/AckFrame.cs +++ b/QuicNet.Infrastructure/Frames/AckFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/ConnectionCloseFrame.cs b/QuicNet.Infrastructure/Frames/ConnectionCloseFrame.cs index 8fff599..101b322 100644 --- a/QuicNet.Infrastructure/Frames/ConnectionCloseFrame.cs +++ b/QuicNet.Infrastructure/Frames/ConnectionCloseFrame.cs @@ -1,16 +1,12 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { public class ConnectionCloseFrame : Frame { public override byte Type => 0x1c; - public UInt16 ErrorCode { get; set; } + public ushort ErrorCode { get; set; } public VariableInteger ReasonPhraseLength { get; set; } public string ReasonPhrase { get; set; } @@ -24,33 +20,33 @@ public ConnectionCloseFrame(ErrorCode error, string reason) { ReasonPhraseLength = new VariableInteger(0); - ErrorCode = (UInt16)error; + ErrorCode = (ushort)error; ReasonPhrase = reason; } public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + array.ReadByte(); ErrorCode = array.ReadUInt16(); ReasonPhraseLength = array.ReadVariableInteger(); - byte[] rp = array.ReadBytes((int)ReasonPhraseLength.Value); + var rp = array.ReadBytes((int)ReasonPhraseLength.Value); ReasonPhrase = ByteUtilities.GetString(rp); } public override byte[] Encode() { - List result = new List(); + var result = new List(); result.Add(Type); - byte[] errorCode = ByteUtilities.GetBytes(ErrorCode); + var errorCode = ByteUtilities.GetBytes(ErrorCode); result.AddRange(errorCode); if (string.IsNullOrWhiteSpace(ReasonPhrase) == false) { - byte[] reasonPhrase = ByteUtilities.GetBytes(ReasonPhrase); - byte[] rpl = new VariableInteger((UInt64)ReasonPhrase.Length); + var reasonPhrase = ByteUtilities.GetBytes(ReasonPhrase); + byte[] rpl = new VariableInteger((ulong)ReasonPhrase.Length); result.AddRange(rpl); result.AddRange(reasonPhrase); } diff --git a/QuicNet.Infrastructure/Frames/CryptoFrame.cs b/QuicNet.Infrastructure/Frames/CryptoFrame.cs index 978cece..008e990 100644 --- a/QuicNet.Infrastructure/Frames/CryptoFrame.cs +++ b/QuicNet.Infrastructure/Frames/CryptoFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/DataBlockedFrame.cs b/QuicNet.Infrastructure/Frames/DataBlockedFrame.cs index 9bccea7..6acbbc8 100644 --- a/QuicNet.Infrastructure/Frames/DataBlockedFrame.cs +++ b/QuicNet.Infrastructure/Frames/DataBlockedFrame.cs @@ -1,37 +1,23 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { public class DataBlockedFrame : Frame { public override byte Type => 0x14; - public VariableInteger DataLimit { get; set; } - - public DataBlockedFrame() - { - - } - public DataBlockedFrame(UInt64 dataLimit) - { - DataLimit = dataLimit; - } + public VariableInteger DataLimit { get; set; } public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + array.ReadByte(); DataLimit = array.ReadVariableInteger(); } public override byte[] Encode() { - List result = new List(); - result.Add(Type); + var result = new List {Type}; byte[] dataLimit = DataLimit; result.AddRange(dataLimit); diff --git a/QuicNet.Infrastructure/Frames/Frame.cs b/QuicNet.Infrastructure/Frames/Frame.cs index ac18628..4237f78 100644 --- a/QuicNet.Infrastructure/Frames/Frame.cs +++ b/QuicNet.Infrastructure/Frames/Frame.cs @@ -1,7 +1,4 @@ using QuickNet.Utilities; -using System; -using System.Collections.Generic; -using System.Text; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/MaxDataFrame.cs b/QuicNet.Infrastructure/Frames/MaxDataFrame.cs index cccfce6..2117730 100644 --- a/QuicNet.Infrastructure/Frames/MaxDataFrame.cs +++ b/QuicNet.Infrastructure/Frames/MaxDataFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { @@ -20,7 +16,7 @@ public override void Decode(ByteArray array) public override byte[] Encode() { - List result = new List(); + var result = new List(); byte[] maxData = MaximumData; result.Add(Type); diff --git a/QuicNet.Infrastructure/Frames/MaxStreamDataFrame.cs b/QuicNet.Infrastructure/Frames/MaxStreamDataFrame.cs index dc5d69b..62aa7e1 100644 --- a/QuicNet.Infrastructure/Frames/MaxStreamDataFrame.cs +++ b/QuicNet.Infrastructure/Frames/MaxStreamDataFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { @@ -15,28 +11,16 @@ public class MaxStreamDataFrame : Frame public StreamId ConvertedStreamId { get; set; } - public MaxStreamDataFrame() - { - - } - - public MaxStreamDataFrame(UInt64 streamId, UInt64 maximumStreamData) - { - StreamId = streamId; - MaximumStreamData = maximumStreamData; - } - public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + array.ReadByte(); StreamId = array.ReadVariableInteger(); MaximumStreamData = array.ReadVariableInteger(); } public override byte[] Encode() { - List result = new List(); - result.Add(Type); + var result = new List {Type}; byte[] streamId = StreamId; result.AddRange(streamId); diff --git a/QuicNet.Infrastructure/Frames/MaxStreamsFrame.cs b/QuicNet.Infrastructure/Frames/MaxStreamsFrame.cs index 9729a93..207e3b7 100644 --- a/QuicNet.Infrastructure/Frames/MaxStreamsFrame.cs +++ b/QuicNet.Infrastructure/Frames/MaxStreamsFrame.cs @@ -1,15 +1,14 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { public class MaxStreamsFrame : Frame { + private readonly StreamType _appliesTo; + public override byte Type => 0x12; + public VariableInteger MaximumStreams { get; set; } public MaxStreamsFrame() @@ -17,22 +16,21 @@ public MaxStreamsFrame() } - public MaxStreamsFrame(UInt64 maximumStreamId, StreamType appliesTo) + public MaxStreamsFrame(ulong maximumStreamId, StreamType appliesTo) { + _appliesTo = appliesTo; MaximumStreams = new VariableInteger(maximumStreamId); } public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + var type = array.ReadByte(); MaximumStreams = array.ReadVariableInteger(); } public override byte[] Encode() { - List result = new List(); - result.Add(Type); - + var result = new List {Type}; byte[] streamId = MaximumStreams; result.AddRange(streamId); diff --git a/QuicNet.Infrastructure/Frames/NewConnectionIdFrame.cs b/QuicNet.Infrastructure/Frames/NewConnectionIdFrame.cs index fc0c043..180f3d5 100644 --- a/QuicNet.Infrastructure/Frames/NewConnectionIdFrame.cs +++ b/QuicNet.Infrastructure/Frames/NewConnectionIdFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/NewTokenFrame.cs b/QuicNet.Infrastructure/Frames/NewTokenFrame.cs index b3a813d..3da1145 100644 --- a/QuicNet.Infrastructure/Frames/NewTokenFrame.cs +++ b/QuicNet.Infrastructure/Frames/NewTokenFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/PaddingFrame.cs b/QuicNet.Infrastructure/Frames/PaddingFrame.cs index 5327200..0002a40 100644 --- a/QuicNet.Infrastructure/Frames/PaddingFrame.cs +++ b/QuicNet.Infrastructure/Frames/PaddingFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { @@ -13,14 +9,12 @@ public class PaddingFrame : Frame public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + array.ReadByte(); } public override byte[] Encode() { - List data = new List(); - data.Add(Type); - + var data = new List {Type}; return data.ToArray(); } } diff --git a/QuicNet.Infrastructure/Frames/PathChallengeFrame.cs b/QuicNet.Infrastructure/Frames/PathChallengeFrame.cs index 024bdbe..59f0d19 100644 --- a/QuicNet.Infrastructure/Frames/PathChallengeFrame.cs +++ b/QuicNet.Infrastructure/Frames/PathChallengeFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/PathResponseFrame.cs b/QuicNet.Infrastructure/Frames/PathResponseFrame.cs index 99e027e..eeeb0d8 100644 --- a/QuicNet.Infrastructure/Frames/PathResponseFrame.cs +++ b/QuicNet.Infrastructure/Frames/PathResponseFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/PingFrame.cs b/QuicNet.Infrastructure/Frames/PingFrame.cs index c758af8..490acff 100644 --- a/QuicNet.Infrastructure/Frames/PingFrame.cs +++ b/QuicNet.Infrastructure/Frames/PingFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/ResetStreamFrame.cs b/QuicNet.Infrastructure/Frames/ResetStreamFrame.cs index b8715eb..7fc5c25 100644 --- a/QuicNet.Infrastructure/Frames/ResetStreamFrame.cs +++ b/QuicNet.Infrastructure/Frames/ResetStreamFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { @@ -11,12 +7,12 @@ public class ResetStreamFrame : Frame { public override byte Type => 0x04; public VariableInteger StreamId { get; set; } - public UInt16 ApplicationErrorCode { get; set; } + public ushort ApplicationErrorCode { get; set; } public VariableInteger FinalOffset { get; set; } public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + array.ReadByte(); StreamId = array.ReadVariableInteger(); ApplicationErrorCode = array.ReadUInt16(); FinalOffset = array.ReadVariableInteger(); @@ -24,9 +20,8 @@ public override void Decode(ByteArray array) public override byte[] Encode() { - List result = new List(); + var result = new List {Type}; - result.Add(Type); byte[] streamId = StreamId; result.AddRange(streamId); diff --git a/QuicNet.Infrastructure/Frames/RetireConnectionIdFrame.cs b/QuicNet.Infrastructure/Frames/RetireConnectionIdFrame.cs index 196e286..f811a18 100644 --- a/QuicNet.Infrastructure/Frames/RetireConnectionIdFrame.cs +++ b/QuicNet.Infrastructure/Frames/RetireConnectionIdFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/StopSendingFrame.cs b/QuicNet.Infrastructure/Frames/StopSendingFrame.cs index 95aa994..79157f8 100644 --- a/QuicNet.Infrastructure/Frames/StopSendingFrame.cs +++ b/QuicNet.Infrastructure/Frames/StopSendingFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/Frames/StreamDataBlockedFrame.cs b/QuicNet.Infrastructure/Frames/StreamDataBlockedFrame.cs index 3a34d04..ee7cc0b 100644 --- a/QuicNet.Infrastructure/Frames/StreamDataBlockedFrame.cs +++ b/QuicNet.Infrastructure/Frames/StreamDataBlockedFrame.cs @@ -1,26 +1,22 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { public class StreamDataBlockedFrame : Frame { public override byte Type => 0x15; + public VariableInteger StreamId { get; set; } - public VariableInteger StreamDataLimit { get; set; } - public StreamId ConvertedStreamId { get; set; } + public VariableInteger StreamDataLimit { get; set; } public StreamDataBlockedFrame() { } - public StreamDataBlockedFrame(UInt64 streamId, UInt64 streamDataLimit) + public StreamDataBlockedFrame(ulong streamId, ulong streamDataLimit) { StreamId = streamId; StreamDataLimit = streamDataLimit; @@ -28,15 +24,14 @@ public StreamDataBlockedFrame(UInt64 streamId, UInt64 streamDataLimit) public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + array.ReadByte(); StreamId = array.ReadVariableInteger(); StreamDataLimit = array.ReadVariableInteger(); } public override byte[] Encode() { - List result = new List(); - result.Add(Type); + var result = new List {Type}; byte[] streamId = StreamId; byte[] streamDataLimit = StreamDataLimit; diff --git a/QuicNet.Infrastructure/Frames/StreamFrame.cs b/QuicNet.Infrastructure/Frames/StreamFrame.cs index 7e7bbf6..02bf7cc 100644 --- a/QuicNet.Infrastructure/Frames/StreamFrame.cs +++ b/QuicNet.Infrastructure/Frames/StreamFrame.cs @@ -1,22 +1,24 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { public class StreamFrame : Frame { - private byte ActualType = 0x08; + private byte _actualType = 0x08; public override byte Type => 0x08; + public VariableInteger StreamId { get; set; } + public VariableInteger Offset { get; set; } + public VariableInteger Length { get; set; } + public byte[] StreamData { get; set; } + public StreamId ConvertedStreamId { get; set; } + public bool EndOfStream { get; set; } public StreamFrame() @@ -24,29 +26,29 @@ public StreamFrame() } - public StreamFrame(UInt64 streamId, byte[] data, UInt64 offset, bool eos) + public StreamFrame(ulong streamId, byte[] data, ulong offset, bool eos) { StreamId = streamId; StreamData = data; - Length = (UInt64)data.Length; + Length = (ulong)data.Length; Offset = offset; EndOfStream = eos; } public override void Decode(ByteArray array) { - byte type = array.ReadByte(); + var type = array.ReadByte(); - byte OFF_BIT = (byte)(type & 0x04); - byte LEN_BIT = (byte)(type & 0x02); - byte FIN_BIT = (byte)(type & 0x01); + var offBit = (byte)(type & 0x04); + var lenBit = (byte)(type & 0x02); + var finBit = (byte)(type & 0x01); StreamId = array.ReadVariableInteger(); - if (OFF_BIT > 0) + if (offBit > 0) Offset = array.ReadVariableInteger(); - if (LEN_BIT > 0) + if (lenBit > 0) Length = array.ReadVariableInteger(); - if (FIN_BIT > 0) + if (finBit > 0) EndOfStream = true; StreamData = array.ReadBytes((int)Length.Value); @@ -56,28 +58,26 @@ public override void Decode(ByteArray array) public override byte[] Encode() { if (Offset != null && Offset.Value > 0) - ActualType = (byte)(ActualType | 0x04); + _actualType = (byte)(_actualType | 0x04); if (Length != null && Length.Value > 0) - ActualType = (byte)(ActualType | 0x02); - if (EndOfStream == true) - ActualType = (byte)(ActualType | 0x01); + _actualType = (byte)(_actualType | 0x02); + if (EndOfStream) + _actualType = (byte)(_actualType | 0x01); - byte OFF_BIT = (byte)(ActualType & 0x04); - byte LEN_BIT = (byte)(ActualType & 0x02); - byte FIN_BIT = (byte)(ActualType & 0x01); + var offBit = (byte)(_actualType & 0x04); + var lenBit = (byte)(_actualType & 0x02); - List result = new List(); - result.Add(ActualType); + var result = new List {_actualType}; byte[] streamId = StreamId; result.AddRange(streamId); - if (OFF_BIT > 0) + if (offBit > 0) { byte[] offset = Offset; result.AddRange(offset); } - if (LEN_BIT > 0) + if (lenBit > 0) { byte[] length = Length; result.AddRange(length); diff --git a/QuicNet.Infrastructure/Frames/StreamsBlockedFrame.cs b/QuicNet.Infrastructure/Frames/StreamsBlockedFrame.cs index 077e30c..4b02402 100644 --- a/QuicNet.Infrastructure/Frames/StreamsBlockedFrame.cs +++ b/QuicNet.Infrastructure/Frames/StreamsBlockedFrame.cs @@ -1,9 +1,5 @@ using QuickNet.Utilities; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.Frames { diff --git a/QuicNet.Infrastructure/NumberSpace.cs b/QuicNet.Infrastructure/NumberSpace.cs index efacf09..1190df4 100644 --- a/QuicNet.Infrastructure/NumberSpace.cs +++ b/QuicNet.Infrastructure/NumberSpace.cs @@ -1,21 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Infrastructure +namespace QuicNet.Infrastructure { public class NumberSpace { - private UInt32 _max = UInt32.MaxValue; - private UInt32 _n = 0; + private readonly uint _max = uint.MaxValue; + + private uint _n; public NumberSpace() { } - public NumberSpace(UInt32 max) + public NumberSpace(uint max) { _max = max; } @@ -25,7 +20,7 @@ public bool IsMax() return _n == _max; } - public UInt32 Get() + public uint Get() { if (_n >= _max) return 0; diff --git a/QuicNet.Infrastructure/PacketProcessing/InitialPacketCreator.cs b/QuicNet.Infrastructure/PacketProcessing/InitialPacketCreator.cs index 0a5c50b..5a0b39c 100644 --- a/QuicNet.Infrastructure/PacketProcessing/InitialPacketCreator.cs +++ b/QuicNet.Infrastructure/PacketProcessing/InitialPacketCreator.cs @@ -8,16 +8,18 @@ public class InitialPacketCreator { public InitialPacket CreateInitialPacket(byte sourceConnectionId, byte destinationConnectionId) { - InitialPacket packet = new InitialPacket(); - packet.PacketNumber = 0; - packet.SourceConnectionId = sourceConnectionId; - packet.DestinationConnectionId = destinationConnectionId; - packet.Version = QuicVersion.CurrentVersion; + var packet = new InitialPacket + { + PacketNumber = 0, + SourceConnectionId = sourceConnectionId, + DestinationConnectionId = destinationConnectionId, + Version = QuicVersion.CurrentVersion + }; - int length = packet.Encode().Length; - int padding = QuicSettings.PMTU - length; + var length = packet.Encode().Length; + var padding = QuicSettings.PMTU - length; - for (int i = 0; i < padding; i++) + for (var i = 0; i < padding; i++) packet.AttachFrame(new PaddingFrame()); return packet; diff --git a/QuicNet.Infrastructure/PacketProcessing/PacketCreator.cs b/QuicNet.Infrastructure/PacketProcessing/PacketCreator.cs index cb4b9d5..9ce6346 100644 --- a/QuicNet.Infrastructure/PacketProcessing/PacketCreator.cs +++ b/QuicNet.Infrastructure/PacketProcessing/PacketCreator.cs @@ -1,46 +1,39 @@ -using QuickNet.Utilities; -using QuicNet.Infrastructure.Frames; +using QuicNet.Infrastructure.Frames; using QuicNet.Infrastructure.Packets; -using QuicNet.Infrastructure.Settings; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Infrastructure.PacketProcessing { public class PacketCreator { - private NumberSpace _ns; - private UInt32 _connectionId; - private UInt32 _peerConnectionId; + private readonly NumberSpace _ns; - public PacketCreator(UInt32 connectionId, UInt32 peerConnectionId) + private readonly uint _peerConnectionId; + + public PacketCreator(uint peerConnectionId) { _ns = new NumberSpace(); - - _connectionId = connectionId; _peerConnectionId = peerConnectionId; } public ShortHeaderPacket CreateConnectionClosePacket(ErrorCode code, string reason) { - ShortHeaderPacket packet = new ShortHeaderPacket(); - packet.PacketNumber = _ns.Get(); - packet.DestinationConnectionId = (byte)_peerConnectionId; - packet.AttachFrame(new ConnectionCloseFrame(code, reason)); + var packet = new ShortHeaderPacket + { + PacketNumber = _ns.Get(), DestinationConnectionId = (byte) _peerConnectionId + }; + packet.AttachFrame(new ConnectionCloseFrame(code, reason)); return packet; } - public ShortHeaderPacket CreateDataPacket(UInt64 streamId, byte[] data) + public ShortHeaderPacket CreateDataPacket(ulong streamId, byte[] data) { - ShortHeaderPacket packet = new ShortHeaderPacket(); - packet.PacketNumber = _ns.Get(); - packet.DestinationConnectionId = (byte)_peerConnectionId; - packet.AttachFrame(new StreamFrame(streamId, data, 0, true)); + var packet = new ShortHeaderPacket + { + PacketNumber = _ns.Get(), DestinationConnectionId = (byte) _peerConnectionId + }; + packet.AttachFrame(new StreamFrame(streamId, data, 0, true)); return packet; } diff --git a/QuicNet.Infrastructure/PacketType.cs b/QuicNet.Infrastructure/PacketType.cs index 8c4920a..1f26930 100644 --- a/QuicNet.Infrastructure/PacketType.cs +++ b/QuicNet.Infrastructure/PacketType.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Infrastructure +namespace QuicNet.Infrastructure { - public enum PacketType : UInt16 + public enum PacketType : ushort { Initial = 0x0, - ZeroRTTProtected = 0x1, + ZeroRttProtected = 0x1, Handshake = 0x2, RetryPacket = 0x3 } diff --git a/QuicNet.Infrastructure/Packets/InitialPacket.cs b/QuicNet.Infrastructure/Packets/InitialPacket.cs index 37f043a..8ceaa60 100644 --- a/QuicNet.Infrastructure/Packets/InitialPacket.cs +++ b/QuicNet.Infrastructure/Packets/InitialPacket.cs @@ -1,7 +1,5 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Text; namespace QuicNet.Infrastructure.Packets { @@ -9,29 +7,29 @@ public class InitialPacket : Packet { public override byte Type => 0x7F | 1 << 7; // 1111 1111 - public byte DCIL_SCIL { get; set; } + public byte DcilScil { get; set; } public byte DestinationConnectionId { get; set; } public byte SourceConnectionId { get; set; } public VariableInteger TokenLength { get; set; } public byte[] Token { get; set; } public VariableInteger Length { get; set; } - public UInt32 PacketNumber { get; set; } + public uint PacketNumber { get; set; } public InitialPacket() { - DCIL_SCIL = 0; + DcilScil = 0; } public override void Decode(byte[] packet) { - ByteArray array = new ByteArray(packet); + var array = new ByteArray(packet); array.ReadByte(); Version = array.ReadUInt32(); - DCIL_SCIL = array.ReadByte(); + DcilScil = array.ReadByte(); - if ((DCIL_SCIL & 0xF0) != 0) + if ((DcilScil & 0xF0) != 0) DestinationConnectionId = array.ReadByte(); - if ((DCIL_SCIL & 0x0F) != 0) + if ((DcilScil & 0x0F) != 0) SourceConnectionId = array.ReadByte(); TokenLength = array.ReadVariableInteger(); @@ -41,25 +39,25 @@ public override void Decode(byte[] packet) Length = array.ReadVariableInteger(); PacketNumber = array.ReadUInt32(); - Length = Length - 4; + Length -= 4; - this.DecodeFrames(array); + DecodeFrames(array); } public override byte[] Encode() { - byte[] frames = EncodeFrames(); + var frames = EncodeFrames(); - List result = new List(); + var result = new List(); result.Add(Type); result.AddRange(ByteUtilities.GetBytes(Version)); if (DestinationConnectionId > 0) - DCIL_SCIL = (byte)(DCIL_SCIL | 0x50); + DcilScil = (byte)(DcilScil | 0x50); if (SourceConnectionId > 0) - DCIL_SCIL = (byte)(DCIL_SCIL | 0x05); + DcilScil = (byte)(DcilScil | 0x05); - result.Add(DCIL_SCIL); + result.Add(DcilScil); if (DestinationConnectionId > 0) result.Add(DestinationConnectionId); @@ -67,7 +65,7 @@ public override byte[] Encode() result.Add(SourceConnectionId); byte[] tokenLength = new VariableInteger(0); - byte[] length = new VariableInteger(4 + (UInt64)frames.Length); + byte[] length = new VariableInteger(4 + (ulong)frames.Length); result.AddRange(tokenLength); result.AddRange(length); diff --git a/QuicNet.Infrastructure/Packets/LongHeaderPacket.cs b/QuicNet.Infrastructure/Packets/LongHeaderPacket.cs index f878354..98d4aad 100644 --- a/QuicNet.Infrastructure/Packets/LongHeaderPacket.cs +++ b/QuicNet.Infrastructure/Packets/LongHeaderPacket.cs @@ -1,7 +1,5 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Text; namespace QuicNet.Infrastructure.Packets { @@ -9,33 +7,40 @@ public class LongHeaderPacket : Packet { public override byte Type => 0xC0; // 1100 0000 - public byte DCIL_SCIL { get; set; } + public byte DcilScil { get; set; } + public byte DestinationConnectionId { get; set; } + public byte SourceConnectionId { get; set; } + public VariableInteger TokenLength { get; set; } + public byte[] Token { get; set; } + public VariableInteger Length { get; set; } - public UInt32 PacketNumber { get; set; } + public uint PacketNumber { get; set; } + public PacketType PacketType { get; set; } + public LongHeaderPacket(PacketType packetType) { - DCIL_SCIL = 0; + DcilScil = 0; PacketType = packetType; } public override void Decode(byte[] packet) { - ByteArray array = new ByteArray(packet); - byte type = array.ReadByte(); + var array = new ByteArray(packet); + var type = array.ReadByte(); PacketType = (PacketType)(type & 0x30); Version = array.ReadUInt32(); - DCIL_SCIL = array.ReadByte(); + DcilScil = array.ReadByte(); - if ((DCIL_SCIL & 0xF0) != 0) + if ((DcilScil & 0xF0) != 0) DestinationConnectionId = array.ReadByte(); - if ((DCIL_SCIL & 0x0F) != 0) + if ((DcilScil & 0x0F) != 0) SourceConnectionId = array.ReadByte(); TokenLength = array.ReadVariableInteger(); @@ -45,25 +50,24 @@ public override void Decode(byte[] packet) Length = array.ReadVariableInteger(); PacketNumber = array.ReadUInt32(); - Length = Length - 4; - - this.DecodeFrames(array); + Length -= 4; + + DecodeFrames(array); } public override byte[] Encode() { - byte[] frames = EncodeFrames(); + var frames = EncodeFrames(); - List result = new List(); - result.Add(EncodeTypeField()); + var result = new List {EncodeTypeField()}; result.AddRange(ByteUtilities.GetBytes(Version)); if (DestinationConnectionId > 0) - DCIL_SCIL = (byte)(DCIL_SCIL | 0x50); + DcilScil = (byte)(DcilScil | 0x50); if (SourceConnectionId > 0) - DCIL_SCIL = (byte)(DCIL_SCIL | 0x05); + DcilScil = (byte)(DcilScil | 0x05); - result.Add(DCIL_SCIL); + result.Add(DcilScil); if (DestinationConnectionId > 0) result.Add(DestinationConnectionId); @@ -71,7 +75,7 @@ public override byte[] Encode() result.Add(SourceConnectionId); byte[] tokenLength = new VariableInteger(0); - byte[] length = new VariableInteger(4 + (UInt64)frames.Length); + byte[] length = new VariableInteger(4 + (ulong)frames.Length); result.AddRange(tokenLength); result.AddRange(length); @@ -83,7 +87,7 @@ public override byte[] Encode() private byte EncodeTypeField() { - byte type = (byte)(Type | (byte)PacketType | 0x03); + var type = (byte)(Type | (byte)PacketType | 0x03); return type; } diff --git a/QuicNet.Infrastructure/Packets/Packet.cs b/QuicNet.Infrastructure/Packets/Packet.cs index db1a684..b34b34e 100644 --- a/QuicNet.Infrastructure/Packets/Packet.cs +++ b/QuicNet.Infrastructure/Packets/Packet.cs @@ -2,9 +2,7 @@ using QuicNet.Infrastructure.Exceptions; using QuicNet.Infrastructure.Frames; using QuicNet.Infrastructure.Settings; -using System; using System.Collections.Generic; -using System.Text; namespace QuicNet.Infrastructure.Packets { @@ -13,34 +11,35 @@ namespace QuicNet.Infrastructure.Packets /// public abstract class Packet { - protected List _frames = new List(); + protected List Frames = new List(); public abstract byte Type { get; } - public UInt32 Version { get; set; } + public uint Version { get; set; } public abstract byte[] Encode(); + public abstract void Decode(byte[] packet); public virtual void AttachFrame(Frame frame) { - _frames.Add(frame); + Frames.Add(frame); } public virtual List GetFrames() { - return _frames; + return Frames; } public virtual void DecodeFrames(ByteArray array) { - FrameParser factory = new FrameParser(array); + var factory = new FrameParser(array); Frame result; - int frames = 0; + var frames = 0; while (array.HasData() && frames <= QuicSettings.PMTU) { result = factory.GetFrame(); if (result != null) - _frames.Add(result); + Frames.Add(result); frames++; @@ -53,8 +52,8 @@ public virtual void DecodeFrames(ByteArray array) public virtual byte[] EncodeFrames() { - List result = new List(); - foreach (Frame frame in _frames) + var result = new List(); + foreach (var frame in Frames) { result.AddRange(frame.Encode()); } diff --git a/QuicNet.Infrastructure/Packets/ShortHeaderPacket.cs b/QuicNet.Infrastructure/Packets/ShortHeaderPacket.cs index 4a30972..52285a3 100644 --- a/QuicNet.Infrastructure/Packets/ShortHeaderPacket.cs +++ b/QuicNet.Infrastructure/Packets/ShortHeaderPacket.cs @@ -1,20 +1,20 @@ using QuickNet.Utilities; -using System; using System.Collections.Generic; -using System.Text; namespace QuicNet.Infrastructure.Packets { public class ShortHeaderPacket : Packet { public override byte Type => 0x43; // 0100 0011; + public byte DestinationConnectionId { get; set; } - public UInt32 PacketNumber { get; set; } + public uint PacketNumber { get; set; } + public override void Decode(byte[] packet) { - ByteArray array = new ByteArray(packet); - byte type = array.ReadByte(); + var array = new ByteArray(packet); + var type = array.ReadByte(); DestinationConnectionId = array.ReadByte(); PacketNumber = array.ReadUInt32(); @@ -23,9 +23,9 @@ public override void Decode(byte[] packet) public override byte[] Encode() { - byte[] frames = EncodeFrames(); + var frames = EncodeFrames(); - List result = new List(); + var result = new List(); result.Add(Type); result.Add(DestinationConnectionId); result.AddRange(ByteUtilities.GetBytes(PacketNumber)); diff --git a/QuicNet.Infrastructure/Packets/Unpacker.cs b/QuicNet.Infrastructure/Packets/Unpacker.cs index 85cbd86..0845215 100644 --- a/QuicNet.Infrastructure/Packets/Unpacker.cs +++ b/QuicNet.Infrastructure/Packets/Unpacker.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Infrastructure.Packets +namespace QuicNet.Infrastructure.Packets { public class Unpacker { @@ -12,7 +6,7 @@ public Packet Unpack(byte[] data) { Packet result = null; - QuicPacketType type = GetPacketType(data); + var type = GetPacketType(data); switch(type) { case QuicPacketType.Initial: result = new InitialPacket(); break; @@ -32,7 +26,7 @@ public QuicPacketType GetPacketType(byte[] data) if (data == null || data.Length <= 0) return QuicPacketType.Broken; - byte type = data[0]; + var type = data[0]; if ((type & 0xC0) == 0xC0) return QuicPacketType.Initial; diff --git a/QuicNet.Infrastructure/Packets/VersionNegotiationPacket.cs b/QuicNet.Infrastructure/Packets/VersionNegotiationPacket.cs index 6bf7ee6..2f6b618 100644 --- a/QuicNet.Infrastructure/Packets/VersionNegotiationPacket.cs +++ b/QuicNet.Infrastructure/Packets/VersionNegotiationPacket.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace QuicNet.Infrastructure.Packets { diff --git a/QuicNet.Infrastructure/Properties/AssemblyInfo.cs b/QuicNet.Infrastructure/Properties/AssemblyInfo.cs deleted file mode 100644 index 4aba79b..0000000 --- a/QuicNet.Infrastructure/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("QuicNet.Infrastructure")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("QuicNet.Infrastructure")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("8159f64b-7511-4dff-8998-efb029334a56")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QuicNet.Infrastructure/QuicNet.Infrastructure.csproj b/QuicNet.Infrastructure/QuicNet.Infrastructure.csproj index 8f54f88..c0b6347 100644 --- a/QuicNet.Infrastructure/QuicNet.Infrastructure.csproj +++ b/QuicNet.Infrastructure/QuicNet.Infrastructure.csproj @@ -1,89 +1,8 @@ - - - + - Debug - AnyCPU - {8159F64B-7511-4DFF-8998-EFB029334A56} - Library - Properties - QuicNet.Infrastructure - QuicNet.Infrastructure - v4.5.2 - 512 - + netstandard2.0 - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {5e344c85-825e-470b-9998-cf9425cb8b8e} - QuickNet.Utilities - + - \ No newline at end of file diff --git a/QuicNet.Infrastructure/QuicPacketType.cs b/QuicNet.Infrastructure/QuicPacketType.cs index a7a902a..1254173 100644 --- a/QuicNet.Infrastructure/QuicPacketType.cs +++ b/QuicNet.Infrastructure/QuicPacketType.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Infrastructure +namespace QuicNet.Infrastructure { public enum QuicPacketType { diff --git a/QuicNet.Infrastructure/Settings/QuicSettings.cs b/QuicNet.Infrastructure/Settings/QuicSettings.cs index f52fe23..9e5a512 100644 --- a/QuicNet.Infrastructure/Settings/QuicSettings.cs +++ b/QuicNet.Infrastructure/Settings/QuicSettings.cs @@ -1,57 +1,54 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace QuicNet.Infrastructure.Settings +namespace QuicNet.Infrastructure.Settings { public class QuicSettings { - /// - /// Path Maximum Transmission Unit. Indicates the mandatory initial packet capacity, and the maximum UDP packet capacity. + /// Path Maximum Transmission Unit. Indicates the mandatory initial packet capacity, and the maximum UDP packet + /// capacity. /// - public const int PMTU = 1200; + public const int Pmtu = 1200; /// - /// Does the server want the first connected client to decide it's initial connection id? + /// Does the server want the first connected client to decide it's initial connection id? /// public const bool CanAcceptInitialClientConnectionId = false; /// - /// TBD. quic-transport 5.1. + /// TBD. quic-transport 5.1. /// public const int MaximumConnectionIds = 8; /// - /// Maximum number of streams that connection can handle. + /// Maximum number of streams that connection can handle. /// public const int MaximumStreamId = 128; /// - /// Maximum packets that can be transferred before any data transfer (loss of packets, packet resent, infinite ack loop) + /// Maximum packets that can be transferred before any data transfer (loss of packets, packet resent, infinite ack + /// loop) /// public const int MaximumInitialPacketNumber = 100; /// - /// Should the server buffer packets that came before the initial packet? + /// Should the server buffer packets that came before the initial packet? /// public const bool ShouldBufferPacketsBeforeConnection = false; /// - /// Limit the maximum number of frames a packet can carry. + /// Limit the maximum number of frames a packet can carry. /// public const int MaximumFramesPerPacket = 10; /// - /// Maximum data that can be transferred for a Connection. - /// Currently 10MB. + /// Maximum data that can be transferred for a Connection. + /// Currently 10MB. /// public const int MaxData = 10 * 1000 * 1000; /// - /// Maximum data that can be transferred for a Stream. - /// Currently 0.078125 MB, which is MaxData / MaximumStreamId + /// Maximum data that can be transferred for a Stream. + /// Currently 0.078125 MB, which is MaxData / MaximumStreamId /// public const int MaxStreamData = 78125; } -} +} \ No newline at end of file diff --git a/QuicNet.Infrastructure/Settings/QuicVersion.cs b/QuicNet.Infrastructure/Settings/QuicVersion.cs index 7d3e508..bcd422f 100644 --- a/QuicNet.Infrastructure/Settings/QuicVersion.cs +++ b/QuicNet.Infrastructure/Settings/QuicVersion.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace QuicNet.Infrastructure.Settings { @@ -8,6 +6,6 @@ public class QuicVersion { public const int CurrentVersion = 16; - public static readonly List SupportedVersions = new List() { 15, 16 }; + public static readonly List SupportedVersions = new List() { 15, 16 }; } } diff --git a/QuicNet.Tests.ConsoleClient/App.config b/QuicNet.Tests.ConsoleClient/App.config index 7eb8d24..a932133 100644 --- a/QuicNet.Tests.ConsoleClient/App.config +++ b/QuicNet.Tests.ConsoleClient/App.config @@ -1,6 +1,6 @@ - + - + diff --git a/QuicNet.Tests.ConsoleClient/Program.cs b/QuicNet.Tests.ConsoleClient/Program.cs index de1244a..1e5cb44 100644 --- a/QuicNet.Tests.ConsoleClient/Program.cs +++ b/QuicNet.Tests.ConsoleClient/Program.cs @@ -1,33 +1,29 @@ -using QuicNet.Connections; -using QuicNet.Context; -using QuicNet.Streams; using System; -using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; +using QuickNet.Utilities; namespace QuicNet.Tests.ConsoleClient { - class Program + internal class Program { - static void Main(string[] args) + private static void Main() { Console.WriteLine("Starting client."); - QuicClient client = new QuicClient(); + var client = new QuicClient(); Console.WriteLine("Connecting to server."); - QuicConnection connection = client.Connect("127.0.0.1", 11000); // Connect to peer (Server) + var connection = client.Connect("127.0.0.1", 11000); // Connect to peer (Server) Console.WriteLine("Connected"); - - QuicStream stream = connection.CreateStream(QuickNet.Utilities.StreamType.ClientBidirectional); // Create a data stream - Console.WriteLine("Create stream with id: " + stream.StreamId.IntegerValue.ToString()); + + var stream = connection.CreateStream(StreamType.ClientBidirectional); // Create a data stream + Console.WriteLine("Create stream with id: " + stream.StreamId.IntegerValue); Console.WriteLine("Send 'Hello From Client!'"); - stream.Send(Encoding.UTF8.GetBytes("Hello from Client!")); // Send Data + stream.Send(Encoding.Unicode.GetBytes("Hello from Client!")); // Send Data Console.WriteLine("Waiting for message from the server"); + try { - byte[] data = stream.Receive(); // Receive from server + var data = stream.Receive(); // Receive from server Console.WriteLine("Received: " + Encoding.UTF8.GetString(data)); } catch (Exception e) @@ -35,7 +31,7 @@ static void Main(string[] args) Console.WriteLine(e.Message); } - Console.ReadKey(); + Console.ReadLine(); } } -} +} \ No newline at end of file diff --git a/QuicNet.Tests.ConsoleClient/Properties/AssemblyInfo.cs b/QuicNet.Tests.ConsoleClient/Properties/AssemblyInfo.cs deleted file mode 100644 index f4c0835..0000000 --- a/QuicNet.Tests.ConsoleClient/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("QuicNet.Tests.ConsoleClient")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("QuicNet.Tests.ConsoleClient")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("1ddc355e-7ff1-4ed5-8431-9d61711a06df")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QuicNet.Tests.ConsoleClient/QuicNet.Tests.ConsoleClient.csproj b/QuicNet.Tests.ConsoleClient/QuicNet.Tests.ConsoleClient.csproj index eaac64c..b4a4a86 100644 --- a/QuicNet.Tests.ConsoleClient/QuicNet.Tests.ConsoleClient.csproj +++ b/QuicNet.Tests.ConsoleClient/QuicNet.Tests.ConsoleClient.csproj @@ -1,35 +1,9 @@ - - - - - Debug - AnyCPU - {1DDC355E-7FF1-4ED5-8431-9D61711A06DF} + + + net472 + Exe - QuicNet.Tests.ConsoleClient - QuicNet.Tests.ConsoleClient - v4.6.1 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + @@ -42,25 +16,6 @@ - - - - - - - - - {5e344c85-825e-470b-9998-cf9425cb8b8e} - QuickNet.Utilities - - - {8159f64b-7511-4dff-8998-efb029334a56} - QuicNet.Infrastructure - - - {139530bb-cb8c-4f82-b7d9-31c445ef2f69} - QuicNet - + - \ No newline at end of file diff --git a/QuicNet.Tests.Unit/ByteArrayTests.cs b/QuicNet.Tests.Unit/ByteArrayTests.cs index 83da56f..09348b6 100644 --- a/QuicNet.Tests.Unit/ByteArrayTests.cs +++ b/QuicNet.Tests.Unit/ByteArrayTests.cs @@ -1,10 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; using QuickNet.Utilities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Tests.Unit { @@ -14,73 +10,70 @@ public class ByteArrayTests [TestMethod] public void SingleByte() { - byte[] data = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - ByteArray array = new ByteArray(data); + byte[] data = {1, 1, 2, 3, 5, 8, 13, 21, 34}; + var array = new ByteArray(data); - byte peek = array.PeekByte(); - byte result = array.ReadByte(); + var peek = array.PeekByte(); + var result = array.ReadByte(); - Assert.AreEqual(peek, (byte)1); - Assert.AreEqual(result, (byte)1); + Assert.AreEqual(peek, (byte) 1); + Assert.AreEqual(result, (byte) 1); } [TestMethod] public void SingleConsecutiveBytes() { - byte[] data = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - ByteArray array = new ByteArray(data); + byte[] data = {1, 1, 2, 3, 5, 8, 13, 21, 34}; + var array = new ByteArray(data); - byte r1 = array.ReadByte(); - byte r2 = array.ReadByte(); - byte r3 = array.ReadByte(); + var r1 = array.ReadByte(); + var r2 = array.ReadByte(); + var r3 = array.ReadByte(); - Assert.AreEqual(r1, (byte)1); - Assert.AreEqual(r2, (byte)1); - Assert.AreEqual(r3, (byte)2); + Assert.AreEqual(r1, (byte) 1); + Assert.AreEqual(r2, (byte) 1); + Assert.AreEqual(r3, (byte) 2); } [TestMethod] public void MultipleBytes() { - byte[] data = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - ByteArray array = new ByteArray(data); + byte[] data = {1, 1, 2, 3, 5, 8, 13, 21, 34}; + var array = new ByteArray(data); - byte[] result = array.ReadBytes(6); + var result = array.ReadBytes(6); Assert.AreEqual(result.Length, 6); - CollectionAssert.AreEquivalent(result, new byte[] { 1, 1, 2, 3, 5, 8 }); + CollectionAssert.AreEquivalent(result, new byte[] {1, 1, 2, 3, 5, 8}); } [TestMethod] public void ReadShort() { - byte[] data = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - ByteArray array = new ByteArray(data); + byte[] data = {1, 1, 2, 3, 5, 8, 13, 21, 34}; + var array = new ByteArray(data); - UInt16 result = array.ReadUInt16(); - Assert.AreEqual(result, (UInt16)257); + var result = array.ReadUInt16(); + Assert.AreEqual(result, (ushort) 257); } [TestMethod] public void ReadInteger() { - byte[] data = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - ByteArray array = new ByteArray(data); + byte[] data = {1, 1, 2, 3, 5, 8, 13, 21, 34}; + var array = new ByteArray(data); - UInt32 result = array.ReadUInt32(); - Assert.AreEqual(result, (UInt32)16843267); + var result = array.ReadUInt32(); + Assert.AreEqual(result, (uint) 16843267); } [TestMethod] public void ReadTooMany() { - byte[] data = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; - ByteArray array = new ByteArray(data); + byte[] data = {1, 1, 2, 3, 5, 8, 13, 21, 34}; + var array = new ByteArray(data); - Assert.ThrowsException(() => - { - byte[] result = array.ReadBytes(10); - }); + Assert.ThrowsException(() => { array.ReadBytes(10); }); } } -} +} \ No newline at end of file diff --git a/QuicNet.Tests.Unit/Properties/AssemblyInfo.cs b/QuicNet.Tests.Unit/Properties/AssemblyInfo.cs index c7cd503..2cd178d 100644 --- a/QuicNet.Tests.Unit/Properties/AssemblyInfo.cs +++ b/QuicNet.Tests.Unit/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; [assembly: AssemblyTitle("QuicNet.Tests.Unit")] diff --git a/QuicNet.Tests.Unit/QuicNet.Tests.Unit.csproj b/QuicNet.Tests.Unit/QuicNet.Tests.Unit.csproj index 8a16c1d..cb3a345 100644 --- a/QuicNet.Tests.Unit/QuicNet.Tests.Unit.csproj +++ b/QuicNet.Tests.Unit/QuicNet.Tests.Unit.csproj @@ -53,9 +53,6 @@ - - - {5e344c85-825e-470b-9998-cf9425cb8b8e} @@ -70,6 +67,9 @@ QuicNet + + + diff --git a/QuicNet.Tests.Unit/StreamIdTests.cs b/QuicNet.Tests.Unit/StreamIdTests.cs index b47a26a..617e8ca 100644 --- a/QuicNet.Tests.Unit/StreamIdTests.cs +++ b/QuicNet.Tests.Unit/StreamIdTests.cs @@ -1,10 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using QuickNet.Utilities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Tests.Unit { @@ -14,7 +9,7 @@ public class StreamIdTests [TestMethod] public void ClientBidirectional() { - StreamId id = new StreamId(123, StreamType.ClientBidirectional); + var id = new StreamId(123, StreamType.ClientBidirectional); byte[] data = id; Assert.IsNotNull(data); @@ -26,7 +21,7 @@ public void ClientBidirectional() [TestMethod] public void ClientUnidirectional() { - StreamId id = new StreamId(123, StreamType.ClientUnidirectional); + var id = new StreamId(123, StreamType.ClientUnidirectional); byte[] data = id; Assert.IsNotNull(data); @@ -38,7 +33,7 @@ public void ClientUnidirectional() [TestMethod] public void ServerBidirectional() { - StreamId id = new StreamId(123, StreamType.ServerBidirectional); + var id = new StreamId(123, StreamType.ServerBidirectional); byte[] data = id; Assert.IsNotNull(data); @@ -50,7 +45,7 @@ public void ServerBidirectional() [TestMethod] public void ServerUnidirectional() { - StreamId id = new StreamId(123, StreamType.ServerUnidirectional); + var id = new StreamId(123, StreamType.ServerUnidirectional); byte[] data = id; Assert.IsNotNull(data); @@ -59,4 +54,4 @@ public void ServerUnidirectional() Assert.AreEqual(data[7], 239); } } -} +} \ No newline at end of file diff --git a/QuicNet.Tests.Unit/VariableIntegerTests.cs b/QuicNet.Tests.Unit/VariableIntegerTests.cs index f60998f..1a74036 100644 --- a/QuicNet.Tests.Unit/VariableIntegerTests.cs +++ b/QuicNet.Tests.Unit/VariableIntegerTests.cs @@ -10,76 +10,76 @@ public class VariableIntegerTests [TestMethod] public void Zero() { - VariableInteger integer = new VariableInteger(0); + var integer = new VariableInteger(0); byte[] bin = integer; - UInt64 num = integer; + ulong num = integer; Assert.IsNotNull(bin); Assert.AreEqual(bin.Length, 1); - Assert.AreEqual(bin[0], (byte)0); - Assert.AreEqual(num, (UInt64)0); + Assert.AreEqual(bin[0], (byte) 0); + Assert.AreEqual(num, (ulong) 0); } [TestMethod] public void One() { - VariableInteger integer = new VariableInteger(1); + var integer = new VariableInteger(1); byte[] bin = integer; - UInt64 num = integer; + ulong num = integer; Assert.IsNotNull(bin); Assert.AreEqual(bin.Length, 1); - Assert.AreEqual(bin[0], (byte)1); - Assert.AreEqual(num, (UInt64)1); + Assert.AreEqual(bin[0], (byte) 1); + Assert.AreEqual(num, (ulong) 1); } [TestMethod] public void Test63() { - VariableInteger integer = new VariableInteger(63); + var integer = new VariableInteger(63); byte[] bin = integer; - UInt64 num = integer; + ulong num = integer; Assert.IsNotNull(bin); Assert.AreEqual(bin.Length, 1); - Assert.AreEqual(bin[0], (byte)63); - Assert.AreEqual(num, (UInt64)63); + Assert.AreEqual(bin[0], (byte) 63); + Assert.AreEqual(num, (ulong) 63); } [TestMethod] public void Test64() { - VariableInteger integer = new VariableInteger(64); + var integer = new VariableInteger(64); byte[] bin = integer; - UInt64 num = integer; + ulong num = integer; Assert.IsNotNull(bin); Assert.AreEqual(bin.Length, 2); - Assert.AreEqual(bin[0], (byte)64); - Assert.AreEqual(bin[1], (byte)64); - Assert.AreEqual(num, (UInt64)64); + Assert.AreEqual(bin[0], (byte) 64); + Assert.AreEqual(bin[1], (byte) 64); + Assert.AreEqual(num, (ulong) 64); } [TestMethod] public void Test256() { - VariableInteger integer = new VariableInteger(256); + var integer = new VariableInteger(256); byte[] bin = integer; - UInt64 num = integer; + ulong num = integer; Assert.IsNotNull(bin); Assert.AreEqual(bin.Length, 2); - Assert.AreEqual(bin[0], (byte)65); - Assert.AreEqual(bin[1], (byte)0); - Assert.AreEqual(num, (UInt64)256); + Assert.AreEqual(bin[0], (byte) 65); + Assert.AreEqual(bin[1], (byte) 0); + Assert.AreEqual(num, (ulong) 256); } [TestMethod] public void TestVariableIntegerMaxValue() { - VariableInteger integer = new VariableInteger(VariableInteger.MaxValue); + var integer = new VariableInteger(VariableInteger.MaxValue); byte[] bin = integer; - UInt64 num = integer; + ulong num = integer; Assert.IsNotNull(bin); Assert.AreEqual(bin.Length, 8); @@ -89,12 +89,7 @@ public void TestVariableIntegerMaxValue() [TestMethod] public void TestUInt64MaxValue() { - Assert.ThrowsException(() => - { - VariableInteger integer = new VariableInteger(UInt64.MaxValue); - byte[] bin = integer; - UInt64 num = integer; - }); + Assert.ThrowsException(() => new VariableInteger(ulong.MaxValue)); } } -} +} \ No newline at end of file diff --git a/QuicNet.sln b/QuicNet.sln index c8b3795..c601541 100644 --- a/QuicNet.sln +++ b/QuicNet.sln @@ -1,18 +1,18 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2042 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29209.152 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuicNet", "QuicNet\QuicNet.csproj", "{139530BB-CB8C-4F82-B7D9-31C445EF2F69}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuicNet", "QuicNet\QuicNet.csproj", "{139530BB-CB8C-4F82-B7D9-31C445EF2F69}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickNet.Utilities", "QuickNet.Utilities\QuickNet.Utilities.csproj", "{5E344C85-825E-470B-9998-CF9425CB8B8E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuickNet.Utilities", "QuickNet.Utilities\QuickNet.Utilities.csproj", "{5E344C85-825E-470B-9998-CF9425CB8B8E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickNet.Tests.ConsoleServer", "QuickNet.Tests.ConsoleServer\QuickNet.Tests.ConsoleServer.csproj", "{D348C0C7-7F21-4E6D-855B-397BCC35F4B6}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuicNet.Infrastructure", "QuicNet.Infrastructure\QuicNet.Infrastructure.csproj", "{8159F64B-7511-4DFF-8998-EFB029334A56}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuicNet.Infrastructure", "QuicNet.Infrastructure\QuicNet.Infrastructure.csproj", "{8159F64B-7511-4DFF-8998-EFB029334A56}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuicNet.Tests.Unit", "QuicNet.Tests.Unit\QuicNet.Tests.Unit.csproj", "{4170C9D8-B2D1-4E59-9F5F-C9665CAD9A37}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickNet.Tests.ConsoleServer", "QuickNet.Tests.ConsoleServer\QuickNet.Tests.ConsoleServer.csproj", "{D348C0C7-7F21-4E6D-855B-397BCC35F4B6}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuicNet.Tests.ConsoleClient", "QuicNet.Tests.ConsoleClient\QuicNet.Tests.ConsoleClient.csproj", "{1DDC355E-7FF1-4ED5-8431-9D61711A06DF}" EndProject Global @@ -29,10 +29,6 @@ Global {5E344C85-825E-470B-9998-CF9425CB8B8E}.Debug|Any CPU.Build.0 = Debug|Any CPU {5E344C85-825E-470B-9998-CF9425CB8B8E}.Release|Any CPU.ActiveCfg = Release|Any CPU {5E344C85-825E-470B-9998-CF9425CB8B8E}.Release|Any CPU.Build.0 = Release|Any CPU - {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Release|Any CPU.Build.0 = Release|Any CPU {8159F64B-7511-4DFF-8998-EFB029334A56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8159F64B-7511-4DFF-8998-EFB029334A56}.Debug|Any CPU.Build.0 = Debug|Any CPU {8159F64B-7511-4DFF-8998-EFB029334A56}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -41,6 +37,10 @@ Global {4170C9D8-B2D1-4E59-9F5F-C9665CAD9A37}.Debug|Any CPU.Build.0 = Debug|Any CPU {4170C9D8-B2D1-4E59-9F5F-C9665CAD9A37}.Release|Any CPU.ActiveCfg = Release|Any CPU {4170C9D8-B2D1-4E59-9F5F-C9665CAD9A37}.Release|Any CPU.Build.0 = Release|Any CPU + {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D348C0C7-7F21-4E6D-855B-397BCC35F4B6}.Release|Any CPU.Build.0 = Release|Any CPU {1DDC355E-7FF1-4ED5-8431-9D61711A06DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1DDC355E-7FF1-4ED5-8431-9D61711A06DF}.Debug|Any CPU.Build.0 = Debug|Any CPU {1DDC355E-7FF1-4ED5-8431-9D61711A06DF}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/QuicNet/Connections/ConnectionPool.cs b/QuicNet/Connections/ConnectionPool.cs index 7c7c33a..75bd252 100644 --- a/QuicNet/Connections/ConnectionPool.cs +++ b/QuicNet/Connections/ConnectionPool.cs @@ -1,66 +1,60 @@ -using QuicNet.Context; +using System.Collections.Generic; using QuicNet.Infrastructure; using QuicNet.Infrastructure.Settings; using QuicNet.InternalInfrastructure; -using System; -using System.Collections.Generic; -using System.Text; namespace QuicNet.Connections { /// - /// Since UDP is a stateless protocol, the ConnectionPool is used as a Conenction Manager to - /// route packets to the right "Connection". + /// Since UDP is a stateless protocol, the ConnectionPool is used as a Conenction Manager to + /// route packets to the right "Connection". /// internal static class ConnectionPool { /// - /// Starting point for connection identifiers. - /// ConnectionId's are incremented sequentially by 1. + /// Starting point for connection identifiers. + /// ConnectionId's are incremented sequentially by 1. /// - private static NumberSpace _ns = new NumberSpace(QuicSettings.MaximumConnectionIds); + private static readonly NumberSpace Ns = new NumberSpace(QuicSettings.MaximumConnectionIds); - private static Dictionary _pool = new Dictionary(); - - private static List _draining = new List(); + private static readonly Dictionary Pool = new Dictionary(); /// - /// Adds a connection to the connection pool. - /// For now assume that the client connection id is valid, and just send it back. - /// Later this should change in a way that the server validates, and regenerates a connection Id. + /// Adds a connection to the connection pool. + /// For now assume that the client connection id is valid, and just send it back. + /// Later this should change in a way that the server validates, and regenerates a connection Id. /// - /// Connection Id + /// ConnectionData object to read values from + /// The ConnectionId available /// - public static bool AddConnection(ConnectionData connection, out UInt32 availableConnectionId) + public static bool AddConnection(ConnectionData connection, out uint availableConnectionId) { availableConnectionId = 0; - if (_pool.ContainsKey(connection.ConnectionId)) + if (Pool.ContainsKey(connection.ConnectionId)) return false; - - if (_pool.Count > QuicSettings.MaximumConnectionIds) + if (Pool.Count > QuicSettings.MaximumConnectionIds) return false; - availableConnectionId = _ns.Get(); - + availableConnectionId = Ns.Get(); connection.PeerConnectionId = connection.ConnectionId; - _pool.Add(availableConnectionId, new QuicConnection(connection)); + Pool.Add(availableConnectionId, new QuicConnection(connection)); return true; } - public static void RemoveConnection(UInt32 id) + public static void RemoveConnection(uint id) { - if (_pool.ContainsKey(id)) - _pool.Remove(id); + if (Pool.ContainsKey(id)) + Pool.Remove(id); } - public static QuicConnection Find(UInt32 id) + public static QuicConnection Find(uint id) { - if (_pool.ContainsKey(id) == false) + if (Pool.ContainsKey(id) == false) return null; - return _pool[id]; + return Pool[id]; } } -} +} \ No newline at end of file diff --git a/QuicNet/Connections/ConnectionState.cs b/QuicNet/Connections/ConnectionState.cs index 4d0dbef..d16b8b8 100644 --- a/QuicNet/Connections/ConnectionState.cs +++ b/QuicNet/Connections/ConnectionState.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Connections +namespace QuicNet.Connections { public enum ConnectionState { @@ -13,4 +7,4 @@ public enum ConnectionState Closed, Draining } -} +} \ No newline at end of file diff --git a/QuicNet/Connections/QuicConnection.cs b/QuicNet/Connections/QuicConnection.cs index a479d06..002ac8e 100644 --- a/QuicNet/Connections/QuicConnection.cs +++ b/QuicNet/Connections/QuicConnection.cs @@ -1,39 +1,39 @@ -using QuickNet.Utilities; -using QuicNet.Context; +using System; +using System.Collections.Generic; +using QuickNet.Utilities; using QuicNet.Exceptions; +using QuicNet.Infrastructure; 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 readonly PacketWireTransfer _pwt; + private readonly Dictionary _streams; + private ulong _currentTransferRate; private string _lastError; - private Dictionary _streams; + private ConnectionState _state; - private PacketWireTransfer _pwt; + public uint ConnectionId { get; } - public UInt32 ConnectionId { get; private set; } - public UInt32 PeerConnectionId { get; private set; } + public uint PeerConnectionId { get; } - public PacketCreator PacketCreator { get; private set; } - public UInt64 MaxData { get; private set; } - public UInt64 MaxStreams { get; private set; } + public PacketCreator PacketCreator { get; } + + public ulong MaxData { get; private set; } + + public ulong MaxStreams { get; private set; } public event Action OnDataReceived; /// - /// Creates a new stream for sending/receiving data. + /// Creates a new stream for sending/receiving data. /// /// Type of the stream (Uni-Bidirectional) /// A new stream instance or Null if the connection is terminated. @@ -42,7 +42,7 @@ public QuicStream CreateStream(StreamType type) if (_state != ConnectionState.Open) return null; - QuicStream stream = new QuicStream(this, new QuickNet.Utilities.StreamId(0, type)); + var stream = new QuicStream(this, new StreamId(0, type)); _streams.Add(0, stream); return stream; @@ -50,7 +50,7 @@ public QuicStream CreateStream(StreamType type) public void ProcessFrames(List frames) { - foreach (Frame frame in frames) + foreach (var frame in frames) { if (frame.Type == 0x01) OnRstStreamFrame(frame); @@ -73,7 +73,7 @@ public void ProcessFrames(List frames) public void IncrementRate(int length) { - _currentTransferRate += (UInt32)length; + _currentTransferRate += (uint) length; } public bool MaximumReached() @@ -86,18 +86,18 @@ public bool MaximumReached() private void OnConnectionCloseFrame(Frame frame) { - ConnectionCloseFrame ccf = (ConnectionCloseFrame)frame; + var ccf = (ConnectionCloseFrame) frame; _state = ConnectionState.Draining; _lastError = ccf.ReasonPhrase; } private void OnRstStreamFrame(Frame frame) { - ResetStreamFrame rsf = (ResetStreamFrame)frame; + var rsf = (ResetStreamFrame) frame; if (_streams.ContainsKey(rsf.StreamId)) { // Find and reset the stream - QuicStream stream = _streams[rsf.StreamId]; + var stream = _streams[rsf.StreamId]; stream.ResetStream(rsf); // Remove the stream from the connection @@ -107,101 +107,89 @@ private void OnRstStreamFrame(Frame frame) private void OnStreamFrame(Frame frame) { - StreamFrame sf = (StreamFrame)frame; + var sf = (StreamFrame) frame; if (_streams.ContainsKey(sf.ConvertedStreamId.Id) == false) { - QuicStream stream = new QuicStream(this, sf.ConvertedStreamId); + var stream = new QuicStream(this, sf.ConvertedStreamId); stream.ProcessData(sf); - if ((UInt64)_streams.Count < MaxStreams) + if ((ulong) _streams.Count < MaxStreams) _streams.Add(sf.StreamId.Value, stream); else SendMaximumStreamReachedError(); } else { - QuicStream stream = _streams[sf.StreamId]; + var stream = _streams[sf.StreamId]; stream.ProcessData(sf); } } private void OnMaxDataFrame(Frame frame) { - MaxDataFrame sf = (MaxDataFrame)frame; + var sf = (MaxDataFrame) frame; if (sf.MaximumData.Value > MaxData) MaxData = sf.MaximumData.Value; } private void OnMaxStreamDataFrame(Frame frame) { - MaxStreamDataFrame msdf = (MaxStreamDataFrame)frame; + var msdf = (MaxStreamDataFrame) frame; if (_streams.ContainsKey(msdf.StreamId)) { // Find and set the new maximum stream data on the stream - QuicStream stream = _streams[msdf.ConvertedStreamId.Id]; + var stream = _streams[msdf.ConvertedStreamId.Id]; stream.SetMaximumStreamData(msdf.MaximumStreamData.Value); } } private void OnMaxStreamFrame(Frame frame) { - MaxStreamsFrame msf = (MaxStreamsFrame)frame; + var msf = (MaxStreamsFrame) frame; if (msf.MaximumStreams > MaxStreams) MaxStreams = msf.MaximumStreams.Value; } + // ReSharper disable once UnusedParameter.Local + // See comment below private void OnDataBlockedFrame(Frame frame) { // TODO: Tuning of data transfer. + // Since no stream id is present on this frame, we should be // stopping the connection. - TerminateConnection(); } - private void OnStreamDataBlockedFrame(Frame frame) - { - StreamDataBlockedFrame sdbf = (StreamDataBlockedFrame)frame; - - if (_streams.ContainsKey(sdbf.ConvertedStreamId.Id) == false) - return; - QuicStream stream = _streams[sdbf.ConvertedStreamId.Id]; - - stream.ProcessStreamDataBlocked(sdbf); - - // Remove the stream from the connection - _streams.Remove(sdbf.ConvertedStreamId.Id); - } - #region Internal - + internal QuicConnection(ConnectionData connection) { _currentTransferRate = 0; _state = ConnectionState.Open; _lastError = string.Empty; - _streams = new Dictionary(); - _pwt = connection.PWT; + _streams = new Dictionary(); + _pwt = connection.Pwt; ConnectionId = connection.ConnectionId; PeerConnectionId = connection.PeerConnectionId; // Also creates a new number space - PacketCreator = new PacketCreator(ConnectionId, PeerConnectionId); + PacketCreator = new PacketCreator(PeerConnectionId); MaxData = QuicSettings.MaxData; MaxStreams = QuicSettings.MaximumStreamId; } /// - /// Client only! + /// Client only! /// /// internal void ReceivePacket() { - Packet packet = _pwt.ReadPacket(); + var packet = _pwt.ReadPacket(); if (packet is ShortHeaderPacket) { - ShortHeaderPacket shp = (ShortHeaderPacket)packet; + var shp = (ShortHeaderPacket) packet; ProcessFrames(shp.GetFrames()); } @@ -215,7 +203,6 @@ internal void ReceivePacket() throw new QuicConnectivityException(_lastError); } - } internal bool SendData(Packet packet) @@ -233,24 +220,25 @@ internal void TerminateConnection() _state = ConnectionState.Draining; _streams.Clear(); - ConnectionPool.RemoveConnection(this.ConnectionId); + ConnectionPool.RemoveConnection(ConnectionId); } internal void SendMaximumStreamReachedError() { - ShortHeaderPacket packet = PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.STREAM_LIMIT_ERROR, "Maximum number of streams reached."); + var packet = PacketCreator.CreateConnectionClosePacket(ErrorCode.STREAM_LIMIT_ERROR, + "Maximum number of streams reached."); Send(packet); } /// - /// Used to send protocol packets to the peer. + /// Used to send protocol packets to the peer. /// /// /// internal bool Send(Packet packet) { // Encode the packet - byte[] data = packet.Encode(); + var data = packet.Encode(); // Increment the connection transfer rate IncrementRate(data.Length); @@ -258,20 +246,21 @@ internal bool Send(Packet packet) // 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."); + packet = PacketCreator.CreateConnectionClosePacket(ErrorCode.FLOW_CONTROL_ERROR, + "Maximum data transfer reached."); TerminateConnection(); } // Ignore empty packets - if (data == null || data.Length <= 0) + if (data.Length <= 0) return true; - bool result = _pwt.SendPacket(packet); + var result = _pwt.SendPacket(packet); return result; } #endregion } -} +} \ No newline at end of file diff --git a/QuicNet/Context/QuicStreamContext.cs b/QuicNet/Context/QuicStreamContext.cs index 563a699..2bee130 100644 --- a/QuicNet/Context/QuicStreamContext.cs +++ b/QuicNet/Context/QuicStreamContext.cs @@ -1,36 +1,24 @@ -using QuicNet.Infrastructure.Frames; -using QuicNet.Infrastructure.Packets; -using QuicNet.Streams; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using QuicNet.Streams; namespace QuicNet.Context { /// - /// Wrapper to represent the stream. + /// Wrapper to represent the stream. /// public class QuicStreamContext { /// - /// The connection's context. - /// - // public QuicContext ConnectionContext { get; set; } - - /// - /// Data received + /// Data received /// public byte[] Data { get; private set; } /// - /// Unique stream identifier + /// Unique stream identifier /// - public UInt64 StreamId { get; private set; } + public ulong StreamId { get; } /// - /// Send data to the client. + /// Send data to the client. /// /// /// @@ -43,12 +31,6 @@ public bool Send(byte[] data) if (data == null || data.Length <= 0) return true; - // Packet packet = ConnectionContext.Connection.PacketCreator.CreateDataPacket(StreamId, data); - - // bool result = ConnectionContext.Send(packet); - - //return result; - return false; } @@ -63,10 +45,9 @@ public void Close() internal QuicStream Stream { get; set; } /// - /// Internal constructor to prevent creating the context outside the scope of Quic. + /// Internal constructor to prevent creating the context outside the scope of Quic. /// - /// - /// + /// The stream to set internal QuicStreamContext(QuicStream stream) { Stream = stream; @@ -80,4 +61,4 @@ internal void SetData(byte[] data) #endregion } -} +} \ No newline at end of file diff --git a/QuicNet/Exceptions/QuicConnectivityException.cs b/QuicNet/Exceptions/QuicConnectivityException.cs index a0f61c1..d84742f 100644 --- a/QuicNet/Exceptions/QuicConnectivityException.cs +++ b/QuicNet/Exceptions/QuicConnectivityException.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Exceptions { @@ -12,4 +8,4 @@ public QuicConnectivityException(string message) : base(message) { } } -} +} \ No newline at end of file diff --git a/QuicNet/Exceptions/QuicListenerNotStartedException.cs b/QuicNet/Exceptions/QuicListenerNotStartedException.cs index 2c9add0..16fb284 100644 --- a/QuicNet/Exceptions/QuicListenerNotStartedException.cs +++ b/QuicNet/Exceptions/QuicListenerNotStartedException.cs @@ -1,17 +1,11 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Exceptions { public class QuicListenerNotStartedException : Exception { - public QuicListenerNotStartedException() { } - public QuicListenerNotStartedException(string message) : base(message) { } } -} +} \ No newline at end of file diff --git a/QuicNet/Exceptions/StreamException.cs b/QuicNet/Exceptions/StreamException.cs index bfaf2ad..ace2dff 100644 --- a/QuicNet/Exceptions/StreamException.cs +++ b/QuicNet/Exceptions/StreamException.cs @@ -1,18 +1,11 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuicNet.Exceptions { public class StreamException : Exception { - public StreamException() { } - public StreamException(string message) : base(message) { - } } -} +} \ No newline at end of file diff --git a/QuicNet/InternalInfrastructure/ConnectionData.cs b/QuicNet/InternalInfrastructure/ConnectionData.cs index a2bc012..baeb888 100644 --- a/QuicNet/InternalInfrastructure/ConnectionData.cs +++ b/QuicNet/InternalInfrastructure/ConnectionData.cs @@ -1,24 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.InternalInfrastructure +namespace QuicNet.InternalInfrastructure { internal class ConnectionData { - public PacketWireTransfer PWT { get; set; } - public UInt32 ConnectionId { get; set; } - public UInt32 PeerConnectionId { get; set; } - - public ConnectionData(PacketWireTransfer pwt, UInt32 connectionId, UInt32 peerConnnectionId) + public ConnectionData(PacketWireTransfer pwt, uint connectionId, uint peerConnnectionId) { - PWT = pwt; + Pwt = pwt; ConnectionId = connectionId; PeerConnectionId = peerConnnectionId; } + + public PacketWireTransfer Pwt { get; set; } + public uint ConnectionId { get; set; } + public uint PeerConnectionId { get; set; } } -} +} \ No newline at end of file diff --git a/QuicNet/InternalInfrastructure/PacketWireTransfer.cs b/QuicNet/InternalInfrastructure/PacketWireTransfer.cs index 3b68e77..849029c 100644 --- a/QuicNet/InternalInfrastructure/PacketWireTransfer.cs +++ b/QuicNet/InternalInfrastructure/PacketWireTransfer.cs @@ -1,47 +1,38 @@ -using QuicNet.Exceptions; -using QuicNet.Infrastructure.Packets; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; +using System.Net; using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; +using QuicNet.Exceptions; +using QuicNet.Infrastructure.Packets; namespace QuicNet.InternalInfrastructure { internal class PacketWireTransfer { - private UdpClient _client; - private IPEndPoint _peerEndpoint; + private readonly UdpClient _client; - private Unpacker _unpacker; + private readonly Unpacker _unpacker; + private IPEndPoint _peerEndpoint; public PacketWireTransfer(UdpClient client, IPEndPoint peerEndpoint) { _client = client; _peerEndpoint = peerEndpoint; - _unpacker = new Unpacker(); } public Packet ReadPacket() { // Await response for sucessfull connection creation by the server - byte[] peerData = _client.Receive(ref _peerEndpoint); + var peerData = _client.Receive(ref _peerEndpoint); if (peerData == null) throw new QuicConnectivityException("Server did not respond properly."); - Packet packet = _unpacker.Unpack(peerData); - - return packet; + return _unpacker.Unpack(peerData); } public bool SendPacket(Packet packet) { - byte[] data = packet.Encode(); - - int sent = _client.Send(data, data.Length, _peerEndpoint); + var data = packet.Encode(); + var sent = _client.Send(data, data.Length, _peerEndpoint); return sent > 0; } @@ -51,4 +42,4 @@ public IPEndPoint LastTransferEndpoint() return _peerEndpoint; } } -} +} \ No newline at end of file diff --git a/QuicNet/Properties/AssemblyInfo.cs b/QuicNet/Properties/AssemblyInfo.cs deleted file mode 100644 index 78b5471..0000000 --- a/QuicNet/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("QuicNet")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("QuicNet")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("139530bb-cb8c-4f82-b7d9-31c445ef2f69")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QuicNet/QuicClient.cs b/QuicNet/QuicClient.cs index e5e920b..ed5b6cd 100644 --- a/QuicNet/QuicClient.cs +++ b/QuicNet/QuicClient.cs @@ -1,49 +1,36 @@ -using QuickNet.Utilities; +using System.Net; +using System.Net.Sockets; using QuicNet.Connections; -using QuicNet.Context; using QuicNet.Exceptions; 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.Linq; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; namespace QuicNet { /// - /// Quic Client. Used for sending and receiving data from a Quic Server. + /// Quic Client. Used for sending and receiving data from a Quic Server. /// public class QuicClient { - private IPEndPoint _peerIp; - private UdpClient _client; + private readonly UdpClient _client; - private QuicConnection _connection; - private QuicStream _stream; + private readonly InitialPacketCreator _packetCreator; - private Unpacker _unpacker; - private InitialPacketCreator _packetCreator; + private QuicConnection _connection; + private IPEndPoint _peerIp; - private UInt64 _maximumStreams = QuicSettings.MaximumStreamId; private PacketWireTransfer _pwt; public QuicClient() { _client = new UdpClient(); - _unpacker = new Unpacker(); _packetCreator = new InitialPacketCreator(); } /// - /// Connect to a remote server. + /// Connect to a remote server. /// /// Ip Address /// Port @@ -57,13 +44,13 @@ public QuicConnection Connect(string ip, int port) _pwt = new PacketWireTransfer(_client, _peerIp); // Start initial protocol process - InitialPacket connectionPacket = _packetCreator.CreateInitialPacket(0, 0); + var connectionPacket = _packetCreator.CreateInitialPacket(0, 0); // Send the initial packet _pwt.SendPacket(connectionPacket); // Await response for sucessfull connection creation by the server - InitialPacket packet = (InitialPacket)_pwt.ReadPacket(); + var packet = (InitialPacket) _pwt.ReadPacket(); HandleInitialFrames(packet); EstablishConnection(packet.SourceConnectionId, packet.SourceConnectionId); @@ -72,27 +59,17 @@ public QuicConnection Connect(string ip, int port) } /// - /// Handles initial packet's frames. (In most cases protocol frames) + /// Handles initial packet's frames. (In most cases protocol frames) /// /// private void HandleInitialFrames(Packet packet) { - List frames = packet.GetFrames(); - for (int i = frames.Count - 1; i > 0; i--) + var frames = packet.GetFrames(); + for (var i = frames.Count - 1; i > 0; i--) { - Frame frame = frames[i]; - if (frame is ConnectionCloseFrame) - { - ConnectionCloseFrame ccf = (ConnectionCloseFrame)frame; - + var frame = frames[i]; + if (frame is ConnectionCloseFrame ccf) throw new QuicConnectivityException(ccf.ReasonPhrase); - } - - if (frame is MaxStreamsFrame) - { - MaxStreamsFrame msf = (MaxStreamsFrame)frame; - _maximumStreams = msf.MaximumStreams.Value; - } // Break out if the first Padding Frame has been reached if (frame is PaddingFrame) @@ -101,14 +78,14 @@ private void HandleInitialFrames(Packet packet) } /// - /// Create a new connection + /// Create a new connection /// /// /// - private void EstablishConnection(UInt32 connectionId, UInt32 peerConnectionId) + private void EstablishConnection(uint connectionId, uint peerConnectionId) { - ConnectionData connection = new ConnectionData(_pwt, connectionId, peerConnectionId); + var connection = new ConnectionData(_pwt, connectionId, peerConnectionId); _connection = new QuicConnection(connection); } } -} +} \ No newline at end of file diff --git a/QuicNet/QuicListener.cs b/QuicNet/QuicListener.cs index c7d233c..7316d6b 100644 --- a/QuicNet/QuicListener.cs +++ b/QuicNet/QuicListener.cs @@ -1,6 +1,8 @@ -using QuickNet.Utilities; +using System; +using System.Net; +using System.Net.Sockets; +using QuickNet.Utilities; using QuicNet.Connections; -using QuicNet.Context; using QuicNet.Exceptions; using QuicNet.Infrastructure; using QuicNet.Infrastructure.Frames; @@ -8,46 +10,36 @@ using QuicNet.Infrastructure.Packets; using QuicNet.Infrastructure.Settings; using QuicNet.InternalInfrastructure; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; namespace QuicNet { /// - /// Quic Listener - a Quic server that processes incomming connections and if possible sends back data on it's peers. + /// Quic Listener - a Quic server that processes incomming connections and if possible sends back data on it's peers. /// public class QuicListener { - private UdpClient _client; + private readonly InitialPacketCreator _packetCreator; - private Unpacker _unpacker; - private InitialPacketCreator _packetCreator; + private readonly int _port; + private UdpClient _client; private PacketWireTransfer _pwt; - private int _port; private bool _started; /// - /// Create a new instance of QuicListener. + /// Create a new instance of QuicListener. /// /// The port that the server will listen on. public QuicListener(int port) { _started = false; _port = port; - - _unpacker = new Unpacker(); _packetCreator = new InitialPacketCreator(); } /// - /// Starts the listener. + /// Starts the listener. /// public void Start() { @@ -57,7 +49,7 @@ public void Start() } /// - /// Stops the listener. + /// Stops the listener. /// public void Close() { @@ -65,7 +57,7 @@ public void Close() } /// - /// Blocks and waits for incomming connection. Does NOT block additional incomming packets. + /// Blocks and waits for incomming connection. Does NOT block additional incomming packets. /// /// Returns an instance of QuicConnection. public QuicConnection AcceptQuicClient() @@ -73,16 +65,14 @@ public QuicConnection AcceptQuicClient() if (!_started) throw new QuicListenerNotStartedException("Please call the Start() method before receving data."); - /* - * Wait until there is initial packet incomming. - * Otherwise we still need to orchestrate any other protocol or data pakcets. - * */ + // Wait until there is initial packet incomming. + // Otherwise we still need to orchestrate any other protocol or data pakcets. while (true) { - Packet packet = _pwt.ReadPacket(); + var packet = _pwt.ReadPacket(); if (packet is InitialPacket) { - QuicConnection connection = ProcessInitialPacket(packet, _pwt.LastTransferEndpoint()); + var connection = ProcessInitialPacket(packet, _pwt.LastTransferEndpoint()); return connection; } @@ -91,37 +81,16 @@ public QuicConnection AcceptQuicClient() } /// - /// Starts receiving data from clients. - /// - private void Receive() - { - while (true) - { - Packet packet = _pwt.ReadPacket(); - - // Discard unknown packets - if (packet == null) - continue; - - // TODO: Validate packet before dispatching - OrchestratePacket(packet); - } - } - - /// - /// Orchestrates packets to connections, depending on the packet type. + /// Orchestrates packets to connections, depending on the packet type. /// /// private void OrchestratePacket(Packet packet) { - if (packet is ShortHeaderPacket) - { - ProcessShortHeaderPacket(packet); - } + if (packet is ShortHeaderPacket) ProcessShortHeaderPacket(packet); } /// - /// Processes incomming initial packet and creates or halts a connection. + /// Processes incomming initial packet and creates or halts a connection. /// /// Initial Packet /// Peer's endpoint @@ -129,30 +98,32 @@ private void OrchestratePacket(Packet packet) private QuicConnection ProcessInitialPacket(Packet packet, IPEndPoint endPoint) { QuicConnection result = null; - UInt32 availableConnectionId; byte[] data; // Unsupported version. Version negotiation packet is sent only on initial connection. All other packets are dropped. (5.2.2 / 16th draft) if (packet.Version != QuicVersion.CurrentVersion || !QuicVersion.SupportedVersions.Contains(packet.Version)) { - VersionNegotiationPacket vnp = _packetCreator.CreateVersionNegotiationPacket(); + var vnp = _packetCreator.CreateVersionNegotiationPacket(); data = vnp.Encode(); _client.Send(data, data.Length, endPoint); return null; } - InitialPacket cast = packet as InitialPacket; - InitialPacket ip = _packetCreator.CreateInitialPacket(0, cast.SourceConnectionId); + if (!(packet is InitialPacket cast)) + throw new ArgumentNullException(nameof(cast)); + + var ip = _packetCreator.CreateInitialPacket(0, cast.SourceConnectionId); // Protocol violation if the initial packet is smaller than the PMTU. (pt. 14 / 16th draft) - if (cast.Encode().Length < QuicSettings.PMTU) + if (cast.Encode().Length < QuicSettings.Pmtu) { ip.AttachFrame(new ConnectionCloseFrame(ErrorCode.PROTOCOL_VIOLATION, "PMTU have not been reached.")); } - else if (ConnectionPool.AddConnection(new ConnectionData(_pwt, cast.SourceConnectionId, 0), out availableConnectionId) == true) + else if (ConnectionPool.AddConnection(new ConnectionData(_pwt, cast.SourceConnectionId, 0), + out var availableConnectionId)) { // Tell the peer the available connection id - ip.SourceConnectionId = (byte)availableConnectionId; + ip.SourceConnectionId = (byte) availableConnectionId; // We're including the maximum possible stream id during the connection handshake. (4.5 / 16th draft) ip.AttachFrame(new MaxStreamsFrame(QuicSettings.MaximumStreamId, StreamType.ServerBidirectional)); @@ -165,11 +136,13 @@ private QuicConnection ProcessInitialPacket(Packet packet, IPEndPoint endPoint) // Not accepting connections. Send initial packet with CONNECTION_CLOSE frame. // TODO: Buffering. The server might buffer incomming 0-RTT packets in anticipation of late delivery InitialPacket. // Maximum buffer size should be set in QuicSettings. - ip.AttachFrame(new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, "The server is too busy to process your request.")); + ip.AttachFrame(new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, + "The server is too busy to process your request.")); } data = ip.Encode(); - int dataSent = _client.Send(data, data.Length, endPoint); + var dataSent = _client.Send(data, data.Length, endPoint); + if (dataSent > 0) return result; @@ -177,14 +150,14 @@ private QuicConnection ProcessInitialPacket(Packet packet, IPEndPoint endPoint) } /// - /// Processes short header packet, by distributing the frames towards connections. + /// Processes short header packet, by distributing the frames towards connections. /// /// private void ProcessShortHeaderPacket(Packet packet) { - ShortHeaderPacket shp = (ShortHeaderPacket)packet; + var shp = (ShortHeaderPacket) packet; - QuicConnection connection = ConnectionPool.Find(shp.DestinationConnectionId); + var connection = ConnectionPool.Find(shp.DestinationConnectionId); // No suitable connection found. Discard the packet. if (connection == null) @@ -193,4 +166,4 @@ private void ProcessShortHeaderPacket(Packet packet) connection.ProcessFrames(shp.GetFrames()); } } -} +} \ No newline at end of file diff --git a/QuicNet/QuicNet.csproj b/QuicNet/QuicNet.csproj index 7b0431c..2c1d378 100644 --- a/QuicNet/QuicNet.csproj +++ b/QuicNet/QuicNet.csproj @@ -1,77 +1,9 @@ - - - + - Debug - AnyCPU - {139530BB-CB8C-4F82-B7D9-31C445EF2F69} - Library - Properties - QuicNet - QuicNet - v4.5.2 - 512 + netstandard2.0 - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {5e344c85-825e-470b-9998-cf9425cb8b8e} - QuickNet.Utilities - - - {8159f64b-7511-4dff-8998-efb029334a56} - QuicNet.Infrastructure - + + - - - \ No newline at end of file diff --git a/QuicNet/Streams/QuicStream.cs b/QuicNet/Streams/QuicStream.cs index 7dbfe09..f970523 100644 --- a/QuicNet/Streams/QuicStream.cs +++ b/QuicNet/Streams/QuicStream.cs @@ -1,38 +1,23 @@ -using QuickNet.Utilities; +using System.Collections.Generic; +using System.Linq; +using QuickNet.Utilities; using QuicNet.Connections; -using QuicNet.Context; using QuicNet.Exceptions; +using QuicNet.Infrastructure; using QuicNet.Infrastructure.Frames; -using QuicNet.Infrastructure.Packets; using QuicNet.Infrastructure.Settings; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace QuicNet.Streams { /// - /// Virtual multiplexing channel. + /// Virtual multiplexing channel. /// public class QuicStream { - private SortedList _data = new SortedList(); - private QuicConnection _connection; - private UInt64 _maximumStreamData; - private UInt64 _currentTransferRate; - - public StreamState State { get; set; } - public StreamType Type { get; set; } - public StreamId StreamId { get; } - - public byte[] Data - { - get - { - return _data.SelectMany(v => v.Value).ToArray(); - } - } + private readonly QuicConnection _connection; + private readonly SortedList _data = new SortedList(); + private ulong _currentTransferRate; + private ulong _maximumStreamData; public QuicStream(QuicConnection connection, StreamId streamId) { @@ -45,6 +30,15 @@ public QuicStream(QuicConnection connection, StreamId streamId) _connection = connection; } + public StreamState State { get; set; } + public StreamType Type { get; set; } + public StreamId StreamId { get; } + + public byte[] Data + { + get { return _data.SelectMany(v => v.Value).ToArray(); } + } + public bool Send(byte[] data) { if (Type == StreamType.ServerUnidirectional) @@ -52,15 +46,15 @@ public bool Send(byte[] data) _connection.IncrementRate(data.Length); - ShortHeaderPacket packet = _connection.PacketCreator.CreateDataPacket(this.StreamId.IntegerValue, data); + var packet = _connection.PacketCreator.CreateDataPacket(StreamId.IntegerValue, data); if (_connection.MaximumReached()) - packet.AttachFrame(new StreamDataBlockedFrame(StreamId.IntegerValue, (UInt64)data.Length)); + packet.AttachFrame(new StreamDataBlockedFrame(StreamId.IntegerValue, (ulong) data.Length)); return _connection.SendData(packet); } /// - /// Client only! + /// Client only! /// /// public byte[] Receive() @@ -68,10 +62,7 @@ public byte[] Receive() if (Type == StreamType.ClientUnidirectional) throw new StreamException("Cannot receive data on unidirectional stream."); - while (!IsStreamFull() || State == StreamState.Recv) - { - _connection.ReceivePacket(); - } + while (!IsStreamFull() || State == StreamState.Recv) _connection.ReceivePacket(); return Data; } @@ -84,7 +75,7 @@ public void ResetStream(ResetStreamFrame frame) _data.Clear(); } - public void SetMaximumStreamData(UInt64 maximumData) + public void SetMaximumStreamData(ulong maximumData) { _maximumStreamData = maximumData; } @@ -93,7 +84,6 @@ public bool CanSendData() { if (Type == StreamType.ServerUnidirectional || Type == StreamType.ClientUnidirectional) return false; - if (State == StreamState.Recv || State == StreamState.SizeKnown) return true; @@ -106,28 +96,25 @@ public void ProcessData(StreamFrame frame) if (State == StreamState.ResetRecvd) return; - byte[] data = frame.StreamData; + var data = frame.StreamData; if (frame.Offset != null) - { _data.Add(frame.Offset.Value, frame.StreamData); - } else - { // TODO: Careful with duplicate 0 offset packets on the same stream. Probably PROTOCOL_VIOLATION? _data.Add(0, frame.StreamData); - } // Either this frame marks the end of the stream, // or fin frame came before the data frames if (frame.EndOfStream) State = StreamState.SizeKnown; - _currentTransferRate += (UInt64)data.Length; + _currentTransferRate += (ulong) data.Length; // Terminate connection if maximum stream data is reached if (_currentTransferRate >= _maximumStreamData) { - ShortHeaderPacket errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(Infrastructure.ErrorCode.FLOW_CONTROL_ERROR, "Maximum stream data transfer reached."); + var errorPacket = _connection.PacketCreator.CreateConnectionClosePacket(ErrorCode.FLOW_CONTROL_ERROR, + "Maximum stream data transfer reached."); _connection.SendData(errorPacket); _connection.TerminateConnection(); @@ -137,7 +124,6 @@ public void ProcessData(StreamFrame frame) if (State == StreamState.SizeKnown && IsStreamFull()) { _connection.DataReceived(this); - State = StreamState.DataRecvd; } } @@ -149,17 +135,17 @@ public void ProcessStreamDataBlocked(StreamDataBlockedFrame frame) private bool IsStreamFull() { - UInt64 length = 0; + ulong length = 0; foreach (var kvp in _data) { if (kvp.Key > 0 && kvp.Key != length) return false; - length = (UInt64)kvp.Value.Length; + length = (ulong) kvp.Value.Length; } return true; } } -} +} \ No newline at end of file diff --git a/QuicNet/Streams/StreamState.cs b/QuicNet/Streams/StreamState.cs index fb8b96a..4ca1c2e 100644 --- a/QuicNet/Streams/StreamState.cs +++ b/QuicNet/Streams/StreamState.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuicNet.Streams +namespace QuicNet.Streams { public enum StreamState { @@ -15,4 +9,4 @@ public enum StreamState DataRead, ResetRecvd } -} +} \ No newline at end of file diff --git a/QuickNet.Tests.ConsoleServer/App.config b/QuickNet.Tests.ConsoleServer/App.config index 88fa402..ecdcf8a 100644 --- a/QuickNet.Tests.ConsoleServer/App.config +++ b/QuickNet.Tests.ConsoleServer/App.config @@ -1,6 +1,6 @@ - + - + - \ No newline at end of file + diff --git a/QuickNet.Tests.ConsoleServer/Program.cs b/QuickNet.Tests.ConsoleServer/Program.cs index c47fdff..1d13922 100644 --- a/QuickNet.Tests.ConsoleServer/Program.cs +++ b/QuickNet.Tests.ConsoleServer/Program.cs @@ -1,117 +1,34 @@ -using QuickNet.Utilities; -using QuicNet; -using QuicNet.Connections; -using QuicNet.Context; -using QuicNet.Infrastructure; -using QuicNet.Infrastructure.Frames; -using QuicNet.Infrastructure.PacketProcessing; -using QuicNet.Infrastructure.Packets; -using System; -using System.Collections.Generic; -using System.Linq; +using System; using System.Text; -using System.Threading.Tasks; +using QuicNet; namespace QuickNet.Tests.ConsoleServer { - class Program + internal class Program { - static void Example() + private static void Main() { - QuicListener listener = new QuicListener(11000); + var listener = new QuicListener(11000); listener.Start(); while (true) { // Blocks while waiting for a connection - QuicConnection client = listener.AcceptQuicClient(); + var client = listener.AcceptQuicClient(); // Assign an action when a data is received from that client. - client.OnDataReceived += (c) => { + client.OnDataReceived += c => + { + var data = c.Data; - byte[] data = c.Data; - - Console.WriteLine("Data received: " + Encoding.UTF8.GetString(data)); + Console.WriteLine("Data received: " + Encoding.Unicode.GetString(data)); c.Send(Encoding.UTF8.GetBytes("Echo!")); }; } - } - - static void Main(string[] args) - { - Example(); - - byte[] bytes = new VariableInteger(12345); - VariableInteger integer = bytes; - UInt64 uinteger = integer; - int size = VariableInteger.Size(bytes[0]); - - InitialPacket packet = new InitialPacket() - { - Version = 16, - SourceConnectionId = 124, - DestinationConnectionId = 0, - PacketNumber = 777521, - TokenLength = 0 - }; - - packet = new InitialPacketCreator().CreateInitialPacket(124, 0); - - ConnectionCloseFrame frame = new ConnectionCloseFrame(ErrorCode.SERVER_BUSY, "The server is too busy to process your request."); - MaxStreamsFrame msidframe = new MaxStreamsFrame(144123, StreamType.ClientUnidirectional); - //packet.AttachFrame(frame); - packet.AttachFrame(msidframe); - - byte[] data = packet.Encode(); - string b64 = ToBase64(data); - - byte[] shpdata1 = new byte[] { 1, 1, 2, 3, 5, 8 }; - byte[] shpdata2 = new byte[] { 13, 21, 34, 55, 89, 144 }; - - ShortHeaderPacket shp = new ShortHeaderPacket(); - shp.DestinationConnectionId = 124; - shp.PacketNumber = 2; - - shp.AttachFrame(new StreamFrame() { StreamId = 1, Length = new VariableInteger((UInt64)shpdata2.Length), StreamData = shpdata2, Offset = 6, EndOfStream = true }); - shp.AttachFrame(new StreamFrame() { StreamId = 1, Length = new VariableInteger((UInt64)shpdata1.Length), StreamData = shpdata1, Offset = 0 }); - - string shpb64 = ToBase64(shp.Encode()); - - packet.Decode(data); - - byte[] ccfData = frame.Encode(); - frame.Decode(new ByteArray(ccfData)); - - byte[] streamIdData = new StreamId(123, StreamType.ClientUnidirectional); - StreamId streamId = streamIdData; - - QuicListener listener = new QuicListener(11000); - // listener.OnClientConnected += Listener_OnClientConnected; - listener.Start(); - } - - //private static void Listener_OnClientConnected(QuicContext obj) - //{ - // System.Console.WriteLine("Client connected."); - // obj.OnDataReceived += Obj_OnDataReceived; - //} - - private static void Obj_OnDataReceived(QuicStreamContext obj) - { - System.Console.WriteLine("Data received"); - foreach (byte b in obj.Data) - { - System.Console.Write(string.Format("{0},", b)); - } - // Echo back to the client - obj.Send(Encoding.UTF8.GetBytes("Echo!")); - } - - static string ToBase64(byte[] data) - { - return Convert.ToBase64String(data); + // ReSharper disable once FunctionNeverReturns + // We need this so the server actually responds } } -} +} \ No newline at end of file diff --git a/QuickNet.Tests.ConsoleServer/Properties/AssemblyInfo.cs b/QuickNet.Tests.ConsoleServer/Properties/AssemblyInfo.cs deleted file mode 100644 index 4783bfb..0000000 --- a/QuickNet.Tests.ConsoleServer/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("QuickNet.Console")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("QuickNet.Console")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("d348c0c7-7f21-4e6d-855b-397bcc35f4b6")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QuickNet.Tests.ConsoleServer/QuickNet.Tests.ConsoleServer.csproj b/QuickNet.Tests.ConsoleServer/QuickNet.Tests.ConsoleServer.csproj index 593e903..b4a4a86 100644 --- a/QuickNet.Tests.ConsoleServer/QuickNet.Tests.ConsoleServer.csproj +++ b/QuickNet.Tests.ConsoleServer/QuickNet.Tests.ConsoleServer.csproj @@ -1,36 +1,9 @@ - - - - - Debug - AnyCPU - {D348C0C7-7F21-4E6D-855B-397BCC35F4B6} + + + net472 + Exe - Properties - QuickNet.Tests.ConsoleServer - QuickNet.Tests.ConsoleServer - v4.5.2 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + @@ -43,32 +16,6 @@ - - - - - - - - - {5e344c85-825e-470b-9998-cf9425cb8b8e} - QuickNet.Utilities - - - {8159f64b-7511-4dff-8998-efb029334a56} - QuicNet.Infrastructure - - - {139530bb-cb8c-4f82-b7d9-31c445ef2f69} - QuicNet - + - - \ No newline at end of file diff --git a/QuickNet.Utilities/ByteArray.cs b/QuickNet.Utilities/ByteArray.cs index 6253941..d3983df 100644 --- a/QuickNet.Utilities/ByteArray.cs +++ b/QuickNet.Utilities/ByteArray.cs @@ -1,79 +1,60 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuickNet.Utilities { public class ByteArray { - private int _length; + private readonly byte[] _array; + private readonly int _length; private int _offset; - private bool _readable; - private byte[] _array; public ByteArray(byte[] array) { - _readable = true; _array = array; - if (array == null || array.Length <= 0) - { - _readable = false; - } - else - { - _length = array.Length; - _offset = 0; - } + _length = array.Length; + _offset = 0; } public byte ReadByte() { - byte result = _array[_offset++]; + var result = _array[_offset++]; return result; } public byte PeekByte() { - byte result = _array[_offset]; + var result = _array[_offset]; return result; } public byte[] ReadBytes(int count) { - byte[] bytes = new byte[count]; + var bytes = new byte[count]; Buffer.BlockCopy(_array, _offset, bytes, 0, count); - _offset += count; return bytes; } - public UInt16 ReadUInt16() + public ushort ReadUInt16() { - byte[] bytes = ReadBytes(2); - UInt16 result = ByteUtilities.ToUInt16(bytes); - - return result; + var bytes = ReadBytes(2); + return ByteUtilities.ToUInt16(bytes); } - public UInt32 ReadUInt32() + public uint ReadUInt32() { - byte[] bytes = ReadBytes(4); - UInt32 result = ByteUtilities.ToUInt32(bytes); - - return result; + return ByteUtilities.ToUInt32(ReadBytes(4)); } public VariableInteger ReadVariableInteger() { // Set Token Length and Token - byte initial = PeekByte(); - int size = VariableInteger.Size(initial); + var initial = PeekByte(); + var size = VariableInteger.Size(initial); - byte[] bytes = new byte[size]; + var bytes = new byte[size]; Buffer.BlockCopy(_array, _offset, bytes, 0, size); _offset += size; @@ -82,10 +63,7 @@ public VariableInteger ReadVariableInteger() public StreamId ReadStreamId() { - byte[] streamId = ReadBytes(8); - StreamId result = streamId; - - return result; + return ReadBytes(8); } public bool HasData() @@ -93,4 +71,4 @@ public bool HasData() return _offset < _length; } } -} +} \ No newline at end of file diff --git a/QuickNet.Utilities/ByteUtilities.cs b/QuickNet.Utilities/ByteUtilities.cs index 6a56551..b8a32db 100644 --- a/QuickNet.Utilities/ByteUtilities.cs +++ b/QuickNet.Utilities/ByteUtilities.cs @@ -1,34 +1,31 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; -using System.Threading.Tasks; namespace QuickNet.Utilities { public static class ByteUtilities { - public static byte[] GetBytes(UInt64 integer) + public static byte[] GetBytes(ulong integer) { - byte[] result = BitConverter.GetBytes(integer); + var result = BitConverter.GetBytes(integer); if (BitConverter.IsLittleEndian) Array.Reverse(result); return result; } - public static byte[] GetBytes(UInt32 integer) + public static byte[] GetBytes(uint integer) { - byte[] result = BitConverter.GetBytes(integer); + var result = BitConverter.GetBytes(integer); if (BitConverter.IsLittleEndian) Array.Reverse(result); return result; } - public static byte[] GetBytes(UInt16 integer) + public static byte[] GetBytes(ushort integer) { - byte[] result = BitConverter.GetBytes(integer); + var result = BitConverter.GetBytes(integer); if (BitConverter.IsLittleEndian) Array.Reverse(result); @@ -37,47 +34,36 @@ public static byte[] GetBytes(UInt16 integer) public static byte[] GetBytes(string str) { - byte[] result = Encoding.UTF8.GetBytes(str); - - return result; + return Encoding.Unicode.GetBytes(str); } - public static UInt64 ToUInt64(byte[] data) + public static ulong ToUInt64(byte[] data) { if (BitConverter.IsLittleEndian) Array.Reverse(data); - UInt64 result = BitConverter.ToUInt64(data, 0); - - return result; + return BitConverter.ToUInt64(data, 0); } - public static UInt32 ToUInt32(byte[] data) + public static uint ToUInt32(byte[] data) { if (BitConverter.IsLittleEndian) Array.Reverse(data); - UInt32 result = BitConverter.ToUInt32(data, 0); - - return result; + return BitConverter.ToUInt32(data, 0); } - public static UInt16 ToUInt16(byte[] data) + public static ushort ToUInt16(byte[] data) { if (BitConverter.IsLittleEndian) Array.Reverse(data); - UInt16 result = BitConverter.ToUInt16(data, 0); - - return result; + return BitConverter.ToUInt16(data, 0); } public static string GetString(byte[] str) { - string result = Encoding.UTF8.GetString(str); - - return result; + return Encoding.UTF8.GetString(str); } - } -} +} \ No newline at end of file diff --git a/QuickNet.Utilities/Properties/AssemblyInfo.cs b/QuickNet.Utilities/Properties/AssemblyInfo.cs deleted file mode 100644 index 37e6e4c..0000000 --- a/QuickNet.Utilities/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("QuickNet.Utilities")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("QuickNet.Utilities")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("5e344c85-825e-470b-9998-cf9425cb8b8e")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QuickNet.Utilities/QuickNet.Utilities.csproj b/QuickNet.Utilities/QuickNet.Utilities.csproj index fb6f441..17d3b29 100644 --- a/QuickNet.Utilities/QuickNet.Utilities.csproj +++ b/QuickNet.Utilities/QuickNet.Utilities.csproj @@ -1,57 +1,5 @@ - - - + - Debug - AnyCPU - {5E344C85-825E-470B-9998-CF9425CB8B8E} - Library - Properties - QuickNet.Utilities - QuickNet.Utilities - v4.5.2 - 512 + netstandard2.0 - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/QuickNet.Utilities/StreamId.cs b/QuickNet.Utilities/StreamId.cs index 67c734d..e1c7682 100644 --- a/QuickNet.Utilities/StreamId.cs +++ b/QuickNet.Utilities/StreamId.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace QuickNet.Utilities +namespace QuickNet.Utilities { public enum StreamType { @@ -16,17 +10,17 @@ public enum StreamType public class StreamId { - public UInt64 Id { get; } - public UInt64 IntegerValue { get; } - public StreamType Type { get; private set; } - - public StreamId(UInt64 id, StreamType type) + public StreamId(ulong id, StreamType type) { Id = id; Type = type; - IntegerValue = id << 2 | (UInt64)type; + IntegerValue = (id << 2) | (ulong) type; } + public ulong Id { get; } + public ulong IntegerValue { get; } + public StreamType Type { get; } + public static implicit operator byte[](StreamId id) { return Encode(id.Id, id.Type); @@ -37,31 +31,25 @@ public static implicit operator StreamId(byte[] data) return Decode(data); } - public static implicit operator UInt64(StreamId streamId) + public static implicit operator ulong(StreamId streamId) { return streamId.Id; } - public static byte[] Encode(UInt64 id, StreamType type) + public static byte[] Encode(ulong id, StreamType type) { - UInt64 identifier = id << 2 | (UInt64)type; - - byte[] result = ByteUtilities.GetBytes(identifier); - - return result; + var identifier = (id << 2) | (ulong) type; + return ByteUtilities.GetBytes(identifier); } public static StreamId Decode(byte[] data) { - StreamId result; - UInt64 id = ByteUtilities.ToUInt64(data); - UInt64 identifier = id >> 2; - UInt64 type = (UInt64)(0x03 & id); - StreamType streamType = (StreamType)type; - - result = new StreamId(identifier, streamType); + var id = ByteUtilities.ToUInt64(data); + var identifier = id >> 2; + var type = 0x03 & id; + var streamType = (StreamType) type; - return result; + return new StreamId(identifier, streamType); } } -} +} \ No newline at end of file diff --git a/QuickNet.Utilities/VariableInteger.cs b/QuickNet.Utilities/VariableInteger.cs index ffd019b..fde2de0 100644 --- a/QuickNet.Utilities/VariableInteger.cs +++ b/QuickNet.Utilities/VariableInteger.cs @@ -1,26 +1,21 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace QuickNet.Utilities { public class VariableInteger { - public const UInt64 MaxValue = 4611686018427387903; + public const ulong MaxValue = 4611686018427387903; - private UInt64 _integer; - public UInt64 Value { get { return _integer; } } - - public VariableInteger(UInt64 integer) + public VariableInteger(ulong integer) { - _integer = integer; + Value = integer; } + public ulong Value { get; } + public static implicit operator byte[](VariableInteger integer) { - return Encode(integer._integer); + return Encode(integer.Value); } public static implicit operator VariableInteger(byte[] bytes) @@ -28,61 +23,58 @@ public static implicit operator VariableInteger(byte[] bytes) return new VariableInteger(Decode(bytes)); } - public static implicit operator VariableInteger(UInt64 integer) + public static implicit operator VariableInteger(ulong integer) { return new VariableInteger(integer); } - public static implicit operator UInt64(VariableInteger integer) + public static implicit operator ulong(VariableInteger integer) { - return integer._integer; + return integer.Value; } public static int Size(byte firstByte) { - int result = (int)Math.Pow(2, (firstByte >> 6)); - - return result; + return (int) Math.Pow(2, firstByte >> 6); } - public static byte[] Encode(UInt64 integer) + public static byte[] Encode(ulong integer) { - int requiredBytes = 0; + int requiredBytes; if (integer <= byte.MaxValue >> 2) /* 63 */ requiredBytes = 1; - else if (integer <= UInt16.MaxValue >> 2) /* 16383 */ + else if (integer <= ushort.MaxValue >> 2) /* 16383 */ requiredBytes = 2; - else if (integer <= UInt32.MaxValue >> 2) /* 1073741823 */ + else if (integer <= uint.MaxValue >> 2) /* 1073741823 */ requiredBytes = 4; - else if (integer <= UInt64.MaxValue >> 2) /* 4611686018427387903 */ + else if (integer <= ulong.MaxValue >> 2) /* 4611686018427387903 */ requiredBytes = 8; else - throw new ArgumentOutOfRangeException("Value is larger than VariableInteger.MaxValue."); + throw new ArgumentOutOfRangeException(nameof(integer), + "Value is larger than VariableInteger.MaxValue."); - int offset = 8 - requiredBytes; + var offset = 8 - requiredBytes; - byte[] uInt64Bytes = ByteUtilities.GetBytes(integer); - byte first = uInt64Bytes[offset]; - first = (byte)(first | (requiredBytes / 2) << 6); + var uInt64Bytes = ByteUtilities.GetBytes(integer); + var first = uInt64Bytes[offset]; + first = (byte) (first | ((requiredBytes / 2) << 6)); uInt64Bytes[offset] = first; - byte[] result = new byte[requiredBytes]; + var result = new byte[requiredBytes]; Buffer.BlockCopy(uInt64Bytes, offset, result, 0, requiredBytes); return result; } - public static UInt64 Decode(byte[] bytes) + public static ulong Decode(byte[] bytes) { - int i = 8 - bytes.Length; - byte[] buffer = new byte[8]; + var i = 8 - bytes.Length; + var buffer = new byte[8]; Buffer.BlockCopy(bytes, 0, buffer, i, bytes.Length); - buffer[i] = (byte)(buffer[i] & (255 >> 2)); - - UInt64 res = ByteUtilities.ToUInt64(buffer); + buffer[i] = (byte) (buffer[i] & (255 >> 2)); - return res; + return ByteUtilities.ToUInt64(buffer); } } -} +} \ No newline at end of file diff --git a/README.md b/README.md index c5ba0cb..21cd0be 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,10 @@

-

QuicNet +

QuicNet

+ +# Preamble +This is a fork, not the original. The purpose of this fork was to port the Libraries from .NET Framework 4.5.2 (and multiple other versions) to .NET Standard 2.0 to allow interoperability with .NET Framework and .NET Core. The Unit Test project and the Console test projects are still .NET Framework, although they are .NET Framework 4.7.2 now. I also updated the .csproj files from their old VS 2015 format to the new VS 2017 and upwards format. I may or may not continue developing this, I just got it into a state where I can work with it and can start experimenting to see if it fits my needs or not. # Table of contents - [What is QuicNet](#what-is-quicnet)