Exemplo n.º 1
0
        public bool Start()
        {
            try
            {
                listener.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now + " [ERROR][TCP] Не удалось запустить. " + ex.Message);
                return(false);
            }

            State = true;

            Task.Factory.StartNew(() =>
            {
                try
                {
                    while (State)
                    {
                        TcpClient tcpClient = listener.AcceptTcpClient();
                        ClientClass client  = new ClientClass(tcpClient, this);
                        clients.Add(client);
                        client.Start();
                    }
                }
                catch (SocketException) { }
            }, TaskCreationOptions.LongRunning);

            Console.WriteLine(DateTime.Now + " [INFO][TCP] Сервер запущен, порт " + port);
            return(true);
        }
Exemplo n.º 2
0
        internal void Message(ClientClass sender, string recipient, string message)
        {
            DateTime date = DateTime.Now;

            if (recipient == "public")
            {
                long id = MySql_Client.AddMessagePub(date, sender.Id, message);
                if (id > 0)
                {
                    foreach (ClientClass cc in clients)
                    {
                        if (cc.Status == ClientClass.ClientStatus.Ready)
                        {
                            cc.SendMessage(id, sender.Login, date, message);
                        }
                    }
                }
            }
            else
            {
                long id = MySql_Client.AddMessage(date, sender.Id, message, recipient);
                if (id > 0)
                {
                    foreach (ClientClass cc in clients)
                    {
                        if (cc.Status == ClientClass.ClientStatus.Ready && (cc.Login == recipient || cc.Login == sender.Login))
                        {
                            cc.SendMessage(id, sender.Login, date, message, recipient);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 internal void ClientLogged(ClientClass client)
 {
     if (!online.Contains(client.Login))
     {
         online.Add(client.Login);
         foreach (ClientClass cc in clients)
         {
             if (cc.Status == ClientClass.ClientStatus.Ready && cc.Login != client.Login)
             {
                 cc.Send_Online(client.Login);
             }
         }
     }
 }
Exemplo n.º 4
0
        internal void ClientClosed(ClientClass forDel)
        {
            forDel.Close();

            Console.WriteLine(DateTime.Now + " [DEBUG][TCP] " + forDel.Address + " " + forDel.Login + " отключен");

            clients.Remove(forDel);
            //if (!clients.Exists(delegate (ClientClass x) { return x.Login == forDel.Login; }))
            if (!clients.Exists(x => x.Login == forDel.Login))
            {
                online.Remove(forDel.Login);
                if (forDel.Status == ClientClass.ClientStatus.Ready || forDel.Status == ClientClass.ClientStatus.Stopped)
                {
                    foreach (ClientClass cc in clients)
                    {
                        if (cc.Status == ClientClass.ClientStatus.Ready)
                        {
                            cc.Send_Offline(forDel.Login);
                        }
                    }
                }
            }
            return;
        }