public async Task <(Message, Stream)> ReceiveAsync(FramingConnection connection, TimeSpan timeout)
        {
            TimeoutHelper           timeoutHelper = new TimeoutHelper(timeout);
            ReadOnlySequence <byte> buffer        = ReadOnlySequence <byte> .Empty;

            for (; ;)
            {
                var readResult = await connection.Input.ReadAsync();

                await Task.Yield();

                if (readResult.IsCompleted || readResult.Buffer.Length == 0)
                {
                    if (!readResult.IsCompleted)
                    {
                        connection.Input.AdvanceTo(readResult.Buffer.Start);
                    }
                    //EnsureDecoderAtEof(connection);
                    connection.EOF = true;
                }

                if (connection.EOF)
                {
                    return(null, null);
                }

                buffer = readResult.Buffer;
                bool atEnvelopeStart = DecodeBytes(connection, ref buffer);
                connection.Input.AdvanceTo(buffer.Start);
                if (atEnvelopeStart)
                {
                    break;
                }


                if (connection.EOF)
                {
                    return(null, null);
                }
            }

            // we're ready to read a message
            Stream connectionStream = new SingletonInputConnectionStream(connection, connection.ServiceDispatcher.Binding);
            Stream inputStream      = new MaxMessageSizeStream(connectionStream, connection.MaxReceivedMessageSize);
            //using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
            //{
            //    if (DiagnosticUtility.ShouldUseActivity)
            //    {
            //        ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage);
            //    }

            Message message = null;

            try
            {
                message = await connection.MessageEncoderFactory.Encoder.ReadMessageAsync(
                    inputStream, connection.MaxBufferSize, connection.FramingDecoder.ContentType);
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SR.MessageXmlProtocolError, xmlException));
            }

            //if (DiagnosticUtility.ShouldUseActivity)
            //{
            //    TraceUtility.TransferFromTransport(message);
            //}

            PrepareMessage(connection, message);

            return(message, inputStream);
            //}
        }
示例#2
0
        public async Task <Message> ReceiveAsync(TimeoutHelper timeoutHelper)
        {
            byte[] buffer = Fx.AllocateByteArray(_connection.AsyncReadBufferSize);

            if (_size > 0)
            {
                Buffer.BlockCopy(_connection.AsyncReadBuffer, _offset, buffer, _offset, _size);
            }

            for (;;)
            {
                if (DecodeBytes(buffer, ref _offset, ref _size, ref _isAtEof))
                {
                    break;
                }

                if (_isAtEof)
                {
                    DoneReceiving(true, timeoutHelper.RemainingTime());
                    return(null);
                }

                if (_size == 0)
                {
                    _offset = 0;
                    _size   = await _connection.ReadAsync(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());

                    if (_size == 0)
                    {
                        DoneReceiving(true, timeoutHelper.RemainingTime());
                        return(null);
                    }
                }
            }

            // we're ready to read a message
            IConnection singletonConnection = _connection;

            if (_size > 0)
            {
                byte[] initialData = Fx.AllocateByteArray(_size);
                Buffer.BlockCopy(buffer, _offset, initialData, 0, _size);
                singletonConnection = new PreReadConnection(singletonConnection, initialData);
            }

            Stream connectionStream = new SingletonInputConnectionStream(this, singletonConnection, _transportSettings);

            _inputStream = new MaxMessageSizeStream(connectionStream, _transportSettings.MaxReceivedMessageSize);
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.Format(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage);
                }

                Message message = null;
                try
                {
                    message = await _transportSettings.MessageEncoderFactory.Encoder.ReadMessageAsync(
                        _inputStream, _transportSettings.MaxBufferSize, this.ContentType);
                }
                catch (XmlException xmlException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                              new ProtocolException(SR.Format(SR.MessageXmlProtocolError), xmlException));
                }

                if (DiagnosticUtility.ShouldUseActivity)
                {
                    TraceUtility.TransferFromTransport(message);
                }

                PrepareMessage(message);

                return(message);
            }
        }
