예제 #1
0
        protected override async Task <IList <BrokeredMessage> > OnPeekAsync(long fromSequenceNumber, int messageCount = 1)
        {
            try
            {
                AmqpRequestMessage requestMessage =
                    AmqpRequestMessage.CreateRequest(
                        ManagementConstants.Operations.PeekMessageOperation,
                        this.OperationTimeout,
                        null);

                requestMessage.Map[ManagementConstants.Properties.FromSequenceNumber] = fromSequenceNumber;
                requestMessage.Map[ManagementConstants.Properties.MessageCount]       = messageCount;

                if (!string.IsNullOrWhiteSpace(this.sessionId))
                {
                    requestMessage.Map[ManagementConstants.Properties.SessionId] = this.sessionId;
                }

                List <BrokeredMessage> messages = new List <BrokeredMessage>();

                AmqpResponseMessage response = await this.ExecuteRequestResponseAsync(requestMessage).ConfigureAwait(false);

                if (response.StatusCode == AmqpResponseStatusCode.OK)
                {
                    BrokeredMessage brokeredMessage = null;
                    var             messageList     = response.GetListValue <AmqpMap>(ManagementConstants.Properties.Messages);
                    foreach (AmqpMap entry in messageList)
                    {
                        var         payload     = (ArraySegment <byte>)entry[ManagementConstants.Properties.Message];
                        AmqpMessage amqpMessage =
                            AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(new[] { payload }), true);
                        brokeredMessage = AmqpMessageConverter.ClientGetMessage(amqpMessage);
                        messages.Add(brokeredMessage);
                    }

                    if (brokeredMessage != null)
                    {
                        this.LastPeekedSequenceNumber = brokeredMessage.SequenceNumber;
                    }

                    return(messages);
                }
                else if (response.StatusCode == AmqpResponseStatusCode.NoContent ||
                         (response.StatusCode == AmqpResponseStatusCode.NotFound && AmqpSymbol.Equals(AmqpClientConstants.MessageNotFoundError, response.GetResponseErrorCondition())))
                {
                    return(messages);
                }
                else
                {
                    throw response.ToMessagingContractException();
                }
            }
            catch (Exception exception)
            {
                throw AmqpExceptionHelper.GetClientException(exception);
            }
        }
예제 #2
0
        /// <summary>
        ///   Translates the AMQP symbol into its corresponding typed value, if it belongs to the
        ///   set of known types.
        /// </summary>
        ///
        /// <param name="symbol">The symbol to consider.</param>
        /// <param name="value">The value of the symbol to translate.</param>
        ///
        /// <returns>The typed value of the symbol, if it belongs to the well-known set; otherwise, <c>null</c>.</returns>
        ///
        private static object TranslateSymbol(AmqpSymbol symbol,
                                              object value)
        {
            if (symbol.Equals(AmqpProperty.Descriptor.Uri))
            {
                return(new Uri((string)value));
            }

            if (symbol.Equals(AmqpProperty.Descriptor.TimeSpan))
            {
                return(new TimeSpan((long)value));
            }

            if (symbol.Equals(AmqpProperty.Descriptor.DateTimeOffset))
            {
                return(new DateTimeOffset((long)value, TimeSpan.Zero));
            }

            return(null);
        }
예제 #3
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);
            }
