/// <summary> /// Считать строки /// </summary> public override List <string> ReadLines(int timeout, TextStopCondition stopCond, out bool stopReceived, out string logText) { try { List <string> lines = new List <string>(); stopReceived = false; DateTime nowDT = DateTime.Now; DateTime startDT = nowDT; DateTime stopDT = startDT.AddMilliseconds(timeout); IPEndPoint endPoint = CreateIPEndPoint(); UdpClient.Client.ReceiveTimeout = DatagramReceiveTimeout; while (!stopReceived && startDT <= nowDT && nowDT <= stopDT) { // считывание данных int readPos; bool isNew; byte[] datagram = ReceiveDatagram(ref endPoint, out readPos, out isNew); if (datagram != null && datagram.Length > 0) { // получение строк из считанных данных int datagramLen = datagram.Length; StringBuilder sbLine = new StringBuilder(datagramLen); while (readPos < datagramLen && !stopReceived) { sbLine.Append(Encoding.Default.GetChars(datagram, readPos, 1)); readPos++; bool newLineFound = StringBuilderEndsWith(sbLine, NewLine); if (newLineFound || readPos == datagramLen) { string line = newLineFound ? sbLine.ToString(0, sbLine.Length - NewLine.Length) : sbLine.ToString(); lines.Add(line); sbLine.Clear(); stopReceived = stopCond.CheckCondition(lines, line); } } } // накопление данных во внутреннем буфере соединения if (!stopReceived && isNew) { Thread.Sleep(DataAccumThreadDelay); } StoreDatagram(datagram, readPos); nowDT = DateTime.Now; } logText = BuildReadLinesLogText(lines); return(lines); } catch (Exception ex) { throw new InvalidOperationException(CommPhrases.ReadLinesError + ": " + ex.Message, ex); } }
/// <summary> /// Считать строки /// </summary> public abstract List <string> ReadLines(int timeout, TextStopCondition stopCond, out bool stopReceived, out string logText);
/// <summary> /// Считать строки /// </summary> public override List <string> ReadLines(int timeout, TextStopCondition stopCond, out bool stopReceived, out string logText) { try { List <string> lines = new List <string>(); stopReceived = false; DateTime utcNowDT = DateTime.UtcNow; DateTime startDT = utcNowDT; DateTime stopDT = startDT.AddMilliseconds(timeout); NetStream.ReadTimeout = OneByteReadTimeout; StringBuilder sbLine = new StringBuilder(maxLineSize); byte[] buffer = new byte[1]; while (!stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT) { // считывание одного байта данных bool readOk; try { readOk = NetStream.DataAvailable && NetStream.Read(buffer, 0, 1) > 0; } catch (IOException) { readOk = false; } if (readOk) { sbLine.Append(Encoding.Default.GetChars(buffer)); } else { // накопление данных во внутреннем буфере соединения Thread.Sleep(DataAccumThreadDelay); } bool newLineFound = StringBuilderEndsWith(sbLine, NewLine); if (newLineFound || sbLine.Length == maxLineSize) { string line = newLineFound ? sbLine.ToString(0, sbLine.Length - NewLine.Length) : sbLine.ToString(); lines.Add(line); sbLine.Clear(); stopReceived = stopCond.CheckCondition(lines, line); } utcNowDT = DateTime.UtcNow; } logText = BuildReadLinesLogText(lines); if (lines.Count > 0) { UpdateActivityDT(); } return(lines); } catch (Exception ex) { throw new InvalidOperationException(CommPhrases.ReadLinesError + ": " + ex.Message, ex); } }