Exemple #1
0
        public static BrokeredMessage ConvertAmqpToSbmp(BrokeredMessage brokeredMessage, ReceiveMode receiveMode)
        {
            BrokeredMessage encoder = brokeredMessage.CreateCopy();

            encoder.MessageFormat  = BrokeredMessageFormat.Sbmp;
            encoder.MessageEncoder = BrokeredMessageEncoder.GetEncoder(BrokeredMessageFormat.Sbmp);
            if (receiveMode == ReceiveMode.PeekLock && brokeredMessage.IsLockTokenSet)
            {
                encoder.LockToken      = brokeredMessage.LockToken;
                encoder.LockedUntilUtc = brokeredMessage.LockedUntilUtc;
            }
            if (encoder.BodyStream != null)
            {
                using (Stream stream = BrokeredMessage.CloneStream(encoder.BodyStream, false))
                {
                    using (AmqpMessage amqpMessage = AmqpMessage.CreateAmqpStreamMessageBody(stream))
                    {
                        encoder.BodyStream = null;
                        if ((int)(amqpMessage.BodyType & SectionFlag.Data) != 0 || (int)(amqpMessage.BodyType & SectionFlag.AmqpSequence) != 0)
                        {
                            encoder.BodyStream = MessageConverter.GetMessageBodyStream(amqpMessage);
                        }
                        else if ((int)(amqpMessage.BodyType & SectionFlag.AmqpValue) != 0)
                        {
                            object value = null;
                            if (!MessageConverter.TryGetNetObjectFromAmqpObject(amqpMessage.ValueBody.Value, MappingType.MessageBody, out value))
                            {
                                value = amqpMessage.ValueBody.Value;
                            }
                            if (value != null)
                            {
                                DataContractBinarySerializer dataContractBinarySerializer = new DataContractBinarySerializer(value.GetType());
                                MemoryStream memoryStream = new MemoryStream(256);
                                dataContractBinarySerializer.WriteObject(memoryStream, value);
                                memoryStream.Flush();
                                memoryStream.Position = (long)0;
                                encoder.BodyStream    = memoryStream;
                            }
                        }
                    }
                }
            }
            return(encoder);
        }
Exemple #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);
        }