예제 #4
0
        public static bool TryGetNetObjectFromAmqpObject(object amqpObject, MappingType mappingType, out object netObject)
        {
            netObject = null;
            if (amqpObject == null)
            {
                return(false);
            }

            switch (SerializationUtilities.GetTypeId(amqpObject))
            {
            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:
                netObject = amqpObject;
                break;

            case PropertyValueType.Unknown:
                if (amqpObject is AmqpSymbol)
                {
                    netObject = ((AmqpSymbol)amqpObject).Value;
                }
                else if (amqpObject is ArraySegment <byte> )
                {
                    ArraySegment <byte> binValue = (ArraySegment <byte>)amqpObject;
                    if (binValue.Count == binValue.Array.Length)
                    {
                        netObject = binValue.Array;
                    }
                    else
                    {
                        byte[] buffer = new byte[binValue.Count];
                        Buffer.BlockCopy(binValue.Array, binValue.Offset, buffer, 0, binValue.Count);
                        netObject = buffer;
                    }
                }
                else if (amqpObject is DescribedType)
                {
                    DescribedType describedType = (DescribedType)amqpObject;
                    if (describedType.Descriptor is AmqpSymbol)
                    {
                        AmqpSymbol symbol = (AmqpSymbol)describedType.Descriptor;
                        if (symbol.Equals((AmqpSymbol)UriName))
                        {
                            netObject = new Uri((string)describedType.Value);
                        }
                        else if (symbol.Equals((AmqpSymbol)TimeSpanName))
                        {
                            netObject = new TimeSpan((long)describedType.Value);
                        }
                        else if (symbol.Equals((AmqpSymbol)DateTimeOffsetName))
                        {
                            netObject = new DateTimeOffset(new DateTime((long)describedType.Value, DateTimeKind.Utc));
                        }
                    }
                }
                else if (mappingType == MappingType.ApplicationProperty)
                {
                    throw FxTrace.Exception.AsError(new SerializationException(IotHubApiResources.GetString(ApiResources.FailedToSerializeUnsupportedType, amqpObject.GetType().FullName)));
                }
                else if (amqpObject is AmqpMap)
                {
                    AmqpMap map = (AmqpMap)amqpObject;
                    Dictionary <string, object> dictionary = new Dictionary <string, object>();
                    foreach (var pair in map)
                    {
                        dictionary.Add(pair.Key.ToString(), pair.Value);
                    }

                    netObject = dictionary;
                }
                else
                {
                    netObject = amqpObject;
                }
                break;

            default:
                break;
            }

            return(netObject != null);
        }
