/// <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"); } if (!op.Start(async, this)) { /* REMOVE ME: if(!async){ // Wait while async operation completes. while(!op.IsCompleted){ Thread.Sleep(1); } return true; } else{ return false; }*/ return false; } // Completed synchronously. else { return true; } }
/// <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 }