public ushort ReadShort()
        {
            ushort result = NetworkOrderDeserializer.ReadUInt16(Span);

            _offset += 2;
            return(result);
        }
        public ulong ReadLonglong()
        {
            ulong result = NetworkOrderDeserializer.ReadUInt64(_memory.Slice(_memoryOffset));

            _memoryOffset += 8;
            return(result);
        }
        public ulong ReadLonglong()
        {
            ulong result = NetworkOrderDeserializer.ReadUInt64(Span);

            _offset += 8;
            return(result);
        }
Exemplo n.º 4
0
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
        /// x and V types and the AMQP 0-9-1 A type.
        ///</remarks>
        /// <returns>A <seealso cref="System.Collections.Generic.Dictionary{TKey,TValue}"/>.</returns>
        public static int ReadDictionary(ReadOnlySpan <byte> span, out Dictionary <string, object> valueDictionary)
        {
            long tableLength = NetworkOrderDeserializer.ReadUInt32(span);

            if (tableLength == 0)
            {
                valueDictionary = null;
                return(4);
            }

            span            = span.Slice(4);
            valueDictionary = new Dictionary <string, object>();
            int bytesRead = 0;

            while (bytesRead < tableLength)
            {
                bytesRead += ReadShortstr(span.Slice(bytesRead), out string key);
                object value = ReadFieldValue(span.Slice(bytesRead), out int valueBytesRead);
                bytesRead += valueBytesRead;

                valueDictionary[key] = value;
            }

            return(4 + bytesRead);
        }
        public ushort ReadShort()
        {
            ushort result = NetworkOrderDeserializer.ReadUInt16(_memory.Slice(_memoryOffset));

            _memoryOffset += 2;
            return(result);
        }
Exemplo n.º 6
0
 public static int ReadTimestamp(ReadOnlySpan <byte> span, out AmqpTimestamp value)
 {
     // 0-9 is afaict silent on the signedness of the timestamp.
     // See also MethodArgumentWriter.WriteTimestamp and AmqpTimestamp itself
     value = new AmqpTimestamp((long)NetworkOrderDeserializer.ReadUInt64(span));
     return(8);
 }
Exemplo n.º 7
0
            // Moved out of outer switch to have a shorter main method (improves performance)
            static object ReadFieldValueSlow(ReadOnlySpan <byte> span, out int bytesRead)
            {
                var slice = span.Slice(1);

                switch ((char)span[0])
                {
                case 'F':
                    bytesRead = 1 + ReadDictionary(slice, out var dictionary);
                    return(dictionary);

                case 'A':
                    IList arrayResult = ReadArray(slice, out int arrayBytesRead);
                    bytesRead = 1 + arrayBytesRead;
                    return(arrayResult);

                case 'l':
                    bytesRead = 9;
                    return(NetworkOrderDeserializer.ReadInt64(slice));

                case 'i':
                    bytesRead = 5;
                    return(NetworkOrderDeserializer.ReadUInt32(slice));

                case 'D':
                    bytesRead = 6;
                    return(ReadDecimal(slice));

                case 'B':
                    bytesRead = 2;
                    return(span[1]);

                case 'b':
                    bytesRead = 2;
                    return((sbyte)span[1]);

                case 'd':
                    bytesRead = 9;
                    return(NetworkOrderDeserializer.ReadDouble(slice));

                case 'f':
                    bytesRead = 5;
                    return(NetworkOrderDeserializer.ReadSingle(slice));

                case 's':
                    bytesRead = 3;
                    return(NetworkOrderDeserializer.ReadInt16(slice));

                case 'T':
                    bytesRead = 1 + ReadTimestamp(slice, out var timestamp);
                    return(timestamp);

                case 'x':
                    bytesRead = 1 + ReadLongstr(slice, out var binaryTableResult);
                    return(new BinaryTableValue(binaryTableResult));

                default:
                    bytesRead = 0;
                    return(ThrowInvalidTableValue((char)span[0]));
                }
            }
