diff options
author | Rat <gmstreetrat@gmail.com> | 2011-11-27 17:04:01 +0100 |
---|---|---|
committer | Rat <gmstreetrat@gmail.com> | 2011-11-27 17:04:01 +0100 |
commit | 5be5eae64fa801b6eedbc993d99ba3af61c273e9 (patch) | |
tree | ff2779f6e183c88ffdc704975683fdc6e713a221 /src | |
parent | 07740f84b53a54f5e6224c12d5521579a180151c (diff) |
Core/Packets: updated WorldPacket compressor
Diffstat (limited to 'src')
-rwxr-xr-x | src/server/game/Entities/Object/Updates/UpdateData.cpp | 60 | ||||
-rwxr-xr-x | src/server/game/Entities/Object/Updates/UpdateData.h | 2 | ||||
-rw-r--r-- | src/server/game/Server/WorldPacket.cpp | 103 | ||||
-rw-r--r-- | src/server/game/Server/WorldPacket.h | 2 |
4 files changed, 107 insertions, 60 deletions
diff --git a/src/server/game/Entities/Object/Updates/UpdateData.cpp b/src/server/game/Entities/Object/Updates/UpdateData.cpp index 7ad98418c7b..a2aba642048 100755 --- a/src/server/game/Entities/Object/Updates/UpdateData.cpp +++ b/src/server/game/Entities/Object/Updates/UpdateData.cpp @@ -45,62 +45,6 @@ void UpdateData::AddUpdateBlock(const ByteBuffer &block) ++m_blockCount; } -void UpdateData::Compress(void* dst, uint32 *dst_size, void* src, int src_size) -{ - z_stream c_stream; - - c_stream.zalloc = (alloc_func)0; - c_stream.zfree = (free_func)0; - c_stream.opaque = (voidpf)0; - - // default Z_BEST_SPEED (1) - int z_res = deflateInit(&c_stream, sWorld->getIntConfig(CONFIG_COMPRESSION)); - if (z_res != Z_OK) - { - sLog->outError("Can't compress update packet (zlib: deflateInit) Error code: %i (%s)", 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) - { - sLog->outError("Can't compress update packet (zlib: deflate) Error code: %i (%s)", z_res, zError(z_res)); - *dst_size = 0; - return; - } - - if (c_stream.avail_in != 0) - { - sLog->outError("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) - { - sLog->outError("Can't compress update packet (zlib: deflate should report Z_STREAM_END instead %i (%s)", z_res, zError(z_res)); - *dst_size = 0; - return; - } - - z_res = deflateEnd(&c_stream); - if (z_res != Z_OK) - { - sLog->outError("Can't compress update packet (zlib: deflateEnd) Error code: %i (%s)", 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 @@ -122,8 +66,8 @@ bool UpdateData::BuildPacket(WorldPacket* packet) packet->append(m_data); - //if (packet->wpos() > 100) - // compress meee!! + if (packet->wpos() > 100) + packet->Compress(SMSG_COMPRESSED_UPDATE_OBJECT); return true; } diff --git a/src/server/game/Entities/Object/Updates/UpdateData.h b/src/server/game/Entities/Object/Updates/UpdateData.h index 526a31f206f..653bf411f26 100755 --- a/src/server/game/Entities/Object/Updates/UpdateData.h +++ b/src/server/game/Entities/Object/Updates/UpdateData.h @@ -68,8 +68,6 @@ class UpdateData uint32 m_blockCount; std::set<uint64> m_outOfRangeGUIDs; ByteBuffer m_data; - - void Compress(void* dst, uint32 *dst_size, void* src, int src_size); }; #endif diff --git a/src/server/game/Server/WorldPacket.cpp b/src/server/game/Server/WorldPacket.cpp new file mode 100644 index 00000000000..20588529098 --- /dev/null +++ b/src/server/game/Server/WorldPacket.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2011 TrinityCore <http://www.trinitycore.org/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "WorldPacket.h" +#include <zlib.h> +#include "World.h" + +void WorldPacket::Compress(Opcodes opcode) +{ + if (opcode == UNKNOWN_OPCODE || opcode == NULL) + { + sLog->outError("Tried to compress packet with unknown opcode (%u)", uint32(opcode)); + return; + } + Opcodes uncompressedOpcode = GetOpcode(); + uint32 size = wpos(); + uint32 destsize = compressBound(size); + + std::vector<uint8> storage(destsize); + + _compress(static_cast<void*>(&storage[0]), &destsize, static_cast<const void*>(contents()), size); + if (destsize == 0) + return; + + clear(); + reserve(destsize + sizeof(uint32)); + *this << uint32(size); + append(&storage[0], destsize); + SetOpcode(opcode); + + sLog->outStaticDebug("Successfully compressed opcode %u (len %u) to %u (len %u)", + uncompressedOpcode, size, opcode, destsize); +} + +void WorldPacket::_compress(void* dst, uint32 *dst_size, const void* src, int src_size) +{ + z_stream c_stream; + + c_stream.zalloc = (alloc_func)0; + c_stream.zfree = (free_func)0; + c_stream.opaque = (voidpf)0; + + // default Z_BEST_SPEED (1) + int z_res = deflateInit(&c_stream, sWorld->getIntConfig(CONFIG_COMPRESSION)); + if (z_res != Z_OK) + { + sLog->outError("Can't compress packet (zlib: deflateInit) Error code: %i (%s)",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) + { + sLog->outError("Can't compress packet (zlib: deflate) Error code: %i (%s)",z_res,zError(z_res)); + *dst_size = 0; + return; + } + + if (c_stream.avail_in != 0) + { + sLog->outError("Can't compress packet (zlib: deflate not greedy)"); + *dst_size = 0; + return; + } + + z_res = deflate(&c_stream, Z_FINISH); + if (z_res != Z_STREAM_END) + { + sLog->outError("Can't compress packet (zlib: deflate should report Z_STREAM_END instead %i (%s)",z_res,zError(z_res)); + *dst_size = 0; + return; + } + + z_res = deflateEnd(&c_stream); + if (z_res != Z_OK) + { + sLog->outError("Can't compress packet (zlib: deflateEnd) Error code: %i (%s)",z_res,zError(z_res)); + *dst_size = 0; + return; + } + + *dst_size = c_stream.total_out; +}
\ No newline at end of file diff --git a/src/server/game/Server/WorldPacket.h b/src/server/game/Server/WorldPacket.h index 63da8652c7d..a9af9ed0563 100644 --- a/src/server/game/Server/WorldPacket.h +++ b/src/server/game/Server/WorldPacket.h @@ -45,9 +45,11 @@ class WorldPacket : public ByteBuffer Opcodes GetOpcode() const { return m_opcode; } void SetOpcode(Opcodes opcode) { m_opcode = opcode; } + void Compress(Opcodes opcode); protected: Opcodes m_opcode; + void _compress(void* dst, uint32 *dst_size, const void* src, int src_size); }; #endif |