internal ArraySegment <byte> BufferMessageStream(Stream stream, BufferManager bufferManager, int maxBufferSize)
        {
            byte[] buffer     = bufferManager.TakeBuffer(0x2000);
            int    offset     = 0;
            int    bufferSize = Math.Min(buffer.Length, maxBufferSize);

            while (offset < bufferSize)
            {
                int num3 = stream.Read(buffer, offset, bufferSize - offset);
                if (num3 == 0)
                {
                    stream.Close();
                    break;
                }
                offset += num3;
                if (offset == bufferSize)
                {
                    if (bufferSize >= maxBufferSize)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException((long)maxBufferSize));
                    }
                    bufferSize = Math.Min(bufferSize * 2, maxBufferSize);
                    byte[] dst = bufferManager.TakeBuffer(bufferSize);
                    Buffer.BlockCopy(buffer, 0, dst, 0, offset);
                    bufferManager.ReturnBuffer(buffer);
                    buffer = dst;
                }
            }
            return(new ArraySegment <byte>(buffer, 0, offset));
        }
Esempio n. 2
0
        // used for buffered streaming
        internal async Task <ArraySegment <byte> > BufferMessageStreamAsync(Stream stream, BufferManager bufferManager, int maxBufferSize, CancellationToken cancellationToken)
        {
            byte[] buffer            = bufferManager.TakeBuffer(ConnectionOrientedTransportDefaults.ConnectionBufferSize);
            int    offset            = 0;
            int    currentBufferSize = Math.Min(buffer.Length, maxBufferSize);

            while (offset < currentBufferSize)
            {
                int count = await stream.ReadAsync(buffer, offset, currentBufferSize - offset, cancellationToken);

                if (count == 0)
                {
                    stream.Dispose();
                    break;
                }

                offset += count;
                if (offset == currentBufferSize)
                {
                    if (currentBufferSize >= maxBufferSize)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(maxBufferSize));
                    }

                    currentBufferSize = Math.Min(currentBufferSize * 2, maxBufferSize);
                    byte[] temp = bufferManager.TakeBuffer(currentBufferSize);
                    Buffer.BlockCopy(buffer, 0, temp, 0, offset);
                    bufferManager.ReturnBuffer(buffer);
                    buffer = temp;
                }
            }

            return(new ArraySegment <byte>(buffer, 0, offset));
        }
        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);
        }
Esempio n. 4
0
 public void Alloc(int requiredSize)
 {
     if (requiredSize > this.remainingSize)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException((long)this.maxSize));
     }
     this.remainingSize -= requiredSize;
 }
        private async Task <Message> ReadStreamedMessageAsync(Task <Stream> inputStreamTask)
        {
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(await inputStreamTask, _factory.MaxReceivedMessageSize);

            try
            {
                return(await _encoder.ReadMessageAsync(maxMessageSizeStream, _factory.MaxBufferSize, _contentType));
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SRServiceModel.MessageXmlProtocolError, xmlException));
            }
        }
        private Message ReadStreamedMessage(Stream inputStream)
        {
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(inputStream, _factory.MaxReceivedMessageSize);

            try
            {
                return(_encoder.ReadMessage(maxMessageSizeStream, _factory.MaxBufferSize, _contentType));
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SR.MessageXmlProtocolError, xmlException));
            }
        }
        private Message ReadStreamedMessage(Stream inputStream)
        {
            Message message;
            MaxMessageSizeStream innerStream = new MaxMessageSizeStream(inputStream, this.settings.MaxReceivedMessageSize);
            Stream stream = new DrainOnCloseStream(innerStream);

            try
            {
                message = this.messageEncoder.ReadMessage(stream, this.settings.MaxBufferSize, this.ContentType);
            }
            catch (XmlException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
            }
            return(message);
        }
Esempio n. 8
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));
            }
        }
