Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
WorldPacket.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "World.h"
7#include "WorldPacket.h"
8#include <zlib.h>
9
11void WorldPacket::Compress(z_stream* compressionStream)
12{
13 Opcodes uncompressedOpcode = GetOpcode();
14 if (uncompressedOpcode & COMPRESSED_OPCODE_MASK)
15 {
16 SF_LOG_ERROR("network", "Packet with opcode 0x%04X is already compressed!", uncompressedOpcode);
17 return;
18 }
19
20 Opcodes opcode = Opcodes(uncompressedOpcode | COMPRESSED_OPCODE_MASK);
21 uint32 size = wpos();
22 uint32 destsize = compressBound(size);
23
24 std::vector<uint8> storage(destsize);
25
26 _compressionStream = compressionStream;
27 Compress(static_cast<void*>(&storage[0]), &destsize, static_cast<const void*>(contents()), size);
28 if (destsize == 0)
29 return;
30
31 clear();
32 reserve(destsize + sizeof(uint32));
33 *this << uint32(size);
34 append(&storage[0], destsize);
35 SetOpcode(opcode);
36 SF_LOG_INFO("network", "%s (len %u) successfully compressed to %04X (len %u)", GetOpcodeNameForLogging(uncompressedOpcode, true).c_str(), size, opcode, destsize);
37}
38
40void WorldPacket::Compress(z_stream* compressionStream, WorldPacket const* source)
41{
42 ASSERT(source != this);
43
44 Opcodes uncompressedOpcode = source->GetOpcode();
45 if (uncompressedOpcode & COMPRESSED_OPCODE_MASK)
46 {
47 SF_LOG_ERROR("network", "Packet with opcode 0x%04X is already compressed!", uncompressedOpcode);
48 return;
49 }
50
51 Opcodes opcode = Opcodes(uncompressedOpcode | COMPRESSED_OPCODE_MASK);
52 uint32 size = source->size();
53 uint32 destsize = compressBound(size);
54
55 size_t sizePos = 0;
56 resize(destsize + sizeof(uint32));
57
58 _compressionStream = compressionStream;
59 Compress(static_cast<void*>(&_storage[0] + sizeof(uint32)), &destsize, static_cast<const void*>(source->contents()), size);
60 if (destsize == 0)
61 return;
62
63 put<uint32>(sizePos, size);
64 resize(destsize + sizeof(uint32));
65
66 SetOpcode(opcode);
67
68 SF_LOG_INFO("network", "%s (len %u) successfully compressed to %04X (len %u)", GetOpcodeNameForLogging(uncompressedOpcode, true).c_str(), size, opcode, destsize);
69}
70
71void WorldPacket::Compress(void* dst, uint32* dst_size, const void* src, int src_size)
72{
73 _compressionStream->next_out = (Bytef*)dst;
74 _compressionStream->avail_out = *dst_size;
75 _compressionStream->next_in = (Bytef*)src;
76 _compressionStream->avail_in = (uInt)src_size;
77
78 int32 z_res = deflate(_compressionStream, Z_SYNC_FLUSH);
79 if (z_res != Z_OK)
80 {
81 SF_LOG_ERROR("network", "Can't compress packet (zlib: deflate) Error code: %i (%s, msg: %s)", z_res, zError(z_res), _compressionStream->msg);
82 *dst_size = 0;
83 return;
84 }
85
86 if (_compressionStream->avail_in != 0)
87 {
88 SF_LOG_ERROR("network", "Can't compress packet (zlib: deflate not greedy)");
89 *dst_size = 0;
90 return;
91 }
92
93 *dst_size -= _compressionStream->avail_out;
94}
std::int32_t int32
Definition Define.h:73
std::uint32_t uint32
Definition Define.h:77
#define ASSERT
Definition Errors.h:29
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
#define SF_LOG_INFO(filterType__,...)
Definition Log.h:137
void put(size_t pos, T value)
Definition ByteBuffer.h:233
std::vector< uint8 > _storage
Definition ByteBuffer.h:727
void reserve(size_t ressize)
Definition ByteBuffer.h:619
void resize(size_t newsize)
Definition ByteBuffer.h:612
void append(T value)
Definition ByteBuffer.h:147
size_t wpos() const
Definition ByteBuffer.h:483
void clear()
Definition ByteBuffer.h:141
size_t size() const
Definition ByteBuffer.h:609
uint8 * contents()
Definition ByteBuffer.h:605
void Compress(z_stream_s *compressionStream)
Opcodes GetOpcode() const
Definition WorldPacket.h:38
void SetOpcode(Opcodes opcode)
Definition WorldPacket.h:39
z_stream_s * _compressionStream
Definition WorldPacket.h:49
#define COMPRESSED_OPCODE_MASK
Definition Opcodes.h:17
std::string GetOpcodeNameForLogging(Opcodes id, bool isServerOpcode, uint16 opcodeNumber=0)
Lookup opcode name for human understandable logging.
Definition Opcodes.h:1157
Opcodes
List of Opcodes.
Definition Opcodes.h:21