Ejemplo n.º 1
0
 /// <summary>
 /// Add a client to the group.
 /// </summary>
 /// <param name="c">The client object to add to the group</param>
 public void AddClient(ClientThread c)
 {
     clients.Add(c);
     if (c.GetGroup() != this)
     {
         c.SetGroup(this);
     }
 }
Ejemplo n.º 2
0
        public void AddClients()
        {
            clients = new List<ClientThread>();
            TcpListener serverSocket = new TcpListener(IPAddress.Any, 8888);
            serverSocket.Start();
            TcpListener heartBeatListener = new TcpListener(IPAddress.Any, 8889);
            heartBeatListener.Start();
            while (true)
            {
                TcpClient tempClientSocket = new TcpClient();
                TcpClient heartBeatSocket = new TcpClient();
                tempClientSocket.ReceiveTimeout = 300;

                //SendMulticast();
                SendBroadcast();

                //Check and see if a computer is trying to connect.
                //If not, then sleep, and resend multicast in a second
                if (serverSocket.Pending())
                {
                    tempClientSocket = serverSocket.AcceptTcpClient();

                    heartBeatSocket = heartBeatListener.AcceptTcpClient();

                    ClientThread c = new ClientThread(tempClientSocket, heartBeatSocket);
                    Thread checkHeartBeat = new Thread(() => CheckHeartBeat(heartBeatSocket,c));
                    checkHeartBeat.Start();
                    string compid = c.GetCompID().Trim();
                    //Is New
                    if (!checkNewClient(c))
                    {

                        this.objectListView1.AddObject(c);
                        try
                        {
                            AddClientToDatabase(c);
                            db.InitialSetUptimes(c.GetCompID(), c.GetUptimeHours());
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    //It is Known Khaleesi
                    //It is known
                    else
                    {
                        changeOfflineclient(compid);

                    Console.WriteLine("Connected to " + c.GetClientIP() + " :: " + c.GetPort());

                        this.objectListView1.AddObject(c);

                        string group = db.GetClientGroup(compid.Trim());

                        foreach (Group g in groups)
                        {
                            if (g.GetID().Trim() == group)
                            {
                                c.SetGroup(g);
                            }
                        }

                        clients.Add(c);
                        SetStartGroups();

                    }

                }
                else
                {
                    Thread.Sleep(1000); //Sleep for a second, before sending the next multicast.
                }
            }
        }