Example #1
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);
                        }
                    }
                }
            }
        }
Example #2
0
        private void Process()
        {
            if (tcpClient.Connected)
            {
                Console.WriteLine(DateTime.Now + " [DEBUG][TCP] " + tcpClient.Client.RemoteEndPoint + " подключился");
                //АВТОРИЗАЦИЯ
                Status = ClientStatus.Authorization;
                try {
                    if (Authorization())
                    {
                        Status = ClientStatus.Logged;
                    }
                }
                catch (Exception) {
                    tcpServer.ClientClosed(this);
                    return;
                }
                if (Status == ClientStatus.Logged)
                {
                    //if (Program.DEBUG) Console.WriteLine(DateTime.Now + " [DEBUG][TCP] " + tcpClient.Client.RemoteEndPoint + " авторизовался как " + Login);

                    Status = ClientStatus.LoadData;
                    {
                        Send(JsonConvert.SerializeObject(new CW("dat", JsonConvert.SerializeObject(new CW.Data {
                            login  = Login,
                            online = tcpServer.Online
                        }))));
                        CW.MessageToClient[] history = MySql_Client.LoadMessagePublic(Id);
                        if (history != null)
                        {
                            for (int i = 0; i < history.Length; i++)
                            {
                                Send(JsonConvert.SerializeObject(new CW("mes", JsonConvert.SerializeObject(history[i]))));
                            }
                        }
                    }

                    Status = ClientStatus.Ready;

                    Count++;
                    Task.Factory.StartNew(() => tcpServer.ClientLogged(this));

                    try {
                        while (true)
                        {
                            string inputData = Read();
                            if (inputData.Length == 2 && inputData[0] == 3 && inputData[1] == 65533)
                            {
                                Count--;
                                Status = ClientStatus.Stopped;
                                tcpServer.ClientClosed(this);
                                return;
                            }
                            else if (inputData.Length > 1500)
                            {
                                continue;
                                /*string json = JsonConvert.SerializeObject(new CW("err", "LEN"));*/
                            }
                            else if (inputData.Length > 20)
                            {
                                CW wrap = JsonConvert.DeserializeObject <CW>(inputData);
                                if (wrap.type == "mes")
                                {
                                    CW.MessageFromClient json = JsonConvert.DeserializeObject <CW.MessageFromClient>(wrap.body);
                                    if (json.message.Length > 0)
                                    {
                                        Task.Factory.StartNew(() => tcpServer.Message(this, json.to, json.message));
                                    }
                                    //if (Program.DEBUG)
                                    //Console.WriteLine(DateTime.Now + " [DEBUG][TCP] " + Login + ": " + json.message.Replace());
                                }
                                else if (wrap.type == "getm")
                                {
                                    Task.Factory.StartNew(() => {
                                        CW.MessageToClient[] history = MySql_Client.LoadMessagePrivate(Id, wrap.body);
                                        if (history != null)
                                        {
                                            for (int i = 0; i < history.Length; i++)
                                            {
                                                Send(JsonConvert.SerializeObject(new CW("mes", JsonConvert.SerializeObject(history[i]))));
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }
                    catch (System.IO.IOException) {
                        Count--;
                        Status = ClientStatus.Stopped;
                        tcpServer.ClientClosed(this);
                        return;
                    }
                }
                else
                {
                    tcpServer.ClientClosed(this);
                }
            }
            else
            {
                tcpServer.ClientClosed(this);
            }
        }
Example #3
0
        private bool Authorization()
        {
            string inputData = ReadHttp();

            if (inputData.Length > 15 && inputData.Substring(0, 14) == "GET /websocket" &&
                Regex.IsMatch(inputData, "Connection: Upgrade") && Regex.IsMatch(inputData, "Upgrade: websocket"))
            {
                SendHttp("HTTP/1.1 101 Switching Protocols" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine
                         + "Upgrade: websocket" + Environment.NewLine + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
                             System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(
                                                                                        new Regex("Sec-WebSocket-Key: (.*)").Match(inputData).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")))
                         + Environment.NewLine + Environment.NewLine);
                useWebsocket = true;
            }
            else if (inputData == "CONNECT STANDART TCP")
            {
                useWebsocket = false;
                Send("CONNECTION OPEN");
            }
            else
            {
                Console.WriteLine(DateTime.Now + " [DEBUG][TCP] " + tcpClient.Client.RemoteEndPoint + " прислал ЭТО " + inputData);
                SendHttp("ERROR-FORMAT");
                return(false);
            }
            while (true)
            {
                inputData = Read();
                CW wrap = JsonConvert.DeserializeObject <CW>(inputData);
                if (wrap.type != null && wrap.body != null)
                {
                    if (wrap.type == "log")
                    {
                        CW.Login loginJSON = JsonConvert.DeserializeObject <CW.Login>(wrap.body);
                        if (loginJSON.login != null && loginJSON.pass != null && loginJSON.login.Length >= 4 && loginJSON.pass.Length >= 4)
                        {
                            if (loginJSON.withpass)
                            {
                                string[] res = MySql_Client.CheckUser(loginJSON.login, loginJSON.pass);
                                if (res != null)
                                {
                                    Id    = Convert.ToUInt32(res[0]);
                                    Login = res[1];
                                    Send("LOGINED-SUCCSESS-ENTER-CHAT");
                                    return(true);
                                }
                                else
                                {
                                    Send("ERROR-LOGIN-PASSWORD");
                                }
                            }
                            else
                            {
                                Send("NOT-WORK-USE-WITHPASS");
                            }
                        }
                        else
                        {
                            Send("ERROR-DATA-CONNECTION-CLOSING");
                            return(false);
                        }
                    }
                    else if (wrap.type == "reg")
                    {
                        CW.Registration regJSON = JsonConvert.DeserializeObject <CW.Registration>(wrap.body);
                        if (regJSON.login != null && regJSON.pass != null && regJSON.email != null)
                        {
                            if (Regular.CheckLogin(regJSON.login))
                            {
                                if (Regular.CheckPass(regJSON.pass))
                                {
                                    if (Regular.CheckEmail(regJSON.email))
                                    {
                                        Id = MySql_Client.RegNewUser(regJSON.login, regJSON.pass, regJSON.email);
                                        if (Id > 0)
                                        {
                                            Login = regJSON.login;
                                            Send("REGISTR-SUCCSESS-ENTER-CHAT");
                                            return(true);
                                        }
                                        else
                                        {
                                            Send("ERR-REGEXIST");
                                        }
                                    }
                                    else
                                    {
                                        Send("ERR-REGEMAIL");
                                    }
                                }
                                else
                                {
                                    Send("ERR-REGPASS");
                                }
                            }
                            else
                            {
                                Send("ERR-REGLOGIN");
                            }
                        }
                        else
                        {
                            Send("ERROR-DATA-CONNECTION-CLOSING");
                            return(false);
                        }
                    }
                    else
                    {
                        Console.WriteLine(inputData);
                        Send("ERROR-DATA-CONNECTION-CLOSING");
                        return(false);
                    }
                }
                else
                {
                    Send("ERROR-DATA-CONNECTION-CLOSING");
                    return(false);
                }
            }
        }