Beispiel #1
0
 public virtual int GetValueEncodeSize()
 {
     return(AmqpEncoding.GetObjectEncodeSize(base.Value));
 }
Beispiel #2
0
 public static int GetObjectEncodeSize(object value)
 {
     return(AmqpEncoding.GetObjectEncodeSize(value));
 }
 public static int GetEncodeSize(DescribedType value)
 {
     return(value == null ?
            FixedWidth.NullEncoded :
            FixedWidth.FormatCode + AmqpEncoding.GetObjectEncodeSize(value.Descriptor) + AmqpEncoding.GetObjectEncodeSize(value.Value));
 }
Beispiel #4
0
 public virtual void EncodeValue(ByteBuffer buffer)
 {
     AmqpEncoding.EncodeObject(base.Value, buffer);
 }
Beispiel #5
0
 public static SerializableType CreateSingleValueType(Type type)
 {
     return(new SerializableType.SingleValueType(type, AmqpEncoding.GetEncoding(type)));
 }
Beispiel #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);
        }
Beispiel #7
0
 internal virtual void EncodeValue(ByteBuffer buffer)
 {
     AmqpEncoding.EncodeObject(this.Value, buffer);
 }
Beispiel #8
0
        public void AmqpSerializerListEncodingTest()
        {
            Action <Person, Person> personValidator = (p1, p2) =>
            {
                Assert.NotNull(p2);
                Assert.True(21 == p2.Age, "Age should be increased by OnDeserialized");
                Assert.Equal(p1.GetType().Name, p2.GetType().Name);
                Assert.Equal(p1.DateOfBirth.Value, p2.DateOfBirth.Value);
                Assert.Equal(p1.Properties.Count, p2.Properties.Count);
                foreach (var k in p1.Properties.Keys)
                {
                    Assert.Equal(p1.Properties[k], p2.Properties[k]);
                }
            };

            Action <List <int>, List <int> > gradesValidator = (l1, l2) =>
            {
                if (l1 == null || l2 == null)
                {
                    Assert.True(l1 == null && l2 == null);
                    return;
                }

                Assert.Equal(l1.Count, l2.Count);
                for (int i = 0; i < l1.Count; ++i)
                {
                    Assert.Equal(l1[i], l2[i]);
                }
            };

            // Create an object to be serialized
            Person p = new Student("Tom")
            {
                Address = new Address()
                {
                    FullAddress = new string('B', 1024)
                },
                Grades = new List <int>()
                {
                    1, 2, 3, 4, 5
                }
            };

            p.Age         = 20;
            p.DateOfBirth = new DateTime(1980, 5, 12, 10, 2, 45, DateTimeKind.Utc);
            p.Properties.Add("height", 6.1);
            p.Properties.Add("male", true);
            p.Properties.Add("nick-name", "big foot");

            var stream = new MemoryStream(new byte[4096], 0, 4096, true, true);

            AmqpContractSerializer.WriteObject(stream, p);
            stream.Flush();

            // Deserialize and verify
            stream.Seek(0, SeekOrigin.Begin);
            Person p3 = AmqpContractSerializer.ReadObject <Person>(stream);

            personValidator(p, p3);
            Assert.Equal(((Student)p).Address.FullAddress, ((Student)p3).Address.FullAddress);
            gradesValidator(((Student)p).Grades, ((Student)p3).Grades);

            // Inter-op: it should be an AMQP described list as other clients see it
            stream.Seek(0, SeekOrigin.Begin);
            DescribedType dl1 = (DescribedType)AmqpEncoding.DecodeObject(new ByteBuffer(stream.ToArray(), 0, (int)stream.Length));

            Assert.Equal(1ul, dl1.Descriptor);
            List <object> lv = dl1.Value as List <object>;

            Assert.NotNull(lv);
            Assert.Equal(p.Name, lv[0]);
            Assert.Equal(p.Age, lv[1]);
            Assert.Equal(p.DateOfBirth.Value, lv[2]);
            Assert.True(lv[3] is DescribedType, "Address is decribed type");
            Assert.Equal(3ul, ((DescribedType)lv[3]).Descriptor);
            Assert.Equal(((List <object>)((DescribedType)lv[3]).Value)[0], ((Student)p).Address.FullAddress);
            Assert.True(lv[4] is AmqpMap, "Properties should be map");
            Assert.Equal(((AmqpMap)lv[4])[new MapKey("height")], p.Properties["height"]);
            Assert.Equal(((AmqpMap)lv[4])[new MapKey("male")], p.Properties["male"]);
            Assert.Equal(((AmqpMap)lv[4])[new MapKey("nick-name")], p.Properties["nick-name"]);
            Assert.True(lv[5] is List <object>);

            // Non-default serializer
            AmqpContractSerializer serializer = new AmqpContractSerializer();
            ByteBuffer             bf1        = new ByteBuffer(1024, true);

            serializer.WriteObjectInternal(bf1, p);

            Person p4 = serializer.ReadObjectInternal <Person, Person>(bf1);

            personValidator(p, p4);

            // Extensible: more items in the payload should not break
            DescribedType dl2 = new DescribedType(
                new AmqpSymbol("teacher"),
                new List <object>()
            {
                "Jerry", 40, null, 50000, lv[4], null, null, "unknown-string", true, new AmqpSymbol("unknown-symbol")
            });
            ByteBuffer bf2 = new ByteBuffer(1024, true);

            AmqpEncoding.EncodeObject(dl2, bf2);
            AmqpCodec.EncodeULong(100ul, bf2);

            Person p5 = serializer.ReadObjectInternal <Person, Person>(bf2);

            Assert.True(p5 is Teacher);
            Assert.Equal(100ul, AmqpCodec.DecodeULong(bf2));   // unknowns should be skipped
            Assert.Equal(0, bf2.Length);

            // teacher
            Teacher teacher = new Teacher("Han");

            teacher.Age     = 30;
            teacher.Sallary = 60000;
            teacher.Classes = new Dictionary <int, string>()
            {
                { 101, "CS" }, { 102, "Math" }, { 205, "Project" }
            };

            ByteBuffer bf3 = new ByteBuffer(1024, true);

            serializer.WriteObjectInternal(bf3, teacher);

            Person p6 = serializer.ReadObjectInternal <Person, Person>(bf3);

            Assert.True(p6 is Teacher);
            Assert.Equal(teacher.Age + 1, p6.Age);
            Assert.Equal(teacher.Sallary * 2, ((Teacher)p6).Sallary);
            Assert.Equal(teacher.Id, ((Teacher)p6).Id);
            Assert.Equal(teacher.Classes[101], ((Teacher)p6).Classes[101]);
            Assert.Equal(teacher.Classes[102], ((Teacher)p6).Classes[102]);
            Assert.Equal(teacher.Classes[205], ((Teacher)p6).Classes[205]);
        }
