Пример #1
0
    public bool Send(byte[] data)
    {
        //---------------------------
        Connect();
        if (!Connected)
        {
            return(false);
        }
        int i = IntToByte.bytesToInt(data, 0);

        OutLog.log(i.ToString());
        Debug.Log("协议头 : " + i.ToString() + "   当前type : " + serverType);
        if (GameInfo.sendbyte == null || GameInfo.sendbyte != data || i == CreateHead.CSXYNUM + 1)
        {
            GameInfo.sendbyte = data;
            try
            {
                return(clientSocket.Send(data) > 0);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
                return(false);
            }
        }
        else
        {
            return(true);//重复发送的情况返回 发送成功。
        }
    }
Пример #2
0
 /// <summary>
 /// 解析数据或者叫做读取数据
 /// </summary>
 public void ReadMessage(int newDataAmount)
 {
     startIndex += newDataAmount;
     while (true)
     {
         if (startIndex <= 12)
         {
             return;
         }
         int number    = IntToByte.bytesToInt(data, 0); //消息号
         int length    = IntToByte.bytesToInt(data, 4); //消息长度
         int resnumber = IntToByte.bytesToInt(data, 8); //返回消息号
         if ((startIndex - 12) >= length)
         {
             nowData = new byte[length];
             Array.Copy(data, 12, nowData, 0, length);
             MethodsByNew(number, nowData);
             nowData = null;
             Array.Copy(data, length + 12, data, 0, startIndex - length - 12);
             startIndex -= (length + 4);
         }
         else
         {
             break;
         }
     }
 }
Пример #3
0
        /// <summary>
        /// 接受从服务器发来的消息,根据不同消息号,分发给不同的方法
        /// </summary>
        public void ReceiveMessage(Socket sc, string openid)
        {
            MemoryStream ms = new MemoryStream();

            try
            {
                while (true)
                {
                    int    count;
                    byte[] data = new byte[2048];
                    // ms = new MemoryStream();
                    while (0 != (count = sc.Receive(data)))
                    {
                        ms.Write(data, 0, count);
                        if (count != data.Length)
                        {
                            break;
                        }
                        // if (count < 1024) break;
                    }
                    while (ms.Length > 0)
                    {
                        var datainfo  = ms.ToArray();
                        int number    = IntToByte.bytesToInt(datainfo, 0); //消息号
                        int length    = IntToByte.bytesToInt(datainfo, 4); //消息长度
                        int resnumber = IntToByte.bytesToInt(datainfo, 8); //返回消息号
                        //  Debug.Log(number + ",长度" + length + ",包长" + datainfo.Length); OutLog.log(number + ",长度" + length + ",包长" + datainfo.Length);
                        if (number < CreateHead.CSXYNUM || number > CreateHead.CSXYNUM * 2)
                        {
                            ms = new MemoryStream();
                            break;
                        }
                        if (length > ms.Length)
                        {
                            break;
                        }
                        byte[] body = new byte[length];
                        Array.Copy(datainfo, 12, body, 0, length);
                        ms = new MemoryStream();
                        if (datainfo.Length > length + 12)
                        {
                            ms.Write(datainfo, length + 12, datainfo.Length - length - 12);
                        }
                        //  Debug.Log(number);
                        MethodsByNew(number, body, openid, sc);
                    }
                }
            }
            catch (Exception ex)
            {
                // Debug.Log(ex.ToString());
            }
        }