Пример #1
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);
        }