Esempio n. 1
0
        static T[] Decode <T>(ByteBuffer buffer, int size, int count, FormatCode formatCode)
        {
            T[]          array      = new T[count];
            EncodingBase encoding   = AmqpEncoding.GetEncoding(formatCode);
            object       descriptor = null;

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

            for (int i = 0; i < count; ++i)
            {
                object value = encoding.DecodeObject(buffer, formatCode);
                if (descriptor != null)
                {
                    value = new DescribedType(descriptor, value);
                }

                array[i] = (T)value;
            }

            return(array);
        }
Esempio n. 2
0
        static void Encode(Array value, int width, int encodeSize, ByteBuffer buffer)
        {
            encodeSize -= FixedWidth.FormatCode + width;
            if (width == FixedWidth.UByte)
            {
                AmqpBitConverter.WriteUByte(buffer, (byte)encodeSize);
                AmqpBitConverter.WriteUByte(buffer, (byte)value.Length);
            }
            else
            {
                AmqpBitConverter.WriteUInt(buffer, (uint)encodeSize);
                AmqpBitConverter.WriteUInt(buffer, (uint)value.Length);
            }

            if (value.Length > 0)
            {
                object       firstItem = value.GetValue(0);
                EncodingBase encoding  = AmqpEncoding.GetEncoding(firstItem);
                AmqpBitConverter.WriteUByte(buffer, encoding.FormatCode);
                if (encoding.FormatCode == FormatCode.Described)
                {
                    DescribedType describedValue = (DescribedType)firstItem;
                    AmqpEncoding.EncodeObject(describedValue.Descriptor, buffer);
                    AmqpBitConverter.WriteUByte(buffer, AmqpEncoding.GetEncoding(describedValue.Value).FormatCode);
                }

                foreach (object item in value)
                {
                    encoding.EncodeObject(item, true, buffer);
                }
            }
        }
Esempio n. 3
0
        static int GetValueSize(Array value, Type type)
        {
            if (value.Length == 0)
            {
                return(0);
            }

            if (type == null)
            {
                type = value.GetValue(0).GetType();
            }

            EncodingBase encoding  = AmqpEncoding.GetEncoding(type);
            int          valueSize = 0;

            foreach (object item in value)
            {
                bool arrayEncoding = true;
                if (encoding.FormatCode == FormatCode.Described && valueSize == 0)
                {
                    arrayEncoding = false;
                }

                valueSize += encoding.GetObjectEncodeSize(item, arrayEncoding);
            }

            return(valueSize);
        }
Esempio n. 4
0
        public static int GetObjectEncodeSize(object value)
        {
            if (value == null)
            {
                return(FixedWidth.NullEncoded);
            }

            IAmqpSerializable serializable = value as IAmqpSerializable;

            if (serializable != null)
            {
                return(serializable.EncodeSize);
            }

            EncodingBase encoding = GetEncoding(value);

            return(encoding.GetObjectEncodeSize(value, false));
        }
Esempio n. 5
0
        public static void EncodeObject(object value, ByteBuffer buffer)
        {
            if (value == null)
            {
                EncodeNull(buffer);
                return;
            }

            IAmqpSerializable serializable = value as IAmqpSerializable;

            if (serializable != null)
            {
                serializable.Encode(buffer);
                return;
            }

            EncodingBase encoding = GetEncoding(value);

            encoding.EncodeObject(value, false, buffer);
        }
Esempio n. 6
0
        public static EncodingBase GetEncoding(Type type)
        {
            EncodingBase encoding = null;

            if (encodingsByType.TryGetValue(type, out encoding))
            {
                return(encoding);
            }
            else if (type.IsArray)
            {
                return(arrayEncoding);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                return(listEncoding);
            }
            else if (typeof(DescribedType).IsAssignableFrom(type))
            {
                return(describedTypeEncoding);
            }
            throw new NotSupportedException(AmqpResources.GetString(AmqpResources.AmqpInvalidType, type.ToString()));
        }
