public void FinalDisposition(MsmqMessageProperty messageProperty)
        {
            switch (receiver.MsmqReceiveParameters.ReceiveErrorHandling)
            {
                case ReceiveErrorHandling.Drop:
                    //Unlocking message here since the message is locked under internal transaction and 
                    //cannot be unlocked by aborting ambient transaction
                    MsmqDefaultLockingQueue queue = this.receiver.Queue as MsmqDefaultLockingQueue;
                    if ((queue != null) && this.receiver.Transactional)
                        queue.UnlockMessage(messageProperty.LookupId, TimeSpan.Zero);
                    this.receiver.DropOrRejectReceivedMessage(messageProperty, false);
                    break;

                case ReceiveErrorHandling.Fault:
                    MsmqReceiveHelper.TryAbortTransactionCurrent();
                    if (null != this.receiver.ChannelListener)
                        this.receiver.ChannelListener.FaultListener();
                    if (null != this.receiver.Channel)
                        this.receiver.Channel.FaultChannel();
                    break;
                default:
                    Fx.Assert("System.ServiceModel.Channels.Msmq3PoisonHandler.FinalDisposition(): (unexpected ReceiveErrorHandling)");
                    break;
            }
        }
 private static Message DecodeSessiongramMessage(MsmqInputSessionChannelListener listener, MsmqInputSessionChannel channel, MessageEncoder encoder, MsmqMessageProperty messageProperty, byte[] buffer, int offset, int size)
 {
     Message message2;
     if (size > listener.MaxReceivedMessageSize)
     {
         channel.FaultChannel();
         listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
         throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
     }
     if ((size + offset) > buffer.Length)
     {
         listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
         throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
     }
     byte[] dst = listener.BufferManager.TakeBuffer(size);
     Buffer.BlockCopy(buffer, offset, dst, 0, size);
     try
     {
         Message message = null;
         using (MsmqDiagnostics.BoundDecodeOperation())
         {
             message = encoder.ReadMessage(new ArraySegment<byte>(dst, 0, size), listener.BufferManager);
             MsmqDiagnostics.TransferFromTransport(message);
         }
         message2 = message;
     }
     catch (XmlException exception)
     {
         channel.FaultChannel();
         listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
         throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadXml"), exception));
     }
     return message2;
 }
        private void InternalFinalDisposition(MsmqQueue disposeFromQueue, MsmqMessageProperty messageProperty)
        {
            switch (this.ReceiveParameters.ReceiveErrorHandling)
            {
                case ReceiveErrorHandling.Fault:
                    MsmqReceiveHelper.TryAbortTransactionCurrent();
                    if (this.receiver.ChannelListener != null)
                    {
                        this.receiver.ChannelListener.FaultListener();
                    }
                    if (this.receiver.Channel == null)
                    {
                        break;
                    }
                    this.receiver.Channel.FaultChannel();
                    return;

                case ReceiveErrorHandling.Drop:
                    this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, false);
                    return;

                case ReceiveErrorHandling.Reject:
                    this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, true);
                    MsmqDiagnostics.PoisonMessageRejected(messageProperty.MessageId, this.receiver.InstanceId);
                    return;

                case ReceiveErrorHandling.Move:
                    MsmqReceiveHelper.MoveReceivedMessage(disposeFromQueue, this.poisonQueue, messageProperty.LookupId);
                    MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, true, this.receiver.InstanceId);
                    break;

                default:
                    return;
            }
        }
 internal void DropOrRejectReceivedMessage(MsmqQueue queue, MsmqMessageProperty messageProperty, bool reject)
 {
     if (this.Transactional)
     {
         TryAbortTransactionCurrent();
         IPostRollbackErrorStrategy strategy = new SimplePostRollbackErrorStrategy(messageProperty.LookupId);
         MsmqQueue.MoveReceiveResult unknown = MsmqQueue.MoveReceiveResult.Unknown;
         do
         {
             using (MsmqEmptyMessage message = new MsmqEmptyMessage())
             {
                 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                 {
                     unknown = queue.TryReceiveByLookupId(messageProperty.LookupId, message, MsmqTransactionMode.CurrentOrThrow);
                     if ((MsmqQueue.MoveReceiveResult.Succeeded == unknown) && reject)
                     {
                         queue.MarkMessageRejected(messageProperty.LookupId);
                     }
                     scope.Complete();
                 }
             }
             if (unknown == MsmqQueue.MoveReceiveResult.Succeeded)
             {
                 MsmqDiagnostics.MessageConsumed(this.instanceId, messageProperty.MessageId, Msmq.IsRejectMessageSupported && reject);
             }
         }
         while ((unknown == MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction) && strategy.AnotherTryNeeded());
     }
     else
     {
         MsmqDiagnostics.MessageConsumed(this.instanceId, messageProperty.MessageId, false);
     }
 }
 public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
 {
     if (this.ReceiveParameters.ReceiveContextSettings.Enabled)
     {
         return this.ReceiveContextPoisonHandling(messageProperty);
     }
     return this.NonReceiveContextPoisonHandling(messageProperty);
 }
 public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
 {
     if (messageProperty.AbortCount > this.receiver.MsmqReceiveParameters.ReceiveRetryCount)
     {
         this.FinalDisposition(messageProperty);
         return true;
     }
     return false;
 }
 internal static Message DecodeIntegrationDatagram(MsmqIntegrationChannelListener listener, MsmqReceiveHelper receiver, MsmqIntegrationInputMessage msmqMessage, MsmqMessageProperty messageProperty)
 {
     Message message2;
     using (MsmqDiagnostics.BoundReceiveBytesOperation())
     {
         Message message = Message.CreateMessage(MessageVersion.None, (string) null);
         bool flag = true;
         try
         {
             SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
             if (property != null)
             {
                 message.Properties.Security = property;
             }
             MsmqIntegrationMessageProperty property2 = new MsmqIntegrationMessageProperty();
             msmqMessage.SetMessageProperties(property2);
             int length = msmqMessage.BodyLength.Value;
             if (length > listener.MaxReceivedMessageSize)
             {
                 receiver.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
             }
             byte[] bufferCopy = msmqMessage.Body.GetBufferCopy(length);
             MemoryStream bodyStream = new MemoryStream(bufferCopy, 0, bufferCopy.Length, false);
             object obj2 = null;
             using (MsmqDiagnostics.BoundDecodeOperation())
             {
                 try
                 {
                     obj2 = DeserializeForIntegration(listener, bodyStream, property2, messageProperty.LookupId);
                 }
                 catch (SerializationException exception)
                 {
                     receiver.FinalDisposition(messageProperty);
                     throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqDeserializationError"), exception));
                 }
                 property2.Body = obj2;
                 message.Properties["MsmqIntegrationMessageProperty"] = property2;
                 bodyStream.Seek(0L, SeekOrigin.Begin);
                 message.Headers.To = listener.Uri;
                 flag = false;
                 MsmqDiagnostics.TransferFromTransport(message);
             }
             message2 = message;
         }
         finally
         {
             if (flag)
             {
                 message.Close();
             }
         }
     }
     return message2;
 }
 public void FinalDisposition(MsmqMessageProperty messageProperty)
 {
     if (this.ReceiveParameters.ReceiveContextSettings.Enabled)
     {
         this.InternalFinalDisposition(this.lockQueueForReceive, messageProperty);
     }
     else
     {
         this.InternalFinalDisposition(this.mainQueue, messageProperty);
     }
 }
 public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
 {
     if (messageProperty.AbortCount > this.receiver.MsmqReceiveParameters.ReceiveRetryCount)
     {
         if (TD.ReceiveRetryCountReachedIsEnabled())
         {
             TD.ReceiveRetryCountReached(messageProperty.MessageId);
         }
         FinalDisposition(messageProperty);
         return true;
     }
     else
     {
         return false;
     }
 }
 public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
 {
     int num2;
     long lookupId = messageProperty.LookupId;
     lock (this.thisLock)
     {
         num2 = this.UpdateSeenCount(lookupId);
         if ((num2 > (this.receiver.MsmqReceiveParameters.ReceiveRetryCount + 1)) && (this.receiver.MsmqReceiveParameters.ReceiveRetryCount != 0x7fffffff))
         {
             this.FinalDisposition(messageProperty);
             this.trackedMessages.Remove(lookupId);
             return true;
         }
     }
     messageProperty.AbortCount = num2 - 1;
     return false;
 }
        public void FinalDisposition(MsmqMessageProperty messageProperty)
        {
            switch (this.receiver.MsmqReceiveParameters.ReceiveErrorHandling)
            {
                case ReceiveErrorHandling.Fault:
                    MsmqReceiveHelper.TryAbortTransactionCurrent();
                    if (this.receiver.ChannelListener != null)
                    {
                        this.receiver.ChannelListener.FaultListener();
                    }
                    if (this.receiver.Channel != null)
                    {
                        this.receiver.Channel.FaultChannel();
                    }
                    return;

                case ReceiveErrorHandling.Drop:
                    this.receiver.DropOrRejectReceivedMessage(messageProperty, false);
                    return;
            }
        }
        public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
        {
            long lookupId = messageProperty.LookupId;
            int  seen;

            lock (thisLock)
            {
                seen = this.UpdateSeenCount(lookupId);
                if (seen > (receiver.MsmqReceiveParameters.ReceiveRetryCount + 1) && receiver.MsmqReceiveParameters.ReceiveRetryCount != Int32.MaxValue)
                {
                    if (TD.ReceiveRetryCountReachedIsEnabled())
                    {
                        TD.ReceiveRetryCountReached(messageProperty.MessageId);
                    }
                    FinalDisposition(messageProperty);
                    this.trackedMessages.Remove(lookupId);
                    return(true);
                }
            }

            messageProperty.AbortCount = seen - 1;
            return(false);
        }
        public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
        {
            long lookupId = messageProperty.LookupId;
            int seen;

            lock (thisLock)
            {
                seen = this.UpdateSeenCount(lookupId);
                if (seen > (receiver.MsmqReceiveParameters.ReceiveRetryCount + 1) && receiver.MsmqReceiveParameters.ReceiveRetryCount != Int32.MaxValue)
                {
                    if (TD.ReceiveRetryCountReachedIsEnabled())
                    {
                        TD.ReceiveRetryCountReached(messageProperty.MessageId);
                    }
                    FinalDisposition(messageProperty);
                    this.trackedMessages.Remove(lookupId);
                    return true;
                }
            }

            messageProperty.AbortCount = seen - 1;
            return false;
        }
        public void FinalDisposition(MsmqMessageProperty messageProperty)
        {
            switch (this.receiver.MsmqReceiveParameters.ReceiveErrorHandling)
            {
                case ReceiveErrorHandling.Drop:
                    this.receiver.DropOrRejectReceivedMessage(messageProperty, false);
                    break;

                case ReceiveErrorHandling.Fault:
                    MsmqReceiveHelper.TryAbortTransactionCurrent();
                    if (null != this.receiver.ChannelListener)
                        this.receiver.ChannelListener.FaultListener();
                    if (null != this.receiver.Channel)
                        this.receiver.Channel.FaultChannel();
                    break;
                case ReceiveErrorHandling.Reject:
                    this.receiver.DropOrRejectReceivedMessage(messageProperty, true);
                    MsmqDiagnostics.PoisonMessageRejected(messageProperty.MessageId, this.receiver.InstanceId);
                    break;
                default:
                    Fx.Assert("System.ServiceModel.Channels.Msmq4PoisonHandler.FinalDisposition(): (unexpected ReceiveErrorHandling)");
                    break;
            }
        }