示例#3
0
        public Message Receive(TimeSpan timeout)
        {
            byte[] dst = DiagnosticUtility.Utility.AllocateByteArray(this.connection.AsyncReadBufferSize);
            if (this.size > 0)
            {
                Buffer.BlockCopy(this.connection.AsyncReadBuffer, this.offset, dst, this.offset, this.size);
            }
            TimeoutHelper helper = new TimeoutHelper(timeout);

            while (!this.DecodeBytes(dst, ref this.offset, ref this.size, ref this.isAtEof))
            {
                if (this.isAtEof)
                {
                    this.DoneReceiving(true, helper.RemainingTime());
                    return(null);
                }
                if (this.size == 0)
                {
                    this.offset = 0;
                    this.size   = this.connection.Read(dst, 0, dst.Length, helper.RemainingTime());
                    if (this.size == 0)
                    {
                        this.DoneReceiving(true, helper.RemainingTime());
                        return(null);
                    }
                }
            }
            IConnection innerConnection = this.connection;

            if (this.size > 0)
            {
                byte[] buffer2 = DiagnosticUtility.Utility.AllocateByteArray(this.size);
                Buffer.BlockCopy(dst, this.offset, buffer2, 0, this.size);
                innerConnection = new PreReadConnection(innerConnection, buffer2);
            }
            Stream stream = new SingletonInputConnectionStream(this, innerConnection, this.transportSettings);

            this.inputStream = new MaxMessageSizeStream(stream, this.transportSettings.MaxReceivedMessageSize);
            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.transportSettings.MessageEncoderFactory.Encoder.ReadMessage(this.inputStream, this.transportSettings.MaxBufferSize, this.ContentType);
                }
                catch (XmlException exception)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
                }
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    TraceUtility.TransferFromTransport(message);
                }
                this.PrepareMessage(message);
                return(message);
            }
        }
        public async Task<Message> ReceiveAsync(TimeoutHelper timeoutHelper)
        {
            byte[] buffer = Fx.AllocateByteArray(_connection.AsyncReadBufferSize);

            if (_size > 0)
            {
                Buffer.BlockCopy(_connection.AsyncReadBuffer, _offset, buffer, _offset, _size);
            }

            for (;;)
            {
                if (DecodeBytes(buffer, ref _offset, ref _size, ref _isAtEof))
                {
                    break;
                }

                if (_isAtEof)
                {
                    DoneReceiving(true, timeoutHelper.RemainingTime());
                    return null;
                }

                if (_size == 0)
                {
                    _offset = 0;
                    _size = await _connection.ReadAsync(buffer, 0, buffer.Length, timeoutHelper.RemainingTime());
                    if (_size == 0)
                    {
                        DoneReceiving(true, timeoutHelper.RemainingTime());
                        return null;
                    }
                }
            }

            // we're ready to read a message
            IConnection singletonConnection = _connection;
            if (_size > 0)
            {
                byte[] initialData = Fx.AllocateByteArray(_size);
                Buffer.BlockCopy(buffer, _offset, initialData, 0, _size);
                singletonConnection = new PreReadConnection(singletonConnection, initialData);
            }

            Stream connectionStream = new SingletonInputConnectionStream(this, singletonConnection, _transportSettings);
            _inputStream = new MaxMessageSizeStream(connectionStream, _transportSettings.MaxReceivedMessageSize);
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity(true) : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.Format(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage);
                }

                Message message = null;
                try
                {
                    message = await _transportSettings.MessageEncoderFactory.Encoder.ReadMessageAsync(
                        _inputStream, _transportSettings.MaxBufferSize, this.ContentType);
                }
                catch (XmlException xmlException)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        new ProtocolException(SR.Format(SR.MessageXmlProtocolError), xmlException));
                }

                if (DiagnosticUtility.ShouldUseActivity)
                {
                    TraceUtility.TransferFromTransport(message);
                }

                PrepareMessage(message);

                return message;
            }
        }
 public Message Receive(TimeSpan timeout)
 {
     byte[] dst = DiagnosticUtility.Utility.AllocateByteArray(this.connection.AsyncReadBufferSize);
     if (this.size > 0)
     {
         Buffer.BlockCopy(this.connection.AsyncReadBuffer, this.offset, dst, this.offset, this.size);
     }
     TimeoutHelper helper = new TimeoutHelper(timeout);
     while (!this.DecodeBytes(dst, ref this.offset, ref this.size, ref this.isAtEof))
     {
         if (this.isAtEof)
         {
             this.DoneReceiving(true, helper.RemainingTime());
             return null;
         }
         if (this.size == 0)
         {
             this.offset = 0;
             this.size = this.connection.Read(dst, 0, dst.Length, helper.RemainingTime());
             if (this.size == 0)
             {
                 this.DoneReceiving(true, helper.RemainingTime());
                 return null;
             }
         }
     }
     IConnection innerConnection = this.connection;
     if (this.size > 0)
     {
         byte[] buffer2 = DiagnosticUtility.Utility.AllocateByteArray(this.size);
         Buffer.BlockCopy(dst, this.offset, buffer2, 0, this.size);
         innerConnection = new PreReadConnection(innerConnection, buffer2);
     }
     Stream stream = new SingletonInputConnectionStream(this, innerConnection, this.transportSettings);
     this.inputStream = new MaxMessageSizeStream(stream, this.transportSettings.MaxReceivedMessageSize);
     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.transportSettings.MessageEncoderFactory.Encoder.ReadMessage(this.inputStream, this.transportSettings.MaxBufferSize, this.ContentType);
         }
         catch (XmlException exception)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageXmlProtocolError"), exception));
         }
         if (DiagnosticUtility.ShouldUseActivity)
         {
             TraceUtility.TransferFromTransport(message);
         }
         this.PrepareMessage(message);
         return message;
     }
 }
