Beispiel #1
0
            public void Dispose()
            {
                type.WriteBytes(cursor);

                var payloadLength = cursor - startLengthOfBody;

                if (payloadLength > MaxLength)
                {
                    throw new EncodingException();
                }

                var payloadData            = cursor.Move(-payloadLength);
                var headerLength           = cursor - startLengthOfMessage;
                var headerData             = cursor.Peek(-headerLength);
                var encryptedPayloadLength = payloadLength + aead.TagLength;

                if (encryptedPayloadLength > MaxLength)
                {
                    throw new EncodingException();
                }

                cursor.Move(encryptedPayloadLength);

                var tag = cursor.Peek(-aead.TagLength);

                NetworkBitConverter.WriteUnaligned(lengthBytes, (ulong)encryptedPayloadLength, 2);

                aead.Encrypt(payloadData.Span, tag.Span, sequenceNumber, headerData.Span);
            }
Beispiel #2
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);
        }
Beispiel #3
0
        public void Fill(Span <byte> bytes, ValueBuffer?mask = null)
        {
            NetworkBitConverter.WriteUnaligned(bytes, value, bytes.Length);

            if (mask.HasValue)
            {
                Mask(bytes, mask.Value);
            }
        }
            public void Dispose()
            {
                var payloadLength = cursor - startLength;

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

                NetworkBitConverter.WriteUnaligned(lengthBytes, (ulong)payloadLength, lengthBytes.Length);
            }
Beispiel #5
0
            public void Dispose()
            {
                var payloadLength = cursor - startOffset;

                if (payloadLength > MaxLength)
                {
                    throw new EncodingException();
                }

                NetworkBitConverter.WriteUnaligned(lengthBytes, (ulong)payloadLength, 2);
            }
            public void Complete(ref Span <byte> bytes)
            {
                var offset = start.Length - bytes.Length;

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

                var payloadLength = offset - 2;

                if (payloadLength > ushort.MaxValue)
                {
                    throw new EncodingException();
                }

                NetworkBitConverter.WriteUnaligned(start, (ulong)payloadLength, 2);
            }
            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);
            }
Beispiel #8
0
        public void WriteBytes(MemoryCursor cursor)
        {
            var bytes = cursor.Move(2).Span;

            NetworkBitConverter.WriteUnaligned(bytes, code, 2);
        }
Beispiel #9
0
 public void WriteBytes(ref Span <byte> bytes)
 {
     bytes = bytes.Slice(NetworkBitConverter.WriteUnaligned(bytes, code, 2));
 }
        public void WriteBytes(ref Span <byte> bytes)
        {
            var writtenLength = NetworkBitConverter.WriteUnaligned(bytes, code, 2);

            bytes = bytes.Slice(writtenLength);
        }