예제 #15
0
        public bool ReceiveContextPoisonHandling(MsmqMessageProperty messageProperty)
        {
            int num            = this.ReceiveParameters.ReceiveRetryCount + 1;
            int maxRetryCycles = this.ReceiveParameters.MaxRetryCycles;
            int num3           = (2 * num) + 1;
            int num4           = messageProperty.MoveCount / (num3 + 2);
            int num5           = num4 * (num3 + 2);
            int num6           = messageProperty.MoveCount - num5;

            lock (this)
            {
                if (this.disposed)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(base.GetType().ToString()));
                }
                if (num4 > maxRetryCycles)
                {
                    this.FinalDisposition(messageProperty);
                    return(true);
                }
                if (num6 >= num3)
                {
                    if (num4 < maxRetryCycles)
                    {
                        MsmqReceiveHelper.MoveReceivedMessage(this.lockQueueForReceive, this.retryQueueForMove, messageProperty.LookupId);
                        MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, false, this.receiver.InstanceId);
                    }
                    else
                    {
                        this.FinalDisposition(messageProperty);
                    }
                    return(true);
                }
                return(false);
            }
        }
        public bool ReceiveContextPoisonHandling(MsmqMessageProperty messageProperty)
        {
            // The basic idea is to use the message move count to get the number of retry attempts the message has been through
            // The computation of the retry count and retry cycle count is slightly involved due to fact that the message being processed
            // could have been recycled message. (Recycled message is the message that moves from lock queue to retry queue to main queue
            // and back to lock queue
            //

            // Count to tally message recycling (lock queue to retry queue to main queue adds move count of 2 to the message)
            const int retryMoveCount = 2;

            // Actual number of times message is received before recycling to retry queue
            int actualReceiveRetryCount = this.ReceiveParameters.ReceiveRetryCount + 1;

            // The message is recycled these many number of times
            int maxRetryCycles = this.ReceiveParameters.MaxRetryCycles;

            // Max change in message move count between recycling
            int maxMovePerCycle = (2 * actualReceiveRetryCount) + 1;

            // Number of recycles the message has been through
            int messageCyclesCompleted = messageProperty.MoveCount / (maxMovePerCycle + retryMoveCount);

            // Total number of moves on the message at the end of the last recycle
            int messageMoveCountForCyclesCompleted = messageCyclesCompleted * (maxMovePerCycle + retryMoveCount);

            // The differential move count for the current cycle
            int messageMoveCountForCurrentCycle = messageProperty.MoveCount - messageMoveCountForCyclesCompleted;

            lock (this)
            {
                if (this.disposed)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));

                // Check if the message has already completed its max recycle count (MaxRetryCycles)
                // and the disposed the message first. Such a message was previously disposed using the ReceiveErrorHandling method
                // and the channel/listener would immediately fault 
                //
                if (messageCyclesCompleted > maxRetryCycles)
                {
                    FinalDisposition(messageProperty);
                    return true;
                }

                // Check if the message is eligible for recycling/disposition
                if (messageMoveCountForCurrentCycle >= maxMovePerCycle)
                {
                    if (TD.ReceiveRetryCountReachedIsEnabled())
                    {
                        TD.ReceiveRetryCountReached(messageProperty.MessageId);
                    }
                    if (messageCyclesCompleted < maxRetryCycles)
                    {
                        // The message is eligible for recycling, move the message the message to retry queue
                        MsmqReceiveHelper.MoveReceivedMessage(this.lockQueueForReceive, this.retryQueueForMove, messageProperty.LookupId);
                        MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, false, this.receiver.InstanceId);
                    }
                    else
                    {
                        if (TD.MaxRetryCyclesExceededMsmqIsEnabled())
                        {
                            TD.MaxRetryCyclesExceededMsmq(messageProperty.MessageId);
                        }
                        // Dispose the message using ReceiveErrorHandling
                        FinalDisposition(messageProperty);
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
예제 #17
0
            internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
            {
                TryNonTransactedReceiveAsyncResult asyncResult = AsyncResult.End <TryNonTransactedReceiveAsyncResult>(result);

                msmqMessage = asyncResult.msmqMessage;
                property    = null;
                if (MsmqQueue.ReceiveResult.Timeout == asyncResult.receiveResult)
                {
                    return(false);
                }
                else if (MsmqQueue.ReceiveResult.OperationCancelled == asyncResult.receiveResult)
                {
                    return(true);
                }
                else
                {
                    property = new MsmqMessageProperty(msmqMessage);
                    return(true);
                }
            }
예제 #18
0
        //
        internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
        {
            msmqMessage  = null;
            msmqProperty = null;

            if (null == result)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }

            if (this.receiveParameters.ExactlyOnce)
            {
                TryTransactedReceiveAsyncResult receiveResult = result as TryTransactedReceiveAsyncResult;
                if (null == receiveResult)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                }
                return(TryTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty));
            }
            else
            {
                TryNonTransactedReceiveAsyncResult receiveResult = result as TryNonTransactedReceiveAsyncResult;
                if (null == receiveResult)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                }
                return(TryNonTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty));
            }
        }