Exemplo n.º 8
0
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
        /// x and V types and the AMQP 0-9-1 A type.
        ///</remarks>
        /// <returns>A <seealso cref="System.Collections.Generic.Dictionary{TKey,TValue}"/>.</returns>
        public static Dictionary <string, object> ReadTable(ReadOnlySpan <byte> span, out int bytesRead)
        {
            bytesRead = 4;
            long tableLength = NetworkOrderDeserializer.ReadUInt32(span);

            if (tableLength == 0)
            {
                return(null);
            }

            Dictionary <string, object> table = new Dictionary <string, object>();

            while ((bytesRead - 4) < tableLength)
            {
                string key = ReadShortstr(span.Slice(bytesRead), out int keyBytesRead);
                bytesRead += keyBytesRead;
                object value = ReadFieldValue(span.Slice(bytesRead), out int valueBytesRead);
                bytesRead += valueBytesRead;

                if (!table.ContainsKey(key))
                {
                    table[key] = value;
                }
            }

            return(table);
        }
        public static decimal ReadDecimal(ReadOnlyMemory <byte> memory)
        {
            byte scale            = memory.Span[0];
            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(memory.Slice(1));

            return(AmqpToDecimal(scale, unsignedMantissa));
        }
Exemplo n.º 10
0
        public static decimal ReadDecimal(ReadOnlySpan <byte> span)
        {
            byte scale            = span[0];
            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));

            return(AmqpToDecimal(scale, unsignedMantissa));
        }
        public uint ReadLong()
        {
            uint result = NetworkOrderDeserializer.ReadUInt32(Span);

            _offset += 4;
            return(result);
        }
        public uint ReadLong()
        {
            uint result = NetworkOrderDeserializer.ReadUInt32(_memory.Slice(_memoryOffset));

            _memoryOffset += 4;
            return(result);
        }
        public uint ReadLong()
        {
            ClearBits();
            uint result = NetworkOrderDeserializer.ReadUInt32(_memory.Slice(_memoryOffset).Span);

            _memoryOffset += 4;
            return(result);
        }
Exemplo n.º 14
0
        public static AmqpTimestamp ReadTimestamp(ReadOnlyMemory <byte> memory)
        {
            ulong stamp = NetworkOrderDeserializer.ReadUInt64(memory);

            // 0-9 is afaict silent on the signedness of the timestamp.
            // See also MethodArgumentWriter.WriteTimestamp and AmqpTimestamp itself
            return(new AmqpTimestamp((long)stamp));
        }
Exemplo n.º 15
0
        ///<summary>
        /// Fill this instance from the given byte buffer stream.
        ///</summary>
        internal ulong ReadFrom(ReadOnlyMemory <byte> memory)
        {
            // Skipping the first two bytes since they arent used (weight - not currently used)
            ulong bodySize = NetworkOrderDeserializer.ReadUInt64(memory.Slice(2));

            ReadPropertiesFrom(new ContentHeaderPropertyReader(memory.Slice(10)));
            return(bodySize);
        }
 public void ReadFlagWord()
 {
     if (!ContinuationBitSet)
     {
         throw new MalformedFrameException("Attempted to read flag word when none advertised");
     }
     m_flagWord     = NetworkOrderDeserializer.ReadUInt16(_memory.Slice(_memoryOffset));
     _memoryOffset += 2;
     m_bitCount     = 0;
 }
 private void ReadBits()
 {
     if (!ContinuationBitSet)
     {
         throw new MalformedFrameException("Attempted to read flag word when none advertised");
     }
     _bits    = NetworkOrderDeserializer.ReadUInt16(Span);
     _offset += 2;
     _bitMask = StartBitMask;
 }
Exemplo n.º 18
0
        public static byte[] ReadLongstr(ReadOnlySpan <byte> span)
        {
            uint byteCount = NetworkOrderDeserializer.ReadUInt32(span);

            if (byteCount > int.MaxValue)
            {
                throw new SyntaxErrorException($"Long string too long; byte length={byteCount}, max={int.MaxValue}");
            }

            return(span.Slice(4, (int)byteCount).ToArray());
        }
