Пример #1
0
        void AcceptedClientThreadFunction(object Client)
        {
            TcpClient     tcpClient = Client as TcpClient;
            List <string> InstructionArray;
            string        message = "";

            try
            {
                message = GetMessage(tcpClient);
            }
            catch
            {
            }
            Console.WriteLine(message);
            InstructionArray = CommandTranslator.Parse(message); //0-команда(signup, login, online)
                                                                 //1-логин
                                                                 //2-пароль
                                                                 //3-геолокация
            switch (InstructionArray[0])
            {
            case "!signup":
                try
                {
                    UserBaseDao.Write(InstructionArray[1], InstructionArray[2]);
                    SendMessage("!accepted" + CommandTranslator.Encode(GetClientsNames), tcpClient);    //+список онлайна
                    ClientObject RegClientObject = new ClientObject(tcpClient, this, InstructionArray[1], InstructionArray[3]);
                    Thread       RegClientThread = new Thread(new ThreadStart(RegClientObject.Process));
                    RegClientThread.Start();
                }
                catch
                {
                    SendMessage("!unacceptedsignup", tcpClient);
                }
                //добавление в базу
                break;

            case "!login":
                if (UserBaseDao.Find(InstructionArray[1], InstructionArray[2]))
                {
                    SendMessage("!accepted" + CommandTranslator.Encode(GetClientsNames), tcpClient);    //+список онлайна
                    ClientObject clientObject = new ClientObject(tcpClient, this, InstructionArray[1], InstructionArray[3]);
                    Thread       clientThread = new Thread(new ThreadStart(clientObject.Process));
                    clientThread.Start();
                }
                else
                {
                    SendMessage("!unaccepted", tcpClient);
                    Console.WriteLine("!unaccepted");
                }
                break;

            case "!online":
                Console.WriteLine("In online");
                SendMessage(CommandTranslator.Encode(GetClientsNames), tcpClient);
                break;

            case "!file":
                Console.WriteLine("File!!!!");
                break;
            }
        }