예제 #19
0
 internal void FinalDisposition(MsmqMessageProperty messageProperty)
 {
     this.poisonHandler.FinalDisposition(messageProperty);
 }
        public bool EndTryReceive(IAsyncResult result, out Message message)
        {
            message = null;

            if (null == result)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }

            DoneReceivingAsyncResult doneRecevingResult = result as DoneReceivingAsyncResult;

            if (doneRecevingResult != null)
            {
                return(DoneReceivingAsyncResult.End(doneRecevingResult));
            }

            MsmqInputMessage    msmqMessage = null;
            MsmqMessageProperty property    = null;

            try
            {
                bool retval = this.receiver.EndTryReceive(result, out msmqMessage, out property);
                if (retval)
                {
                    if (null != property)
                    {
                        message = DecodeMsmqMessage(msmqMessage, property);
                        message.Properties[MsmqMessageProperty.Name] = property;

                        if (this.receiveParameters.ReceiveContextSettings.Enabled)
                        {
                            message.Properties[MsmqReceiveContext.Name] = this.receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                        }

                        MsmqDiagnostics.DatagramReceived(msmqMessage.MessageId, message);
                        this.listener.RaiseMessageReceived();
                    }
                    else if (CommunicationState.Opened == this.State)
                    {
                        this.listener.FaultListener();
                        this.Fault();
                    }
                }
                return(retval);
            }
            catch (MsmqException ex)
            {
                if (ex.FaultReceiver)
                {
                    this.listener.FaultListener();
                    this.Fault();
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
            }
            finally
            {
                if (null != msmqMessage)
                {
                    this.receiver.ReturnMessage(msmqMessage);
                }
            }
        }
 public void FinalDisposition(MsmqMessageProperty messageProperty)
 {
     this.receiver.DropOrRejectReceivedMessage(messageProperty, false);
 }
 internal void FinalDisposition(MsmqMessageProperty messageProperty)
 {
     this.poisonHandler.FinalDisposition(messageProperty);
 }
        internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            Message message2;

            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long              num1        = msmqMessage.LookupId.Value;
                int               size        = msmqMessage.BodyLength.Value;
                int               offset      = 0;
                byte[]            incoming    = msmqMessage.Body.Buffer;
                ServerModeDecoder modeDecoder = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException exception)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
                }
                if (modeDecoder.Mode != FramingMode.SingletonSized)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
                }
                ServerSingletonSizedDecoder decoder2 = new ServerSingletonSizedDecoder(0L, 0x800, 0x100);
                try
                {
                    do
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                        }
                        int num3 = decoder2.Decode(incoming, offset, size);
                        offset += num3;
                        size   -= num3;
                    }while (decoder2.CurrentState != ServerSingletonSizedDecoder.State.Start);
                }
                catch (ProtocolException exception2)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
                }
                if (size > listener.MaxReceivedMessageSize)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                }
                if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder2.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
                }
                byte[] dst = listener.BufferManager.TakeBuffer(size);
                Buffer.BlockCopy(incoming, offset, dst, 0, size);
                Message message = null;
                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    try
                    {
                        message = listener.MessageEncoderFactory.Encoder.ReadMessage(new ArraySegment <byte>(dst, 0, size), listener.BufferManager);
                    }
                    catch (XmlException exception3)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadXml"), exception3));
                    }
                    bool flag = true;
                    try
                    {
                        SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
                        if (property != null)
                        {
                            message.Properties.Security = property;
                        }
                        flag = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                        message2 = message;
                    }
                    catch (Exception exception4)
                    {
                        if (Fx.IsFatal(exception4))
                        {
                            throw;
                        }
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
                    }
                    finally
                    {
                        if (flag)
                        {
                            message.Close();
                        }
                    }
                }
            }
            return(message2);
        }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     TryNonTransactedReceiveAsyncResult asyncResult = AsyncResult.End<TryNonTransactedReceiveAsyncResult>(result);
     msmqMessage = asyncResult.msmqMessage;
     property = null;
     if (MsmqQueue.ReceiveResult.Timeout == asyncResult.receiveResult)
         return false;
     else if (MsmqQueue.ReceiveResult.OperationCancelled == asyncResult.receiveResult)
         return true;
     else
     {
         property = new MsmqMessageProperty(msmqMessage);
         return true;
     }
 }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     TryTransactedReceiveAsyncResult receiveResult = AsyncResult.End<TryTransactedReceiveAsyncResult>(result);
     msmqMessage = receiveResult.msmqMessage;
     property = receiveResult.messageProperty;
     return !receiveResult.expired;
 }
        //
        internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
        {
            msmqMessage = null;
            msmqProperty = null;

            if (null == result)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");

            if (this.receiveParameters.ExactlyOnce)
            {
                TryTransactedReceiveAsyncResult receiveResult = result as TryTransactedReceiveAsyncResult;
                if (null == receiveResult)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                return TryTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty);
            }
            else
            {
                TryNonTransactedReceiveAsyncResult receiveResult = result as TryNonTransactedReceiveAsyncResult;
                if (null == receiveResult)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                return TryNonTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty);
            }
        }
        internal bool TryReceive(MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, out MsmqMessageProperty property)
        {
            property = null;

            MsmqQueue.ReceiveResult receiveResult = this.Queue.TryReceive(msmqMessage, timeout, transactionMode);
            if (MsmqQueue.ReceiveResult.OperationCancelled == receiveResult)
                return true;
            if (MsmqQueue.ReceiveResult.Timeout == receiveResult)
                return false;
            else
            {
                property = new MsmqMessageProperty(msmqMessage);
                if (this.Transactional)
                {
                    if (this.PoisonHandler.CheckAndHandlePoisonMessage(property))
                    {
                        long lookupId = property.LookupId;
                        property = null;
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new MsmqPoisonMessageException(lookupId));
                    }
                }
                return true;
            }
        }
        public bool ReceiveContextPoisonHandling(MsmqMessageProperty messageProperty)
        {
            // The basic idea is to use the message move count to get the number of retry attempts the message has been through
            // The computation of the retry count and retry cycle count is slightly involved due to fact that the message being processed
            // could have been recycled message. (Recycled message is the message that moves from lock queue to retry queue to main queue
            // and back to lock queue
            //

            // Count to tally message recycling (lock queue to retry queue to main queue adds move count of 2 to the message)
            const int retryMoveCount = 2;

            // Actual number of times message is received before recycling to retry queue
            int actualReceiveRetryCount = this.ReceiveParameters.ReceiveRetryCount + 1;

            // The message is recycled these many number of times
            int maxRetryCycles = this.ReceiveParameters.MaxRetryCycles;

            // Max change in message move count between recycling
            int maxMovePerCycle = (2 * actualReceiveRetryCount) + 1;

            // Number of recycles the message has been through
            int messageCyclesCompleted = messageProperty.MoveCount / (maxMovePerCycle + retryMoveCount);

            // Total number of moves on the message at the end of the last recycle
            int messageMoveCountForCyclesCompleted = messageCyclesCompleted * (maxMovePerCycle + retryMoveCount);

            // The differential move count for the current cycle
            int messageMoveCountForCurrentCycle = messageProperty.MoveCount - messageMoveCountForCyclesCompleted;

            lock (this)
            {
                if (this.disposed)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));
                }

                // Check if the message has already completed its max recycle count (MaxRetryCycles)
                // and the disposed the message first. Such a message was previously disposed using the ReceiveErrorHandling method
                // and the channel/listener would immediately fault
                //
                if (messageCyclesCompleted > maxRetryCycles)
                {
                    FinalDisposition(messageProperty);
                    return(true);
                }

                // Check if the message is eligible for recycling/disposition
                if (messageMoveCountForCurrentCycle >= maxMovePerCycle)
                {
                    if (TD.ReceiveRetryCountReachedIsEnabled())
                    {
                        TD.ReceiveRetryCountReached(messageProperty.MessageId);
                    }
                    if (messageCyclesCompleted < maxRetryCycles)
                    {
                        // The message is eligible for recycling, move the message the message to retry queue
                        MsmqReceiveHelper.MoveReceivedMessage(this.lockQueueForReceive, this.retryQueueForMove, messageProperty.LookupId);
                        MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, false, this.receiver.InstanceId);
                    }
                    else
                    {
                        if (TD.MaxRetryCyclesExceededMsmqIsEnabled())
                        {
                            TD.MaxRetryCyclesExceededMsmq(messageProperty.MessageId);
                        }
                        // Dispose the message using ReceiveErrorHandling
                        FinalDisposition(messageProperty);
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
 internal static IInputSessionChannel DecodeTransportSessiongram(MsmqInputSessionChannelListener listener, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty, MsmqReceiveContextLockManager receiveContextManager)
 {
     using (MsmqDiagnostics.BoundReceiveBytesOperation())
     {
         int num4;
         long num1 = msmqMessage.LookupId.Value;
         int size = msmqMessage.BodyLength.Value;
         int offset = 0;
         byte[] incoming = msmqMessage.Body.Buffer;
         MsmqReceiveHelper msmqReceiveHelper = listener.MsmqReceiveHelper;
         ServerModeDecoder modeDecoder = new ServerModeDecoder();
         try
         {
             ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
         }
         catch (ProtocolException exception)
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
         }
         if (modeDecoder.Mode != FramingMode.Simplex)
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
         }
         MsmqInputSessionChannel channel = null;
         ServerSessionDecoder decoder2 = new ServerSessionDecoder(0L, 0x800, 0x100);
         try
         {
             do
             {
                 if (size <= 0)
                 {
                     throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                 }
                 int num3 = decoder2.Decode(incoming, offset, size);
                 offset += num3;
                 size -= num3;
             }
             while (ServerSessionDecoder.State.EnvelopeStart != decoder2.CurrentState);
         }
         catch (ProtocolException exception2)
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
         }
         MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();
         if (!encoder.IsContentTypeSupported(decoder2.ContentType))
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
         }
         ReceiveContext sessiongramReceiveContext = null;
         if (msmqReceiveHelper.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
         {
             sessiongramReceiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
         }
         channel = new MsmqInputSessionChannel(listener, Transaction.Current, sessiongramReceiveContext);
         Message item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
         SecurityMessageProperty property = null;
         try
         {
             property = listener.ValidateSecurity(msmqMessage);
         }
         catch (Exception exception3)
         {
             if (Fx.IsFatal(exception3))
             {
                 throw;
             }
             channel.FaultChannel();
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception3);
         }
         if (property != null)
         {
             item.Properties.Security = property;
         }
         item.Properties["MsmqMessageProperty"] = messageProperty;
         channel.EnqueueAndDispatch(item);
         listener.RaiseMessageReceived();
     Label_01F6:
         try
         {
             if (size <= 0)
             {
                 channel.FaultChannel();
                 msmqReceiveHelper.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
             }
             num4 = decoder2.Decode(incoming, offset, size);
         }
         catch (ProtocolException exception4)
         {
             channel.FaultChannel();
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
         }
         offset += num4;
         size -= num4;
         if (ServerSessionDecoder.State.End != decoder2.CurrentState)
         {
             if (ServerSessionDecoder.State.EnvelopeStart == decoder2.CurrentState)
             {
                 item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
                 if (property != null)
                 {
                     item.Properties.Security = (SecurityMessageProperty) property.CreateCopy();
                 }
                 item.Properties["MsmqMessageProperty"] = messageProperty;
                 channel.EnqueueAndDispatch(item);
                 listener.RaiseMessageReceived();
             }
             goto Label_01F6;
         }
         channel.Shutdown();
         MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);
         return channel;
     }
 }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     MsmqReceiveHelper.TryNonTransactedReceiveAsyncResult result2 = AsyncResult.End <MsmqReceiveHelper.TryNonTransactedReceiveAsyncResult>(result);
     msmqMessage = result2.msmqMessage;
     property    = null;
     if (MsmqQueue.ReceiveResult.Timeout == result2.receiveResult)
     {
         return(false);
     }
     if (MsmqQueue.ReceiveResult.OperationCancelled != result2.receiveResult)
     {
         property = new MsmqMessageProperty(msmqMessage);
     }
     return(true);
 }
        internal void DropOrRejectReceivedMessage(MsmqQueue queue, MsmqMessageProperty messageProperty, bool reject)
        {
            if (this.Transactional)
            {
                TryAbortTransactionCurrent();
                IPostRollbackErrorStrategy postRollback = new SimplePostRollbackErrorStrategy(messageProperty.LookupId);
                MsmqQueue.MoveReceiveResult result = MsmqQueue.MoveReceiveResult.Unknown;
                do
                {
                    using (MsmqEmptyMessage emptyMessage = new MsmqEmptyMessage())
                    {
                        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                        {
                            result = queue.TryReceiveByLookupId(messageProperty.LookupId, emptyMessage, MsmqTransactionMode.CurrentOrThrow);
                            if (MsmqQueue.MoveReceiveResult.Succeeded == result && reject)
                                queue.MarkMessageRejected(messageProperty.LookupId);
                            scope.Complete();
                        }
                    }

                    if (result == MsmqQueue.MoveReceiveResult.Succeeded)
                        // If 'Reject' supported and 'Reject' requested, put reject in the trace, otherwise put 'Drop'
                        MsmqDiagnostics.MessageConsumed(instanceId, messageProperty.MessageId, (Msmq.IsRejectMessageSupported && reject));

                    if (result != MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction)
                        break;
                }
                while (postRollback.AnotherTryNeeded());
            }
            else
            {
                MsmqDiagnostics.MessageConsumed(instanceId, messageProperty.MessageId, false);
            }
        }
 public bool CheckAndHandlePoisonMessage(MsmqMessageProperty messageProperty)
 {
     return(false);
 }
        internal static IInputSessionChannel DecodeTransportSessiongram(
            MsmqInputSessionChannelListener listener,
            MsmqInputMessage msmqMessage,
            MsmqMessageProperty messageProperty,
            MsmqReceiveContextLockManager receiveContextManager)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long lookupId = msmqMessage.LookupId.Value;

                int size = msmqMessage.BodyLength.Value;
                int offset = 0;
                byte[] incoming = msmqMessage.Body.Buffer;
                MsmqReceiveHelper receiver = listener.MsmqReceiveHelper;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.Simplex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                MsmqInputSessionChannel channel = null;
                ServerSessionDecoder sessionDecoder = new ServerSessionDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);

                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        int decoded = sessionDecoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size -= decoded;
                        if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                            break;
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();

                if (!encoder.IsContentTypeSupported(sessionDecoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                ReceiveContext receiveContext = null;

                // tack on the receive context property depending on the receive mode
                if (receiver.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
                {
                    receiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                }

                channel = new MsmqInputSessionChannel(listener, Transaction.Current, receiveContext);

                Message message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);

                SecurityMessageProperty securityProperty = null;
                try
                {
                    securityProperty = listener.ValidateSecurity(msmqMessage);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                        throw;
                    channel.FaultChannel();
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (null != securityProperty)
                    message.Properties.Security = securityProperty;

                message.Properties[MsmqMessageProperty.Name] = messageProperty;
                channel.EnqueueAndDispatch(message);
                listener.RaiseMessageReceived();

                for (;;)
                {
                    int decoded;
                    try
                    {
                        if (size <= 0)
                        {
                            channel.FaultChannel();
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        decoded = sessionDecoder.Decode(incoming, offset, size);
                    }
                    catch (ProtocolException ex)
                    {
                        channel.FaultChannel();
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    offset += decoded;
                    size -= decoded;
                    if (ServerSessionDecoder.State.End == sessionDecoder.CurrentState)
                        break;
                    if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                    {
                        message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);
                        if (null != securityProperty)
                        {
                            message.Properties.Security = (SecurityMessageProperty)securityProperty.CreateCopy();
                        }
                        message.Properties[MsmqMessageProperty.Name] = messageProperty;
                        channel.EnqueueAndDispatch(message);
                        listener.RaiseMessageReceived();
                    }
                }

                channel.Shutdown();
                MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);

                return channel;
            }
        }
        internal static IInputSessionChannel DecodeTransportSessiongram(MsmqInputSessionChannelListener listener, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty, MsmqReceiveContextLockManager receiveContextManager)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                int               num4;
                long              num1              = msmqMessage.LookupId.Value;
                int               size              = msmqMessage.BodyLength.Value;
                int               offset            = 0;
                byte[]            incoming          = msmqMessage.Body.Buffer;
                MsmqReceiveHelper msmqReceiveHelper = listener.MsmqReceiveHelper;
                ServerModeDecoder modeDecoder       = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException exception)
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
                }
                if (modeDecoder.Mode != FramingMode.Simplex)
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
                }
                MsmqInputSessionChannel channel  = null;
                ServerSessionDecoder    decoder2 = new ServerSessionDecoder(0L, 0x800, 0x100);
                try
                {
                    do
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                        }
                        int num3 = decoder2.Decode(incoming, offset, size);
                        offset += num3;
                        size   -= num3;
                    }while (ServerSessionDecoder.State.EnvelopeStart != decoder2.CurrentState);
                }
                catch (ProtocolException exception2)
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
                }
                MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();
                if (!encoder.IsContentTypeSupported(decoder2.ContentType))
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
                }
                ReceiveContext sessiongramReceiveContext = null;
                if (msmqReceiveHelper.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
                {
                    sessiongramReceiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                }
                channel = new MsmqInputSessionChannel(listener, Transaction.Current, sessiongramReceiveContext);
                Message item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
                SecurityMessageProperty property = null;
                try
                {
                    property = listener.ValidateSecurity(msmqMessage);
                }
                catch (Exception exception3)
                {
                    if (Fx.IsFatal(exception3))
                    {
                        throw;
                    }
                    channel.FaultChannel();
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception3);
                }
                if (property != null)
                {
                    item.Properties.Security = property;
                }
                item.Properties["MsmqMessageProperty"] = messageProperty;
                channel.EnqueueAndDispatch(item);
                listener.RaiseMessageReceived();