Esempio n. 7
0
        public static EncodingBase GetEncoding(object value)
        {
            EncodingBase encoding = null;
            Type         type     = value.GetType();

            if (encodingsByType.TryGetValue(type, out encoding))
            {
                return(encoding);
            }
            else if (type.IsArray)
            {
                return(arrayEncoding);
            }
            else if (value is IList)
            {
                return(listEncoding);
            }
            else if (value is DescribedType)
            {
                return(describedTypeEncoding);
            }

            throw new NotSupportedException(AmqpResources.GetString(AmqpResources.AmqpInvalidType, type.ToString()));
        }
Esempio n. 8
0
        static T[] Decode <T>(ByteBuffer buffer, int size, int count, FormatCode formatCode)
        {
            object descriptor = null;

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

            // Special cases for primitive types
            switch (formatCode)
            {
            case FormatCode.UInt0:
            case FormatCode.SmallUInt:
                return((T[])DecodeArray <uint>(buffer, formatCode, count, (b, f) => UIntEncoding.Decode(b, f).Value));

            case FormatCode.UInt:
                return((T[])DecodeArray <uint>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUInt(b)));

            case FormatCode.SmallInt:
                return((T[])DecodeArray <int>(buffer, formatCode, count, (b, f) => IntEncoding.Decode(b, f).Value));

            case FormatCode.Int:
                return((T[])DecodeArray <int>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadInt(b)));

            case FormatCode.ULong0:
            case FormatCode.SmallULong:
                return((T[])DecodeArray <ulong>(buffer, formatCode, count, (b, f) => ULongEncoding.Decode(b, f).Value));

            case FormatCode.ULong:
                return((T[])DecodeArray <ulong>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadULong(b)));

            case FormatCode.SmallLong:
                return((T[])DecodeArray <long>(buffer, formatCode, count, (b, f) => LongEncoding.Decode(b, f).Value));

            case FormatCode.Long:
                return((T[])DecodeArray <long>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadLong(b)));

            case FormatCode.Symbol8:
            case FormatCode.Symbol32:
                return((T[])DecodeArray <AmqpSymbol>(buffer, formatCode, count, (b, f) => SymbolEncoding.Decode(b, f)));

            case FormatCode.Boolean:
                return((T[])DecodeArray <bool>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUByte(b) > 0));

            case FormatCode.UByte:
                return((T[])DecodeArray <byte>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUByte(b)));

            case FormatCode.Byte:
                return((T[])DecodeArray <sbyte>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadByte(b)));

            case FormatCode.UShort:
                return((T[])DecodeArray <ushort>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUShort(b)));

            case FormatCode.Short:
                return((T[])DecodeArray <short>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadShort(b)));

            case FormatCode.Float:
                return((T[])DecodeArray <float>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadFloat(b)));

            case FormatCode.Double:
                return((T[])DecodeArray <double>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadDouble(b)));

            case FormatCode.Decimal32:
            case FormatCode.Decimal64:
            case FormatCode.Decimal128:
                return((T[])DecodeArray <decimal>(buffer, formatCode, count, (b, f) => DecimalEncoding.DecodeValue(b, f)));

            case FormatCode.Char:
                return((T[])DecodeArray <char>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadChar(b)));

            case FormatCode.TimeStamp:
                return((T[])DecodeArray <DateTime>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadTimestamp(b)));

            case FormatCode.Uuid:
                return((T[])DecodeArray <Guid>(buffer, formatCode, count, (b, f) => AmqpBitConverter.ReadUuid(b)));

            default:
                break;
            }

            // General path
            EncodingBase encoding = AmqpEncoding.GetEncoding(formatCode);

            T[] array = new T[count];
            for (int i = 0; i < count; ++i)
            {
                object value = encoding.DecodeObject(buffer, formatCode);
                if (descriptor != null)
                {
                    value = new DescribedType(descriptor, value);
                }

                array[i] = (T)value;
            }

            return(array);
        }
