Beispiel #1
0
        internal static Multiple <T> Decode(ByteBuffer buffer)
        {
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == FormatCode.Null)
            {
                return(null);
            }

            if (formatCode == FormatCode.Array8 || formatCode == FormatCode.Array32)
            {
                T[] array = ArrayEncoding.Decode <T>(buffer, formatCode);
                return(new Multiple <T>(array));
            }

            if (formatCode == FormatCode.Symbol8 || formatCode == FormatCode.Symbol32)
            {
                var symbol = SymbolEncoding.Decode(buffer, formatCode);
                return(new Multiple <AmqpSymbol>()
                {
                    symbol
                } as Multiple <T>);
            }

            object value = AmqpEncoding.DecodeObject(buffer, formatCode);

            if (value is T)
            {
                Multiple <T> multiple = new Multiple <T>();
                multiple.Add((T)value);
                return(multiple);
            }

            throw new AmqpException(AmqpErrorCode.InvalidField, $"The expected type is '{typeof(T).Name}' but got '{value.GetType().Name}'");
        }
Beispiel #2
0
        public static object DecodeObject(ByteBuffer buffer)
        {
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == 64)
            {
                return(null);
            }
            if (formatCode != 0)
            {
                return(AmqpEncoding.DecodeObject(buffer, formatCode));
            }
            object obj = AmqpCodec.DecodeObject(buffer);
            Func <AmqpDescribed> func = null;

            if (obj is AmqpSymbol)
            {
                Dictionary <string, Func <AmqpDescribed> > strs = AmqpCodec.knownTypesByName;
                AmqpSymbol amqpSymbol = (AmqpSymbol)obj;
                strs.TryGetValue(amqpSymbol.Value, out func);
            }
            else if (obj is ulong)
            {
                AmqpCodec.knownTypesByCode.TryGetValue((ulong)obj, out func);
            }
            if (func == null)
            {
                return(new DescribedType(obj, AmqpCodec.DecodeObject(buffer)));
            }
            AmqpDescribed amqpDescribed = func();

            amqpDescribed.DecodeValue(buffer);
            return(amqpDescribed);
        }
Beispiel #3
0
        public static MessageId Decode(ByteBuffer buffer)
        {
            object obj = AmqpEncoding.DecodeObject(buffer);

            if (obj == null)
            {
                return(null);
            }
            if (obj is ulong)
            {
                return((ulong)obj);
            }
            if (obj is Guid)
            {
                return((Guid)obj);
            }
            if (obj is ArraySegment <byte> )
            {
                return((ArraySegment <byte>)obj);
            }
            if (!(obj is string))
            {
                throw new NotSupportedException(obj.GetType().ToString());
            }
            return((string)obj);
        }
Beispiel #4
0
 internal override void OnDecode(ByteBuffer buffer, int count)
 {
     for (int i = 0; i < count; i++)
     {
         this.innerList.Add(AmqpEncoding.DecodeObject(buffer));
     }
 }
Beispiel #5
0
        internal static MessageId Decode(ByteBuffer buffer)
        {
            object value = AmqpEncoding.DecodeObject(buffer);

            if (value == null)
            {
                return(null);
            }

            if (value is ulong)
            {
                return((ulong)value);
            }

            if (value is Guid)
            {
                return((Guid)value);
            }

            if (value is ArraySegment <byte> )
            {
                return((ArraySegment <byte>)value);
            }

            if (value is string)
            {
                return((string)value);
            }

            throw new NotSupportedException(value.GetType().ToString());
        }
Beispiel #6
0
 protected override void OnDecode(ByteBuffer buffer, int count)
 {
     if (count-- > 0)
     {
         this.GlobalId = AmqpEncoding.DecodeObject(buffer);
     }
 }