Label_01F6:
                try
                {
                    if (size <= 0)
                    {
                        channel.FaultChannel();
                        msmqReceiveHelper.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                    }
                    num4 = decoder2.Decode(incoming, offset, size);
                }
                catch (ProtocolException exception4)
                {
                    channel.FaultChannel();
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
                }
                offset += num4;
                size   -= num4;
                if (ServerSessionDecoder.State.End != decoder2.CurrentState)
                {
                    if (ServerSessionDecoder.State.EnvelopeStart == decoder2.CurrentState)
                    {
                        item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
                        if (property != null)
                        {
                            item.Properties.Security = (SecurityMessageProperty)property.CreateCopy();
                        }
                        item.Properties["MsmqMessageProperty"] = messageProperty;
                        channel.EnqueueAndDispatch(item);
                        listener.RaiseMessageReceived();
                    }
                    goto Label_01F6;
                }
                channel.Shutdown();
                MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);
                return(channel);
            }
        }
        static Message DecodeSessiongramMessage(
            MsmqInputSessionChannelListener listener,
            MsmqInputSessionChannel channel,
            MessageEncoder encoder,
            MsmqMessageProperty messageProperty,
            byte[] buffer,
            int offset,
            int size)
        {
            if (size > listener.MaxReceivedMessageSize)
            {
                channel.FaultChannel();
                listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
                throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
            }

            // Fix for CSDMain bug 17842
            // size is derived from user data, check for corruption
            if ((size + offset) > buffer.Length)
            {
                listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
                throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
            }

            byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size);
            Buffer.BlockCopy(buffer, offset, envelopeBuffer, 0, size);
            try
            {
                Message message = null;
                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    message = encoder.ReadMessage(new ArraySegment<byte>(envelopeBuffer, 0, size), listener.BufferManager);
                    MsmqDiagnostics.TransferFromTransport(message);
                }
                return message;
            }
            catch (XmlException e)
            {
                channel.FaultChannel();
                listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
                throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e));
            }
        }
 abstract protected Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty property);
        internal static Message DecodeIntegrationDatagram(MsmqIntegrationChannelListener listener, MsmqReceiveHelper receiver, MsmqIntegrationInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                Message message = Message.CreateMessage(MessageVersion.None, (string)null);
                bool closeMessage = true;

                try
                {
                    SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                    if (null != securityProperty)
                        message.Properties.Security = securityProperty;

                    MsmqIntegrationMessageProperty integrationProperty = new MsmqIntegrationMessageProperty();
                    msmqMessage.SetMessageProperties(integrationProperty);

                    int size = msmqMessage.BodyLength.Value;

                    if (size > listener.MaxReceivedMessageSize)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                    }

                    byte[] bodyBytes = msmqMessage.Body.GetBufferCopy(size);

                    MemoryStream bodyStream = new MemoryStream(bodyBytes, 0, bodyBytes.Length, false);

                    object body = null;
                    using (MsmqDiagnostics.BoundDecodeOperation())
                    {
                        try
                        {
                            body = DeserializeForIntegration(listener, bodyStream, integrationProperty, messageProperty.LookupId);
                        }
                        catch (SerializationException e)
                        {
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqDeserializationError), e));
                        }

                        integrationProperty.Body = body;
                        message.Properties[MsmqIntegrationMessageProperty.Name] = integrationProperty;
                        bodyStream.Seek(0, SeekOrigin.Begin);
                        message.Headers.To = listener.Uri;
                        closeMessage = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                    }
                    return message;
                }
                finally
                {
                    if (closeMessage)
                        message.Close();
                }
            }
        }
