/// <summary> /// Waits for new data to arrive that can be used to recheck for new data. /// </summary> /// <param name="sbuffer">The byte buffer to read from.</param> /// <param name="timeout">The time out in milliseconds.</param> /// <returns><c>true</c> if one more byte is available since the last <see cref="ReadTo"/> /// call; <c>false</c> otherwise</returns> public bool ReadToWaitForNewData(SerialBuffer sbuffer, int timeout) { return(sbuffer.Stream.WaitForRead(m_ReadOffset + 1, timeout)); }
/// <summary> /// Reads from the cached and byte stream looking for the text specified. /// </summary> /// <param name="sbuffer">The byte buffer to read from.</param> /// <param name="text">The text to indicate where the read operation stops.</param> /// <param name="line">On success, contains the line up to the text string requested.</param> /// <returns><c>true</c> if a line was found; <c>false</c> otherwise.</returns> public bool ReadTo(SerialBuffer sbuffer, string text, out string line) { bool changedText = !text.Equals(m_ReadToString); if (changedText) { if (Log.ReadToTrace(System.Diagnostics.TraceEventType.Verbose)) { Log.ReadTo.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "ReadTo: Text changed!"); } m_ReadToString = text; if (IsOverflowed) { Reset(true); } int readLen = m_ReadCache.Length; if (readLen >= text.Length) { // Check if the text already exists string lbuffer = m_ReadCache.GetString(); int p = lbuffer.IndexOf(text, StringComparison.Ordinal); if (p != -1) { if (Log.ReadToTrace(System.Diagnostics.TraceEventType.Verbose)) { Log.ReadTo.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "ReadTo: Text changed! And Found!"); } // It does exist, so consume up to the buffered portion line = lbuffer.Substring(0, p); int l = p + text.Length; ReadToConsume(sbuffer, l); return(true); } } } else { if (Log.ReadToTrace(System.Diagnostics.TraceEventType.Verbose)) { Log.ReadTo.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "ReadTo: No reset, text the same."); } } lock (sbuffer.ReadLock) { while (!ReadToMatch(text)) { // Decoders in .NET are designed for streams and not really for reading // a little bit of data. By design, they are "greedy", they consume as much // byte data as possible. The data that they consume is cached internally. // Because it's not possible to ask the decoder only to decode if there // is sufficient bytes, we have to keep account of how many bytes are // consumed for each character. bool newChar = PeekChar(sbuffer); if (!newChar) { // Didn't find the string and there's no new data. line = null; return(false); } } // Found the string if (Log.ReadToTrace(System.Diagnostics.TraceEventType.Verbose)) { Log.ReadTo.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "ReadTo: Found! Discarding {0} bytes", m_ReadOffset); } sbuffer.Serial.ReadBuffer.Consume(m_ReadOffset); } line = m_ReadCache.GetString(m_ReadCache.Length - text.Length); Reset(false); return(true); }