Beispiel #7
0
        protected override void OnDecode(ByteBuffer buffer, int count)
        {
            int num = count;

            count = num - 1;
            if (num > 0)
            {
                this.GlobalId = AmqpEncoding.DecodeObject(buffer);
            }
        }
 public override object DecodeObject(ByteBuffer buffer, FormatCode formatCode)
 {
     if (formatCode == FormatCode.Described)
     {
         return(DescribedEncoding.Decode(buffer, formatCode));
     }
     else
     {
         return(AmqpEncoding.DecodeObject(buffer, formatCode));
     }
 }
        static DescribedType Decode(ByteBuffer buffer, FormatCode formatCode)
        {
            if (formatCode != FormatCode.Described)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
            }

            object descriptor = AmqpEncoding.DecodeObject(buffer);
            object value      = AmqpEncoding.DecodeObject(buffer);

            return(new DescribedType(descriptor, value));
        }
Beispiel #10
0
            public override object ReadObject(ByteBuffer buffer)
            {
                object value = AmqpEncoding.DecodeObject(buffer);

                if (value == null)
                {
                    return(null);
                }
                else
                {
                    return(this.GetSource(value));
                }
            }
Beispiel #11
0
        public static Address Decode(ByteBuffer buffer)
        {
            object obj = AmqpEncoding.DecodeObject(buffer);

            if (obj == null)
            {
                return(null);
            }
            if (!(obj is string))
            {
                throw new NotSupportedException(obj.GetType().ToString());
            }
            return((string)obj);
        }
Beispiel #12
0
        internal static Address Decode(ByteBuffer buffer)
        {
            object value = AmqpEncoding.DecodeObject(buffer);

            if (value == null)
            {
                return(null);
            }

            if (value is string)
            {
                return((string)value);
            }

            throw new NotSupportedException(value.GetType().ToString());
        }
Beispiel #13
0
        public static Multiple <T> Decode(ByteBuffer buffer)
        {
            object obj = AmqpEncoding.DecodeObject(buffer);

            if (obj == null)
            {
                return(null);
            }
            if (obj is T)
            {
                Multiple <T> multiple = new Multiple <T>();
                multiple.Add((T)obj);
                return(multiple);
            }
            if (!obj.GetType().IsArray)
            {
                throw new AmqpException(AmqpError.InvalidField);
            }
            return(new Multiple <T>((T[])obj));
        }
Beispiel #14
0
        /// <summary>
        /// Decodes an AMQP object from the buffer and advances the buffer's position.
        /// </summary>
        /// <param name="buffer">The buffer to read.</param>
        /// <returns>An AMQP object.</returns>
        public static object DecodeObject(ByteBuffer buffer)
        {
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == FormatCode.Null)
            {
                return(null);
            }
            else if (formatCode == FormatCode.Described)
            {
                object descriptor = AmqpCodec.DecodeObject(buffer);
                Func <AmqpDescribed> knownTypeCtor = null;
                if (descriptor is AmqpSymbol)
                {
                    knownTypesByName.TryGetValue(((AmqpSymbol)descriptor).Value, out knownTypeCtor);
                }
                else if (descriptor is ulong)
                {
                    knownTypesByCode.TryGetValue((ulong)descriptor, out knownTypeCtor);
                }

                if (knownTypeCtor != null)
                {
                    AmqpDescribed amqpDescribed = knownTypeCtor();
                    amqpDescribed.DecodeValue(buffer);
                    return(amqpDescribed);
                }
                else
                {
                    object value = AmqpCodec.DecodeObject(buffer);
                    return(new DescribedType(descriptor, value));
                }
            }
            else
            {
                return(AmqpEncoding.DecodeObject(buffer, formatCode));
            }
        }
Beispiel #15
0
        public static Multiple <T> Decode(ByteBuffer buffer)
        {
            object value = AmqpEncoding.DecodeObject(buffer);

            if (value == null)
            {
                return(null);
            }
            else if (value is T)
            {
                Multiple <T> multiple = new Multiple <T>();
                multiple.Add((T)value);
                return(multiple);
            }
            else if (value.GetType().IsArray)
            {
                Multiple <T> multiple = new Multiple <T>((T[])value);
                return(multiple);
            }
            else
            {
                throw new AmqpException(AmqpError.InvalidField);
            }
        }