Esempio n. 9
0
        private async Task <Message> ReadStreamedMessageAsync(Task <Stream> inputStreamTask)
        {
            var inputStream         = await inputStreamTask;
            var bufferedInputStream = inputStream as BufferedReadStream;
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(inputStream, _factory.MaxReceivedMessageSize);

            try
            {
                var message = await _encoder.ReadMessageAsync(maxMessageSizeStream, _factory.MaxBufferSize, _contentType);

                if (bufferedInputStream != null)
                {
                    message.Properties[BufferedReadStream.BufferedReadStreamPropertyName] = bufferedInputStream;
                }

                return(message);
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SR.MessageXmlProtocolError, xmlException));
            }
        }
Esempio n. 10
0
        private Message ReadStreamedMessage(Stream inputStream)
        {
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(inputStream, _factory.MaxReceivedMessageSize);

            try
            {
                return _encoder.ReadMessage(maxMessageSizeStream, _factory.MaxBufferSize, _contentType);
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new ProtocolException(SR.MessageXmlProtocolError, xmlException));
            }
        }
Esempio n. 11
0
            public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
            {
                if (stream == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("stream");
                }

                CompressionFormat compressionFormat = this.CheckContentType(contentType);

                if (WcfEventSource.Instance.BinaryMessageDecodingStartIsEnabled())
                {
                    WcfEventSource.Instance.BinaryMessageDecodingStart();
                }

                if (compressionFormat != CompressionFormat.None)
                {
                    stream = new MaxMessageSizeStream(
                        MessageEncoderCompressionHandler.GetDecompressStream(stream, compressionFormat), _maxReceivedMessageSize);
                }

                var wrappingStream = new ReadAheadWrappingStream(stream, 4096);
                wrappingStream.EnsureBuffered();
                XmlDictionaryReader reader = _factory.TakeStreamedReader(wrappingStream);
                Message message = Message.CreateMessage(reader, maxSizeOfHeaders, _factory._messageVersion);
                message.Properties.Encoder = this;
                message.Properties[ReadAheadWrappingStream.ReadAheadWrappingStreamPropertyName] = wrappingStream;

                if (WcfEventSource.Instance.StreamedMessageReadByEncoderIsEnabled())
                {
                    WcfEventSource.Instance.StreamedMessageReadByEncoder(
                        EventTraceActivityHelper.TryExtractActivity(message, true));
                }

                if (MessageLogger.LogMessagesAtTransportLevel)
                {
                    MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportReceive);
                }
                return message;
            }
        private async Task<Message> ReadStreamedMessageAsync(Task<Stream> inputStreamTask)
        {
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(await inputStreamTask, _factory.MaxReceivedMessageSize);

            try
            {
                return await _encoder.ReadMessageAsync(maxMessageSizeStream, _factory.MaxBufferSize, _contentType);
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new ProtocolException(SR.MessageXmlProtocolError, xmlException));
            }
        }
