/// <summary> /// 读取一行 /// </summary> /// <typeparam name="TReadState"></typeparam> /// <param name="callback"></param> /// <param name="state"></param> protected void ReadLine <TReadState>(ReadLineCallback <TReadState> callback, TReadState state) { if (packet != null && packetOffset < packet.Size) { var packet = this.packet.Buffer; for (; packetOffset < this.packet.Size; packetOffset++) { if (bufferOffset == MaxLineSize) // 读取至超出最大行数据时,中止管道处理 { Trace.TraceError("line size is over " + MaxLineSize); pipeline.Interrupte(); return; } if (packet[packetOffset] == '\n' && bufferOffset > 0 && buffer[bufferOffset - 1] == '\r') // 解析至 \r\n { packetOffset++; // packet缓冲区指针向后移动指向 \n 的后续字符 var length = bufferOffset - 1; /*char \r*/ bufferOffset = 0; // 重置缓冲 callback(buffer, length, state); return; } else { buffer[bufferOffset++] = (char)packet[packetOffset]; } } } ReadPaket(OnReadLine <TReadState>, new object[] { callback, state }); }
public static bool IsInputChar(char c) => Char.IsLetterOrDigit(c) || Char.IsPunctuation(c) || Char.IsWhiteSpace(c);// || Char.IsSymbol(c); public static string ReadLine_esc(string input = "", bool newLine = true, ReadLineCallback callback = null) { string v = input; ReadLine_esc(ref v, input, newLine, callback); return(v); }
/// <summary> /// Starts reading line from source stream. /// </summary> /// <param name="buffer">Buffer where to store line data.</param> /// <param name="exceededAction">Specifies how this method behaves when maximum line size exceeded.</param> /// <param name="tag">User data.</param> /// <param name="callback">Callback to be called whan asynchronous operation completes.</param> /// <exception cref="ArgumentNullException">Raised when <b>buffer</b> is null.</exception> /// <exception cref="InvalidOperationException">Raised when there already is pending read operation.</exception> public void BeginReadLine(byte[] buffer,SizeExceededAction exceededAction,object tag,ReadLineCallback callback) { if(buffer == null){ throw new ArgumentNullException("buffer"); } lock(this){ if(m_IsReadActive){ throw new InvalidOperationException("There is pending read operation, multiple read operations not allowed !"); } m_IsReadActive = false; } BeginReadLineInternal(buffer,exceededAction,tag,callback,true,true); }
/// <summary> /// Starts reading line from source stream. /// </summary> /// <param name="buffer">Buffer where to store line data.</param> /// <param name="tag">User data.</param> /// <param name="callback">Callback to be called whan asynchronous operation completes.</param> /// <exception cref="ArgumentNullException">Raised when <b>buffer</b> is null.</exception> /// <exception cref="InvalidOperationException">Raised when there already is pending read operation.</exception> public void BeginReadLine(byte[] buffer,object tag,ReadLineCallback callback) { BeginReadLine(buffer,SizeExceededAction.ThrowException,tag,callback); }
/// <summary> /// Starts reading line from source stream. This method does not do any checks and read locks. /// </summary> /// <param name="buffer">Buffer where to store line data.</param> /// <param name="exceededAction">Specifies how this method behaves when maximum line size exceeded.</param> /// <param name="tag">User data.</param> /// <param name="callback">Callback to be called whan asynchronous operation completes.</param> /// <param name="unlockRead">Specifies if read lock is released.</param> /// <param name="log">User data.</param> private void BeginReadLineInternal(byte[] buffer,SizeExceededAction exceededAction,object tag,ReadLineCallback callback,bool unlockRead,bool log) { m_pRLine_LineBuffer = buffer; m_RLine_ExceedAction = exceededAction; m_pRLine_Tag = tag; m_pRLine_Callback = callback; m_RLine_LineBufferOffset = 0; m_RLine_TotalReadedCount = 0; m_RLine_LineBufferSize = buffer.Length; m_RLine_UnlockRead = unlockRead; m_RLine_Log = log; if(this.IsReadBuffered){ DoReadLine_Buffered(); } else{ m_pStream.BeginRead(m_pRLine_ByteBuffer,0,1,new AsyncCallback(this.OnReadByte_Completed),null); } }
public static ConsoleKeyInfo ReadLine_esc_key(ref string value, string input = "", bool newLine = true, ReadLineCallback callback = null) { int left = Console.CursorLeft, pos = 0; StringBuilder buffer = new StringBuilder(); if (input != "") { buffer.Append(input); pos = input.Length; Console.Write(input); } bool callbackExists = callback != null; bool callbackEnter = false; ConsoleKeyInfo key = Console.ReadKey(true); while (key.Key != ConsoleKey.Enter && key.Key != ConsoleKey.Escape) { if (key.Key == ConsoleKey.Backspace && pos > 0) { buffer.Remove(--pos, 1); Console.CursorLeft--; Console.Write(buffer.ToString(pos, buffer.Length - pos) + ' '); Console.CursorLeft = left + pos; } else if (IsInputChar(key.KeyChar)) { if (/*oneLine && */ !(left + pos + 1 < Console.WindowWidth)) { continue; } buffer.Insert(pos++, key.KeyChar); Console.Write(buffer.ToString(pos - 1, buffer.Length - pos + 1)); Console.CursorLeft = left + pos; } else if (key.Key == ConsoleKey.LeftArrow && pos > 0) { Console.CursorLeft--; pos--; } else if (key.Key == ConsoleKey.RightArrow && pos < buffer.Length) { Console.CursorLeft++; pos++; } else if (callbackExists) { ReadLineCallbackResult res = callback(key); if (res == ReadLineCallbackResult.Enter) { callbackEnter = true; break; } if (res == ReadLineCallbackResult.Escape) { break; } } key = Console.ReadKey(true); } if (key.Key == ConsoleKey.Enter || callbackEnter) { if (newLine) { Console.WriteLine(); } value = buffer.ToString(); return(key); } if (input != buffer.ToString()) { Console.CursorLeft = left; Console.Write(input); if (input.Length < buffer.Length) { Console.Write(new string(' ', buffer.Length - input.Length)); } Console.CursorLeft = left + input.Length; } return(key); }
public static bool ReadLine_esc(ref string value, string input = "", bool newLine = true, ReadLineCallback callback = null) => ReadLine_esc_key(ref value, input, newLine, callback).Key == ConsoleKey.Enter;