mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-23 13:46:24 +00:00
feat(Core/Compression): Move packets compression from map to network thread (#18602)
* feat(Code/Compression): Move packets compression from map to network thread. * Code style fix * Remove unicode letter
This commit is contained in:
committed by
GitHub
parent
3ff8de2086
commit
73340b94e3
@@ -512,7 +512,7 @@ void GameObject::Update(uint32 diff)
|
||||
UpdateData udata;
|
||||
WorldPacket packet;
|
||||
BuildValuesUpdateBlockForPlayer(&udata, caster->ToPlayer());
|
||||
udata.BuildPacket(&packet);
|
||||
udata.BuildPacket(packet);
|
||||
caster->ToPlayer()->GetSession()->SendPacket(&packet);
|
||||
|
||||
SendCustomAnim(GetGoAnimProgress());
|
||||
|
||||
@@ -250,7 +250,7 @@ void Object::SendUpdateToPlayer(Player* player)
|
||||
WorldPacket packet;
|
||||
|
||||
BuildCreateUpdateBlockForPlayer(&upd, player);
|
||||
upd.BuildPacket(&packet);
|
||||
upd.BuildPacket(packet);
|
||||
player->GetSession()->SendPacket(&packet);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include "Opcodes.h"
|
||||
#include "World.h"
|
||||
#include "WorldPacket.h"
|
||||
#include "zlib.h"
|
||||
|
||||
UpdateData::UpdateData() : m_blockCount(0)
|
||||
{
|
||||
@@ -46,103 +45,25 @@ void UpdateData::AddUpdateBlock(const UpdateData& block)
|
||||
m_blockCount += block.m_blockCount;
|
||||
}
|
||||
|
||||
void UpdateData::Compress(void* dst, uint32* dst_size, void* src, int src_size)
|
||||
bool UpdateData::BuildPacket(WorldPacket& packet)
|
||||
{
|
||||
z_stream c_stream;
|
||||
ASSERT(packet.empty());
|
||||
|
||||
c_stream.zalloc = (alloc_func)0;
|
||||
c_stream.zfree = (free_func)0;
|
||||
c_stream.opaque = (voidpf)0;
|
||||
packet.reserve(4 + (m_outOfRangeGUIDs.empty() ? 0 : 1 + 4 + 9 * m_outOfRangeGUIDs.size()) + m_data.wpos());
|
||||
|
||||
// default Z_BEST_SPEED (1)
|
||||
int z_res = deflateInit(&c_stream, sWorld->getIntConfig(CONFIG_COMPRESSION));
|
||||
if (z_res != Z_OK)
|
||||
{
|
||||
LOG_ERROR("entities.object", "Can't compress update packet (zlib: deflateInit) Error code: {} ({})", z_res, zError(z_res));
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
c_stream.next_out = (Bytef*)dst;
|
||||
c_stream.avail_out = *dst_size;
|
||||
c_stream.next_in = (Bytef*)src;
|
||||
c_stream.avail_in = (uInt)src_size;
|
||||
|
||||
z_res = deflate(&c_stream, Z_NO_FLUSH);
|
||||
if (z_res != Z_OK)
|
||||
{
|
||||
LOG_ERROR("entities.object", "Can't compress update packet (zlib: deflate) Error code: {} ({})", z_res, zError(z_res));
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (c_stream.avail_in != 0)
|
||||
{
|
||||
LOG_ERROR("entities.object", "Can't compress update packet (zlib: deflate not greedy)");
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
z_res = deflate(&c_stream, Z_FINISH);
|
||||
if (z_res != Z_STREAM_END)
|
||||
{
|
||||
LOG_ERROR("entities.object", "Can't compress update packet (zlib: deflate should report Z_STREAM_END instead {} ({})", z_res, zError(z_res));
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
z_res = deflateEnd(&c_stream);
|
||||
if (z_res != Z_OK)
|
||||
{
|
||||
LOG_ERROR("entities.object", "Can't compress update packet (zlib: deflateEnd) Error code: {} ({})", z_res, zError(z_res));
|
||||
*dst_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
*dst_size = c_stream.total_out;
|
||||
}
|
||||
|
||||
bool UpdateData::BuildPacket(WorldPacket* packet)
|
||||
{
|
||||
ASSERT(packet->empty()); // shouldn't happen
|
||||
|
||||
ByteBuffer buf(4 + (m_outOfRangeGUIDs.empty() ? 0 : 1 + 4 + 9 * m_outOfRangeGUIDs.size()) + m_data.wpos());
|
||||
|
||||
buf << (uint32) (!m_outOfRangeGUIDs.empty() ? m_blockCount + 1 : m_blockCount);
|
||||
packet << (uint32) (!m_outOfRangeGUIDs.empty() ? m_blockCount + 1 : m_blockCount);
|
||||
|
||||
if (!m_outOfRangeGUIDs.empty())
|
||||
{
|
||||
buf << (uint8) UPDATETYPE_OUT_OF_RANGE_OBJECTS;
|
||||
buf << (uint32) m_outOfRangeGUIDs.size();
|
||||
packet << (uint8) UPDATETYPE_OUT_OF_RANGE_OBJECTS;
|
||||
packet << (uint32) m_outOfRangeGUIDs.size();
|
||||
|
||||
for (ObjectGuid const& guid : m_outOfRangeGUIDs)
|
||||
{
|
||||
buf << guid.WriteAsPacked();
|
||||
}
|
||||
packet << guid.WriteAsPacked();
|
||||
}
|
||||
|
||||
buf.append(m_data);
|
||||
|
||||
size_t pSize = buf.wpos(); // use real used data size
|
||||
|
||||
if (pSize > 100) // compress large packets
|
||||
{
|
||||
uint32 destsize = compressBound(pSize);
|
||||
packet->resize(destsize + sizeof(uint32));
|
||||
|
||||
packet->put<uint32>(0, pSize);
|
||||
Compress(const_cast<uint8*>(packet->contents()) + sizeof(uint32), &destsize, (void*)buf.contents(), pSize);
|
||||
if (destsize == 0)
|
||||
return false;
|
||||
|
||||
packet->resize(destsize + sizeof(uint32));
|
||||
packet->SetOpcode(SMSG_COMPRESSED_UPDATE_OBJECT);
|
||||
}
|
||||
else // send small packets without compression
|
||||
{
|
||||
packet->append(buf);
|
||||
packet->SetOpcode(SMSG_UPDATE_OBJECT);
|
||||
}
|
||||
packet.append(m_data);
|
||||
packet.SetOpcode(SMSG_UPDATE_OBJECT);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public:
|
||||
void AddOutOfRangeGUID(ObjectGuid guid);
|
||||
void AddUpdateBlock(const ByteBuffer& block);
|
||||
void AddUpdateBlock(const UpdateData& block);
|
||||
bool BuildPacket(WorldPacket* packet);
|
||||
bool BuildPacket(WorldPacket& packet);
|
||||
[[nodiscard]] bool HasData() const { return m_blockCount > 0 || !m_outOfRangeGUIDs.empty(); }
|
||||
void Clear();
|
||||
|
||||
@@ -64,7 +64,5 @@ protected:
|
||||
uint32 m_blockCount;
|
||||
GuidVector m_outOfRangeGUIDs;
|
||||
ByteBuffer m_data;
|
||||
|
||||
void Compress(void* dst, uint32* dst_size, void* src, int src_size);
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -1739,7 +1739,7 @@ void Player::UpdateTriggerVisibility()
|
||||
if (!udata.HasData())
|
||||
return;
|
||||
|
||||
udata.BuildPacket(&packet);
|
||||
udata.BuildPacket(packet);
|
||||
GetSession()->SendPacket(&packet);
|
||||
}
|
||||
|
||||
@@ -1791,7 +1791,7 @@ void Player::UpdateForQuestWorldObjects()
|
||||
}
|
||||
}
|
||||
|
||||
udata.BuildPacket(&packet);
|
||||
udata.BuildPacket(packet);
|
||||
GetSession()->SendPacket(&packet);
|
||||
}
|
||||
|
||||
|
||||
@@ -10595,7 +10595,7 @@ void Unit::SetOwnerGUID(ObjectGuid owner)
|
||||
UpdateData udata;
|
||||
WorldPacket packet;
|
||||
BuildValuesUpdateBlockForPlayer(&udata, player);
|
||||
udata.BuildPacket(&packet);
|
||||
udata.BuildPacket(packet);
|
||||
player->SendDirectMessage(&packet);
|
||||
|
||||
RemoveFieldNotifyFlag(UF_FLAG_OWNER);
|
||||
|
||||
Reference in New Issue
Block a user