Example #1
0
        bool ContinueReading()
        {
            while (true)
            {
                if (size == 0)
                {
                    if (readCallback == null)
                    {
                        readCallback = new WaitCallback(ReadCallback);
                    }

                    // if we already have buffered some data we need
                    // to accrue it in case we're duping the connection
                    if (buffer != null)
                    {
                        int dataOffset = 0;
                        if (accruedData == null)
                        {
                            accruedData = new byte[offset];
                        }
                        else
                        {
                            byte[] newAccruedData = new byte[accruedData.Length + offset];
                            Buffer.BlockCopy(accruedData, 0, newAccruedData, 0, accruedData.Length);
                            dataOffset  = this.accruedData.Length;
                            accruedData = newAccruedData;
                        }

                        Buffer.BlockCopy(buffer, 0, accruedData, dataOffset, offset);
                    }

                    if (Connection.BeginRead(0, Connection.AsyncReadBufferSize, GetRemainingTimeout(),
                                             readCallback, this) == AsyncCompletionResult.Queued)
                    {
                        return(false);
                    }
                    GetReadResult();
                }

                while (true)
                {
                    int bytesDecoded = decoder.Decode(buffer, offset, size);
                    if (bytesDecoded > 0)
                    {
                        offset += bytesDecoded;
                        size   -= bytesDecoded;
                    }
                    if (decoder.CurrentState == ServerModeDecoder.State.Done)
                    {
                        return(true);
                    }
                    if (size == 0)
                    {
                        break;
                    }
                }
            }
        }
Example #2
0
        bool ContinueReading()
        {
            for (;;)
            {
                if (size == 0)
                {
                    if (readCallback == null)
                    {
                        readCallback = ReadCallback;
                    }

                    if (Connection.BeginRead(0, Connection.AsyncReadBufferSize, GetRemainingTimeout(),
                                             readCallback, this) == AsyncCompletionResult.Queued)
                    {
                        break;
                    }
                    if (!GetReadResult()) // we're at EOF, bail
                    {
                        return(false);
                    }
                }

                for (;;)
                {
                    int bytesDecoded;
                    try
                    {
                        bytesDecoded = decoder.Decode(buffer, offset, size);
                    }
                    catch (CommunicationException e)
                    {
                        // see if we need to send back a framing fault
                        string framingFault;
                        if (FramingEncodingString.TryGetFaultString(e, out framingFault))
                        {
                            byte[] drainBuffer = new byte[128];
                            InitialServerConnectionReader.SendFault(
                                Connection, framingFault, drainBuffer, GetRemainingTimeout(),
                                MaxViaSize + MaxContentTypeSize);
                            base.Close(GetRemainingTimeout());
                        }
                        throw;
                    }

                    if (bytesDecoded > 0)
                    {
                        offset += bytesDecoded;
                        size   -= bytesDecoded;
                    }
                    if (decoder.CurrentState == ServerModeDecoder.State.Done)
                    {
                        return(true);
                    }
                    if (size == 0)
                    {
                        break;
                    }
                }
            }

            return(false);
        }