Example #1
0
        /// <summary>
        /// Begins line reading.
        /// </summary>
        /// <param name="op">Read line opeartion.</param>
        /// <param name="async">If true then this method can complete asynchronously. If false, this method completed always syncronously.</param>
        /// <returns>Returns true if read line completed synchronously, false if asynchronous operation pending.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>op</b> is null reference.</exception>
        public bool ReadLine(ReadLineAsyncOP op,bool async)
        {
            if(op == null){
                throw new ArgumentNullException("op");
            }

            #region async

            if(async){
                return op.Start(async,this);
            }

            #endregion

            #region sync

            else{
                byte[]             buffer         = op.Buffer;
                int                bytesInBuffer  = 0;
                int                lastByte       = -1;
                bool               CRLFLinesOnly  = true;
                int                lineBuffSize   = buffer.Length;
                SizeExceededAction exceededAction = op.SizeExceededAction;
                Exception          exception      = null;

                try{
                    while(true){
                        // Read buffer empty, buff next data block.
                        if(m_ReadBufferOffset >= m_ReadBufferCount){
                            this.BufferRead(false,null);

                            // We reached end of stream, no more data.
                            if(m_ReadBufferCount == 0){
                                break;
                            }
                        }

                        byte b = m_pReadBuffer[m_ReadBufferOffset++];

                        // Line buffer full.
                        if(bytesInBuffer >= lineBuffSize){
                            if(exception == null){
                                exception = new LineSizeExceededException();
                            }

                            if(exceededAction == SizeExceededAction.ThrowException){
                                break;
                            }
                        }
                        // Store byte.
                        else{
                            buffer[bytesInBuffer++] = b;
                        }

                        // We have LF line.
                        if(b == '\n'){
                            if(!CRLFLinesOnly || CRLFLinesOnly && lastByte == '\r'){
                                break;
                            }
                        }

                        lastByte = b;
                    }
                }
                catch(Exception x){
                    exception = x;
                }

                // Set read line operation result data.
                op.SetInfo(bytesInBuffer,exception);

                return true;
            }

            #endregion
        }
Example #2
0
            /// <summary>
            /// Starts/continues line reading.
            /// </summary>
            /// <param name="async">If true then this method can complete asynchronously. If false, this method completed always syncronously.</param>
            /// <returns>Returns true if line reading completed.</returns>
            private bool DoLineReading(bool async)
            {
                try{
                    while(true){
                        // Read buffer empty, buff next data block.
                        if(m_pOwner.BytesInReadBuffer == 0){
                            // Buffering started asynchronously.
                            if(m_pOwner.BufferRead(async,this.Buffering_Completed)){
                                return false;
                            }
                            // Buffering completed synchronously, continue processing.
                            else{
                                // We reached end of stream, no more data.
                                if(m_pOwner.BytesInReadBuffer == 0){
                                    return true;
                                }
                            }
                        }

                        byte b = m_pOwner.m_pReadBuffer[m_pOwner.m_ReadBufferOffset++];

                        // Line buffer full.
                        if(m_BytesInBuffer >= m_pBuffer.Length){
                            if(m_pException == null){
                                m_pException = new LineSizeExceededException();
                            }

                            if(m_ExceededAction == SizeExceededAction.ThrowException){
                                return true;
                            }
                        }
                        // Store byte.
                        else{
                            m_pBuffer[m_BytesInBuffer++] = b;
                        }

                        // We have LF line.
                        if(b == '\n'){
                            if(!m_CRLFLinesOnly || m_CRLFLinesOnly && m_LastByte == '\r'){
                                m_IsCompleted = true;
                                return true;
                            }
                        }

                        m_LastByte = b;
                    }
                }
                catch(Exception x){
                    m_pException = x;
                }

                return true;
            }