Core/Misc: Add extra ByteBuffer and WorldPacket constructors allowing to set size immediately

This commit is contained in:
Shauren
2020-05-20 17:28:38 +02:00
parent 4acae3992b
commit aedab76a11
5 changed files with 32 additions and 20 deletions

View File

@@ -2945,8 +2945,7 @@ bool ConditionMgr::IsPlayerMeetingCondition(Player const* player, PlayerConditio
ByteBuffer HexToBytes(const std::string& hex)
{
ByteBuffer buffer;
buffer.resize(hex.length() / 2);
ByteBuffer buffer(hex.length() / 2, ByteBuffer::Resize{});
HexStrToByteArray(hex, buffer.contents());
return buffer;
}

View File

@@ -211,7 +211,7 @@ void Object::BuildCreateUpdateBlockForPlayer(UpdateData* data, Player* target) c
if (unit->GetVictim())
flags.CombatVictim = true;
ByteBuffer buf(0x400);
ByteBuffer buf(0x400, ByteBuffer::Reserve{});
buf << uint8(updateType);
buf << GetGUID();
buf << uint8(objectType);
@@ -265,7 +265,7 @@ void Object::BuildOutOfRangeUpdateBlock(UpdateData* data) const
ByteBuffer Object::PrepareValuesUpdateBuffer() const
{
ByteBuffer buffer(500);
ByteBuffer buffer(500, ByteBuffer::Reserve{});
buffer << uint8(UPDATETYPE_VALUES);
buffer << GetGUID();
return buffer;

View File

@@ -649,8 +649,7 @@ void WorldSession::HandleUpdateAccountData(WorldPackets::ClientConfig::UserClien
return;
}
ByteBuffer dest;
dest.resize(packet.Size);
ByteBuffer dest(packet.Size, ByteBuffer::Resize{});
uLongf realSize = packet.Size;
if (uncompress(dest.contents(), &realSize, packet.CompressedData.contents(), packet.CompressedData.size()) != Z_OK)

View File

@@ -25,20 +25,26 @@ class WorldPacket : public ByteBuffer
{
public:
// just container for later use
WorldPacket() : ByteBuffer(0), m_opcode(UNKNOWN_OPCODE), _connection(CONNECTION_TYPE_DEFAULT)
WorldPacket() : ByteBuffer(0, Reserve{}), m_opcode(UNKNOWN_OPCODE), _connection(CONNECTION_TYPE_DEFAULT)
{
}
WorldPacket(uint32 opcode, size_t res = 200, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(res),
WorldPacket(uint32 opcode, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(0, Reserve{}),
m_opcode(opcode), _connection(connection) { }
WorldPacket(WorldPacket&& packet) : ByteBuffer(std::move(packet)), m_opcode(packet.m_opcode), _connection(packet._connection)
WorldPacket(uint32 opcode, size_t res, Reserve, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(res, Reserve{}),
m_opcode(opcode), _connection(connection) { }
WorldPacket(uint32 opcode, size_t res, Resize, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(res, Resize{}),
m_opcode(opcode), _connection(connection) { }
WorldPacket(uint32 opcode, size_t res, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : WorldPacket(opcode, res, Reserve{}, connection) { }
WorldPacket(WorldPacket&& packet) noexcept : ByteBuffer(std::move(packet)), m_opcode(packet.m_opcode), _connection(packet._connection)
{
}
WorldPacket(WorldPacket const& right) : ByteBuffer(right), m_opcode(right.m_opcode), _connection(right._connection)
{
}
WorldPacket(WorldPacket const& right) = default;
WorldPacket& operator=(WorldPacket const& right)
{
@@ -52,7 +58,7 @@ class WorldPacket : public ByteBuffer
return *this;
}
WorldPacket& operator=(WorldPacket&& right)
WorldPacket& operator=(WorldPacket&& right) noexcept
{
if (this != &right)
{

View File

@@ -30,7 +30,7 @@ class MessageBuffer;
class TC_SHARED_API ByteBufferException : public std::exception
{
public:
~ByteBufferException() noexcept { }
~ByteBufferException() noexcept = default;
char const* what() const noexcept override { return msg_.c_str(); }
@@ -46,7 +46,7 @@ class TC_SHARED_API ByteBufferPositionException : public ByteBufferException
public:
ByteBufferPositionException(size_t pos, size_t size, size_t valueSize);
~ByteBufferPositionException() noexcept { }
~ByteBufferPositionException() noexcept = default;
};
class TC_SHARED_API ByteBuffer
@@ -61,16 +61,24 @@ class TC_SHARED_API ByteBuffer
_storage.reserve(DEFAULT_SIZE);
}
ByteBuffer(size_t reserve) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
// reserve/resize tag
struct Reserve { };
struct Resize { };
ByteBuffer(size_t size, Reserve) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
{
_storage.reserve(reserve);
_storage.reserve(size);
}
ByteBuffer(size_t size, Resize) : _rpos(0), _wpos(size), _bitpos(InitialBitPos), _curbitval(0)
{
_storage.resize(size);
}
ByteBuffer(ByteBuffer&& buf) noexcept : _rpos(buf._rpos), _wpos(buf._wpos),
_bitpos(buf._bitpos), _curbitval(buf._curbitval), _storage(buf.Move()) { }
ByteBuffer(ByteBuffer const& right) : _rpos(right._rpos), _wpos(right._wpos),
_bitpos(right._bitpos), _curbitval(right._curbitval), _storage(right._storage) { }
ByteBuffer(ByteBuffer const& right) = default;
ByteBuffer(MessageBuffer&& buffer);
@@ -111,7 +119,7 @@ class TC_SHARED_API ByteBuffer
return *this;
}
virtual ~ByteBuffer() { }
virtual ~ByteBuffer() = default;
void clear()
{