Exemplo n.º 1
0
        /// <summary>
        /// Main server thread
        /// </summary>
        private void WorkerThread()
        {
            DateTime dtLastActivityReportSent = DateTime.Now;
            TimeSpan tsGuardTime = TimeSpan.FromSeconds(15);

            while (!m_evStop.WaitOne(500))
            {
                lock (m_tcpListener)
                {
                    if (m_AcceptIncommingConnections)
                    {
                        if (m_tcpListener.Pending())
                        {
                            try
                            {
                                AgentRelay agent = AgentRelay.FromClient(m_tcpListener.AcceptTcpClient());
                                if (agent.IsFaulty)
                                {
                                    agent.Disconnect();
                                    agent = null;
                                }
                                else
                                {
                                    if (OnNewAgentConnected != null)
                                    {
                                        OnNewAgentConnected(agent);
                                    }

                                    // Append it to agents list
                                    lock (m_Agents)
                                    {
                                        m_Agents.Add(agent);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (OnServerFailedToAcceptConnection != null)
                                {
                                    OnServerFailedToAcceptConnection(ex);
                                }
                            }
                        }
                    }
                } // lock

                // Remove Faulties 91/08/01
                for (int i = 0; i < m_Agents.Count; i++)
                {
                    if (m_Agents[i].IsFaulty)
                    {
                        m_Agents[i].Disconnect();
                        m_Agents[i] = null;
                        m_Agents.RemoveAt(i);
                        --i;
                    }
                }

                // Report clients about us
                if (m_bEnableAutoHandshake && (DateTime.Now - dtLastActivityReportSent > tsGuardTime))
                {
                    for (int i = 0; i < m_Agents.Count; i++)
                    {
                        // It is clear, if the other side is not accessable, it will become Faulty
                        // so there is no need to check handshake result for that.
                        try { m_Agents[i].StartHandshakeAsync(); }
                        catch { }
                    }
                    dtLastActivityReportSent = DateTime.Now;
                }
            }

            // Close all connections and cleanup...
            if (m_bAutoCleanupConnections)
            {
                lock (m_Agents)
                {
                    for (int i = 0; i < m_Agents.Count; i++)
                    {
                        m_Agents[i].Dispose();
                        m_Agents[i] = null;
                    }
                    m_Agents.Clear();
                }
            }
        }