Ejemplo n.º 1
0
        /// <summary>
        /// 接收到服务器发来的数据的处理方法
        /// </summary>
        /// <param name="obj"></param>
        void ReceiveData(object obj)
        {
            TempPakeage str = obj as TempPakeage;

            mytemppakeList.Add(str);
            if (ReceiveMessageEvent != null)
            {
                ReceiveMessageEvent(str.command, str.date);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 通过主线程执行方法避免跨线程UI问题
 /// </summary>
 public void OnTick()
 {
     if (mytemppakeList.Count > 0)
     {
         try
         {
             TempPakeage str = mytemppakeList[0];
             //xmhelper.Init(str.date, null);
             //receiveServerEvent(str.command, str.date);
         }
         catch
         {
         }
         try
         {
             mytemppakeList.RemoveAt(0);
         }
         catch { }
     }
     Debug.Log("队列中没有排队的方法需要执行。");
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 线程启动的方法,初始化连接后要接收服务器发来的token,并更新
        /// </summary>
        void CheckToken_UpdateListDataThread()
        {
            while (isok)
            {
                System.Threading.Thread.Sleep(10);

                try
                {
                    int count = ListData.Count;
                    if (count > 0)
                    {
                        int bytesRead = ListData[0] != null ? ListData[0].Length : 0;
                        if (bytesRead == 0)
                        {
                            continue;
                        }
                        //如果到这里的continue内部,那么下面的代码不执行,重新到 --》 System.Threading.Thread.Sleep(10);开始


                        byte[] tempbtye = new byte[bytesRead];
                        //解析消息体ListData,,
                        Array.Copy(ListData[0], tempbtye, tempbtye.Length);
                        // 检查tempbtye(理论上里面应该没有0x99,有方法已经处理掉了,但是可能会有)检测还有没有0x99开头的心跳包
_0x99:
                        if (tempbtye[0] == 0x99)
                        {
                            if (bytesRead > 1)
                            {
                                byte[] b = new byte[bytesRead - 1];
                                byte[] t = tempbtye;
                                //把心跳包0x99去掉
                                Array.Copy(t, 1, b, 0, b.Length);
                                ListData[0] = b;
                                tempbtye    = b;
                                goto _0x99;
                            }
                            else
                            {   //说明只有1个字节,心跳包包头,无任何意思,那么直接删除
                                ListData.RemoveAt(0);
                                continue;
                            }
                        }

                        //ListData[0]第一个元素的长度 大于2
                        if (bytesRead > 2)
                        {
                            //第二段是固定一个字节,最高255,代表了第三段的长度
                            //这样第三段最高可以255*255位,这样表示数据内容的长度基本可以无限大了
                            // int a = tempbtye[1];
                            int part3_Length = tempbtye[1];
                            //tempbtye既是ListData[0]数据,
                            //第一位为command,指令
                            //第二位的数据是第三段的长度
                            //第三段的数据是第四段的长度
                            //第四段是内容数据
                            if (bytesRead > 2 + part3_Length)
                            {   //如果收到数据这段数据,大于
                                // int len = 0;
                                int part4_Length = 0;
                                if (s_datatype == SocketDataType.Bytes)
                                {
                                    byte[] bb = new byte[part3_Length];
                                    byte[] part4_LengthBitArray = new byte[part3_Length];

                                    //将tempbtye从第三位开始,复制数据到part4_LengthBitArray
                                    Array.Copy(tempbtye, 2, part4_LengthBitArray, 0, part3_Length);
                                    //len = ConvertToInt(bb);
                                    //获得实际数据的长度,也就是第四部分数据的长度
                                    part4_Length = MyGameClientHelper.ConvertToInt(part4_LengthBitArray);
                                }
                                else
                                {   //如果DataType不是DataType.bytes类型,,, 是Json类型
                                    //从某个Data中第三位开始截取数据,,获取第四段数据长度的字符串string
                                    String temp            = System.Text.Encoding.UTF8.GetString(tempbtye, 2, part3_Length);
                                    String part4_Lengthstr = System.Text.Encoding.UTF8.GetString(tempbtye, 2, part3_Length);

                                    part4_Length = 0;
                                    //len = 0;
                                    //int part4_Lengthstrlength = 0;
                                    try
                                    {
                                        // len = int.Parse(temp);
                                        part4_Length = int.Parse(part4_Lengthstr);
                                        if (part4_Length == 0) //len
                                        {                      //如果第四段数据的长度为0 ,,,说明发的空消息,,没有第四段数据
                                            //如果第二位没有数据,,说明发的是空消息
                                            ListData.RemoveAt(0);
                                            continue;
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }

                                try
                                {
                                    //如果计算出来的(2位数据位+第三段长度+第四度长度) 比当前ListData[0]长度还大...
                                    if ((part4_Length + 2 + part3_Length) > tempbtye.Length)
                                    {
                                        if (ListData.Count > 1)
                                        {
                                            //将第一个数据包删除
                                            ListData.RemoveAt(0);
                                            //重新读取第一个数据包(第二个数据包变为第一个)内容
                                            byte[] temps = new byte[ListData[0].Length];
                                            //将 数据表内容 拷贝到 temps中
                                            Array.Copy(ListData[0], temps, temps.Length);
                                            //新byte数组长度扩充,原数据长度 + (第二个)元素长度
                                            byte[] temps2 = new byte[tempbtye.Length + temps.Length];

                                            Array.Copy(tempbtye, 0, temps2, 0, tempbtye.Length);
                                            //将第一个元素ListData[0]完全从第一个地址开始 ,完全拷贝到 temps2数组中
                                            Array.Copy(temps, 0, temps2, tempbtye.Length, temps.Length);
                                            //将第二个元素拼接到temps2中,从刚复制数据的最后一位 +1 开始
                                            ListData[0] = temps2;
                                            //最后将更新数据包里面的 第一个元素为新元素
                                        }
                                        else
                                        {
                                            System.Threading.Thread.Sleep(20);
                                        }
                                        continue;
                                    }
                                    else if (tempbtye.Length > (part4_Length + 2 + part3_Length))
                                    {
                                        //如果 数据包长度 比  计算出来的(2位数据位+第三段长度+第四度长度)  还大
                                        //考虑大出的部分
                                        int    currentAddcount = (part4_Length + 2 + part3_Length);
                                        int    offset_length   = tempbtye.Length - currentAddcount;
                                        byte[] temps           = new byte[offset_length];


                                        //Array.Copy(tempbtye, (part4_Length + 2 + part3_Length), temps, 0, temps.Length);
                                        Array.Copy(tempbtye, currentAddcount, temps, 0, temps.Length);
                                        //把当前ListData[0]中 后面的数据,复制到 temps数组中...

                                        ListData[0] = temps;
                                    }
                                    else if (tempbtye.Length == (part4_Length + 2 + part3_Length))
                                    {   //长度刚好匹配
                                        ListData.RemoveAt(0);
                                    }
                                }
                                catch (Exception e)
                                {
                                    if (ErrorMessageEvent != null)
                                    {
                                        ErrorMessageEvent(3, e.StackTrace + "unup001:" + e.Message + "2 + a" + 2 + part3_Length + "---len" + part4_Length + "--tempbtye" + tempbtye.Length);
                                    }
                                }

                                try
                                {
                                    if (s_datatype == SocketDataType.Json)
                                    {
                                        //读取出第四部分数据内容,,
                                        string      temp = System.Text.Encoding.UTF8.GetString(tempbtye, 2 + part3_Length, part4_Length);
                                        TempPakeage str  = new TempPakeage();
                                        str.command = tempbtye[0];
                                        //命令等于第一位
                                        str.date = temp;
                                        //服务器发来执行是0xff,说明发送的是token指令
                                        if (tempbtye[0] == 0xff)
                                        {
                                            if (temp.IndexOf("token") >= 0)
                                            {
                                                tokan = temp.Split('|')[1];
                                            }

                                            //用单个字符来分隔字符串,并获取第二个元素,,
                                            //这里因为服务端发来的token后面跟了一个|字符
                                            else if (temp.IndexOf("jump") >= 0)
                                            {
                                                //0xff就是指服务器满了
                                                tokan = "连接数量满";
                                                if (JumpServerEvent != null)
                                                {
                                                    JumpServerEvent(temp.Split('|')[1]);
                                                }
                                            }
                                            else
                                            {  // 当上面条件都不为真时执行 ,如果虽然指令是0xff,但是不包含token或jump
                                                ReceiveData(str);
                                            }
                                        }
                                        else if (ReceiveMessageEvent != null)
                                        {
                                            //如果tempbtye[0] == 0xff 表示token,不等的情况
                                            ReceiveData(str);
                                        }
                                    }
                                    //if (DT == DataType.bytes)
                                    //{

                                    //    byte[] bs = new byte[len - 2 + a];
                                    //    Array.Copy(tempbtye, bs, bs.Length);
                                    //    temppake str = new temppake();
                                    //    str.command = tempbtye[0];
                                    //    str.datebit = bs;
                                    //    rec(str);

                                    //}
                                    continue;
                                }
                                catch (Exception e)
                                {
                                    if (ErrorMessageEvent != null)
                                    {
                                        ErrorMessageEvent(3, e.StackTrace + "unup122:" + e.Message);
                                    }
                                }
                            }
                        }
                        else
                        {   // //ListData[0]第一个元素的Length 不大于2 ,,
                            if (tempbtye[0] == 0)
                            {
                                ListData.RemoveAt(0);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (ErrorMessageEvent != null)
                    {
                        ErrorMessageEvent(3, "unup:" + e.Message + "---" + e.StackTrace);
                    }
                    try
                    {
                        ListData.RemoveAt(0);
                    }
                    catch { }
                }
            }
        }