Exemplo n.º 1
0
        void sendfilework(Object pobject)
        {
            do
            {
                String file_str = filestringpayloadsendtoclient.Length > 32768 ? filestringpayloadsendtoclient.Substring(0, 32768) : filestringpayloadsendtoclient;//string should be encode by unicode
                Send(file_str);
                filestringpayloadsendtoclient = filestringpayloadsendtoclient.Length > 32768 ? filestringpayloadsendtoclient.Substring(32768) : filestringpayloadsendtoclient.Substring(filestringpayloadsendtoclient.Length);
                Console.WriteLine("send file frame");
                while (!isfilegoing)
                {
                    Thread.Sleep(10);
                }
                isfilegoing = false;
            } while (!String.IsNullOrEmpty(filestringpayloadsendtoclient));
            FMessagePackage filesend = new FMessagePackage();

            filesend.MT = MessageType.CLIENT_FILEEND;//receive ok
            string strsend = JsonConvert.SerializeObject(filesend);

            Send(strsend);
            while (!isclientsidefilereceiveok)
            {
                Thread.Sleep(20);
            }
            isclientsidefilereceiveok = false;
            filesend    = new FMessagePackage();
            filesend.MT = (MessageType)pobject;//tell client what infor is in this file
            strsend     = JsonConvert.SerializeObject(filesend);
            Send(strsend);
            SendFileThread.Abort();
        }
Exemplo n.º 2
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();
            }
        }