Beispiel #16
0
 public override object ReadObject(ByteBuffer buffer)
 {
     return(AmqpEncoding.DecodeObject(buffer));
 }
Beispiel #17
0
        static void DumpAmqpData(ByteBuffer buffer, int indent)
        {
            int  offset     = buffer.Offset;
            byte formatCode = buffer.Buffer[buffer.Offset];

            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)));
            }
            else if (formatCode == 0x60)
            {
                WriteAmqpValue(offset, indent, string.Format("UShort:{0}", (ushort)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x70)
            {
                WriteAmqpValue(offset, indent, string.Format("UInt:{0}", (uint)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x52)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallUInt:{0}", (uint)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x80)
            {
                WriteAmqpValue(offset, indent, string.Format("ULong:{0}", (ulong)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x53)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallULong:{0}", (ulong)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x51)
            {
                WriteAmqpValue(offset, indent, string.Format("Byte:{0}", (sbyte)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x61)
            {
                WriteAmqpValue(offset, indent, string.Format("Short:{0}", (short)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x71)
            {
                WriteAmqpValue(offset, indent, string.Format("Int:{0}", (int)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x54)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallInt:{0}", (int)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x81)
            {
                WriteAmqpValue(offset, indent, string.Format("Long:{0}", (long)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x54)
            {
                WriteAmqpValue(offset, indent, string.Format("SmallLong:{0}", (long)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x72)
            {
                WriteAmqpValue(offset, indent, string.Format("Float:{0}", (float)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x82)
            {
                WriteAmqpValue(offset, indent, string.Format("Double:{0}", (double)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x73)
            {
                WriteAmqpValue(offset, indent, string.Format("Char:{0}", (char)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x83)
            {
                WriteAmqpValue(offset, indent, string.Format("TimeStamp:{0}", (DateTime)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0x98)
            {
                WriteAmqpValue(offset, indent, string.Format("Uuid:{0}", (Guid)AmqpEncoding.DecodeObject(buffer)));
            }
            else if (formatCode == 0xa0 || formatCode == 0xb0)
            {
                ArraySegment <byte> bin = (ArraySegment <byte>)AmqpEncoding.DecodeObject(buffer);
                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);
                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);
                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)
            {
                IList <object> list = AmqpEncoding.DecodeObject(buffer) as List <object>;
                WriteAmqpValue(offset, indent, string.Format("List{0}:{1}", formatCode == 0xc0 ? 8 : 32, list.Count));
                for (int i = 0; i < list.Count; ++i)
                {
                    WriteAmqpValue(-1, indent + 1, list[i].ToString());
                }
            }
            else if (formatCode == 0xe0 || formatCode == 0xf0)
            {
                Array array = (Array)AmqpEncoding.DecodeObject(buffer);
                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)
            {
                AmqpMap map = (AmqpMap)AmqpEncoding.DecodeObject(buffer);
                WriteAmqpValue(offset, indent, string.Format("Map{0}:{1}", formatCode == 0xc0 ? 8 : 32, map.Count));
                foreach (var kvp in map)
                {
                    WriteAmqpValue(-1, indent + 1, kvp.Key.ToString());
                    WriteAmqpValue(-1, indent + 1, kvp.Value?.ToString() ?? "Null");
                }
            }
            else if (formatCode == 0x00)
            {
                WriteAmqpValue(offset, indent, "Described");
                DumpAmqpData(buffer, indent + 1);
                DumpAmqpData(buffer, indent + 1);
            }
        }
Beispiel #18
0
 public virtual void DecodeValue(ByteBuffer buffer)
 {
     this.Value = AmqpEncoding.DecodeObject(buffer);
 }
Beispiel #19
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]);
        }