Ejemplo n.º 1
0
        public static void UpdateBrokeredMessageHeaderAndProperties(AmqpMessage amqpMessage, BrokeredMessage message)
        {
            DateTime    dateTime;
            string      str;
            string      str1;
            SectionFlag sections = amqpMessage.Sections;

            if ((int)(sections & SectionFlag.Header) != 0 && amqpMessage.Header.Ttl.HasValue)
            {
                uint?ttl = amqpMessage.Header.Ttl;
                message.TimeToLive = TimeSpan.FromMilliseconds((double)((float)ttl.Value));
            }
            if ((int)(sections & SectionFlag.Properties) != 0)
            {
                if (amqpMessage.Properties.MessageId != null)
                {
                    message.MessageId = amqpMessage.Properties.MessageId.ToString();
                }
                if (amqpMessage.Properties.CorrelationId != null)
                {
                    message.CorrelationId = amqpMessage.Properties.CorrelationId.ToString();
                }
                if (amqpMessage.Properties.ContentType.Value != null)
                {
                    message.ContentType = amqpMessage.Properties.ContentType.Value;
                }
                if (amqpMessage.Properties.Subject != null)
                {
                    message.Label = amqpMessage.Properties.Subject;
                }
                if (amqpMessage.Properties.To != null)
                {
                    message.To = amqpMessage.Properties.To.ToString();
                }
                if (amqpMessage.Properties.ReplyTo != null)
                {
                    message.ReplyTo = amqpMessage.Properties.ReplyTo.ToString();
                }
                if (amqpMessage.Properties.GroupId != null)
                {
                    message.SessionId = amqpMessage.Properties.GroupId;
                }
                if (amqpMessage.Properties.ReplyToGroupId != null)
                {
                    message.ReplyToSessionId = amqpMessage.Properties.ReplyToGroupId;
                }
            }
            if ((int)(sections & SectionFlag.MessageAnnotations) != 0)
            {
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <DateTime>("x-opt-scheduled-enqueue-time", out dateTime))
                {
                    message.ScheduledEnqueueTimeUtc = dateTime;
                }
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <string>("x-opt-publisher", out str))
                {
                    message.Publisher = str;
                }
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <string>("x-opt-partition-key", out str1))
                {
                    message.PartitionKey = str1;
                }
            }
            if ((int)(sections & SectionFlag.ApplicationProperties) != 0)
            {
                foreach (KeyValuePair <MapKey, object> map in (IEnumerable <KeyValuePair <MapKey, object> >)amqpMessage.ApplicationProperties.Map)
                {
                    object obj = null;
                    if (!MessageConverter.TryGetNetObjectFromAmqpObject(map.Value, MappingType.ApplicationProperty, out obj))
                    {
                        continue;
                    }
                    message.InternalProperties[map.Key.ToString()] = obj;
                }
            }
        }
