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 void DecodeDescriptor(ByteBuffer buffer, out AmqpSymbol name, out ulong code)
        {
            name = default(AmqpSymbol);
            code = 0;

            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == FormatCode.Described)
            {
                formatCode = AmqpEncoding.ReadFormatCode(buffer);
            }

            if (formatCode == FormatCode.Symbol8 ||
                formatCode == FormatCode.Symbol32)
            {
                name = SymbolEncoding.Decode(buffer, formatCode);
            }
            else if (formatCode == FormatCode.ULong ||
                     formatCode == FormatCode.ULong0 ||
                     formatCode == FormatCode.SmallULong)
            {
                code = ULongEncoding.Decode(buffer, formatCode).Value;
            }
            else
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
            }
        }
Beispiel #3
0
        public static AmqpDescribed CreateAmqpDescribed(ByteBuffer buffer, Dictionary <string, Func <AmqpDescribed> > byName, Dictionary <ulong, Func <AmqpDescribed> > byCode)
        {
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == 64)
            {
                return(null);
            }
            EncodingBase.VerifyFormatCode(formatCode, 0, buffer.Offset);
            Func <AmqpDescribed> func = null;

            formatCode = AmqpEncoding.ReadFormatCode(buffer);
            if (formatCode == 163 || formatCode == 179)
            {
                AmqpSymbol amqpSymbol = SymbolEncoding.Decode(buffer, formatCode);
                byName.TryGetValue(amqpSymbol.Value, out func);
            }
            else if (formatCode == 68 || formatCode == 128 || formatCode == 83)
            {
                ulong?nullable = ULongEncoding.Decode(buffer, formatCode);
                byCode.TryGetValue(nullable.Value, out func);
            }
            if (func == null)
            {
                throw AmqpEncoding.GetEncodingException(SRAmqp.AmqpInvalidFormatCode(formatCode, buffer.Offset));
            }
            return(func());
        }
Beispiel #4
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);
            }
Beispiel #5
0
        public static void DecodeDescriptor(ByteBuffer buffer, out AmqpSymbol name, out ulong code)
        {
            name = new AmqpSymbol();
            code = (ulong)0;
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

            if (formatCode == 0)
            {
                formatCode = AmqpEncoding.ReadFormatCode(buffer);
            }
            if (formatCode == 163 || formatCode == 179)
            {
                name = SymbolEncoding.Decode(buffer, formatCode);
                return;
            }
            if (!(formatCode == 128) && !(formatCode == 68) && !(formatCode == 83))
            {
                throw AmqpEncoding.GetEncodingException(SRAmqp.AmqpInvalidFormatCode(formatCode, buffer.Offset));
            }
            code = ULongEncoding.Decode(buffer, formatCode).Value;
        }
Beispiel #6
0
        internal static AmqpDescribed CreateAmqpDescribed(
            ByteBuffer buffer,
            Dictionary <string, Func <AmqpDescribed> > byName,
            Dictionary <ulong, Func <AmqpDescribed> > byCode)
        {
            FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);

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

            EncodingBase.VerifyFormatCode(formatCode, buffer.Offset, FormatCode.Described);

            Func <AmqpDescribed> knownTypeCtor = null;

            formatCode = AmqpEncoding.ReadFormatCode(buffer);
            if (formatCode == FormatCode.Symbol8 || formatCode == FormatCode.Symbol32)
            {
                AmqpSymbol name = SymbolEncoding.Decode(buffer, formatCode);
                byName.TryGetValue(name.Value, out knownTypeCtor);
            }
            else if (formatCode == FormatCode.ULong0 || formatCode == FormatCode.ULong || formatCode == FormatCode.SmallULong)
            {
                ulong code = ULongEncoding.Decode(buffer, formatCode).Value;
                byCode.TryGetValue(code, out knownTypeCtor);
            }

            if (knownTypeCtor == null)
            {
                throw AmqpEncoding.GetEncodingException(AmqpResources.GetString(AmqpResources.AmqpInvalidFormatCode, formatCode, buffer.Offset));
            }

            AmqpDescribed value = knownTypeCtor();

            return(value);
        }
Beispiel #7
0
 /// <summary>
 /// Decodes a symbol from the buffer and advances the buffer's position.
 /// </summary>
 /// <param name="buffer">The buffer to read.</param>
 /// <returns>A symbol.</returns>
 public static AmqpSymbol DecodeSymbol(ByteBuffer buffer)
 {
     return(SymbolEncoding.Decode(buffer, 0));
 }
Beispiel #8
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 #9
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 #10
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);
            }