Esempio n. 13
0
        private async Task<Message> ReadStreamedMessageAsync(Task<Stream> inputStreamTask)
        {
            var inputStream = await inputStreamTask;
            var bufferedInputStream = inputStream as BufferedReadStream;
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(inputStream, _factory.MaxReceivedMessageSize);

            try
            {
                var message = await _encoder.ReadMessageAsync(maxMessageSizeStream, _factory.MaxBufferSize, _contentType);
                if (bufferedInputStream != null)
                {
                    message.Properties[BufferedReadStream.BufferedReadStreamPropertyName] = bufferedInputStream;
                }

                return message;
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new ProtocolException(SR.MessageXmlProtocolError, xmlException));
            }
        }
            Message PrepareMessage(WebSocketReceiveResult result, byte[] buffer, int count)
            {
                if (result.MessageType != WebSocketMessageType.Close)
                {
                    Message message;
                    if (_useStreaming)
                    {
                        using (var wrappedStream = new MaxMessageSizeStream(
                            new TimeoutStream(
                                new WebSocketStream(
                                    this,
                                    new ArraySegment<byte>(buffer, 0, count),
                                    _webSocket,
                                    result.EndOfMessage,
                                    _bufferManager,
                                    _defaultTimeouts.CloseTimeout),
                                _defaultTimeouts.ReceiveTimeout),
                            _maxReceivedMessageSize))
                        {
                            message = _encoder.ReadMessage(wrappedStream, _maxBufferSize);
                        }
                    }
                    else
                    {
                        ArraySegment<byte> bytes = new ArraySegment<byte>(buffer, 0, count);
                        message = _encoder.ReadMessage(bytes, _bufferManager);
                    }

                    if (message.Version.Addressing != AddressingVersion.None || !_localAddress.IsAnonymous)
                    {
                        _localAddress.ApplyTo(message);
                    }

                    if (message.Version.Addressing == AddressingVersion.None && message.Headers.Action == null)
                    {
                        if (result.MessageType == WebSocketMessageType.Binary)
                        {
                            message.Headers.Action = WebSocketTransportSettings.BinaryMessageReceivedAction;
                        }
                        else
                        {
                            // WebSocketMesssageType should always be binary or text at this moment. The layer below us will help protect this.
                            Fx.Assert(result.MessageType == WebSocketMessageType.Text, "result.MessageType must be WebSocketMessageType.Text.");
                            message.Headers.Action = WebSocketTransportSettings.TextMessageReceivedAction;
                        }
                    }

                    return message;
                }

                return null;
            }
 private Message ReadStreamedMessage(Stream inputStream)
 {
     Message message;
     MaxMessageSizeStream innerStream = new MaxMessageSizeStream(inputStream, this.settings.MaxReceivedMessageSize);
     Stream stream = new DrainOnCloseStream(innerStream);
     try
     {
         message = this.messageEncoder.ReadMessage(stream, this.settings.MaxBufferSize, this.ContentType);
     }
     catch (XmlException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
     }
     return message;
 }
        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);
        }
Esempio n. 17
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 DrainOnCloseStream(MaxMessageSizeStream innerStream) : base(innerStream)
 {
 }