Beispiel #9
0
        SerializableType CompileNonContractTypes(Type type)
        {
            if (type.GetTypeInfo().IsGenericType&&
                type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                Type[] argTypes = type.GetGenericArguments();
                Fx.Assert(argTypes.Length == 1, "Nullable type must have one argument");
                return(this.GetType(argTypes[0]));
            }

            if (type.GetTypeInfo().IsInterface)
            {
                if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
                {
                    // if a member is defined as enumerable interface, we have to change it
                    // to list, otherwise the decoder cannot initialize an object of an interface
                    Type itemType = typeof(object);
                    Type listType = typeof(List <object>);
                    if (type.GetTypeInfo().IsGenericType)
                    {
                        Type[] argTypes = type.GetGenericArguments();
                        Fx.Assert(argTypes.Length == 1, "IEnumerable type must have one argument");
                        itemType = argTypes[0];
                        listType = typeof(List <>).MakeGenericType(argTypes);
                    }

                    MethodAccessor addAccess = MethodAccessor.Create(listType.GetMethod("Add", new Type[] { itemType }));
                    return(new SerializableType.List(this, listType, itemType, addAccess)
                    {
                        Final = true
                    });
                }

                return(null);
            }

            if (type.GetTypeInfo().IsEnum)
            {
                Type underlyingType = Enum.GetUnderlyingType(type);
                return(new SerializableType.Converted(
                           AmqpType.Converted,
                           type,
                           underlyingType,
                           (o, t) => Convert.ChangeType(o, t),
                           (o, t) => Enum.ToObject(t, o)));
            }

            if (type.GetInterfaces().Any(it => it == typeof(IAmqpSerializable)))
            {
                return(new SerializableType.Serializable(this, type));
            }

            if (type.IsArray)
            {
                // validate item type to be AMQP types only
                AmqpEncoding.GetEncoding(type.GetElementType());
                return(SerializableType.CreatePrimitiveType(type));
            }

            foreach (Type it in type.GetInterfaces())
            {
                if (it.GetTypeInfo().IsGenericType)
                {
                    Type genericTypeDef = it.GetGenericTypeDefinition();
                    if (genericTypeDef == typeof(IDictionary <,>))
                    {
                        Type[]         argTypes      = it.GetGenericArguments();
                        Type           itemType      = typeof(KeyValuePair <,>).MakeGenericType(argTypes);
                        MemberAccessor keyAccessor   = MemberAccessor.Create(itemType.GetProperty("Key"), false);
                        MemberAccessor valueAccessor = MemberAccessor.Create(itemType.GetProperty("Value"), false);
                        MethodAccessor addAccess     = MethodAccessor.Create(type.GetMethod("Add", argTypes));

                        return(new SerializableType.Map(this, type, keyAccessor, valueAccessor, addAccess));
                    }

                    if (genericTypeDef == typeof(ICollection <>))
                    {
                        Type[]         argTypes  = it.GetGenericArguments();
                        Type           itemType  = argTypes[0];
                        MethodAccessor addAccess = MethodAccessor.Create(type.GetMethod("Add", argTypes));

                        return(new SerializableType.List(this, type, itemType, addAccess));
                    }
                }
            }

            return(null);
        }
