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 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 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 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);
        }