예제 #38
0
 internal void DropOrRejectReceivedMessage(MsmqMessageProperty messageProperty, bool reject)
 {
     this.DropOrRejectReceivedMessage(this.Queue, messageProperty, reject);
 }
예제 #39
0
        protected override Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            MsmqInputChannelListener manager = base.Manager as MsmqInputChannelListener;

            return(MsmqDecodeHelper.DecodeTransportDatagram(manager, base.MsmqReceiveHelper, msmqMessage, messageProperty));
        }
예제 #40
0
        internal bool TryReceive(MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, out MsmqMessageProperty property)
        {
            property = null;

            MsmqQueue.ReceiveResult receiveResult = this.Queue.TryReceive(msmqMessage, timeout, transactionMode);
            if (MsmqQueue.ReceiveResult.OperationCancelled == receiveResult)
            {
                return(true);
            }
            if (MsmqQueue.ReceiveResult.Timeout == receiveResult)
            {
                return(false);
            }
            else
            {
                property = new MsmqMessageProperty(msmqMessage);
                if (this.Transactional)
                {
                    if (this.PoisonHandler.CheckAndHandlePoisonMessage(property))
                    {
                        long lookupId = property.LookupId;
                        property = null;
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new MsmqPoisonMessageException(lookupId));
                    }
                }
                return(true);
            }
        }
 public bool ReceiveContextPoisonHandling(MsmqMessageProperty messageProperty)
 {
     int num = this.ReceiveParameters.ReceiveRetryCount + 1;
     int maxRetryCycles = this.ReceiveParameters.MaxRetryCycles;
     int num3 = (2 * num) + 1;
     int num4 = messageProperty.MoveCount / (num3 + 2);
     int num5 = num4 * (num3 + 2);
     int num6 = messageProperty.MoveCount - num5;
     lock (this)
     {
         if (this.disposed)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(base.GetType().ToString()));
         }
         if (num4 > maxRetryCycles)
         {
             this.FinalDisposition(messageProperty);
             return true;
         }
         if (num6 >= num3)
         {
             if (num4 < maxRetryCycles)
             {
                 MsmqReceiveHelper.MoveReceivedMessage(this.lockQueueForReceive, this.retryQueueForMove, messageProperty.LookupId);
                 MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, false, this.receiver.InstanceId);
             }
             else
             {
                 this.FinalDisposition(messageProperty);
             }
             return true;
         }
         return false;
     }
 }
