Example #1
0
        /// <summary>
        /// 解析字节数据
        /// </summary>
        /// <param name="connection">要解析数据的连接</param>
        private void ProcessData(Connection connection)
        {
            if (connection.length < sizeof(Int32))
            {
                Debug.Log("获取不到信息大小重新接包解析:" + connection.length.ToString());
                return;
            }
            Array.Copy(connection.readBuff, connection.lenBytes, sizeof(Int32));
            connection.msgLength = BitConverter.ToInt32(connection.lenBytes, 0);

            if (connection.length < connection.msgLength + sizeof(Int32))
            {
                Debug.Log("信息大小不匹配重新接包解析:" + connection.length + ":" + (connection.msgLength + 4).ToString());
                return;
            }
            //ServerDebug.Log("接收信息大小:" + connection.msgLength.ToString(), 1);
            // string str = Encoding.UTF8.GetString(connection.readBuff, sizeof(Int32), connection.length);

            ProtocolBase message = new ByteProtocol();

            // Debug.Log(DateTime.Now.ToString() + ":" + DateTime.Now.Millisecond+"接收消息大小:" + connection.msgLength);
            message.InitMessage(connection.ReceiveBytes);
            MessageList.Enqueue(message);
            //    Debug.Log("ProcessDataOver");
            //Send(connection, str);
            int count = connection.length - connection.msgLength - sizeof(Int32);

            Array.Copy(connection.readBuff, sizeof(Int32) + connection.msgLength, connection.readBuff, 0, count);
            connection.length = count;
            if (connection.length > 0)
            {
                ProcessData(connection);
            }
        }
        private void ProcessData(Connection connection)
        {
            if (connection.length < sizeof(Int32))
            {
                // Debug.Log("获取不到信息大小重新接包解析:" + connection.length.ToString());
                return;
            }
            Array.Copy(connection.readBuff, connection.lenBytes, sizeof(Int32));
            connection.msgLength = BitConverter.ToInt32(connection.lenBytes, 0);

            if (connection.length < connection.msgLength + sizeof(Int32))
            {
                //Debug.Log("信息大小不匹配重新接包解析:" + connection.msgLength.ToString());
                return;
            }
            ProtocolBase message = new ByteProtocol();

            message.InitMessage(connection.ReceiveBytes);
            ParseMessage(connection, message);

            int count = connection.length - connection.msgLength - sizeof(Int32);

            Array.Copy(connection.readBuff, sizeof(Int32) + connection.msgLength, connection.readBuff, 0, count);
            connection.length = count;
            if (connection.length > 0)
            {
                ProcessData(connection);
            }
        }