public override int Read(byte[] buffer, int offset, int count)
            {
                int num = 0;

                while (count != 0)
                {
                    if (this.atEof)
                    {
                        return(num);
                    }
                    if (this.chunkBufferSize <= 0)
                    {
                        if (this.chunkBytesRemaining > 0)
                        {
                            int num1 = this.ReadCore(buffer, offset, Math.Min(count, this.chunkBytesRemaining + 5));
                            this.DecodeData(buffer, offset, Math.Min(num1, this.chunkBytesRemaining));
                            if (num1 <= this.chunkBytesRemaining)
                            {
                                num = num + num1;
                                Microsoft.ServiceBus.Channels.SingletonConnectionReader.SingletonInputConnectionStream singletonInputConnectionStream = this;
                                singletonInputConnectionStream.chunkBytesRemaining = singletonInputConnectionStream.chunkBytesRemaining - num1;
                            }
                            else
                            {
                                num = num + this.chunkBytesRemaining;
                                int num2 = num1 - this.chunkBytesRemaining;
                                int num3 = offset + this.chunkBytesRemaining;
                                this.chunkBytesRemaining = 0;
                                this.DecodeSize(buffer, ref num3, ref num2);
                            }
                            return(num);
                        }
                        if (count >= 5)
                        {
                            int num4 = this.ReadCore(buffer, offset, 5);
                            int num5 = offset;
                            this.DecodeSize(buffer, ref num5, ref num4);
                        }
                        else
                        {
                            this.chunkBufferOffset = 0;
                            this.chunkBufferSize   = this.ReadCore(this.chunkBuffer, 0, (int)this.chunkBuffer.Length);
                            this.DecodeSize(this.chunkBuffer, ref this.chunkBufferOffset, ref this.chunkBufferSize);
                        }
                    }
                    else
                    {
                        int num6 = Math.Min(this.chunkBytesRemaining, Math.Min(this.chunkBufferSize, count));
                        Buffer.BlockCopy(this.chunkBuffer, this.chunkBufferOffset, buffer, offset, num6);
                        this.DecodeData(this.chunkBuffer, this.chunkBufferOffset, num6);
                        Microsoft.ServiceBus.Channels.SingletonConnectionReader.SingletonInputConnectionStream singletonInputConnectionStream1 = this;
                        singletonInputConnectionStream1.chunkBufferOffset = singletonInputConnectionStream1.chunkBufferOffset + num6;
                        Microsoft.ServiceBus.Channels.SingletonConnectionReader.SingletonInputConnectionStream singletonInputConnectionStream2 = this;
                        singletonInputConnectionStream2.chunkBufferSize = singletonInputConnectionStream2.chunkBufferSize - num6;
                        Microsoft.ServiceBus.Channels.SingletonConnectionReader.SingletonInputConnectionStream singletonInputConnectionStream3 = this;
                        singletonInputConnectionStream3.chunkBytesRemaining = singletonInputConnectionStream3.chunkBytesRemaining - num6;
                        if (this.chunkBytesRemaining == 0 && this.chunkBufferSize > 0)
                        {
                            this.DecodeSize(this.chunkBuffer, ref this.chunkBufferOffset, ref this.chunkBufferSize);
                        }
                        num    = num + num6;
                        offset = offset + num6;
                        count  = count - num6;
                    }
                }
                return(num);
            }
 public ReadAsyncResult(Microsoft.ServiceBus.Channels.SingletonConnectionReader.SingletonInputConnectionStream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state) : base(callback, state)
 {
     this.parent = parent;
     this.result = this.parent.Read(buffer, offset, count);
     base.Complete(true);
 }
        public Message Receive(TimeSpan timeout)
        {
            Message message;
            ServiceModelActivity    serviceModelActivity;
            SecurityMessageProperty securityMessageProperty;

            byte[] numArray = this.transportSettings.BufferManager.TakeBuffer(this.connection.AsyncReadBufferSize);
            if (this.size > 0)
            {
                Buffer.BlockCopy(this.connection.AsyncReadBuffer, this.offset, numArray, this.offset, this.size);
            }
            try
            {
                TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
                while (!this.DecodeBytes(numArray, ref this.offset, ref this.size, ref this.isAtEof))
                {
                    if (!this.isAtEof)
                    {
                        if (this.size != 0)
                        {
                            continue;
                        }
                        this.offset = 0;
                        this.size   = this.connection.Read(numArray, 0, (int)numArray.Length, timeoutHelper.RemainingTime());
                        if (this.size != 0)
                        {
                            continue;
                        }
                        this.DoneReceiving(true, timeoutHelper.RemainingTime());
                        message = null;
                        return(message);
                    }
                    else
                    {
                        this.DoneReceiving(true, timeoutHelper.RemainingTime());
                        message = null;
                        return(message);
                    }
                }
                Microsoft.ServiceBus.Channels.IConnection preReadConnection = this.connection;
                if (this.size > 0)
                {
                    byte[] numArray1 = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.Utility.AllocateByteArray(this.size);
                    Buffer.BlockCopy(numArray, this.offset, numArray1, 0, this.size);
                    preReadConnection = new Microsoft.ServiceBus.Channels.PreReadConnection(preReadConnection, numArray1);
                }
                Stream singletonInputConnectionStream = new Microsoft.ServiceBus.Channels.SingletonConnectionReader.SingletonInputConnectionStream(this, preReadConnection, this.transportSettings);
                this.inputStream = new Microsoft.ServiceBus.Channels.MaxMessageSizeStream(singletonInputConnectionStream, this.transportSettings.MaxReceivedMessageSize);
                if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldUseActivity)
                {
                    serviceModelActivity = ServiceModelActivity.CreateBoundedActivity(true);
                }
                else
                {
                    serviceModelActivity = null;
                }
                using (serviceModelActivity)
                {
                    Message message1 = null;
                    try
                    {
                        message1 = this.transportSettings.MessageEncoderFactory.Encoder.ReadMessage(this.inputStream, this.transportSettings.MaxBufferSize, this.ContentType);
                    }
                    catch (XmlException xmlException1)
                    {
                        XmlException xmlException = xmlException1;
                        throw Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(Resources.MessageXmlProtocolError, new object[0]), xmlException));
                    }
                    message1.Properties.Via = this.via;
                    MessageProperties properties = message1.Properties;
                    if (this.security != null)
                    {
                        securityMessageProperty = (SecurityMessageProperty)this.security.CreateCopy();
                    }
                    else
                    {
                        securityMessageProperty = null;
                    }
                    properties.Security = securityMessageProperty;
                    this.PrepareMessage(message1);
                    message = message1;
                }
            }
            finally
            {
                this.transportSettings.BufferManager.ReturnBuffer(numArray);
            }
            return(message);
        }