예제 #5
0
        public void AmqpCodecMapTest()
        {
            byte[]     workBuffer = new byte[4096];
            ByteBuffer buffer     = new ByteBuffer(workBuffer);
            string     strBig     = new string('A', 512);

            AmqpMap map = new AmqpMap();

            map.Add(new MapKey("boolTrue"), boolTrue);
            map.Add(new MapKey("boolFalse"), boolFalse);
            map.Add(new MapKey("ubyte"), ubyteValue);
            map.Add(new MapKey("ushort"), ushortValue);
            map.Add(new MapKey("uint"), uintValue);
            map.Add(new MapKey("ulong"), ulongValue);
            map.Add(new MapKey("byte"), byteValue);
            map.Add(new MapKey("short"), shortValue);
            map.Add(new MapKey("int"), intValue);
            map.Add(new MapKey("long"), longValue);
            map.Add(new MapKey("null"), null);
            map.Add(new MapKey("float"), floatValue);
            map.Add(new MapKey("double"), doubleValue);
            map.Add(new MapKey("decimal32"), decimal32Value);
            map.Add(new MapKey("decimal64"), decimal64Value);
            map.Add(new MapKey("decimal128"), decimal128Value);
            map.Add(new MapKey("char"), charValue);
            map.Add(new MapKey("datetime"), dtValue);
            map.Add(new MapKey("uuid"), uuidValue);
            map.Add(new MapKey("binaryNull"), new ArraySegment <byte>());
            map.Add(new MapKey("binary8"), bin8Value);
            map.Add(new MapKey("binary32"), bin32Value);
            map.Add(new MapKey("symbolNull"), new AmqpSymbol());
            map.Add(new MapKey("symbol8"), new AmqpSymbol(strValue));
            map.Add(new MapKey("symbol32"), new AmqpSymbol(strBig));
            map.Add(new MapKey("string8"), strValue);
            map.Add(new MapKey("string32"), strBig);
            map.Add(new MapKey("described1"), described1);

            AmqpCodec.EncodeMap(map, buffer);

            // make sure the size written is correct (it has to be Map32)
            // the first byte is FormatCode.Map32
            int mapSize = (int)AmqpBitConverter.ReadUInt(workBuffer, 1, 4);

            Assert.Equal(buffer.Length - 5, mapSize);

            AmqpMap decMap = AmqpCodec.DecodeMap(buffer);

            Assert.True(decMap[new MapKey("boolTrue")].Equals(true), "Boolean true expected.");
            Assert.True(decMap[new MapKey("boolFalse")].Equals(false), "Boolean false expected.");
            Assert.True(decMap[new MapKey("ubyte")].Equals(ubyteValue), "UByte value not equal.");
            Assert.True(decMap[new MapKey("ushort")].Equals(ushortValue), "UShort value not equal.");
            Assert.True(decMap[new MapKey("uint")].Equals(uintValue), "UInt value not equal.");
            Assert.True(decMap[new MapKey("ulong")].Equals(ulongValue), "ULong value not equal.");
            Assert.True(decMap[new MapKey("byte")].Equals(byteValue), "Byte value not equal.");
            Assert.True(decMap[new MapKey("short")].Equals(shortValue), "Short value not equal.");
            Assert.True(decMap[new MapKey("int")].Equals(intValue), "Int value not equal.");
            Assert.True(decMap[new MapKey("long")].Equals(longValue), "Long value not equal.");
            Assert.True(decMap[new MapKey("null")] == null, "Null object expected.");
            Assert.True(decMap[new MapKey("float")].Equals(floatValue), "Float value not equal.");
            Assert.True(decMap[new MapKey("double")].Equals(doubleValue), "Double value not equal.");
            Assert.True(decMap[new MapKey("decimal32")].Equals(decimal32Value), "Decimal32 value not equal.");
            Assert.True(decMap[new MapKey("decimal64")].Equals(decimal64Value), "Decimal64 value not equal.");
            Assert.True(decMap[new MapKey("decimal128")].Equals(decimal128Value), "Decimal128 value not equal.");
            Assert.True(decMap[new MapKey("char")].Equals(charValue), "Char value not equal.");
            Assert.True(decMap[new MapKey("datetime")].Equals(dtValue), "TimeStamp value not equal.");
            Assert.True(decMap[new MapKey("uuid")].Equals(uuidValue), "Uuid value not equal.");
            Assert.True(decMap[new MapKey("binaryNull")] == null, "Null binary expected.");
            ArraySegment <byte> bin8 = (ArraySegment <byte>)decMap[new MapKey("binary8")];

            EnsureEqual(bin8.Array, bin8.Offset, bin8.Count, bin8Value.Array, bin8Value.Offset, bin8Value.Count);
            ArraySegment <byte> bin32 = (ArraySegment <byte>)decMap[new MapKey("binary32")];

            EnsureEqual(bin32.Array, bin32.Offset, bin32.Count, bin32Value.Array, bin32Value.Offset, bin32Value.Count);

            Assert.True(decMap[new MapKey("symbolNull")] == null, "Null symbol expected.");
            AmqpSymbol symDecode = (AmqpSymbol)decMap[new MapKey("symbol8")];

            Assert.True(symDecode.Equals(strValue), "AmqpSymbol value not equal.");
            symDecode = (AmqpSymbol)decMap[new MapKey("symbol32")];
            Assert.True(symDecode.Equals(strBig), "AmqpSymbol value (big) not equal.");

            string strDecode = (string)decMap[new MapKey("string8")];

            Assert.True(strDecode.Equals(strValue), "string value not equal.");
            strDecode = (string)decMap[new MapKey("string32")];
            Assert.True(strDecode.Equals(strBig), "string value (big) not equal.");

            DescribedType described = (DescribedType)decMap[new MapKey("described1")];

            Assert.True(described.Descriptor.Equals(described1.Descriptor), "Described value 1 descriptor is different");
            Assert.True(described.Value.Equals(described1.Value), "Described value 1 value is different");
        }
