Exemple #1
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));
            }
        }
Exemple #2
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());
        }
Exemple #3
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}'");
        }
Exemple #4
0
 public override void WriteObject(ByteBuffer buffer, object value)
 {
     if (value == null)
     {
         AmqpEncoding.EncodeNull(buffer);
         return;
     }
     AmqpBitConverter.WriteUByte(buffer, 0);
     SymbolEncoding.Encode(this.symbol, buffer);
     this.encoder.EncodeObject(this.getter((TValue)value), false, buffer);
 }
Exemple #5
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);
            }
Exemple #6
0
 protected override bool WriteFormatCode(ByteBuffer buffer)
 {
     AmqpBitConverter.WriteUByte(buffer, 0);
     if (!this.descriptorCode.HasValue)
     {
         SymbolEncoding.Encode(this.descriptorName, buffer);
     }
     else
     {
         ULongEncoding.Encode(this.descriptorCode, buffer);
     }
     AmqpBitConverter.WriteUByte(buffer, this.Code);
     return(true);
 }
            protected override bool WriteFormatCode(ByteBuffer buffer)
            {
                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Described);
                if (this.descriptorCode != null)
                {
                    ULongEncoding.Encode(this.descriptorCode, buffer);
                }
                else
                {
                    SymbolEncoding.Encode(this.descriptorName, buffer);
                }

                AmqpBitConverter.WriteUByte(buffer, this.Code);
                return(true);
            }
Exemple #8
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;
        }
Exemple #9
0
            protected override void OnWriteObject(ByteBuffer buffer, object graph)
            {
                if (graph == null)
                {
                    AmqpEncoding.EncodeNull(buffer);
                    return;
                }

                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Described);
                if (this.descriptorName.Value != null)
                {
                    SymbolEncoding.Encode(this.descriptorName, buffer);
                }
                else
                {
                    ULongEncoding.Encode(this.descriptorCode, buffer);
                }

                if (this.members.Length == 0)
                {
                    AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.List0);
                    return;
                }

                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.List32);
                int endCopy = buffer.End;               // remember the current position

                AmqpBitConverter.WriteUInt(buffer, 0);  // reserve space for list size
                AmqpBitConverter.WriteUInt(buffer, (uint)this.members.Length);

                for (int i = 0; i < this.members.Length; ++i)
                {
                    object memberValue = this.members[i].Accessor.ReadObject(graph);
                    this.members[i].Type.OnWriteObject(buffer, memberValue);
                }

                // write the correct size
                AmqpBitConverter.WriteUInt(buffer.Buffer, endCopy, (uint)(buffer.End - endCopy - FixedWidth.UInt));
            }
Exemple #10
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);
        }
Exemple #11
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));
 }
Exemple #12
0
 /// <summary>
 /// Encodes a symbol and appends the bytes to the buffer.
 /// </summary>
 /// <param name="data">The symbol.</param>
 /// <param name="buffer">The destination buffer.</param>
 public static void EncodeSymbol(AmqpSymbol data, ByteBuffer buffer)
 {
     SymbolEncoding.Encode(data, buffer);
 }
Exemple #13
0
 /// <summary>
 /// Gets the encode size of a symbol.
 /// </summary>
 /// <param name="value">The symbol.</param>
 /// <returns>Encode size in bytes of the symbol.</returns>
 public static int GetSymbolEncodeSize(AmqpSymbol value)
 {
     return(SymbolEncoding.GetEncodeSize(value));
 }
Exemple #14
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);
        }
            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);
                }
            }
Exemple #16
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);
            }