示例#1
0
            protected override object OnReadObject(ByteBuffer buffer)
            {
                object     container  = Activator.CreateInstance(this.type);
                FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);    // FormatCode.Described

                if (formatCode != FormatCode.Described)
                {
                    throw new AmqpException(AmqpError.InvalidField, "format-code");
                }

                bool validDescriptor = false;

                formatCode = AmqpEncoding.ReadFormatCode(buffer);
                if (formatCode == FormatCode.ULong || formatCode == FormatCode.SmallULong)
                {
                    ulong code = AmqpBitConverter.ReadULong(buffer);
                    validDescriptor = this.descriptorCode == null || code == this.descriptorCode.Value;
                }
                else if (formatCode == FormatCode.Symbol8 || formatCode == FormatCode.Symbol32)
                {
                    AmqpSymbol symbol = SymbolEncoding.Decode(buffer, formatCode);
                    validDescriptor = this.descriptorName.Value == null || symbol.Equals(this.descriptorName);
                }

                if (!validDescriptor)
                {
                    throw new AmqpException(AmqpError.InvalidField, "descriptor");
                }

                formatCode = AmqpEncoding.ReadFormatCode(buffer);    // FormatCode.List
                if (formatCode == FormatCode.List0)
                {
                    return(container);
                }

                int size  = 0;
                int count = 0;

                AmqpEncoding.ReadSizeAndCount(buffer, formatCode, FormatCode.List8, FormatCode.List32, out size, out count);

                // prefetch bytes from the stream
                buffer.EnsureLength(size - FixedWidth.UInt);
                for (int i = 0; i < count; ++i)
                {
                    object value = this.members[i].Type.OnReadObject(buffer);
                    this.members[i].Accessor.SetObject(container, value);
                }

                return(container);
            }
示例#2
0
 public static ulong ReadULong(ByteBuffer buffer, byte formatCode)
 {
     if (formatCode == FormatCode.ULong0)
     {
         return(0);
     }
     else if (formatCode == FormatCode.SmallULong)
     {
         return(AmqpBitConverter.ReadUByte(buffer));
     }
     else if (formatCode == FormatCode.ULong)
     {
         return(AmqpBitConverter.ReadULong(buffer));
     }
     else
     {
         throw DecodeException(formatCode, buffer.Offset);
     }
 }
示例#3
0
        public static ulong?Decode(ByteBuffer buffer, FormatCode formatCode)
        {
            if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null)
            {
                return(null);
            }

            VerifyFormatCode(formatCode, buffer.Offset, FormatCode.ULong, FormatCode.SmallULong, FormatCode.ULong0);
            if (formatCode == FormatCode.ULong0)
            {
                return(0);
            }
            else
            {
                return(formatCode == FormatCode.SmallULong ?
                       AmqpBitConverter.ReadUByte(buffer) :
                       AmqpBitConverter.ReadULong(buffer));
            }
        }
示例#4
0
            ulong ReadDescriptorCode()
            {
                FormatCode formatCode     = this.ReadFormatCode();
                ulong      descriptorCode = 0;

                if (formatCode == FormatCode.SmallULong)
                {
                    descriptorCode = (ulong)this.stream.ReadByte();
                }
                else if (formatCode == FormatCode.ULong)
                {
                    ArraySegment <byte> buffer = this.ReadBytes(FixedWidth.ULong);
                    descriptorCode = AmqpBitConverter.ReadULong(buffer.Array, buffer.Offset, FixedWidth.ULong);
                }
                else if (formatCode == FormatCode.Symbol8 || formatCode == FormatCode.Symbol32)
                {
                    int count = this.ReadInt(formatCode == FormatCode.Symbol8);
                    ArraySegment <byte> nameBuffer = this.ReadBytes(count);
                    string descriptorName          = System.Text.Encoding.ASCII.GetString(nameBuffer.Array, nameBuffer.Offset, count);
                    sectionCodeByName.TryGetValue(descriptorName, out descriptorCode);
                }

                return(descriptorCode);
            }
