Beispiel #1
0
        public static FrameType Peek(MemoryCursor cursor)
        {
            var bytes = cursor.PeekEnd();
            var value = VariableLengthEncoding.Decode(bytes.Span, out _);

            return(new FrameType(value));
        }
Beispiel #2
0
        private static void SliceRange(MemoryCursor cursor)
        {
            var bytes      = cursor.PeekEnd();
            var rangeBytes = VariableLengthEncoding.Slice(bytes.Span);

            cursor.Move(rangeBytes.Length);
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out NewConnectionIdFrame result, out ReadOnlyMemory <byte> remainings)
        {
            result     = new NewConnectionIdFrame();
            remainings = ReadOnlyMemory <byte> .Empty;

            var type = FrameType.Parse(bytes, out var afterTypeBytes);

            if (!type.IsNewConnectionId())
            {
                return(false);
            }

            var sequenceNumber                   = VariableLengthEncoding.Decode32(afterTypeBytes.Span, out var decodedLength);
            var afterSequenceNumberBytes         = afterTypeBytes.Slice(decodedLength);
            var sequenceNumberToRetire           = VariableLengthEncoding.Decode32(afterSequenceNumberBytes.Span, out decodedLength);
            var afterSequenceNumberToRetiryBytes = afterSequenceNumberBytes.Slice(decodedLength);

            if (sequenceNumberToRetire > sequenceNumber)
            {
                throw new EncodingException();
            }

            var connectionId = PacketConnectionId.Parse(afterSequenceNumberToRetiryBytes, out var afterConnectionIdBytes);
            var resetToken   = ParseResetToken(afterConnectionIdBytes, out var afterTokenBytes);

            result     = new NewConnectionIdFrame(sequenceNumber, sequenceNumberToRetire, connectionId, resetToken);
            remainings = afterTokenBytes;

            return(true);
        }
Beispiel #4
0
        public static Error ParseTransport(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> remainings)
        {
            var code = VariableLengthEncoding.Decode(bytes.Span, out var decodedLength);

            remainings = bytes.Slice(decodedLength);

            return(new Error(code, true, false));
        }
Beispiel #5
0
        public static FrameType Parse(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> remainings)
        {
            var code = VariableLengthEncoding.Decode(bytes.Span, out var decodedLength);

            remainings = bytes.Slice(decodedLength);

            return(new FrameType(code));
        }
Beispiel #6
0
        public static StreamId Parse(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> remainings)
        {
            var id = VariableLengthEncoding.Decode(bytes.Span, out var decodedLength);

            remainings = bytes.Slice(decodedLength);

            return(new StreamId(id));
        }
        public void ReadValue_ResultIsExpected(string bytes, ulong expectedValue, int expectedLength)
        {
            //Arrange
            //Act
            var resultValue = VariableLengthEncoding.Decode(Utils.ParseHexString(bytes), out var resultLength);

            //Assert
            Assert.Equal(expectedValue, resultValue);
            Assert.Equal(expectedLength, resultLength);
        }
        public void WriteValue_ResultIsExpected(string expectedBytes, ulong value, int expectedLength)
        {
            //Arrange
            var buffer = new byte[8];

            //Act
            VariableLengthEncoding.Encode(buffer, value, out var resultLength);
            Array.Resize(ref buffer, resultLength);

            //Assert
            Assert.Equal(expectedBytes, Utils.ToHexString(buffer), true);
            Assert.Equal(expectedLength, resultLength);
        }
            private int ReadLength()
            {
                var length = VariableLengthEncoding.Decode(remainings.Span, out var decodedLength);

                if (length > int.MaxValue)
                {
                    throw new EncodingException();
                }

                remainings = remainings.Slice(decodedLength);

                return((int)length);
            }
        public static bool TrySlice(MemoryCursor cursor, TransportParameterType expected)
        {
            var bytes = cursor.PeekEnd();
            var id    = VariableLengthEncoding.Decode(bytes.Span, out var decodedLength);

            if (id != expected.id)
            {
                return(false);
            }

            cursor.Move(decodedLength);

            return(true);
        }