Esempio n. 9
0
        internal static void EncodeValue(IList value, int count, Type itemType, ByteBuffer buffer)
        {
            if (count == 0)
            {
                return;
            }

            EncodingBase encoding = AmqpEncoding.GetEncoding(itemType);

            switch (encoding.FormatCode)
            {
            case FormatCode.UInt:
                EncodeArray <uint>(buffer, (IList <uint>)value, count, FormatCode.UInt, (b, v, i) => AmqpBitConverter.WriteUInt(b, v));
                return;

            case FormatCode.Int:
                buffer.Validate(true, FixedWidth.Int * count);
                EncodeArray <int>(buffer, (IList <int>)value, count, FormatCode.Int, (b, v, i) =>
                {
                    AmqpBitConverter.WriteInt(b.Buffer, b.WritePos + i * FixedWidth.Int, v);
                });
                buffer.Append(FixedWidth.Int * count);
                return;

            case FormatCode.ULong:
                EncodeArray <ulong>(buffer, (IList <ulong>)value, count, FormatCode.ULong, (b, v, i) => AmqpBitConverter.WriteULong(b, v));
                return;

            case FormatCode.Long:
                EncodeArray <long>(buffer, (IList <long>)value, count, FormatCode.Long, (b, v, i) => AmqpBitConverter.WriteLong(b, v));
                return;

            case FormatCode.Symbol32:
                EncodeArray <AmqpSymbol>(buffer, (IList <AmqpSymbol>)value, count, FormatCode.Symbol32, (b, v, i) => SymbolEncoding.EncodeValue(v, FormatCode.Symbol32, b));
                return;

            case FormatCode.Boolean:
                EncodeArray <bool>(buffer, (IList <bool>)value, count, FormatCode.Boolean, (b, v, i) => AmqpBitConverter.WriteUByte(b, (byte)(v ? 1 : 0)));
                return;

            case FormatCode.UByte:
                EncodeArray <byte>(buffer, (IList <byte>)value, count, FormatCode.UByte, (b, v, i) => AmqpBitConverter.WriteUByte(b, v));
                return;

            case FormatCode.Byte:
                EncodeArray <sbyte>(buffer, (IList <sbyte>)value, count, FormatCode.Byte, (b, v, i) => AmqpBitConverter.WriteByte(b, v));
                return;

            case FormatCode.UShort:
                EncodeArray <ushort>(buffer, (IList <ushort>)value, count, FormatCode.UShort, (b, v, i) => AmqpBitConverter.WriteUShort(b, v));
                return;

            case FormatCode.Short:
                EncodeArray <short>(buffer, (IList <short>)value, count, FormatCode.Short, (b, v, i) => AmqpBitConverter.WriteShort(b, v));
                return;

            case FormatCode.Float:
                EncodeArray <float>(buffer, (IList <float>)value, count, FormatCode.Float, (b, v, i) => AmqpBitConverter.WriteFloat(b, v));
                return;

            case FormatCode.Double:
                EncodeArray <double>(buffer, (IList <double>)value, count, FormatCode.Double, (b, v, i) => AmqpBitConverter.WriteDouble(b, v));
                return;

            case FormatCode.Decimal128:
                EncodeArray <decimal>(buffer, (IList <decimal>)value, count, FormatCode.Decimal128, (b, v, i) => DecimalEncoding.EncodeValue(v, b));
                return;

            case FormatCode.Char:
                EncodeArray <char>(buffer, (IList <char>)value, count, FormatCode.Char, (b, v, i) => AmqpBitConverter.WriteChar(b, v));
                return;

            case FormatCode.TimeStamp:
                EncodeArray <DateTime>(buffer, (IList <DateTime>)value, count, FormatCode.TimeStamp, (b, v, i) => AmqpBitConverter.WriteTimestamp(b, v));
                return;

            case FormatCode.Uuid:
                EncodeArray <Guid>(buffer, (IList <Guid>)value, count, FormatCode.Uuid, (b, v, i) => AmqpBitConverter.WriteUuid(b, v));
                return;

            default:
                break;
            }

            object firstItem = value[0];

            AmqpBitConverter.WriteUByte(buffer, encoding.FormatCode);
            if (encoding.FormatCode == FormatCode.Described)
            {
                DescribedType describedValue = (DescribedType)firstItem;
                AmqpEncoding.EncodeObject(describedValue.Descriptor, buffer);
                AmqpBitConverter.WriteUByte(buffer, AmqpEncoding.GetEncoding(describedValue.Value).FormatCode);
            }

            for (int i = 0; i < count; i++)
            {
                encoding.EncodeObject(value[i], true, buffer);
            }
        }