Exemplo n.º 19
0
        public static byte[] ReadLongstr(ReadOnlyMemory <byte> memory)
        {
            int byteCount = (int)NetworkOrderDeserializer.ReadUInt32(memory);

            if (byteCount > int.MaxValue)
            {
                throw new SyntaxError($"Long string too long; byte length={byteCount}, max={int.MaxValue}");
            }

            return(memory.Slice(4, byteCount).ToArray());
        }
Exemplo n.º 20
0
        public Command HandleFrame(InboundFrame f)
        {
            switch (m_state)
            {
            case AssemblyState.ExpectingMethod:
                if (!f.IsMethod())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_method = m_protocol.DecodeMethodFrom(f.Payload);
                m_state  = m_method.HasContent ? AssemblyState.ExpectingContentHeader : AssemblyState.Complete;
                return(CompletedCommand());

            case AssemblyState.ExpectingContentHeader:
                if (!f.IsHeader())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_header = m_protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(f.Payload));
                ulong totalBodyBytes = m_header.ReadFrom(f.Payload.Slice(2));
                if (totalBodyBytes > MaxArrayOfBytesSize)
                {
                    throw new UnexpectedFrameException(f);
                }

                m_remainingBodyBytes = (int)totalBodyBytes;
                m_body = MemoryPool <byte> .Shared.Rent(m_remainingBodyBytes);

                UpdateContentBodyState();
                return(CompletedCommand());

            case AssemblyState.ExpectingContentBody:
                if (!f.IsBody())
                {
                    throw new UnexpectedFrameException(f);
                }

                if (f.Payload.Length > m_remainingBodyBytes)
                {
                    throw new MalformedFrameException($"Overlong content body received - {m_remainingBodyBytes} bytes remaining, {f.Payload.Length} bytes received");
                }

                f.Payload.CopyTo(m_body.Memory.Slice(_offset));
                m_remainingBodyBytes -= f.Payload.Length;
                _offset += f.Payload.Length;
                UpdateContentBodyState();
                return(CompletedCommand());

            case AssemblyState.Complete:
            default:
                return(null);
            }
        }
Exemplo n.º 21
0
        public static int ReadLongstr(ReadOnlySpan <byte> span, out byte[] value)
        {
            uint byteCount = NetworkOrderDeserializer.ReadUInt32(span);

            if (byteCount > int.MaxValue)
            {
                value = null;
                return(ThrowSyntaxErrorException(byteCount));
            }

            value = span.Slice(4, (int)byteCount).ToArray();
            return(4 + value.Length);
        }
Exemplo n.º 22
0
        public static decimal ReadDecimal(ReadOnlySpan <byte> span)
        {
            byte scale = span[0];

            if (scale > 28)
            {
                ThrowInvalidDecimalScale(scale);
            }

            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));
            var  data             = new DecimalData(((uint)(scale << 16)) | unsignedMantissa & 0x80000000, 0, unsignedMantissa & 0x7FFFFFFF, 0);

            return(Unsafe.As <DecimalData, decimal>(ref data));
        }
Exemplo n.º 23
0
        public static IList ReadArray(ReadOnlyMemory <byte> memory, out int bytesRead)
        {
            IList array       = new List <object>();
            long  arrayLength = NetworkOrderDeserializer.ReadUInt32(memory);

            bytesRead = 4;
            while (bytesRead - 4 < arrayLength)
            {
                object value = ReadFieldValue(memory.Slice(bytesRead), out int fieldValueBytesRead);
                bytesRead += fieldValueBytesRead;
                array.Add(value);
            }

            return(array);
        }
Exemplo n.º 24
0
        public static IList ReadArray(ReadOnlySpan <byte> span, out int bytesRead)
        {
            bytesRead = 4;
            long arrayLength = NetworkOrderDeserializer.ReadUInt32(span);

            if (arrayLength == 0)
            {
                return(null);
            }
            List <object> array = new List <object>();

            while (bytesRead - 4 < arrayLength)
            {
                array.Add(ReadFieldValue(span.Slice(bytesRead), out int fieldValueBytesRead));
                bytesRead += fieldValueBytesRead;
            }

            return(array);
        }
