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 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);
        }
Beispiel #3
0
        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));
            }
        }
        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 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();
                    }
                }
            }
        }
        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);
        }