Exemplo n.º 1
0
        private void ReceiverTask()
        {
            int knownCount = 0;
            int i          = 0;

            while (!m_stopReceiver)
            {
                var nextWaitTime = 2000;
                lock (m_sync)
                {
                    if (m_currentExecutingCommand != null)
                    {
                        var to = (m_currentExecutingCommand.TimeoutTime - DateTime.Now).Ticks / TimeSpan.TicksPerMillisecond;
                        if (to <= 0)
                        {
                            m_currentExecutingCommand.SetTimeoutResult();
                            AddToLog(LogType.ReceivedError, 0, "<timeout>");
                            OnEndCommand();
                        }
                        else if (to < 3000)
                        {
                            nextWaitTime = (int)to;
                        }
                    }
                }
                if (m_inputBuffer.AwaitNewData(knownCount, nextWaitTime))
                {
                    knownCount = m_inputBuffer.Count;
                    while (i < knownCount)
                    {
                        char ch = m_inputBuffer[i];
                        if (ch == '\n' || ch == '\r')
                        {
                            if (i == 0)
                            {
                                m_inputBuffer.Eat(1);
                                continue;
                            }
                            var line = m_inputBuffer.Get(0, i, i + 1);
                            i          = 0;
                            knownCount = m_inputBuffer.Count;

                            var s = new string(line, 1, line.Length - 1);
                            if (line[0] == EventLineChar)
                            {
                                lock (m_sync)
                                {
                                    AddToLog(LogType.ReceivedAsync, 0, new string(line));
                                }
                                m_events.Enqueue(new string(line, 1, line.Length - 1));
                                while (m_events.Count > 1000)
                                {
                                    m_events.Dequeue();     // Ensure queue buffer is not eating all memory.
                                }
                            }
                            else
                            {
                                if (line[0] == ResponseMultiLineChar)
                                {
                                    lock (m_sync)
                                    {
                                        AddToLog(LogType.ReceivedPartial, 0, new string(line));
                                    }
                                    m_responseLines.Enqueue(s);
                                }
                                else if (line[0] == ResponseEndLineChar)
                                {
                                    var l = new string(line);
                                    lock (m_sync)
                                    {
                                        AddToLog((l.StartsWith(ResponseErrorPrefix)) ? LogType.ReceivedError : LogType.ReceivedEnd, 0, l);
                                    }
                                    if (m_currentExecutingCommand != null)
                                    {
                                        try
                                        {
                                            m_currentExecutingCommand.SetResult(new string(line), m_responseLines.ToArray());
                                        }
                                        finally
                                        {
                                        }
                                    }
                                    OnEndCommand();
                                }
                            }
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                //else
                //{
                //    if (m_currentExecutingCommand != null)
                //    {
                //        var timeTillTimeout = (DateTime.Now - m_currentExecutingCommand.TimeoutTime).Ticks;
                //    }
                //}
            }
        }