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
            public override object ReadObject(ByteBuffer buffer)
            {
                AmqpDescribedType describedType = DescribedEncoding.Decode(buffer);

                if (describedType == null)
                {
                    return(null);
                }
                else
                {
                    if (!this.symbol.Equals(describedType.Descriptor))
                    {
                        throw new SerializationException(describedType.Descriptor.ToString());
                    }

                    return(this.setter((TAs)describedType.Value));
                }
            }
        public static bool TryGetAmqpObjectFromNetObject(object netObject, MappingType mappingType, out object amqpObject)
        {
            amqpObject = null;
            if (netObject == null)
            {
                return false;
            }

            switch (SerializationUtilities.GetTypeId(netObject))
            {
                case PropertyValueType.Byte:
                case PropertyValueType.SByte:
                case PropertyValueType.Int16:
                case PropertyValueType.Int32:
                case PropertyValueType.Int64:
                case PropertyValueType.UInt16:
                case PropertyValueType.UInt32:
                case PropertyValueType.UInt64:
                case PropertyValueType.Single:
                case PropertyValueType.Double:
                case PropertyValueType.Boolean:
                case PropertyValueType.Decimal:
                case PropertyValueType.Char:
                case PropertyValueType.Guid:
                case PropertyValueType.DateTime:
                case PropertyValueType.String:
                    amqpObject = netObject;
                    break;
                case PropertyValueType.Stream:
                    if (mappingType == MappingType.ApplicationProperty)
                    {
                        amqpObject = ReadStream((Stream)netObject);
                    }
                    break;
                case PropertyValueType.Uri:
                    amqpObject = new DescribedType((AmqpSymbol)UriName, ((Uri)netObject).AbsoluteUri);
                    break;
                case PropertyValueType.DateTimeOffset:
                    amqpObject = new DescribedType((AmqpSymbol)DateTimeOffsetName, ((DateTimeOffset)netObject).UtcTicks);
                    break;
                case PropertyValueType.TimeSpan:
                    amqpObject = new DescribedType((AmqpSymbol)TimeSpanName, ((TimeSpan)netObject).Ticks);
                    break;
                case PropertyValueType.Unknown:
                    if (netObject is Stream)
                    {
                        if (mappingType == MappingType.ApplicationProperty)
                        {
                            amqpObject = ReadStream((Stream)netObject);
                        }
                    }
                    else if (mappingType == MappingType.ApplicationProperty)
                    {
                        throw FxTrace.Exception.AsError(new SerializationException(IotHubApiResources.GetString(ApiResources.FailedToSerializeUnsupportedType, netObject.GetType().FullName)));
                    }
                    else if (netObject is byte[])
                    {
                        amqpObject = new ArraySegment<byte>((byte[])netObject);
                    }
                    else if (netObject is IList)
                    {
                        // Array is also an IList
                        amqpObject = netObject;
                    }
                    else if (netObject is IDictionary)
                    {
                        amqpObject = new AmqpMap((IDictionary)netObject);
                    }
                    break;
                default:
                    break;
            }

            return amqpObject != null;
        }
Esempio n. 5
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. 6
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);
            }
        }