Ejemplo n.º 1
0
        public void OnClientExit()
        {
            string cmd = String.Format(
                "SELECT UserName FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, TCPClient_username
                );
            MySQLOperation msqlo = MySQLOperation.getinstance();
            bool           b     = msqlo.find(cmd);

            if (b)
            {
                cmd = String.Format(
                    // "SELECT * FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                    "SELECT PassWord FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, TCPClient_username
                    );
                List <List <object> > values = msqlo.get(cmd);
                foreach (var vr in values)
                {
                    if (vr[0].ToString().Equals(TCPClient_password))//password match
                    {
                        cmd = String.Format(
                            " UPDATE {0} SET IsOnLine = '0' WHERE UserName='******'", MySQLOperation.tablebasename, TCPClient_username
                            );
                        msqlo.modify(cmd);
                    }
                }
            }
            mclosed = true;
            CloseSocket();
            ReceiveThread.Abort();
        }
Ejemplo n.º 2
0
        void ReceiveFilehandler(ref String str)
        {
            MySQLOperation msqlo = MySQLOperation.getinstance();
            string         cmd   = String.Format(
                "SELECT {0} FROM {1} WHERE UserName='******'", TCPClient_MapName, MySQLOperation.tablebasename, TCPClient_username
                );
            bool b = msqlo.find(cmd);

            if (!b)
            {
                msqlo.addcolumnv1(TCPClient_MapName);
            }
            cmd = String.Format(
                " UPDATE {0} SET {1} = '{2}' WHERE UserName='******'", MySQLOperation.tablebasename, TCPClient_MapName, filestringpayload, TCPClient_username
                );
            msqlo.modify(cmd);
            filestringpayload = null;
        }
        public static MySQLOperation getinstance()
        {
            MySQLOperation instance = new MySQLOperation();
            bool           b        = instance.opendatabase();

            if (!b)
            {
                Console.WriteLine("open database Error: ");
            }
            bool b1 = instance.databaseexist(databasename);

            if (!b1)
            {
                instance.createdatabase(databasename);
            }
            instance.connection.ChangeDatabase(databasename);
            bool b2 = instance.tableeexist(databasename, tablebasename);

            if (!b2)
            {
                instance.creatusersstable(databasename, tablebasename);
            }
            return(instance);
        }