Beispiel #11
0
        public static bool TrySlice(MemoryCursor cursor, FrameType expected)
        {
            var bytes = cursor.PeekEnd();
            var type  = VariableLengthEncoding.Decode(bytes.Span, out var decodedLength);

            if (type != expected.type)
            {
                return(false);
            }

            cursor.Move(decodedLength);

            return(true);
        }
        private static int ParseOffset(ReadOnlyMemory <byte> bytes, FrameType type, out ReadOnlyMemory <byte> remainings)
        {
            remainings = bytes;

            if (!type.HasOffset())
            {
                return(0);
            }

            var offset = VariableLengthEncoding.Decode32(bytes.Span, out var decodedLength);

            remainings = bytes.Slice(decodedLength);

            return(offset);
        }
        public static ReasonPhrase Parse(ReadOnlyMemory <byte> bytes, out ReadOnlyMemory <byte> remainings)
        {
            remainings = ReadOnlyMemory <byte> .Empty;

            var length           = VariableLengthEncoding.Decode32(bytes.Span, out var decodedLength);
            var afterLengthBytes = bytes.Slice(decodedLength);

            if (afterLengthBytes.Length < length)
            {
                throw new EncodingException();
            }

            var reasonPhraseBytes = afterLengthBytes.Slice(0, length);

            remainings = afterLengthBytes.Slice(length);

            return(new ReasonPhrase(reasonPhraseBytes));
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out DataBlockedFrame result, out ReadOnlyMemory <byte> remainings)
        {
            result     = new DataBlockedFrame();
            remainings = ReadOnlyMemory <byte> .Empty;

            var type = FrameType.Parse(bytes, out var afterTypeBytes);

            if (!type.IsDataBlocked())
            {
                return(false);
            }

            var dataLimit = VariableLengthEncoding.Decode32(afterTypeBytes.Span, out var decodedLength);

            result     = new DataBlockedFrame(dataLimit);
            remainings = afterTypeBytes.Slice(decodedLength);

            return(true);
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out StreamsBlockedFrame result, out ReadOnlyMemory <byte> remainings)
        {
            result     = new StreamsBlockedFrame();
            remainings = ReadOnlyMemory <byte> .Empty;

            var type = FrameType.Parse(bytes, out var afterTypeBytes);

            if (!type.IsStreamsBlocked())
            {
                return(false);
            }

            var streamLimit = VariableLengthEncoding.Decode32(afterTypeBytes.Span, out var decodedLength);

            result     = new StreamsBlockedFrame(streamLimit, type.ForBidirectional(), type.ForUnidirectional());
            remainings = afterTypeBytes.Slice(decodedLength);

            return(true);
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out MaxStreamDataFrame result, out ReadOnlyMemory <byte> remainings)
        {
            result     = new MaxStreamDataFrame();
            remainings = ReadOnlyMemory <byte> .Empty;

            var type = FrameType.Parse(bytes, out var afterTypeBytes);

            if (!type.IsMaxStreamData())
            {
                return(false);
            }

            var streamId      = StreamId.Parse(afterTypeBytes, out var afterStreamIdBytes);
            var maxDataLength = VariableLengthEncoding.Decode32(afterStreamIdBytes.Span, out var decodedLength);

            result     = new MaxStreamDataFrame(streamId, maxDataLength);
            remainings = afterStreamIdBytes.Slice(decodedLength);

            return(true);
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out RetireConnectionIdFrame result, out ReadOnlyMemory <byte> remainings)
        {
            result     = new RetireConnectionIdFrame();
            remainings = ReadOnlyMemory <byte> .Empty;

            var type = FrameType.Parse(bytes, out var afterTypeBytes);

            if (!type.IsRetireConnectionId())
            {
                return(false);
            }

            var sequenceNumber           = VariableLengthEncoding.Decode32(afterTypeBytes.Span, out var decodedLength);
            var afterSequenceNumberBytes = afterTypeBytes.Slice(decodedLength);

            result     = new RetireConnectionIdFrame(sequenceNumber);
            remainings = afterSequenceNumberBytes;

            return(true);
        }
        private static ReadOnlyMemory <byte> ParseData(ReadOnlyMemory <byte> bytes, FrameType type, out ReadOnlyMemory <byte> remainings)
        {
            remainings = ReadOnlyMemory <byte> .Empty;

            if (!type.HasLength())
            {
                return(bytes);
            }

            var length           = VariableLengthEncoding.Decode32(bytes.Span, out var decodedLength);
            var afterLengthBytes = bytes.Slice(decodedLength);

            if (afterLengthBytes.Length < length)
            {
                throw new EncodingException();
            }

            remainings = afterLengthBytes.Slice(length);

            return(afterLengthBytes.Slice(0, length));
        }
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out ResetStreamFrame result, out ReadOnlyMemory <byte> remainings)
        {
            result     = new ResetStreamFrame();
            remainings = ReadOnlyMemory <byte> .Empty;

            var type = FrameType.Parse(bytes, out var afterTypeRemainings);

            if (!type.IsResetStream())
            {
                return(false);
            }

            var streamId  = StreamId.Parse(afterTypeRemainings, out var afterStreamIdBytes);
            var error     = Error.ParseApplication(afterStreamIdBytes, out var afterApplicationErrorBytes);
            var finalSize = VariableLengthEncoding.Decode32(afterApplicationErrorBytes.Span, out var decodedLength);

            result     = new ResetStreamFrame(streamId, error, finalSize);
            remainings = afterApplicationErrorBytes.Slice(decodedLength);

            return(true);
        }
Beispiel #20
0
        public void WriteBytes(ref Span <byte> bytes)
        {
            VariableLengthEncoding.Encode(bytes, type, out var encodedLength);

            bytes = bytes.Slice(encodedLength);
        }
Beispiel #21
0
        public void Slice(MemoryCursor cursor)
        {
            var length = VariableLengthEncoding.GetLength(type);

            cursor.Move(length);
        }