예제 #6
0
        public void AmqpCodecListTest()
        {
            byte[]     workBuffer = new byte[4096];
            ByteBuffer buffer     = new ByteBuffer(workBuffer);
            string     strBig     = new string('A', 512);

            List <object> list = new List <object>();

            list.Add(boolTrue);
            list.Add(boolFalse);
            list.Add(ubyteValue);
            list.Add(ushortValue);
            list.Add(uintValue);
            list.Add(ulongValue);
            list.Add(byteValue);
            list.Add(shortValue);
            list.Add(intValue);
            list.Add(longValue);
            list.Add(null);
            list.Add(floatValue);
            list.Add(doubleValue);
            list.Add(decimal32Value);
            list.Add(decimal64Value);
            list.Add(decimal128Value);
            list.Add(charValue);
            list.Add(dtValue);
            list.Add(uuidValue);
            list.Add(new ArraySegment <byte>());
            list.Add(bin8Value);
            list.Add(bin32Value);
            list.Add(new AmqpSymbol());
            list.Add(new AmqpSymbol(strValue));
            list.Add(new AmqpSymbol(strBig));
            list.Add(strValue);
            list.Add(strBig);
            list.Add(described1);
            list.Add(described2);
            list.Add(described3);
            list.Add(described4);

            AmqpCodec.EncodeList(list, buffer);

            // make sure the size written is correct (it has to be List32)
            // the first byte is FormatCode.List32
            int listSize = (int)AmqpBitConverter.ReadUInt(workBuffer, 1, 4);

            Assert.Equal(buffer.Length - 5, listSize);

            IList decList = AmqpCodec.DecodeList(buffer);
            int   index   = 0;

            Assert.True(decList[index++].Equals(true), "Boolean true expected.");
            Assert.True(decList[index++].Equals(false), "Boolean false expected.");
            Assert.True(decList[index++].Equals(ubyteValue), "UByte value not equal.");
            Assert.True(decList[index++].Equals(ushortValue), "UShort value not equal.");
            Assert.True(decList[index++].Equals(uintValue), "UInt value not equal.");
            Assert.True(decList[index++].Equals(ulongValue), "ULong value not equal.");
            Assert.True(decList[index++].Equals(byteValue), "Byte value not equal.");
            Assert.True(decList[index++].Equals(shortValue), "Short value not equal.");
            Assert.True(decList[index++].Equals(intValue), "Int value not equal.");
            Assert.True(decList[index++].Equals(longValue), "Long value not equal.");
            Assert.True(decList[index++] == null, "Null object expected.");
            Assert.True(decList[index++].Equals(floatValue), "Float value not equal.");
            Assert.True(decList[index++].Equals(doubleValue), "Double value not equal.");
            Assert.True(decList[index++].Equals(decimal32Value), "Decimal32 value not equal.");
            Assert.True(decList[index++].Equals(decimal64Value), "Decimal64 value not equal.");
            Assert.True(decList[index++].Equals(decimal128Value), "Decimal128 value not equal.");
            Assert.True(decList[index++].Equals(charValue), "Char value not equal.");
            Assert.True(decList[index++].Equals(dtValue), "TimeStamp value not equal.");
            Assert.True(decList[index++].Equals(uuidValue), "Uuid value not equal.");

            Assert.True(decList[index++] == null, "Null binary expected.");
            ArraySegment <byte> bin8 = (ArraySegment <byte>)decList[index++];

            EnsureEqual(bin8.Array, bin8.Offset, bin8.Count, bin8Value.Array, bin8Value.Offset, bin8Value.Count);
            ArraySegment <byte> bin32 = (ArraySegment <byte>)decList[index++];

            EnsureEqual(bin32.Array, bin32.Offset, bin32.Count, bin32Value.Array, bin32Value.Offset, bin32Value.Count);

            Assert.True(decList[index++] == null, "Null symbol expected.");
            AmqpSymbol symDecode = (AmqpSymbol)decList[index++];

            Assert.True(symDecode.Equals(strValue), "AmqpSymbol value not equal.");
            symDecode = (AmqpSymbol)decList[index++];
            Assert.True(symDecode.Equals(strBig), "AmqpSymbol value (big) not equal.");

            string strDecode = (string)decList[index++];

            Assert.True(strDecode.Equals(strValue), "string value not equal.");
            strDecode = (string)decList[index++];
            Assert.True(strDecode.Equals(strBig), "string value (big) not equal.");

            DescribedType described = (DescribedType)decList[index++];

            Assert.True(described.Descriptor.Equals(described1.Descriptor), "Described value 1 descriptor is different");
            Assert.True(described.Value.Equals(described1.Value), "Described value 1 value is different");
            described = (DescribedType)decList[index++];
            Assert.True(described.Descriptor.Equals(described2.Descriptor), "Described value 2 descriptor is different");
            Assert.True(described.Value.Equals(described2.Value), "Described value 2 value is different");
            described = (DescribedType)decList[index++];
            Assert.True(described.Descriptor.Equals(described3.Descriptor), "Described value 3 descriptor is different");
            Assert.True(described.Value.Equals(described3.Value), "Described value 3 value is different");
            described = (DescribedType)decList[index++];
            EnsureEqual((DateTime)described4.Descriptor, (DateTime)described.Descriptor);
            EnsureEqual((IList)described.Value, (IList)described4.Value);
        }
