/// <summary>
    /// 对数据进行处理
    /// </summary>
    void OnReceiveData()
    {
        if (m_ReadBuff.Length <= 4 || m_ReadBuff.ReadIndex < 0)
        {
            return;
        }


        int readIndex = m_ReadBuff.ReadIndex;

        byte[] bytes = m_ReadBuff.Bytes;

        int bodyLength = BitConverter.ToInt32(bytes, readIndex);


        // 读取协议长度,然后进行判断,如果消息长度小于都出来的消息长度,证明不是一条完整的消息(分包现象)
        if (m_ReadBuff.Length < bodyLength + 4)
        {
            return;
        }

        // 解析协议名称
        m_ReadBuff.ReadIndex += 4;
        int          nameCount = 0;
        ProtocolEnum protocol  = MessageBase.DecodeName(m_ReadBuff.Bytes, m_ReadBuff.ReadIndex, out nameCount);

        if (protocol == ProtocolEnum.None)
        {
            Debug.LogError("OnReceiveData MessageBase.DecodeName Fail");
            Close();
            return;
        }

        // 解析协议
        m_ReadBuff.ReadIndex += nameCount;
        int bodyCount = bodyLength - nameCount;

        try
        {
            MessageBase messageBase = MessageBase.Decode(protocol, m_ReadBuff.Bytes, m_ReadBuff.ReadIndex, bodyCount);
            if (messageBase == null)
            {
                Debug.LogError("接收数据协议内容解析出错");
                Close();

                return;
            }

            m_ReadBuff.ReadIndex += bodyCount;
            m_ReadBuff.CheckAndMoveBytes();

            // 协议具体的操作
            lock (m_MessageList)
            {
                m_MessageList.Add(messageBase);
            }
            m_MessageCount++;

            // 粘包数据处理
            if (m_ReadBuff.Length > 4)
            {
                OnReceiveData();
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("Socket OnReceiveData Error: " + ex);
            Close();
        }
    }
Example #2
0
        /// <summary>
        /// 接收信息,并解析
        /// </summary>
        /// <param name="cleint"></param>
        void OnReceiveData(ClientSocket clintSocket)
        {
            ByteArray readBuff = clintSocket.ReadBuff;

            // 基本消息长度判断(一个协议头为 4)
            if (readBuff.Length <= 4 || readBuff.ReadIndex < 0)
            {
                Debug.Log("数据小于 4");
                return;
            }

            int readIndex = readBuff.ReadIndex;

            byte[] bytes = readBuff.Bytes;

            //解析协议头,获得中的长度
            int bodyLength = BitConverter.ToInt32(bytes, readIndex);

            // (分包处理)判断接收消息长度是否小于包体长度+包头长度,如果小于,代表接收到的消息不全(返回,不做处理,等下一次全了在处理),大于则代表消息全了(也有可能有粘包存在)
            if (readBuff.Length < bodyLength + 4)
            {
                Debug.Log("数据不完整");
                return;
            }

            // 移动到消息体位置
            readBuff.ReadIndex += 4;

            // 解析协议名称
            int          nameCount = 0;
            ProtocolEnum protocol  = ProtocolEnum.None;

            try
            {
                protocol = MessageBase.DecodeName(readBuff.Bytes, readBuff.ReadIndex, out nameCount);
            }
            catch (Exception ex)
            {
                Debug.LogError("解析协议名称 Error :" + ex);
                CloseClient(clintSocket);
                return;
            }

            // 协议名称为空
            if (protocol == ProtocolEnum.None)
            {
                Debug.LogError("OnReceiveData MessageBase.DecodeName() Fail :");
                CloseClient(clintSocket);
                return;
            }

            // 解析协议内容
            readBuff.ReadIndex += nameCount;

            int bodyCount = bodyLength - nameCount;

            MessageBase messageBase = null;

            try
            {
                messageBase = MessageBase.Decode(protocol, readBuff.Bytes, readBuff.ReadIndex, bodyCount);
                if (messageBase == null)
                {
                    Debug.LogError("{0} 解析协议内容 Error ", protocol.ToString());
                    CloseClient(clintSocket);
                    return;
                }
            }
            catch (Exception ex)
            {
                Debug.LogError("解析协议内容 Error :" + ex);
                CloseClient(clintSocket);
                return;
            }

            // 移动读取位置,并且继续判断移动数据
            readBuff.ReadIndex += bodyCount;
            readBuff.CheckAndMoveBytes();

            //通过反射,分发消息(解析完数据,对应的处理)
            MethodInfo methodInfo = typeof(MessageHandler).GetMethod(protocol.ToString());

            object[] o = { clintSocket, messageBase };
            if (methodInfo != null)
            {
                methodInfo.Invoke(null, o);
            }
            else
            {
                Debug.LogError("OnReceiveData Invok Fail:" + protocol.ToString());
            }


            // 继续读取消息(粘包现象处理)
            if (readBuff.Length > 4)
            {
                OnReceiveData(clintSocket);
            }
        }