Ejemplo n.º 4
0
        void messagehandler(byte[] buffer)
        {
            FMessagePackage mp;

            try
            {
#if UTF16
                var str = System.Text.Encoding.Unicode.GetString(buffer);
#else
                var str = System.Text.Encoding.UTF8.GetString(buffer);
#endif
                var      strfile   = System.Text.Encoding.UTF8.GetString(buffer);
                String[] seperator = { "\r\n" };
                receives = strfile.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
                //"POST / HTTP/1.1"
                //"Accept: */*"
                //"Accept-Encoding: deflate, gzip"
                //"User-Agent: X-UnrealEngine-Agent"
                //"Content-Type: application/json"
                //"Accepts: application/json"
                //"UserName: 002"
                //"Password: 123"
                //"Content-Length: 2"
                //"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                string[] seperator1 = { "\0" };
                if (!isfile)
                {
                    bool b = receives[0].Equals("POST / HTTP/1.1") && receives[10].Contains("Content-Length:");
                    if (b)
                    {
                        string length = receives[10].Split(':')[1];
                        if (!Int32.TryParse(length, out filelength))
                        {
                        }
                        TCPClient_username = receives[7].Split(':')[1].Replace(" ", String.Empty);
                        TCPClient_password = receives[8].Split(':')[1].Replace(" ", String.Empty);
                        TCPClient_MapName  = receives[9].Split(':')[1].Replace(" ", String.Empty);
                        int startindex = 0;
                        int i          = 0;
                        for (; i < 11; i++)
                        {
                            int templen = receives[i].Length + 2;
                            startindex += templen;
                        }
                        startindex += 2;//\r\n
                        string   content1 = strfile.Substring(startindex);
                        String[] contents = content1.Split(seperator1, StringSplitOptions.RemoveEmptyEntries);
                        if (contents.Length > 0)
                        {
                            filestringpayload = contents[0];
                            if (filestringpayload.Length == filelength)
                            {
                                onfilereceivedcompleted.Invoke(ref filestringpayload);
                            }
                        }
                        isfile = true;
                        return;
                    }
                    //message windows here
                }
                else
                {
                    String[] contents = strfile.Split(seperator1, StringSplitOptions.RemoveEmptyEntries);
                    if (contents.Length > 0)
                    {
                        filestringpayload += contents[0];
                        if (filestringpayload.Length == filelength)
                        {
                            onfilereceivedcompleted.Invoke(ref filestringpayload);
                        }
                    }
                    return;
                }

                mp = JsonConvert.DeserializeObject <FMessagePackage>(str);
                String[] payloads;
                switch (mp.MT)
                {
                    #region MessageType.SINGUP singup
                case MessageType.SINGUP:
                    payloads = mp.PayLoad.Split('?');
                    MySQLOperation msqlo = MySQLOperation.getinstance();
                    String         cmd;
                    Dictionary <String, String> value;
                    cmd = String.Format(
                        "SELECT UserName FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                        );
                    bool b = msqlo.find(cmd);
                    if (!b)
                    {
                        //cmd = "INSERT INTO students(name,class) VALUES(@name,@class)";
                        cmd = String.Format(
                            "INSERT INTO {0}(UserName,PassWord) VALUES(@UserName,@PassWord)", MySQLOperation.tablebasename
                            );
                        value = new Dictionary <string, string>();
                        value.Add("@UserName", payloads[0]);
                        value.Add("@PassWord", payloads[1]);
                        msqlo.add(cmd, value);
                    }
                    FMessagePackage feedback = new FMessagePackage();
                    feedback.MT      = MessageType.SINGUP;
                    feedback.PayLoad = b ? "failed" : "succeed";
                    String strsend = JsonConvert.SerializeObject(feedback);
                    Send(strsend);
                    break;
                    #endregion
                    #region  MessageType.LOGIN login

                case MessageType.LOGIN:
                    payloads           = mp.PayLoad.Split('?');
                    TCPClient_username = payloads[0];
                    TCPClient_password = payloads[1];
                    msqlo = MySQLOperation.getinstance();
                    cmd   = String.Format(
                        "SELECT UserName FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                        );
                    b = msqlo.find(cmd);

                    List <List <object> > values;
                    if (b)
                    {
                        cmd = String.Format(
                            // "SELECT * FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                            "SELECT PassWord FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                            );
                        values = msqlo.get(cmd);
                        foreach (var vr in values)
                        {
                            if (vr[0].ToString().Equals(payloads[1]))    //password match
                            {
                                cmd = String.Format(
                                    " UPDATE {0} SET IsOnLine = '1' WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                                    );
                                msqlo.modify(cmd);
                            }
                            else
                            {
                                b = false;
                            }
                            // Console.WriteLine(vr[0] + ":" + vr[1] + ":" + vr[2] + ":" + vr[3]);
                        }
                    }
                    feedback         = new FMessagePackage();
                    feedback.MT      = MessageType.LOGIN;
                    feedback.PayLoad = !b ? "failed" : "succeed";
                    strsend          = JsonConvert.SerializeObject(feedback);
                    Send(strsend);
                    //////////////////////////////////////////////////////////////////////////////////////
                    ///test area

                    break;
                    #endregion
                    #region MessageType.SAVEMAPACTORINFOR save map actor infor

                case MessageType.SAVEMAPACTORINFOR:
                    //payloads = mp.PayLoad.Split('?');
                    string mapname = mp.PayLoad;
                    msqlo = MySQLOperation.getinstance();
                    cmd   = String.Format(
                        "SELECT {0} FROM {1} WHERE UserName='******'", mapname, MySQLOperation.tablebasename, TCPClient_username
                        );
                    b = msqlo.find(cmd);
                    if (!b)
                    {
                        msqlo.addcolumnv1(mapname);
                    }
                    cmd = String.Format(
                        " UPDATE {0} SET {1} = '{2}' WHERE UserName='******'", MySQLOperation.tablebasename, mapname, filestringpayload, TCPClient_username
                        );
                    msqlo.modify(cmd);
                    filestringpayload = null;
                    break;

                    #endregion
                    #region MessageType.GETMAPACTORINFOR get map actor infor
                case MessageType.GETMAPACTORINFOR:
                    payloads = mp.PayLoad.Split('?');
                    string map  = payloads[0];
                    string name = payloads[1];
                    msqlo = MySQLOperation.getinstance();
                    cmd   = String.Format(
                        // "SELECT * FROM {0} WHERE UserName='******'", MySQLOperation.tablebasename, payloads[0]
                        "SELECT {0} FROM {1} WHERE UserName='******'", map, MySQLOperation.tablebasename, name
                        );
                    b = msqlo.find(cmd);
                    if (b)
                    {
                        values = msqlo.get(cmd);
                        foreach (var vr in values)
                        {
                            filestringpayloadsendtoclient = vr[0].ToString();
                            // Console.WriteLine(vr[0] + ":" + vr[1] + ":" + vr[2] + ":" + vr[3]);
                        }
                        //Thread.Sleep(10000);
                        feedback    = new FMessagePackage();
                        feedback.MT = MessageType.CLIENT_FILE;
                        strsend     = JsonConvert.SerializeObject(feedback);
                        Send(strsend);
                        Console.WriteLine(strsend);
                        Thread.Sleep(200);    //here must has enough delay for filestringpayloadsendtoclient maybe a big file so cpu must cost more time to handle it
                        Console.WriteLine("Thread.Sleep(10000)");
                        SendFileThread = new Thread(new ParameterizedThreadStart(sendfilework));
                        SendFileThread.IsBackground = true;
                        SendFileThread.Start(MessageType.MAPACTORINFORSENDOK);
                    }
                    break;

                    #endregion
                case MessageType.EXITGAME:
                    OnClientExit();
                    break;

                case MessageType.CLIENT_FILE:     //client say keep sending
                    isfilegoing = true;
                    break;

                case MessageType.CLIENT_FILERECEIVEOK:
                    isclientsidefilereceiveok = true;
                    break;

                default:

                    break;
                }
            }
            catch (Newtonsoft.Json.JsonSerializationException)
            {//buffer all zero//occur when mobile client force kill the game client
                OnClientExit();
            }
        }