コード例 #1
0
    public void EncodeGamePackage(object messageBody)
    {
        try
        {
            // clear send buffer
            ResetSendBuffer();

            //
            int messageId = 0;
            if (!m_MessageMapTypeToId.TryGetValue(messageBody.GetType(), out messageId))
            {
                Debug.LogError("Can't encode message " + messageId);
                return;
            }

            // push message id
            Array.Copy(ByteArrayUtil.intToBytes(messageId), 0, m_SendBuffer, m_SendBufferSize, 4);
            m_SendBufferSize += 4;

            // push prefix
            m_SendBufferSize += 4;

            //push header (default length = 0,body = null)
            Array.Copy(ByteArrayUtil.shortToByteArray(0), 0, m_SendBuffer, m_SendBufferSize, 2);
            m_SendBufferSize += 2;

            // push message body length
            TBase  Message     = messageBody as TBase;
            byte[] tmpSendBody = ThriftSerialize.Serialize(Message);
            Array.Copy(ByteArrayUtil.intToBytes(tmpSendBody.Length), 0, m_SendBuffer, m_SendBufferSize, 4);
            m_SendBufferSize += 4;

            Array.Copy(tmpSendBody, 0, m_SendBuffer, m_SendBufferSize, tmpSendBody.Length);
            m_SendBufferSize += tmpSendBody.Length;

            Debug.Log("Send msg:" + messageBody.ToString());
        }
        catch (Exception e)
        {
            Debug.LogError("Error on encode game package ");
            Debug.LogException(e);
        }
    }