예제 #7
0
        public static bool TryGetNetObjectFromAmqpObject(object amqpObject, MappingType mappingType, out object netObject)
        {
            netObject = null;
            if (amqpObject == null)
            {
                return(false);
            }
            switch (SerializationUtilities.GetTypeId(amqpObject))
            {
            case PropertyValueType.Byte:
            case PropertyValueType.SByte:
            case PropertyValueType.Char:
            case PropertyValueType.Int16:
            case PropertyValueType.UInt16:
            case PropertyValueType.Int32:
            case PropertyValueType.UInt32:
            case PropertyValueType.Int64:
            case PropertyValueType.UInt64:
            case PropertyValueType.Single:
            case PropertyValueType.Double:
            case PropertyValueType.Decimal:
            case PropertyValueType.Boolean:
            case PropertyValueType.Guid:
            case PropertyValueType.String:
            case PropertyValueType.DateTime:
            {
                netObject = amqpObject;
                return(netObject != null);
            }

            case PropertyValueType.Uri:
            case PropertyValueType.DateTimeOffset:
            case PropertyValueType.TimeSpan:
            case PropertyValueType.Stream:
            {
                return(netObject != null);
            }

            case PropertyValueType.Unknown:
            {
                if (amqpObject is AmqpSymbol)
                {
                    netObject = ((AmqpSymbol)amqpObject).Value;
                    return(netObject != null);
                }
                else if (amqpObject is ArraySegment <byte> )
                {
                    ArraySegment <byte> nums = (ArraySegment <byte>)amqpObject;
                    if (nums.Count != (int)nums.Array.Length)
                    {
                        byte[] numArray = new byte[nums.Count];
                        Buffer.BlockCopy(nums.Array, nums.Offset, numArray, 0, nums.Count);
                        netObject = numArray;
                        return(netObject != null);
                    }
                    else
                    {
                        netObject = nums.Array;
                        return(netObject != null);
                    }
                }
                else if (!(amqpObject is DescribedType))
                {
                    if (mappingType == MappingType.ApplicationProperty)
                    {
                        throw Microsoft.ServiceBus.Messaging.FxTrace.Exception.AsError(new SerializationException(SRClient.FailedToSerializeUnsupportedType(amqpObject.GetType().FullName)), null);
                    }
                    if (!(amqpObject is AmqpMap))
                    {
                        netObject = amqpObject;
                        return(netObject != null);
                    }
                    else
                    {
                        AmqpMap amqpMaps = (AmqpMap)amqpObject;
                        Dictionary <string, object> strs = new Dictionary <string, object>();
                        foreach (KeyValuePair <MapKey, object> keyValuePair in (IEnumerable <KeyValuePair <MapKey, object> >)amqpMaps)
                        {
                            strs.Add(keyValuePair.Key.ToString(), keyValuePair.Value);
                        }
                        netObject = strs;
                        return(netObject != null);
                    }
                }
                else
                {
                    DescribedType describedType = (DescribedType)amqpObject;
                    if (!(describedType.Descriptor is AmqpSymbol))
                    {
                        return(netObject != null);
                    }
                    AmqpSymbol descriptor = (AmqpSymbol)describedType.Descriptor;
                    if (descriptor.Equals("com.microsoft:uri"))
                    {
                        netObject = new Uri((string)describedType.Value);
                        return(netObject != null);
                    }
                    else if (!descriptor.Equals("com.microsoft:timespan"))
                    {
                        if (!descriptor.Equals("com.microsoft:datetime-offset"))
                        {
                            return(netObject != null);
                        }
                        netObject = new DateTimeOffset(new DateTime((long)describedType.Value, DateTimeKind.Utc));
                        return(netObject != null);
                    }
                    else
                    {
                        netObject = new TimeSpan((long)describedType.Value);
                        return(netObject != null);
                    }
                }
            }

            default:
            {
                return(netObject != null);
            }
            }
        }