예제 #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);
            }
예제 #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);
        }
예제 #3
0
        public void Fill(Span <byte> bytes, ValueBuffer?mask = null)
        {
            NetworkBitConverter.WriteUnaligned(bytes, value, bytes.Length);

            if (mask.HasValue)
            {
                Mask(bytes, mask.Value);
            }
        }
예제 #4
0
            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);
            }
예제 #5
0
            public void Dispose()
            {
                var payloadLength = cursor - startOffset;

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

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

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

            bytes = bytes.Slice(writtenLength);
        }