Beispiel #10
0
 internal virtual int GetValueEncodeSize()
 {
     return(AmqpEncoding.GetObjectEncodeSize(this.Value));
 }
Beispiel #11
0
            protected override void Initialize(ByteBuffer buffer, FormatCode formatCode,
                                               out int size, out int count, out int encodeWidth, out Collection effectiveType)
            {
                if (formatCode != FormatCode.Described)
                {
                    throw new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
                }

                effectiveType = null;
                formatCode    = AmqpEncoding.ReadFormatCode(buffer);
                ulong?     code   = null;
                AmqpSymbol symbol = default(AmqpSymbol);

                if (formatCode == FormatCode.ULong0)
                {
                    code = 0;
                }
                else if (formatCode == FormatCode.ULong || formatCode == FormatCode.SmallULong)
                {
                    code = ULongEncoding.Decode(buffer, formatCode);
                }
                else if (formatCode == FormatCode.Symbol8 || formatCode == FormatCode.Symbol32)
                {
                    symbol = SymbolEncoding.Decode(buffer, formatCode);
                }

                if (this.AreEqual(this.descriptorCode, this.descriptorName, code, symbol))
                {
                    effectiveType = this;
                }
                else if (this.knownTypes != null)
                {
                    for (int i = 0; i < this.knownTypes.Count; ++i)
                    {
                        Composite knownType = (Composite)this.serializer.GetType(this.knownTypes[i]);
                        if (this.AreEqual(knownType.descriptorCode, knownType.descriptorName, code, symbol))
                        {
                            effectiveType = knownType;
                            break;
                        }
                    }
                }

                if (effectiveType == null)
                {
                    throw new SerializationException(AmqpResources.GetString(AmqpResources.AmqpUnknownDescriptor, code ?? (object)symbol.Value, this.type.Name));
                }

                formatCode = AmqpEncoding.ReadFormatCode(buffer);
                if (this.Code == FormatCode.List32)
                {
                    if (formatCode == FormatCode.List0)
                    {
                        size = count = encodeWidth = 0;
                    }
                    else
                    {
                        encodeWidth = formatCode == FormatCode.List8 ? FixedWidth.UByte : FixedWidth.UInt;
                        AmqpEncoding.ReadSizeAndCount(buffer, formatCode, FormatCode.List8,
                                                      FormatCode.List32, out size, out count);
                    }
                }
                else
                {
                    encodeWidth = formatCode == FormatCode.Map8 ? FixedWidth.UByte : FixedWidth.UInt;
                    AmqpEncoding.ReadSizeAndCount(buffer, formatCode, FormatCode.Map8,
                                                  FormatCode.Map32, out size, out count);
                }
            }
