Example #1
0
bool UpdateData::buildPacket(WorldPacket& packet, bool hasTransport)
{
	ByteBuffer buf(m_data.size() + 10 + m_guidList.size() * 8);

	buf << (u_int)(!m_guidList.empty() ? m_blockCount + 1 : m_blockCount);
	buf << (u_char)(hasTransport ? 1 : 0);

	if (!m_guidList.empty())
	{
		buf << (u_char)UPDATETYPE_OUT_OF_RANGE_OBJECTS;
		buf << (u_int)m_guidList.size();

		for (IDList::const_iterator itr = m_guidList.begin(); itr != m_guidList.end(); ++itr)
		{
			buf << (u_char)0xFF;
			buf << (u_int64)*itr;
		}
	}

	buf.append(m_data);

	packet.clear();

	// 大小超过50字节的数据包要压缩
	if (m_data.size() > 50)
	{
		u_int destSize = (u_int)buf.size() + buf.size() / 10 + 16;
		packet.resize(destSize);

		packet.put(0, (u_int)buf.size());

		compress(const_cast<u_char*>(packet.contents()) + sizeof(u_int), &destSize,
			(void*)buf.contents(), (u_int)buf.size());
		if (destSize == 0)
			return false;

		packet.resize(destSize + sizeof(u_int));
		packet.setOpcode(SMSG_COMPRESSED_UPDATE_OBJECT);
	}
	else
	{
		packet.append(buf);
		packet.setOpcode(SMSG_UPDATE_OBJECT);
	}

	return true;
}