Пример #1
0
    private int DecodeGamePackage(int index, int size)
    {
        m_bIsWaitingPkgComplete = false;

        int initIndex = index;

        if (IsOutOfSize(index, size, 4))
        {
            m_bIsWaitingPkgComplete = true;
            return(initIndex);
        }
        //decode message id
        int messageId = ByteArrayUtil.bytesToInt(m_DecodingBuffer.ToArray(), index);

        index += 4;

        if (IsOutOfSize(index, size, 4))
        {
            m_bIsWaitingPkgComplete = true;
            return(initIndex);
        }
        //skip header
        index += 4;

        if (IsOutOfSize(index, size, 2))
        {
            m_bIsWaitingPkgComplete = true;
            return(initIndex);
        }
        short headerLength = ByteArrayUtil.bytesToShort(m_DecodingBuffer.ToArray(), index);

        index += 2;

        if (IsOutOfSize(index, size, headerLength))
        {
            m_bIsWaitingPkgComplete = true;
            return(initIndex);
        }
        //skip header
        index += headerLength;

        if (IsOutOfSize(index, size, 4))
        {
            m_bIsWaitingPkgComplete = true;
            return(initIndex);
        }
        //get message length
        int messageLength = ByteArrayUtil.bytesToInt(m_DecodingBuffer.ToArray(), index);

        index += 4;

        if (IsOutOfSize(index, size, messageLength))
        {
            m_bIsWaitingPkgComplete = true;
            return(initIndex);
        }
        byte[] messageBody = new byte[messageLength];
        Array.Copy(m_DecodingBuffer.ToArray(), index, messageBody, 0, messageLength);

        //update index
        index += messageLength;

        TBase message = null;
        Type  tmpType;

        if (!m_MessageMapIdToType.TryGetValue(messageId, out tmpType))
        {
            Debug.LogError("Can't decode message " + messageId);
            //return -1;
            return(index);
        }
        message = Activator.CreateInstance(tmpType) as TBase;
        ThriftSerialize.DeSerialize(message, messageBody);

        //broad cast
        MessageDispatcher.Instance.BroadcastMessage(new MessageObject(messageId, message));

        Debug.Log("Rec msg:" + message.ToString());

        return(index);
    }