Beispiel #12
0
 public override object ReadObject(ByteBuffer buffer)
 {
     return(AmqpEncoding.DecodeObject(buffer));
 }
Beispiel #13
0
 public override void WriteObject(ByteBuffer buffer, object value)
 {
     AmqpEncoding.EncodeObject(value, buffer);
 }
Beispiel #14
0
 public static void EncodeObject(object data, ByteBuffer buffer)
 {
     AmqpEncoding.EncodeObject(data, buffer);
 }
Beispiel #15
0
        static void DumpAmqpData(ByteBuffer buffer, int indent)
        {
            int        offset     = buffer.Offset;
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == 0x40)
            {
                WriteAmqpValue(offset, indent, "Null");
            }
            else if (formatCode == 0x41)
            {
                WriteAmqpValue(offset, indent, "Bool:True");
            }
            else if (formatCode == 0x42)
            {
                WriteAmqpValue(offset, indent, "Bool:False");
            }
            else if (formatCode == 0x50)
            {
                WriteAmqpValue(offset, indent, string.Format("UByte:{0}", (byte)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x60)
            {
                WriteAmqpValue(offset, indent, string.Format("UShort:{0}", (ushort)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x70)
            {
                WriteAmqpValue(offset, indent, string.Format("UInt:{0}", (uint)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x52)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallUInt:{0}", (uint)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x80)
            {
                WriteAmqpValue(offset, indent, string.Format("ULong:{0}", (ulong)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x53)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallULong:{0}", (ulong)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x51)
            {
                WriteAmqpValue(offset, indent, string.Format("Byte:{0}", (sbyte)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x61)
            {
                WriteAmqpValue(offset, indent, string.Format("Short:{0}", (short)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x71)
            {
                WriteAmqpValue(offset, indent, string.Format("Int:{0}", (int)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x54)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallInt:{0}", (int)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x81)
            {
                WriteAmqpValue(offset, indent, string.Format("Long:{0}", (long)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x54)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallLong:{0}", (long)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x72)
            {
                WriteAmqpValue(offset, indent, string.Format("Float:{0}", (float)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x82)
            {
                WriteAmqpValue(offset, indent, string.Format("Double:{0}", (double)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x73)
            {
                WriteAmqpValue(offset, indent, string.Format("Char:{0}", (char)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x83)
            {
                WriteAmqpValue(offset, indent, string.Format("TimeStamp:{0}", (DateTime)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0x98)
            {
                WriteAmqpValue(offset, indent, string.Format("Uuid:{0}", (Guid)AmqpEncoding.DecodeObject(buffer, formatCode)));
            }
            else if (formatCode == 0xa0 || formatCode == 0xb0)
            {
                ArraySegment <byte> bin = (ArraySegment <byte>)AmqpEncoding.DecodeObject(buffer, formatCode);
                WriteAmqpValue(offset, indent, string.Format("Binary{0}:{1}", formatCode == 0xa0 ? 8 : 32, bin.Array == null ? "Null" : bin.Count.ToString()));
            }
            else if (formatCode == 0xa1 || formatCode == 0xb1 || formatCode == 0xa2 || formatCode == 0xb2)
            {
                string str       = (string)AmqpEncoding.DecodeObject(buffer, formatCode);
                string toDisplay = "Null";
                if (str != null)
                {
                    toDisplay = str.Length > 20 ? str.Substring(0, 8) + "..." + str.Substring(str.Length - 8) : str;
                }

                WriteAmqpValue(offset, indent, string.Format("Utf{0}String{1}:{2}", (formatCode & 0x0F) == 0x01 ? 8 : 16, (formatCode & 0xF0) == 0xa0 ? 8 : 32, toDisplay));
            }
            else if (formatCode == 0xa3 || formatCode == 0xb3)
            {
                AmqpSymbol sym       = (AmqpSymbol)AmqpEncoding.DecodeObject(buffer, formatCode);
                string     toDisplay = "Null";
                if (sym.Value != null)
                {
                    toDisplay = sym.Value.Length > 16 ? sym.Value.Substring(0, 6) + "..." + sym.Value.Substring(sym.Value.Length - 6) : sym.Value;
                }

                WriteAmqpValue(offset, indent, string.Format("Symbol{0}:{1}", formatCode == 0xa3 ? 8 : 32, toDisplay));
            }
            else if (formatCode == 0xc0 || formatCode == 0xd0)
            {
                int size;
                int count;
                AmqpEncoding.ReadSizeAndCount(buffer, formatCode, 0xc0, 0xd0, out size, out count);
                WriteAmqpValue(offset, indent, string.Format("List{0}:{1},{2}", formatCode == 0xc0 ? 8 : 32, size, count));
                for (int i = 0; i < count; ++i)
                {
                    DumpAmqpData(buffer, indent + 1);
                }
            }
            else if (formatCode == 0xe0 || formatCode == 0xf0)
            {
                Array array = (Array)AmqpEncoding.DecodeObject(buffer, formatCode);
                WriteAmqpValue(offset, indent, string.Format("Array{0}:{1}", formatCode == 0xe0 ? 8 : 32, array.Length));
                for (int i = 0; i < array.Length; ++i)
                {
                    WriteAmqpValue(-1, indent + 1, array.GetValue(i).ToString());
                }
            }
            else if (formatCode == 0xc1 || formatCode == 0xd1)
            {
                int size;
                int count;
                AmqpEncoding.ReadSizeAndCount(buffer, formatCode, 0xc1, 0xd1, out size, out count);
                WriteAmqpValue(offset, indent, string.Format("Map{0}:{1},{2}", formatCode == 0xc0 ? 8 : 32, size, count));
                for (int i = 0; i < count; i += 2)
                {
                    DumpAmqpData(buffer, indent + 1);
                    DumpAmqpData(buffer, indent + 1);
                }
            }
            else if (formatCode == 0x00)
            {
                WriteAmqpValue(offset, indent, "Described type");
                DumpAmqpData(buffer, indent + 1);
                DumpAmqpData(buffer, indent + 1);
            }
        }
Beispiel #16
0
 static void ThrowInvalidFormatCodeException(FormatCode formatCode, int offset)
 {
     throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, offset));
 }
Beispiel #17
0
        // Parse the buffer for message sections. If a section is specified in the flags, fully
        // decode it; otherwise attempt to fast advance the buffer. The handler is invoked at
        // least once with the first section found. The section could be null if it is not in
        // the flags. The parser stops if handler returns false.
        public static bool TryRead <T1, T2>(T1 t1, T2 t2, ByteBuffer buffer, SectionFlag sections,
                                            Func <T1, T2, Section, bool> sectionHandler, string context, out Error error)
        {
            error = null;

            if (buffer.TryAddReference())
            {
                int pos = buffer.Offset;

                try
                {
                    while (buffer.Length > 0)
                    {
                        int offset = buffer.Offset;

                        SectionInfo info;
                        if (!TryReadSectionInfo(buffer, out info, out error))
                        {
                            return(false);
                        }

                        AmqpDescribed section;
                        int           length;
                        if ((info.Flag & sections) > 0)
                        {
                            section = info.Ctor();
                            if (section.DescriptorCode == Data.Code)
                            {
                                section.Value = BinaryEncoding.Decode(buffer, 0, false);
                            }
                            else
                            {
                                section.DecodeValue(buffer);
                            }

                            section.Offset = offset;
                            length         = buffer.Offset - offset;
                            section.Length = length;
                        }
                        else
                        {
                            // fast forward to next section
                            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);
                            if (formatCode != FormatCode.Null)
                            {
                                error = info.Scanner(formatCode, buffer);
                                if (error != null)
                                {
                                    return(false);
                                }
                            }

                            section = null;
                            length  = buffer.Offset - offset;
                        }

                        Section s = new Section(info.Flag, offset, length, section);
                        bool    shouldContinue = sectionHandler(t1, t2, s);

                        if (!shouldContinue)
                        {
                            break;
                        }
                    }

                    return(true);
                }
                catch (SerializationException se)
                {
                    error = GetDecodeError(se.Message);
                }
                catch (AmqpException ae)
                {
                    error = ae.Error;
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }

                    error = GetDecodeError(exception.Message);
                }
                finally
                {
                    buffer.Seek(pos);
                    buffer.RemoveReference();
                }
            }
            else
            {
                // The delivery is already disposed. Treat it as decode error.
                error = GetDecodeError(Resources.AmqpBufferAlreadyReclaimed);
            }

            return(false);
        }
Beispiel #18
0
 public virtual void DecodeValue(ByteBuffer buffer)
 {
     this.Value = AmqpEncoding.DecodeObject(buffer);
 }
Beispiel #19
0
            protected override void Initialize(ByteBuffer buffer, FormatCode formatCode, out int size, out int count, out int encodeWidth, out SerializableType.CollectionType effectiveType)
            {
                object valueOrDefault;

                if (formatCode != 0)
                {
                    throw new AmqpException(AmqpError.InvalidField, SRAmqp.AmqpInvalidFormatCode(formatCode, buffer.Offset));
                }
                effectiveType = null;
                formatCode    = AmqpEncoding.ReadFormatCode(buffer);
                ulong?     nullable   = null;
                AmqpSymbol amqpSymbol = new AmqpSymbol();

                if (formatCode == 68)
                {
                    nullable = new ulong?((ulong)0);
                }
                else if (formatCode == 128 || formatCode == 83)
                {
                    nullable = ULongEncoding.Decode(buffer, formatCode);
                }
                else if (formatCode == 163 || formatCode == 179)
                {
                    amqpSymbol = SymbolEncoding.Decode(buffer, formatCode);
                }
                if (this.AreEqual(this.descriptorCode, this.descriptorName, nullable, amqpSymbol))
                {
                    effectiveType = this;
                }
                else if (this.knownTypes != null)
                {
                    int num = 0;
                    while (num < (int)this.knownTypes.Length)
                    {
                        KeyValuePair <Type, SerializableType> keyValuePair = this.knownTypes[num];
                        if (keyValuePair.Value == null)
                        {
                            SerializableType type = this.serializer.GetType(keyValuePair.Key);
                            keyValuePair = new KeyValuePair <Type, SerializableType>(keyValuePair.Key, type);
                            KeyValuePair <Type, SerializableType> keyValuePair1 = keyValuePair;
                            keyValuePair         = keyValuePair1;
                            this.knownTypes[num] = keyValuePair1;
                        }
                        SerializableType.DescribedType value = (SerializableType.DescribedType)keyValuePair.Value;
                        if (!this.AreEqual(value.descriptorCode, value.descriptorName, nullable, amqpSymbol))
                        {
                            num++;
                        }
                        else
                        {
                            effectiveType = value;
                            break;
                        }
                    }
                }
                if (effectiveType == null)
                {
                    ulong?nullable1 = nullable;
                    if (nullable1.HasValue)
                    {
                        valueOrDefault = nullable1.GetValueOrDefault();
                    }
                    else
                    {
                        valueOrDefault = amqpSymbol.Value;
                    }
                    throw new SerializationException(SRAmqp.AmqpUnknownDescriptor(valueOrDefault, this.type.Name));
                }
                formatCode = AmqpEncoding.ReadFormatCode(buffer);
                if (this.Code != 208)
                {
                    encodeWidth = (formatCode == 193 ? 1 : 4);
                    AmqpEncoding.ReadSizeAndCount(buffer, formatCode, 193, 209, out size, out count);
                    return;
                }
                if (formatCode == 69)
                {
                    int num1 = 0;
                    int num2 = num1;
                    encodeWidth = num1;
                    int num3 = num2;
                    int num4 = num3;
                    count = num3;
                    size  = num4;
                    return;
                }
                encodeWidth = (formatCode == 192 ? 1 : 4);
                AmqpEncoding.ReadSizeAndCount(buffer, formatCode, 192, 208, out size, out count);
            }