Exemple #1
0
        public int Write(MemoryCursor cursor, int?minLength = null, ValueBuffer?mask = null)
        {
            if (minLength < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minLength));
            }

            var length = NetworkBitConverter.GetByteLength(value);

            if (minLength.HasValue)
            {
                length = Math.Max(length, minLength.Value);
            }

            var bytes = cursor.Move(length);

            NetworkBitConverter.WriteUnaligned(bytes.Span, value, length);

            if (mask.HasValue)
            {
                Mask(bytes.Span, mask.Value);
            }

            return(length);
        }
        public static CursorWritingContext StartVectorWriting(MemoryCursor cursor, Range range)
        {
            var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);
            var lengthBytes       = cursor.Move(lengthSizeInBytes).Span;
            var startLength       = cursor.AsOffset();

            return(new CursorWritingContext(cursor, startLength, lengthBytes, range));
        }
        public static WritingContext StartVectorWriting(ref Span <byte> bytes, Range range)
        {
            var context           = new WritingContext(bytes, range);
            var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);

            if (bytes.Length < lengthSizeInBytes)
            {
                throw new EncodingException();
            }

            bytes = bytes.Slice(lengthSizeInBytes);

            return(context);
        }
            public void Complete(ref Span <byte> bytes)
            {
                var offset            = start.Length - bytes.Length;
                var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);

                if (offset < lengthSizeInBytes)
                {
                    throw new EncodingException();
                }

                var payloadLength = offset - lengthSizeInBytes;

                if (payloadLength < range.Start.Value || payloadLength > range.End.Value)
                {
                    throw new EncodingException();
                }

                NetworkBitConverter.WriteUnaligned(start, (ulong)payloadLength, lengthSizeInBytes);
            }
Exemple #5
0
        public PacketNumber DecodeByLargestAcknowledged(PacketNumber largestAcknowledged)
        {
            var bits      = NetworkBitConverter.GetByteLength(value) * 8;
            var expected  = largestAcknowledged.value + 1;
            var win       = 1UL << bits;
            var hwin      = win / 2;
            var mask      = win - 1;
            var candidate = (expected & ~mask) | value;

            if (candidate <= expected - hwin && candidate < (1 << 62) - win)
            {
                return(new PacketNumber(candidate + win));
            }

            if (candidate > expected + hwin && candidate >= win)
            {
                return(new PacketNumber(candidate - win));
            }

            return(new PacketNumber(candidate));
        }
        public static ReadOnlyMemory <byte> SliceVectorBytes(ReadOnlyMemory <byte> data, Range range, out ReadOnlyMemory <byte> remainings)
        {
            var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);

            if (data.Length < lengthSizeInBytes || lengthSizeInBytes > 4)
            {
                throw new EncodingException();
            }

            var lengthBytes      = data.Slice(0, lengthSizeInBytes);
            var length           = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);
            var afterLengthBytes = data.Slice(lengthSizeInBytes);

            if (afterLengthBytes.Length < length || length < range.Start.Value || length > range.End.Value)
            {
                throw new EncodingException();
            }

            remainings = afterLengthBytes.Slice(length);

            return(afterLengthBytes.Slice(0, length));
        }
        public static MemoryBuffer SliceVectorBytes(MemoryCursor cursor, Range range)
        {
            var lengthSizeInBytes = NetworkBitConverter.GetByteLength((ulong)range.End.Value);

            if (lengthSizeInBytes > 4)
            {
                throw new EncodingException();
            }

            var lengthBytes = cursor.Move(lengthSizeInBytes);
            var length      = (int)NetworkBitConverter.ParseUnaligned(lengthBytes.Span);

            if (length < range.Start.Value || length > range.End.Value)
            {
                throw new EncodingException();
            }

            var startOffset = cursor.AsOffset();

            cursor.Move(length);

            return(new MemoryBuffer(startOffset, length));
        }