Exemplo n.º 25
0
        public static InboundFrame ReadFrom(Stream reader)
        {
            int type;

            try
            {
                type = reader.ReadByte();
                if (type == -1)
                {
                    throw new EndOfStreamException("Reached the end of the stream. Possible authentication failure.");
                }
            }
            catch (IOException ioe) when
                (ioe.InnerException != null &&
                (ioe.InnerException is SocketException) &&
                ((SocketException)ioe.InnerException).SocketErrorCode == SocketError.TimedOut)
            {
                throw ioe.InnerException;
            }

            if (type == 'A')
            {
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
            }

            using IMemoryOwner <byte> headerMemory = MemoryPool <byte> .Shared.Rent(6);

            Memory <byte> headerSlice = headerMemory.Memory.Slice(0, 6);

            reader.Read(headerSlice);
            int channel                 = NetworkOrderDeserializer.ReadUInt16(headerSlice);
            int payloadSize             = NetworkOrderDeserializer.ReadInt32(headerSlice.Slice(2)); // FIXME - throw exn on unreasonable value
            IMemoryOwner <byte> payload = MemoryPool <byte> .Shared.Rent(payloadSize);

            int bytesRead = 0;

            try
            {
                while (bytesRead < payloadSize)
                {
                    bytesRead += reader.Read(payload.Memory[bytesRead..payloadSize]);
Exemplo n.º 26
0
        ///<summary>Reads an AMQP "table" definition from the reader.</summary>
        ///<remarks>
        /// Supports the AMQP 0-8/0-9 standard entry types S, I, D, T
        /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
        /// x and V types and the AMQP 0-9-1 A type.
        ///</remarks>
        /// <returns>A <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
        public static IDictionary <string, object> ReadTable(ReadOnlyMemory <byte> memory, out int bytesRead)
        {
            IDictionary <string, object> table = new Dictionary <string, object>();
            long tableLength = NetworkOrderDeserializer.ReadUInt32(memory);

            bytesRead = 4;
            while ((bytesRead - 4) < tableLength)
            {
                string key = ReadShortstr(memory.Slice(bytesRead), out int keyBytesRead);
                bytesRead += keyBytesRead;
                object value = ReadFieldValue(memory.Slice(bytesRead), out int valueBytesRead);
                bytesRead += valueBytesRead;

                if (!table.ContainsKey(key))
                {
                    table[key] = value;
                }
            }

            return(table);
        }
        public static decimal ReadDecimal(ReadOnlySpan <byte> span)
        {
            byte scale = span[0];

            if (scale > 28)
            {
                throw new SyntaxErrorException($"Unrepresentable AMQP decimal table field: scale={scale}");
            }

            uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(span.Slice(1));

            return(new decimal(
                       // The low 32 bits of a 96-bit integer
                       lo: (int)(unsignedMantissa & 0x7FFFFFFF),
                       // The middle 32 bits of a 96-bit integer.
                       mid: 0,
                       // The high 32 bits of a 96-bit integer.
                       hi: 0,
                       isNegative: (unsignedMantissa & 0x80000000) != 0,
                       // A power of 10 ranging from 0 to 28.
                       scale: scale));
        }
Exemplo n.º 28
0
        public static object ReadFieldValue(ReadOnlySpan <byte> span, out int bytesRead)
        {
            switch ((char)span[0])
            {
            case 'S':
                bytesRead = 1 + ReadLongstr(span.Slice(1), out var bytes);
                return(bytes);

            case 't':
                bytesRead = 2;
                return(span[1] != 0 ? TrueBoolean : FalseBoolean);

            case 'I':
                bytesRead = 5;
                return(NetworkOrderDeserializer.ReadInt32(span.Slice(1)));

            case 'V':
                bytesRead = 1;
                return(null);

            default:
                return(ReadFieldValueSlow(span, out bytesRead));
            }
 public void TestDoubleDecoding()
 {
     Assert.Equal(1.234, NetworkOrderDeserializer.ReadDouble(_expectedDoubleBytes.AsSpan()));
 }
 public void TestReadUInt64()
 {
     Assert.Equal(0x89ABCDEF01234567, NetworkOrderDeserializer.ReadUInt64(new byte[] { 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67 }.AsSpan()));
 }