示例#5
0
        public void TestMethod_AmqpBitConverter()
        {
            ByteBuffer buffer = new ByteBuffer(128, true);

            AmqpBitConverter.WriteByte(buffer, 0x22);
            AmqpBitConverter.WriteByte(buffer, -0x22);

            AmqpBitConverter.WriteUByte(buffer, 0x22);
            AmqpBitConverter.WriteUByte(buffer, 0xB2);

            AmqpBitConverter.WriteShort(buffer, 0x22B7);
            AmqpBitConverter.WriteShort(buffer, -0x22B7);

            AmqpBitConverter.WriteUShort(buffer, 0x22B7);
            AmqpBitConverter.WriteUShort(buffer, 0xC2B7);

            AmqpBitConverter.WriteInt(buffer, 0x340da287);
            AmqpBitConverter.WriteInt(buffer, -0x340da287);

            AmqpBitConverter.WriteUInt(buffer, 0x340da287);
            AmqpBitConverter.WriteUInt(buffer, 0xF40da287);

            AmqpBitConverter.WriteLong(buffer, 0x5d00BB9A340da287);
            AmqpBitConverter.WriteLong(buffer, -0x5d00BB9A340da287);

            AmqpBitConverter.WriteULong(buffer, 0x5d00BB9A340da287);
            AmqpBitConverter.WriteULong(buffer, 0xad00BB9A340da287);

            AmqpBitConverter.WriteFloat(buffer, 12344.4434F);
            AmqpBitConverter.WriteFloat(buffer, -12344.4434F);

            AmqpBitConverter.WriteDouble(buffer, 39432123244.44352334);
            AmqpBitConverter.WriteDouble(buffer, -39432123244.44352334);

            Guid uuid = Guid.NewGuid();

            AmqpBitConverter.WriteUuid(buffer, uuid);

            sbyte b  = AmqpBitConverter.ReadByte(buffer);
            sbyte b2 = AmqpBitConverter.ReadByte(buffer);

            byte ub  = AmqpBitConverter.ReadUByte(buffer);
            byte ub2 = AmqpBitConverter.ReadUByte(buffer);

            short s  = AmqpBitConverter.ReadShort(buffer);
            short s2 = AmqpBitConverter.ReadShort(buffer);

            ushort us  = AmqpBitConverter.ReadUShort(buffer);
            ushort us2 = AmqpBitConverter.ReadUShort(buffer);

            int i  = AmqpBitConverter.ReadInt(buffer);
            int i2 = AmqpBitConverter.ReadInt(buffer);

            uint ui  = AmqpBitConverter.ReadUInt(buffer);
            uint ui2 = AmqpBitConverter.ReadUInt(buffer);

            long l  = AmqpBitConverter.ReadLong(buffer);
            long l2 = AmqpBitConverter.ReadLong(buffer);

            ulong ul  = AmqpBitConverter.ReadULong(buffer);
            ulong ul2 = AmqpBitConverter.ReadULong(buffer);

            float f  = AmqpBitConverter.ReadFloat(buffer);
            float f2 = AmqpBitConverter.ReadFloat(buffer);

            double d  = AmqpBitConverter.ReadDouble(buffer);
            double d2 = AmqpBitConverter.ReadDouble(buffer);

            Guid uuid2 = AmqpBitConverter.ReadUuid(buffer);
        }
示例#6
0
        static bool TryReadSectionInfo(ByteBuffer buffer, out SectionInfo info, out Error error)
        {
            info = default(SectionInfo);

            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode != FormatCode.Described)
            {
                error = GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidFormatCode, formatCode, buffer.Offset - FixedWidth.FormatCode));
                return(false);
            }

            ulong code = ulong.MaxValue;

            formatCode = AmqpEncoding.ReadFormatCode(buffer);
            switch (formatCode)
            {
            case FormatCode.SmallULong:
                code = AmqpBitConverter.ReadUByte(buffer);
                break;

            case FormatCode.ULong:
                code = AmqpBitConverter.ReadULong(buffer);
                break;

            case FormatCode.Symbol32:
            case FormatCode.Symbol8:
                // symbol name should be seldom used so do not optimize for it
                AmqpSymbol name = SymbolEncoding.Decode(buffer, formatCode);
                for (int i = 0; i < MessageSections.Length; i++)
                {
                    if (MessageSections[i].Name.Equals(name.Value))
                    {
                        code = MessageSections[i].Code;
                        break;
                    }
                }
                if (code == ulong.MaxValue)
                {
                    error = GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidMessageSectionCode, name));
                    return(false);
                }
                break;

            default:
                error = GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidFormatCode, formatCode, buffer.Offset - FixedWidth.FormatCode));
                return(false);
            }

            int index = (int)(code - MessageSections[0].Code);

            if (index < 0 || index >= MessageSections.Length)
            {
                error = GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidMessageSectionCode, code));
                return(false);
            }

            info  = MessageSections[index];
            error = null;

            return(true);
        }