Ejemplo n.º 2
0
        public static BrokeredMessage ClientGetMessage(AmqpMessage amqpMessage)
        {
            BrokeredMessage brokeredMessage;

            if ((int)(amqpMessage.BodyType & SectionFlag.Data) != 0 || (int)(amqpMessage.BodyType & SectionFlag.AmqpSequence) != 0)
            {
                brokeredMessage = new BrokeredMessage(MessageConverter.GetMessageBodyStream(amqpMessage), true);
            }
            else if ((int)(amqpMessage.BodyType & SectionFlag.AmqpValue) == 0)
            {
                brokeredMessage = new BrokeredMessage();
            }
            else
            {
                object value = null;
                if (!MessageConverter.TryGetNetObjectFromAmqpObject(amqpMessage.ValueBody.Value, MappingType.MessageBody, out value))
                {
                    value = amqpMessage.ValueBody.Value;
                }
                brokeredMessage = new BrokeredMessage(value, amqpMessage.BodyStream);
            }
            SectionFlag sections = amqpMessage.Sections;

            if ((int)(sections & SectionFlag.Header) != 0)
            {
                if (amqpMessage.Header.Ttl.HasValue)
                {
                    uint?ttl = amqpMessage.Header.Ttl;
                    brokeredMessage.TimeToLive = TimeSpan.FromMilliseconds((double)((float)ttl.Value));
                }
                if (amqpMessage.Header.DeliveryCount.HasValue)
                {
                    brokeredMessage.DeliveryCount = (int)(amqpMessage.Header.DeliveryCount.Value + 1);
                }
            }
            if ((int)(sections & SectionFlag.Properties) != 0)
            {
                if (amqpMessage.Properties.MessageId != null)
                {
                    brokeredMessage.MessageId = amqpMessage.Properties.MessageId.ToString();
                }
                if (amqpMessage.Properties.CorrelationId != null)
                {
                    brokeredMessage.CorrelationId = amqpMessage.Properties.CorrelationId.ToString();
                }
                if (amqpMessage.Properties.ContentType.Value != null)
                {
                    brokeredMessage.ContentType = amqpMessage.Properties.ContentType.Value;
                }
                if (amqpMessage.Properties.Subject != null)
                {
                    brokeredMessage.Label = amqpMessage.Properties.Subject;
                }
                if (amqpMessage.Properties.To != null)
                {
                    brokeredMessage.To = amqpMessage.Properties.To.ToString();
                }
                if (amqpMessage.Properties.ReplyTo != null)
                {
                    brokeredMessage.ReplyTo = amqpMessage.Properties.ReplyTo.ToString();
                }
                if (amqpMessage.Properties.GroupId != null)
                {
                    brokeredMessage.SessionId = amqpMessage.Properties.GroupId;
                }
                if (amqpMessage.Properties.ReplyToGroupId != null)
                {
                    brokeredMessage.ReplyToSessionId = amqpMessage.Properties.ReplyToGroupId;
                }
            }
            if ((int)(sections & SectionFlag.ApplicationProperties) != 0)
            {
                foreach (KeyValuePair <MapKey, object> map in (IEnumerable <KeyValuePair <MapKey, object> >)amqpMessage.ApplicationProperties.Map)
                {
                    object obj = null;
                    if (!MessageConverter.TryGetNetObjectFromAmqpObject(map.Value, MappingType.ApplicationProperty, out obj))
                    {
                        continue;
                    }
                    brokeredMessage.Properties[map.Key.ToString()] = obj;
                }
            }
            if ((int)(sections & SectionFlag.MessageAnnotations) != 0)
            {
                foreach (KeyValuePair <MapKey, object> keyValuePair in (IEnumerable <KeyValuePair <MapKey, object> >)amqpMessage.MessageAnnotations.Map)
                {
                    string str  = keyValuePair.Key.ToString();
                    string str1 = str;
                    string str2 = str1;
                    if (str1 != null)
                    {
                        switch (str2)
                        {
                        case "x-opt-enqueued-time":
                        {
                            brokeredMessage.EnqueuedTimeUtc = (DateTime)keyValuePair.Value;
                            continue;
                        }

                        case "x-opt-scheduled-enqueue-time":
                        {
                            brokeredMessage.ScheduledEnqueueTimeUtc = (DateTime)keyValuePair.Value;
                            continue;
                        }

                        case "x-opt-sequence-number":
                        {
                            brokeredMessage.SequenceNumber = (long)keyValuePair.Value;
                            continue;
                        }

                        case "x-opt-offset":
                        {
                            brokeredMessage.EnqueuedSequenceNumber = (long)keyValuePair.Value;
                            continue;
                        }

                        case "x-opt-locked-until":
                        {
                            brokeredMessage.LockedUntilUtc = (DateTime)keyValuePair.Value;
                            continue;
                        }

                        case "x-opt-publisher":
                        {
                            brokeredMessage.Publisher = (string)keyValuePair.Value;
                            continue;
                        }

                        case "x-opt-partition-key":
                        {
                            brokeredMessage.PartitionKey = (string)keyValuePair.Value;
                            continue;
                        }
                        }
                    }
                    object obj1 = null;
                    if (!MessageConverter.TryGetNetObjectFromAmqpObject(keyValuePair.Value, MappingType.ApplicationProperty, out obj1))
                    {
                        continue;
                    }
                    brokeredMessage.Properties[str] = obj1;
                }
            }
            if (amqpMessage.DeliveryTag.Count == 16)
            {
                byte[] numArray = new byte[16];
                byte[] array    = amqpMessage.DeliveryTag.Array;
                ArraySegment <byte> deliveryTag = amqpMessage.DeliveryTag;
                Buffer.BlockCopy((Array)array, deliveryTag.Offset, numArray, 0, 16);
                brokeredMessage.LockToken = new Guid(numArray);
            }
            brokeredMessage.AttachDisposables(new AmqpMessage[] { amqpMessage });
            return(brokeredMessage);
        }