示例#6
0
        public async Task <Message> ReceiveAsync(CancellationToken token)
        {
            //byte[] buffer = Fx.AllocateByteArray(connection.AsyncReadBufferSize);

            //if (size > 0)
            //{
            //    Buffer.BlockCopy(connection.AsyncReadBuffer, offset, buffer, offset, size);
            //}
            var timeoutHelper = new TimeoutHelper(TimeoutHelper.GetOriginalTimeout(token));

            for (;;)
            {
                if (DecodeBytes(connection.AsyncReadBuffer, ref offset, ref size, ref isAtEof))
                {
                    break;
                }

                if (isAtEof)
                {
                    await DoneReceivingAsync(true, token);

                    return(null);
                }

                if (size == 0)
                {
                    offset = 0;
                    size   = await connection.ReadAsync(0, connection.AsyncReadBufferSize, timeoutHelper.RemainingTime());

                    if (size == 0)
                    {
                        await DoneReceivingAsync(true, token);

                        return(null);
                    }
                }
            }

            // we're ready to read a message
            IConnection singletonConnection = connection;

            if (size > 0)
            {
                singletonConnection = new PreReadConnection(singletonConnection, offset, size);
            }

            Stream connectionStream = new SingletonInputConnectionStream(this, singletonConnection, transportSettings);

            inputStream = new MaxMessageSizeStream(connectionStream, transportSettings.MaxReceivedMessageSize);
            Message message = null;

            try
            {
                message = transportSettings.MessageEncoderFactory.Encoder.ReadMessage(
                    inputStream, transportSettings.MaxBufferSize, ContentType);
            }
            catch (XmlException xmlException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SR.Format(SR.MessageXmlProtocolError), xmlException));
            }

            PrepareMessage(message);

            return(message);
        }