Пример #2
0
        public void Process()
        {
            Console.WriteLine(ClientName + " logged in");
            bool          Locker = true;
            List <string> InstructionArray;
            List <string> FileInstructionArray;

            while (Locker)
            {
                try
                {
                    Stream = client.GetStream();
                    Console.WriteLine("before");
                    string message = GetMessage();
                    Console.WriteLine("after");
                    InstructionArray = CommandTranslator.Parse(message);
                    Console.WriteLine(message);

                    switch (InstructionArray[0])
                    {
                    case "!newchat":    // если клиент захотел создать чат(он прислал имя Челика с которым хочет чатиться)
                        foreach (ClientObject clientObj in server.GetClients)
                        {
                            if (InstructionArray[1] == clientObj.ClientName)
                            {
                                server.SendMessage("!newchat?:" + ClientName, clientObj.client);                                                 //**либо переделать отправку
                            }
                        }
                        string answer = GetMessage();
                        if (answer == "!yes")    // если согласен
                        {
                            CompanionName = InstructionArray[1];

                            SendHistory();
                            //Console.WriteLine("History sended to " + ClientName);
                            Console.WriteLine(HistoryPath);
                            server.AddBusyClient(this);
                            while (true)
                            {
                                try
                                {
                                    message = GetMessage();
                                    FileInstructionArray = CommandTranslator.Parse(message);
                                    if (message == "!exitchat")
                                    {
                                        server.SendMessageById(message, CompanionName);
                                        CompanionName = null;
                                        server.RemoveBusyClient(ClientName);
                                        break;
                                    }
                                    else if (message == "!exitchataccept")
                                    {
                                        server.RemoveBusyClient(ClientName);
                                        CompanionName = null;
                                        break;
                                    }
                                    else if (FileInstructionArray[0] == "!file")
                                    {
                                        Thread fileThread = new Thread(new ParameterizedThreadStart(FileTransfering));
                                        fileThread.Start(FileInstructionArray[1]);
                                        Console.WriteLine("new file " + ClientName);
                                    }
                                    if (message != "" && message != null)
                                    {
                                        XmlProcessing.WriteXML(HistoryPath, message, ClientName, DateTime.Now.ToString());
                                    }
                                    //Console.WriteLine("Wrote message from " + ClientName);
                                    if (FileInstructionArray[0] != "!file")
                                    {
                                        server.SendMessageById(message, CompanionName);
                                    }
                                }
                                catch
                                {
                                    message = "!exitchat";
                                    server.SendMessageById(message, CompanionName);
                                    CompanionName = null;
                                    Locker        = false;
                                    server.RemoveBusyClient(ClientName);
                                    server.RemoveConnection(ClientName);
                                    Close();
                                    break;
                                }
                            }
                        }
                        else
                        {
                            CompanionName = null;
                        }
                        break;

                    case "!yes":                                              //либо здесь обрабатывать ответ на вопрос о чате
                        foreach (ClientObject clientObj in server.GetClients) // ищем в онлайне этого челика и отправляем ему запрос
                        {
                            if (InstructionArray[1] == clientObj.ClientName)
                            {
                                server.SendMessage("!yes", clientObj.client);
                                //Console.WriteLine("Sended yes from " + ClientName + " to " + clientObj.ClientName);
                            }
                        }
                        CompanionName = InstructionArray[1];
                        Thread.Sleep(100);
                        SendHistory();
                        //Console.WriteLine("History sended to " + ClientName);
                        server.AddBusyClient(this);
                        while (true)
                        {
                            try
                            {
                                message = GetMessage();
                                FileInstructionArray = CommandTranslator.Parse(message);
                                if (message == "!exitchat")
                                {
                                    server.SendMessageById(message, CompanionName);
                                    CompanionName = null;
                                    server.RemoveBusyClient(ClientName);
                                    break;
                                }
                                else if (message == "!exitchataccept")
                                {
                                    CompanionName = null;
                                    server.RemoveBusyClient(ClientName);
                                    break;
                                }
                                else if (FileInstructionArray[0] == "!file")
                                {
                                    Thread fileThread = new Thread(new ParameterizedThreadStart(FileTransfering));
                                    fileThread.Start(FileInstructionArray[1]);
                                    Console.WriteLine("new file " + ClientName);
                                }
                                if (message != "" && message != null)
                                {
                                    XmlProcessing.WriteXML(HistoryPath, message, ClientName, DateTime.Now.ToString());
                                }
                                //Console.WriteLine("Wrote message from " + ClientName);
                                if (FileInstructionArray[0] != "!file")
                                {
                                    server.SendMessageById(message, CompanionName);
                                }
                            }
                            catch
                            {
                                message = "!exitchat";
                                server.SendMessageById(message, CompanionName);
                                CompanionName = null;
                                Locker        = false;
                                server.RemoveBusyClient(ClientName);
                                server.RemoveConnection(ClientName);
                                Console.WriteLine(ClientName + " disconnected");
                                Close();
                                break;
                            }
                        }
                        break;

                    case "!no":
                    {
                        foreach (ClientObject clientObj in server.GetClients)
                        {
                            if (InstructionArray[1] == clientObj.ClientName)
                            {
                                server.SendMessage("!no", clientObj.client);
                            }
                        }
                    }
                    break;

                    case "!exit":
                        Console.WriteLine(ClientName + " disconnected");
                        Locker = false;
                        server.RemoveBusyClient(ClientName);
                        server.RemoveConnection(ClientName);
                        Close();
                        break;
                    }
                }
                catch
                {
                    Console.WriteLine(ClientName + " disconnected");
                    Locker = false;
                    server.RemoveBusyClient(ClientName);
                    server.RemoveConnection(ClientName);
                    Close();
                }
            }
        }