コード例 #1
0
        private bool TryHandleOneProtocol(NetBuffer stream, uint connId)
        {
            ProtocolHead head = ProtocolHead.Deserialize(stream);

            if (head == null)
            {
                return(false);
            }

            if (stream.Length < head.dataSize + ProtocolHead.Length)
            {
                return(false);
            }

            if (m_recvBufferTemp.Capacity < head.dataSize)
            {
                Debuger.LogError(TAG, "OnRecv() 缓冲空间不够! connId:{0}, dataSize:{1}, RecvBuffCapacity:{2}",
                                 connId, head.dataSize, m_recvBufferTemp.Capacity);

                return(false);
            }

            stream.ReadBytes(m_recvBufferTemp.GetBytes(), 0, head.dataSize);
            m_recvBufferTemp.AddLength(head.dataSize, 0);

            ushort sum = SGFEncoding.CheckSum(m_recvBufferTemp.GetBytes(), head.dataSize);

            if (sum != head.checksum)
            {
                Debuger.LogError(TAG, "OnRecv() CheckSum失败! connId:{0}, dataSize:{1}, RecvBuffSize:{2}",
                                 connId, head.dataSize, m_recvBufferTemp.Length);

                //这个时候,可能要重置连接
                stream.Clear();
                //ClearConnection();
                return(false);
            }

            object ptlObj = DeserializeProtocol(head.pid, m_recvBufferTemp.GetBytes(), m_recvBufferTemp.Length);

            if (ptlObj != null)
            {
                DispatchProtocol(head.pid, head.index, ptlObj);
            }
            else
            {
                m_recvBufferTemp.Position = 0;
                byte[] buffer = m_recvBufferTemp.ReadBytes(head.dataSize);
                DispatchProtocol(head.pid, head.index, buffer);
            }

            return(true);
        }
コード例 #2
0
        private bool TryHandleOneProtocol(NetBuffer stream, uint connId)
        {
            ProtocolHeader head = ProtocolHeader.Deserialize(stream);

            if (head == null)
            {
                return(false);
            }

            if (stream.Length < head.dataSize + ProtocolHeader.Length)
            {
                return(false);
            }

            if (m_recvBufferTemp.Capacity < head.dataSize)
            {
                MyLogger.LogError(TAG, "OnRecv() no enough buffer! connId:{0}, dataSize:{1}, RecvBuffCapacity:{2}",
                                  connId.ToString(), head.dataSize, m_recvBufferTemp.Capacity);

                return(false);
            }

            stream.ReadBytes(m_recvBufferTemp.GetBytes(), 0, head.dataSize);
            m_recvBufferTemp.AddLength(head.dataSize, 0);

            ushort sum = SGFEncoder.CheckSum(m_recvBufferTemp.GetBytes(), head.dataSize);

            if (sum != head.checksum)
            {
                MyLogger.LogError(TAG, "OnRecv() CheckSum failed! connId:{0}, dataSize:{1}, RecvBuffSize:{2}",
                                  connId.ToString(), head.dataSize, m_recvBufferTemp.Length);

                //reset conncetion
                stream.Clear();
                return(false);
            }

            object ptlObj = DeserializeProtocol(head.pid, m_recvBufferTemp.GetBytes(), m_recvBufferTemp.Length);

            if (ptlObj != null)
            {
                DispatchProtocol(head.pid, head.index, ptlObj);
            }
            else
            {
                m_recvBufferTemp.Position = 0;
                byte[] buffer = m_recvBufferTemp.ReadBytes(head.dataSize);
                DispatchProtocol(head.pid, head.index, buffer);
            }

            return(true);
        }