예제 #42
0
            internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
            {
                TryTransactedReceiveAsyncResult receiveResult = AsyncResult.End <TryTransactedReceiveAsyncResult>(result);

                msmqMessage = receiveResult.msmqMessage;
                property    = receiveResult.messageProperty;
                return(!receiveResult.expired);
            }
예제 #43
0
        internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long   lookupId = msmqMessage.LookupId.Value;
                int    size     = msmqMessage.BodyLength.Value;
                int    offset   = 0;
                byte[] incoming = msmqMessage.Body.Buffer;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();

                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.SingletonSized)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                ServerSingletonSizedDecoder decoder = new ServerSingletonSizedDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);
                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder.CreatePrematureEOFException());
                        }

                        int decoded = decoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size   -= decoded;
                        if (decoder.CurrentState == ServerSingletonSizedDecoder.State.Start)
                        {
                            break;
                        }
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (size > listener.MaxReceivedMessageSize)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                }

                if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size);
                Buffer.BlockCopy(incoming, offset, envelopeBuffer, 0, size);

                Message message = null;

                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    try
                    {
                        message = listener.MessageEncoderFactory.Encoder.ReadMessage(
                            new ArraySegment <byte>(envelopeBuffer, 0, size), listener.BufferManager);
                    }
                    catch (XmlException e)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e));
                    }

                    bool closeMessage = true;
                    try
                    {
                        SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                        if (null != securityProperty)
                        {
                            message.Properties.Security = securityProperty;
                        }

                        closeMessage = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                        return(message);
                    }
                    catch (Exception ex)
                    {
                        if (Fx.IsFatal(ex))
                        {
                            throw;
                        }
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    finally
                    {
                        if (closeMessage)
                        {
                            message.Close();
                        }
                    }
                }
            }
        }
 protected override Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty property)
 {
     MsmqIntegrationChannelListener listener = this.Manager as MsmqIntegrationChannelListener;
     return MsmqDecodeHelper.DecodeIntegrationDatagram(listener, this.MsmqReceiveHelper, msmqMessage as MsmqIntegrationInputMessage, property);
 }