Esempio n. 19
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 DecodeMessage(byte[] buffer, ref int offset, ref int size, ref bool isAtEOF, TimeSpan timeout)
        {
            while (size > 0)
            {
                int envelopeSize;
                int count = this.decoder.Decode(buffer, offset, size);
                if (count > 0)
                {
                    if (base.EnvelopeBuffer != null)
                    {
                        if (!object.ReferenceEquals(buffer, base.EnvelopeBuffer))
                        {
                            Buffer.BlockCopy(buffer, offset, base.EnvelopeBuffer, base.EnvelopeOffset, count);
                        }
                        base.EnvelopeOffset += count;
                    }
                    offset += count;
                    size   -= count;
                }
                switch (this.decoder.CurrentState)
                {
                case ClientFramingDecoderState.EnvelopeStart:
                    envelopeSize = this.decoder.EnvelopeSize;
                    if (envelopeSize > this.maxBufferSize)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException((long)this.maxBufferSize));
                    }
                    break;

                case ClientFramingDecoderState.ReadingEnvelopeBytes:
                case ClientFramingDecoderState.ReadingEndRecord:
                {
                    continue;
                }

                case ClientFramingDecoderState.EnvelopeEnd:
                {
                    if (base.EnvelopeBuffer == null)
                    {
                        continue;
                    }
                    Message message = null;
                    try
                    {
                        using (CreateProcessActionActivity())
                        {
                            message = this.messageEncoder.ReadMessage(new ArraySegment <byte>(base.EnvelopeBuffer, 0, base.EnvelopeSize), this.bufferManager);
                            if (DiagnosticUtility.ShouldUseActivity)
                            {
                                TraceUtility.TransferFromTransport(message);
                            }
                        }
                    }
                    catch (XmlException exception)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
                    }
                    base.EnvelopeBuffer = null;
                    return(message);
                }

                case ClientFramingDecoderState.End:
                    isAtEOF = true;
                    return(null);

                case ClientFramingDecoderState.Fault:
                    this.channel.Session.CloseOutputSession(this.channel.InternalCloseTimeout);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(FaultStringDecoder.GetFaultException(this.decoder.Fault, this.channel.RemoteAddress.Uri.ToString(), this.messageEncoder.ContentType));

                default:
                {
                    continue;
                }
                }
                base.EnvelopeBuffer = this.bufferManager.TakeBuffer(envelopeSize);
                base.EnvelopeOffset = 0;
                base.EnvelopeSize   = envelopeSize;
            }
            return(null);
        }
                protected override Message DecodeMessage(byte[] buffer, ref int offset, ref int size, ref bool isAtEof, TimeSpan timeout)
                {
                    while (!isAtEof && (size > 0))
                    {
                        int envelopeSize;
                        int count = this.decoder.Decode(buffer, offset, size);
                        if (count > 0)
                        {
                            if (base.EnvelopeBuffer != null)
                            {
                                if (!object.ReferenceEquals(buffer, base.EnvelopeBuffer))
                                {
                                    Buffer.BlockCopy(buffer, offset, base.EnvelopeBuffer, base.EnvelopeOffset, count);
                                }
                                base.EnvelopeOffset += count;
                            }
                            offset += count;
                            size -= count;
                        }
                        switch (this.decoder.CurrentState)
                        {
                            case ServerSessionDecoder.State.EnvelopeStart:
                                envelopeSize = this.decoder.EnvelopeSize;
                                if (envelopeSize > this.maxBufferSize)
                                {
                                    base.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/MaxMessageSizeExceededFault", timeout);
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException((long) this.maxBufferSize));
                                }
                                break;

                            case ServerSessionDecoder.State.ReadingEnvelopeBytes:
                            case ServerSessionDecoder.State.ReadingEndRecord:
                            {
                                continue;
                            }
                            case ServerSessionDecoder.State.EnvelopeEnd:
                            {
                                if (base.EnvelopeBuffer == null)
                                {
                                    continue;
                                }
                                using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
                                {
                                    if (DiagnosticUtility.ShouldUseActivity)
                                    {
                                        ServiceModelActivity.Start(activity, System.ServiceModel.SR.GetString("ActivityProcessingMessage", new object[] { TraceUtility.RetrieveMessageNumber() }), ActivityType.ProcessMessage);
                                    }
                                    Message message = null;
                                    try
                                    {
                                        message = this.messageEncoder.ReadMessage(new ArraySegment<byte>(base.EnvelopeBuffer, 0, base.EnvelopeSize), this.bufferManager, this.contentType);
                                    }
                                    catch (XmlException exception)
                                    {
                                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
                                    }
                                    if (DiagnosticUtility.ShouldUseActivity)
                                    {
                                        TraceUtility.TransferFromTransport(message);
                                    }
                                    base.EnvelopeBuffer = null;
                                    return message;
                                }
                            }
                            case ServerSessionDecoder.State.End:
                                goto Label_01A8;

                            default:
                            {
                                continue;
                            }
                        }
                        base.EnvelopeBuffer = this.bufferManager.TakeBuffer(envelopeSize);
                        base.EnvelopeOffset = 0;
                        base.EnvelopeSize = envelopeSize;
                        continue;
                    Label_01A8:
                        isAtEof = true;
                    }
                    return null;
                }
Esempio n. 22
0
        Message ReadStreamedMessage(Stream inputStream)
        {
            MaxMessageSizeStream maxMessageSizeStream = new MaxMessageSizeStream(inputStream, settings.MaxReceivedMessageSize);

            try
            {
                return messageEncoder.ReadMessage(maxMessageSizeStream, settings.MaxBufferSize, ContentType);
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new ProtocolException(SR.GetString(SR.MessageXmlProtocolError), xmlException));
            }
        }
        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);
        }