コード例 #1
0
        /// <summary>
        /// It will send all connected clients a specific message
        /// </summary>
        /// <param name="cmdCode">can be one of the <code>eResponseTypes</code> codes or any user defined codes</param>
        /// <param name="content">can be null</param>
        /// <param name="excludedAgent">message will not be sent to this specific agent, can be null</param>
        /// <param name="desc"></param>
        public void BroadcastMessage(int cmdCode, string content, AgentRelay excludedAgent)
        {
            lock (m_Agents)
            {
                foreach (AgentRelay agent in m_Agents)
                {
                    if (agent == null || agent.IsFaulty)
                    {
                        continue;
                    }

                    try
                    {
                        if (excludedAgent != null && excludedAgent == agent)
                        {
                            continue;
                        }
                        if (content == null)
                        {
                            agent.SendMessage(cmdCode);
                        }
                        else
                        {
                            agent.SendMessage(cmdCode, content);
                        }
                    }
                    catch { }
                }
            }
        }
コード例 #2
0
        public static AgentRelay FromClient(TcpClient client)
        {
            AgentRelay relay = new AgentRelay();

            relay.m_TcpClient = client;
            relay.m_RawSocket = client.Client;
            relay.StartWorker();
            return(relay);
        }
コード例 #3
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();
                }
            }
        }