예제 #45
0
        internal static Message DecodeIntegrationDatagram(MsmqIntegrationChannelListener listener, MsmqReceiveHelper receiver, MsmqIntegrationInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                Message message      = Message.CreateMessage(MessageVersion.None, (string)null);
                bool    closeMessage = true;

                try
                {
                    SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                    if (null != securityProperty)
                    {
                        message.Properties.Security = securityProperty;
                    }

                    MsmqIntegrationMessageProperty integrationProperty = new MsmqIntegrationMessageProperty();
                    msmqMessage.SetMessageProperties(integrationProperty);

                    int size = msmqMessage.BodyLength.Value;

                    if (size > listener.MaxReceivedMessageSize)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                    }

                    byte[] bodyBytes = msmqMessage.Body.GetBufferCopy(size);

                    MemoryStream bodyStream = new MemoryStream(bodyBytes, 0, bodyBytes.Length, false);

                    object body = null;
                    using (MsmqDiagnostics.BoundDecodeOperation())
                    {
                        try
                        {
                            body = DeserializeForIntegration(listener, bodyStream, integrationProperty, messageProperty.LookupId);
                        }
                        catch (SerializationException e)
                        {
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqDeserializationError), e));
                        }

                        integrationProperty.Body = body;
                        message.Properties[MsmqIntegrationMessageProperty.Name] = integrationProperty;
                        bodyStream.Seek(0, SeekOrigin.Begin);
                        message.Headers.To = listener.Uri;
                        closeMessage       = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                    }
                    return(message);
                }
                finally
                {
                    if (closeMessage)
                    {
                        message.Close();
                    }
                }
            }
        }
        public bool NonReceiveContextPoisonHandling(MsmqMessageProperty messageProperty)
        {
            if (messageProperty.AbortCount <= this.ReceiveParameters.ReceiveRetryCount)
                return false;
            int retryCycle = messageProperty.MoveCount / 2;

            lock (this)
            {
                if (this.disposed)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(this.GetType().ToString()));

                if (retryCycle >= this.ReceiveParameters.MaxRetryCycles)
                {
                    if (TD.MaxRetryCyclesExceededMsmqIsEnabled())
                    {
                        TD.MaxRetryCyclesExceededMsmq(messageProperty.MessageId);
                    }
                    FinalDisposition(messageProperty);
                }
                else
                {
                    MsmqReceiveHelper.MoveReceivedMessage(this.mainQueue, this.retryQueueForMove, messageProperty.LookupId);
                    MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, false, this.receiver.InstanceId);
                }
            }
            return true;
        }
