Esempio n. 1
0
            static public void Send_message(Message mail, Client_Stream user, bool bSave_to_story)
            {
                string mess = JsonSerializer.Serialize <Message>(mail) + "\n";

                user.stream.Write(Encoding.UTF8.GetBytes(mess));
                if (bSave_to_story)
                {
                    DataWR.save_message(mail);
                }
            }
Esempio n. 2
0
            public void tcpConnection()
            {
                List <byte> some_data = new List <byte>();


                password = "";
                try
                {
                    stream = client.GetStream();

                    //регистрация/аутентификация
                    string targ = sRead_stream(stream);
                    stream.Write(Encoding.UTF8.GetBytes("get"));
                    if (targ == "reg")
                    {
                        while (!is_auth)
                        {
                            name = sRead_stream(stream);
                            if (DataWR.can_register(name))
                            {
                                is_auth = true;
                                stream.Write(Encoding.UTF8.GetBytes("логин доступен"));
                            }
                            else
                            {
                                stream.Write(Encoding.UTF8.GetBytes("логин занят"));
                            }
                        }
                        is_auth = false;
                        while (!is_auth)
                        {
                            password = sRead_stream(stream);
                            if (DataWR.register_user(name, password))
                            {
                                is_auth = true;
                                stream.Write(Encoding.UTF8.GetBytes("пароль принят"));
                            }
                            else
                            {
                                stream.Write(Encoding.UTF8.GetBytes("плохой пароль"));
                            }
                        }

                        Console.WriteLine("Пользователь " + name + "  зарегистрирован");
                    }
                    else if (targ == "log")
                    {
                        name = sRead_stream(stream);
                        if (!DataWR.is_registred(name))
                        {
                            stream.Write(Encoding.UTF8.GetBytes("логин не найден"));
                            throw new Exception("ошибка авторизации: не найден логин для " + name);
                        }
                        lock (locker_online_list)
                        {
                            foreach (Client_Stream client in online_list)
                            {
                                if (client.name == name)
                                {
                                    stream.Write(Encoding.UTF8.GetBytes("пользователь уже в сети"));
                                    throw new Exception("!!! ошибка авторизации: пользователь " + name + " уже в сети");
                                }
                            }
                        }
                        stream.Write(Encoding.UTF8.GetBytes("доступен"));
                        password = sRead_stream(stream);

                        if (System.Linq.Enumerable.SequenceEqual(DataWR.get_password_by_name(name), password)) //процесс авторизации
                        {
                            is_auth = true;
                            stream.Write(Encoding.UTF8.GetBytes("авторизирован"));
                        }
                        else
                        {
                            stream.Write(Encoding.UTF8.GetBytes("неверный пароль"));
                            throw new Exception("ошибка авторизации: неверный пароль для " + name);
                        }
                        Console.WriteLine("пользователь " + name + " вошёл в сеть");
                    }
                    else
                    {
                        throw new Exception("ошибка подключения: не указанна цель");
                    }
                    lock (locker_online_list)
                    {
                        idList = online_list.Count;
                        online_list.Add(this);
                    }
                    byte[] buffer = new byte[64];
                    int    count = 0;
                    string command, input = "";

                    while (true)
                    {
                        string command_pattern = @"(^[A-z0-9]+ )";
                        command = "";
                        input   = "";
                        Message mail;
                        count = 0;
                        // чтение команды
                        input = sRead_stream(stream);
                        if (input.Length == 0)
                        {
                            continue;
                        }
                        command = Regex.Match(input, command_pattern).Value.Trim();
                        if (command.Length + 1 < input.Length)
                        {
                            input = input.Substring(command.Length + 1);
                        }

                        switch (command)
                        {
                        case "send":
                            #region
                            //функция для чтения mail

                            mail = JsonSerializer.Deserialize <Message>(input);
                            lock (locker_online_list)
                            {
                                //отправка всем пользователям
                                if (mail.reciever == "@all")
                                {
                                    foreach (Client_Stream user in online_list)
                                    {
                                        Send_message(mail, user, false);
                                    }
                                }
                                //отправка сообщения конкретному пользователю
                                for (int i = 0; i < online_list.Count; i++)
                                {
                                    if (online_list[i].name == mail.reciever)
                                    {
                                        Send_message(mail, online_list[i], true);
                                    }
                                }
                            }
                            #endregion
                            break;

                        case "GetStory":
                            #region
                            input = input.Trim();

                            //наверное это хороший способ? надо спросить кого0нибудь кто гарит...
                            FileStream            fileStream     = null;
                            DataWR.Message_worker message_Worker = new DataWR.Message_worker(name, input, out fileStream);
                            Message ms = message_Worker.Next();
                            while (ms.sender != null)
                            {
                                Send_message(ms, this, false);
                                ms = message_Worker.Next();
                            }
                            fileStream.Close();
                            #endregion
                            break;

                        case "GetMessage":
                            #region

                            #endregion
                            break;

                        default:
                            break;
                        }
                    }
                }
                catch (IOException exp)
                {
                    if (name != "")
                    {
                        Console.WriteLine("");
                    }
                    else
                    {
                        Console.WriteLine("подключение закрыто для " + name);
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    File.AppendAllText(log_patch, exception.ToString());
                }
                finally
                {
                    if (idList >= 0)
                    {
                        lock (locker_online_list)
                        {
                            for (int i = idList + 1; i < online_list.Count; i++)
                            {
                                online_list[i].idList--;
                            }
                            online_list.RemoveAt(idList);
                        }
                    }
                    if (stream != null)
                    {
                        stream.Close();
                    }
                    if (client != null)
                    {
                        client.Close();
                    }
                }
            }