예제 #1
0
 /// <summary>
 /// Считать строки и вывести информацию в журнал
 /// </summary>
 public virtual List<string> ReadLines(int timeout, TextStopCondition stopCond,
     out bool stopReceived)
 {
     string logText;
     List<string> lines = ReadLines(timeout, stopCond, out stopReceived, out logText);
     WriteToLog(logText);
     return lines;
 }
예제 #2
0
 /// <summary>
 /// Считать строки
 /// </summary>
 public abstract List<string> ReadLines(int timeout, TextStopCondition stopCond,
     out bool stopReceived, out string logText);
예제 #3
0
        /// <summary>
        /// Reads lines.
        /// </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(MaxLineLength);
                byte[]        buffer = new byte[1];

                while (!stopReceived && startDT <= utcNowDT && utcNowDT <= stopDT)
                {
                    // read one byte
                    bool readOk;
                    try { readOk = NetStream.DataAvailable && NetStream.Read(buffer, 0, 1) > 0; }
                    catch (IOException) { readOk = false; }

                    if (readOk)
                    {
                        sbLine.Append(Encoding.GetChars(buffer));
                    }
                    else
                    {
                        Thread.Sleep(DataAccumDelay);
                    }

                    bool newLineFound = sbLine.EndsWith(NewLine);

                    if (newLineFound || sbLine.Length == MaxLineLength)
                    {
                        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)
                {
                    UpdateActivityTime();
                }

                return(lines);
            }
            catch (Exception ex)
            {
                throw new ScadaException(CommPhrases.ReadLinesError + ": " + ex.Message, ex);
            }
        }
예제 #4
0
        /// <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);
                NetStream.ReadTimeout = OneByteReadTimeout;

                StringBuilder sbLine = new StringBuilder(maxLineSize);
                byte[] buffer = new byte[1];

                while (!stopReceived && startDT <= nowDT && nowDT <= stopDT)
                {
                    // считывание одного байта данных
                    bool readOk;
                    try { readOk = 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);
                    }

                    nowDT = DateTime.Now;
                }

                logText = BuildReadLinesLogText(lines);

                if (lines.Count > 0)
                    UpdateActivityDT();

                return lines;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(CommPhrases.ReadLinesError + ": " + ex.Message, ex);
            }
        }
예제 #5
0
        /// <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);
            }
        }