예제 #47
0
        internal static IInputSessionChannel DecodeTransportSessiongram(
            MsmqInputSessionChannelListener listener,
            MsmqInputMessage msmqMessage,
            MsmqMessageProperty messageProperty,
            MsmqReceiveContextLockManager receiveContextManager)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long lookupId = msmqMessage.LookupId.Value;

                int               size     = msmqMessage.BodyLength.Value;
                int               offset   = 0;
                byte[]            incoming = msmqMessage.Body.Buffer;
                MsmqReceiveHelper receiver = listener.MsmqReceiveHelper;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.Simplex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                MsmqInputSessionChannel channel        = null;
                ServerSessionDecoder    sessionDecoder = new ServerSessionDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);

                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        int decoded = sessionDecoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size   -= decoded;
                        if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                        {
                            break;
                        }
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();

                if (!encoder.IsContentTypeSupported(sessionDecoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                ReceiveContext receiveContext = null;

                // tack on the receive context property depending on the receive mode
                if (receiver.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
                {
                    receiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                }

                channel = new MsmqInputSessionChannel(listener, Transaction.Current, receiveContext);

                Message message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);

                SecurityMessageProperty securityProperty = null;
                try
                {
                    securityProperty = listener.ValidateSecurity(msmqMessage);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                    {
                        throw;
                    }
                    channel.FaultChannel();
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (null != securityProperty)
                {
                    message.Properties.Security = securityProperty;
                }

                message.Properties[MsmqMessageProperty.Name] = messageProperty;
                channel.EnqueueAndDispatch(message);
                listener.RaiseMessageReceived();

                for (;;)
                {
                    int decoded;
                    try
                    {
                        if (size <= 0)
                        {
                            channel.FaultChannel();
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        decoded = sessionDecoder.Decode(incoming, offset, size);
                    }
                    catch (ProtocolException ex)
                    {
                        channel.FaultChannel();
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    offset += decoded;
                    size   -= decoded;
                    if (ServerSessionDecoder.State.End == sessionDecoder.CurrentState)
                    {
                        break;
                    }
                    if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                    {
                        message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);
                        if (null != securityProperty)
                        {
                            message.Properties.Security = (SecurityMessageProperty)securityProperty.CreateCopy();
                        }
                        message.Properties[MsmqMessageProperty.Name] = messageProperty;
                        channel.EnqueueAndDispatch(message);
                        listener.RaiseMessageReceived();
                    }
                }

                channel.Shutdown();
                MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);

                return(channel);
            }
        }
        internal static Message DecodeIntegrationDatagram(MsmqIntegrationChannelListener listener, MsmqReceiveHelper receiver, MsmqIntegrationInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            Message message2;

            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                Message message = Message.CreateMessage(MessageVersion.None, (string)null);
                bool    flag    = true;
                try
                {
                    SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
                    if (property != null)
                    {
                        message.Properties.Security = property;
                    }
                    MsmqIntegrationMessageProperty property2 = new MsmqIntegrationMessageProperty();
                    msmqMessage.SetMessageProperties(property2);
                    int length = msmqMessage.BodyLength.Value;
                    if (length > listener.MaxReceivedMessageSize)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                    }
                    byte[]       bufferCopy = msmqMessage.Body.GetBufferCopy(length);
                    MemoryStream bodyStream = new MemoryStream(bufferCopy, 0, bufferCopy.Length, false);
                    object       obj2       = null;
                    using (MsmqDiagnostics.BoundDecodeOperation())
                    {
                        try
                        {
                            obj2 = DeserializeForIntegration(listener, bodyStream, property2, messageProperty.LookupId);
                        }
                        catch (SerializationException exception)
                        {
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqDeserializationError"), exception));
                        }
                        property2.Body = obj2;
                        message.Properties["MsmqIntegrationMessageProperty"] = property2;
                        bodyStream.Seek(0L, SeekOrigin.Begin);
                        message.Headers.To = listener.Uri;
                        flag = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                    }
                    message2 = message;
                }
                finally
                {
                    if (flag)
                    {
                        message.Close();
                    }
                }
            }
            return(message2);
        }
 internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
 {
     Message message2;
     using (MsmqDiagnostics.BoundReceiveBytesOperation())
     {
         long num1 = msmqMessage.LookupId.Value;
         int size = msmqMessage.BodyLength.Value;
         int offset = 0;
         byte[] incoming = msmqMessage.Body.Buffer;
         ServerModeDecoder modeDecoder = new ServerModeDecoder();
         try
         {
             ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
         }
         catch (ProtocolException exception)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
         }
         if (modeDecoder.Mode != FramingMode.SingletonSized)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
         }
         ServerSingletonSizedDecoder decoder2 = new ServerSingletonSizedDecoder(0L, 0x800, 0x100);
         try
         {
             do
             {
                 if (size <= 0)
                 {
                     throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                 }
                 int num3 = decoder2.Decode(incoming, offset, size);
                 offset += num3;
                 size -= num3;
             }
             while (decoder2.CurrentState != ServerSingletonSizedDecoder.State.Start);
         }
         catch (ProtocolException exception2)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
         }
         if (size > listener.MaxReceivedMessageSize)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
         }
         if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder2.ContentType))
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
         }
         byte[] dst = listener.BufferManager.TakeBuffer(size);
         Buffer.BlockCopy(incoming, offset, dst, 0, size);
         Message message = null;
         using (MsmqDiagnostics.BoundDecodeOperation())
         {
             try
             {
                 message = listener.MessageEncoderFactory.Encoder.ReadMessage(new ArraySegment<byte>(dst, 0, size), listener.BufferManager);
             }
             catch (XmlException exception3)
             {
                 receiver.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadXml"), exception3));
             }
             bool flag = true;
             try
             {
                 SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
                 if (property != null)
                 {
                     message.Properties.Security = property;
                 }
                 flag = false;
                 MsmqDiagnostics.TransferFromTransport(message);
                 message2 = message;
             }
             catch (Exception exception4)
             {
                 if (Fx.IsFatal(exception4))
                 {
                     throw;
                 }
                 receiver.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
             }
             finally
             {
                 if (flag)
                 {
                     message.Close();
                 }
             }
         }
     }
     return message2;
 }
 abstract protected Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty property);
        private static Message DecodeSessiongramMessage(MsmqInputSessionChannelListener listener, MsmqInputSessionChannel channel, MessageEncoder encoder, MsmqMessageProperty messageProperty, byte[] buffer, int offset, int size)
        {
            Message message2;

            if (size > listener.MaxReceivedMessageSize)
            {
                channel.FaultChannel();
                listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
                throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
            }
            if ((size + offset) > buffer.Length)
            {
                listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
                throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
            }
            byte[] dst = listener.BufferManager.TakeBuffer(size);
            Buffer.BlockCopy(buffer, offset, dst, 0, size);
            try
            {
                Message message = null;
                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    message = encoder.ReadMessage(new ArraySegment <byte>(dst, 0, size), listener.BufferManager);
                    MsmqDiagnostics.TransferFromTransport(message);
                }
                message2 = message;
            }
            catch (XmlException exception)
            {
                channel.FaultChannel();
                listener.MsmqReceiveHelper.FinalDisposition(messageProperty);
                throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadXml"), exception));
            }
            return(message2);
        }
        internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long lookupId = msmqMessage.LookupId.Value;
                int size = msmqMessage.BodyLength.Value;
                int offset = 0;
                byte[] incoming = msmqMessage.Body.Buffer;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();

                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.SingletonSized)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                ServerSingletonSizedDecoder decoder = new ServerSingletonSizedDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);
                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder.CreatePrematureEOFException());
                        }

                        int decoded = decoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size -= decoded;
                        if (decoder.CurrentState == ServerSingletonSizedDecoder.State.Start)
                            break;
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (size > listener.MaxReceivedMessageSize)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                }

                if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size);
                Buffer.BlockCopy(incoming, offset, envelopeBuffer, 0, size);

                Message message = null;

                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    try
                    {
                        message = listener.MessageEncoderFactory.Encoder.ReadMessage(
                            new ArraySegment<byte>(envelopeBuffer, 0, size), listener.BufferManager);
                    }
                    catch (XmlException e)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e));
                    }

                    bool closeMessage = true;
                    try
                    {
                        SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                        if (null != securityProperty)
                            message.Properties.Security = securityProperty;

                        closeMessage = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                        return message;
                    }
                    catch (Exception ex)
                    {
                        if (Fx.IsFatal(ex))
                            throw;
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    finally
                    {
                        if (closeMessage)
                        {
                            message.Close();
                        }
                    }
                }
            }
        }
        internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
        {
            msmqMessage  = null;
            msmqProperty = null;
            if (result == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }
            if (this.receiveParameters.ExactlyOnce)
            {
                TryTransactedReceiveAsyncResult result2 = result as TryTransactedReceiveAsyncResult;
                if (result2 == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("InvalidAsyncResult"));
                }
                return(TryTransactedReceiveAsyncResult.End(result2, out msmqMessage, out msmqProperty));
            }
            TryNonTransactedReceiveAsyncResult result3 = result as TryNonTransactedReceiveAsyncResult;

            if (result3 == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("InvalidAsyncResult"));
            }
            return(TryNonTransactedReceiveAsyncResult.End(result3, out msmqMessage, out msmqProperty));
        }