Ejemplo n.º 3
0
        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.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:
            {
                amqpObject = netObject;
                break;
            }

            case PropertyValueType.Uri:
            {
                amqpObject = new DescribedType((object)"com.microsoft:uri", ((Uri)netObject).AbsoluteUri);
                break;
            }

            case PropertyValueType.DateTimeOffset:
            {
                object         obj            = "com.microsoft:datetime-offset";
                DateTimeOffset dateTimeOffset = (DateTimeOffset)netObject;
                amqpObject = new DescribedType(obj, (object)dateTimeOffset.UtcTicks);
                break;
            }

            case PropertyValueType.TimeSpan:
            {
                object   obj1     = "com.microsoft:timespan";
                TimeSpan timeSpan = (TimeSpan)netObject;
                amqpObject = new DescribedType(obj1, (object)timeSpan.Ticks);
                break;
            }

            case PropertyValueType.Stream:
            {
                if (mappingType != MappingType.ApplicationProperty)
                {
                    break;
                }
                amqpObject = MessageConverter.ReadStream((Stream)netObject);
                break;
            }

            case PropertyValueType.Unknown:
            {
                if (!(netObject is Stream))
                {
                    if (mappingType == MappingType.ApplicationProperty)
                    {
                        throw Microsoft.ServiceBus.Messaging.FxTrace.Exception.AsError(new SerializationException(SRClient.FailedToSerializeUnsupportedType(netObject.GetType().FullName)), null);
                    }
                    if (netObject is byte[])
                    {
                        amqpObject = new ArraySegment <byte>((byte[])netObject);
                        break;
                    }
                    else if (!(netObject is IList))
                    {
                        if (!(netObject is IDictionary))
                        {
                            break;
                        }
                        amqpObject = new AmqpMap((IDictionary)netObject);
                        break;
                    }
                    else
                    {
                        amqpObject = netObject;
                        break;
                    }
                }
                else
                {
                    if (mappingType != MappingType.ApplicationProperty)
                    {
                        break;
                    }
                    amqpObject = MessageConverter.ReadStream((Stream)netObject);
                    break;
                }
            }
            }
            return(amqpObject != null);
        }
Ejemplo n.º 4
0
        public static void UpdateEventDataHeaderAndProperties(AmqpMessage amqpMessage, EventData data)
        {
            string              str;
            string              str1;
            DateTime            dateTime;
            long                num;
            string              str2;
            ArraySegment <byte> deliveryTag = amqpMessage.DeliveryTag;

            Fx.AssertAndThrow(true, "AmqpMessage should always contain delivery tag.");
            data.DeliveryTag = amqpMessage.DeliveryTag;
            SectionFlag sections = amqpMessage.Sections;

            if ((int)(sections & SectionFlag.MessageAnnotations) != 0)
            {
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <string>("x-opt-publisher", out str))
                {
                    data.Publisher = str;
                }
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <string>("x-opt-partition-key", out str1))
                {
                    data.PartitionKey = str1;
                }
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <DateTime>("x-opt-enqueued-time", out dateTime))
                {
                    data.EnqueuedTimeUtc = dateTime;
                }
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <long>("x-opt-sequence-number", out num))
                {
                    data.SequenceNumber = num;
                }
                if (amqpMessage.MessageAnnotations.Map.TryGetValue <string>("x-opt-offset", out str2))
                {
                    data.Offset = str2;
                }
            }
            if ((int)(sections & SectionFlag.ApplicationProperties) != 0)
            {
                foreach (KeyValuePair <MapKey, object> map in (IEnumerable <KeyValuePair <MapKey, object> >)amqpMessage.ApplicationProperties.Map)
                {
                    object obj = null;
                    if (!MessageConverter.TryGetNetObjectFromAmqpObject(map.Value, MappingType.ApplicationProperty, out obj))
                    {
                        continue;
                    }
                    data.Properties[map.Key.ToString()] = obj;
                }
            }
            if ((int)(sections & SectionFlag.Properties) != 0)
            {
                foreach (KeyValuePair <string, object> dictionary in amqpMessage.Properties.ToDictionary())
                {
                    if (dictionary.Value is MessageId || dictionary.Value is Address || dictionary.Value is AmqpSymbol)
                    {
                        data.SystemProperties.Add(dictionary.Key, dictionary.Value.ToString());
                    }
                    else
                    {
                        data.SystemProperties.Add(dictionary);
                    }
                }
            }
        }