Core/PacketIO: Remove ByteBuffer constructor from MessageBuffer

This commit is contained in:
Shauren
2025-05-23 14:41:39 +02:00
parent 5bebf05209
commit 85e621f01a
6 changed files with 38 additions and 78 deletions

View File

@@ -15,8 +15,8 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MESSAGEBUFFER_H_
#define __MESSAGEBUFFER_H_
#ifndef TRINITYCORE_MESSAGE_BUFFER_H
#define TRINITYCORE_MESSAGE_BUFFER_H
#include "Define.h"
#include <vector>
@@ -36,11 +36,11 @@ public:
{
}
MessageBuffer(MessageBuffer const& right) : _wpos(right._wpos), _rpos(right._rpos), _storage(right._storage)
{
}
MessageBuffer(MessageBuffer const& right) = default;
MessageBuffer(MessageBuffer&& right) noexcept : _wpos(right._wpos), _rpos(right._rpos), _storage(right.Move()) { }
MessageBuffer(MessageBuffer&& right) noexcept : _wpos(right._wpos), _rpos(right._rpos), _storage(std::move(right).Release()) { }
~MessageBuffer() = default;
void Reset()
{
@@ -76,6 +76,7 @@ public:
{
if (_rpos != _wpos)
memmove(GetBasePointer(), GetReadPointer(), GetActiveSize());
_wpos -= _rpos;
_rpos = 0;
}
@@ -98,24 +99,13 @@ public:
}
}
std::vector<uint8>&& Move()
std::vector<uint8>&& Release() &&
{
_wpos = 0;
_rpos = 0;
Reset();
return std::move(_storage);
}
MessageBuffer& operator=(MessageBuffer const& right)
{
if (this != &right)
{
_wpos = right._wpos;
_rpos = right._rpos;
_storage = right._storage;
}
return *this;
}
MessageBuffer& operator=(MessageBuffer const& right) = default;
MessageBuffer& operator=(MessageBuffer&& right) noexcept
{
@@ -123,7 +113,7 @@ public:
{
_wpos = right._wpos;
_rpos = right._rpos;
_storage = right.Move();
_storage = std::move(right).Release();
}
return *this;

View File

@@ -3285,13 +3285,6 @@ bool ConditionMgr::IsPlayerMeetingCondition(Player const* player, PlayerConditio
return true;
}
static ByteBuffer HexToBytes(std::string_view const& hex)
{
ByteBuffer buffer(hex.length() / 2, ByteBuffer::Resize{});
Trinity::Impl::HexStrToByteArray(hex, buffer.data(), buffer.size());
return buffer;
}
static constexpr int32(* const WorldStateExpressionFunctions[WSE_FUNCTION_MAX])(Map const*, uint32, uint32) =
{
// WSE_FUNCTION_NONE
@@ -3636,7 +3629,7 @@ bool EvalRelOp(ByteBuffer& buffer, Map const* map)
bool ConditionMgr::IsMeetingWorldStateExpression(Map const* map, WorldStateExpressionEntry const* expression) try
{
ByteBuffer buffer = HexToBytes(expression->Expression);
ByteBuffer buffer(HexStrToByteVector(expression->Expression));
if (buffer.empty())
return false;

View File

@@ -26,27 +26,28 @@ class WorldPacket : public ByteBuffer
{
public:
// just container for later use
WorldPacket() : ByteBuffer(0, Reserve{}), m_opcode(UNKNOWN_OPCODE), _connection(CONNECTION_TYPE_DEFAULT)
{
}
explicit WorldPacket() : ByteBuffer(0, Reserve{}),
m_opcode(UNKNOWN_OPCODE), _connection(CONNECTION_TYPE_DEFAULT) { }
WorldPacket(uint32 opcode, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(0, Reserve{}),
explicit WorldPacket(uint32 opcode, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(0, Reserve{}),
m_opcode(opcode), _connection(connection) { }
WorldPacket(uint32 opcode, size_t res, Reserve, ConnectionType connection = CONNECTION_TYPE_DEFAULT) : ByteBuffer(res, Reserve{}),
explicit 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{}),
explicit 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) { }
explicit 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), m_receivedTime(packet.m_receivedTime)
{
}
WorldPacket(WorldPacket&& packet) noexcept : ByteBuffer(std::move(packet)),
m_opcode(packet.m_opcode), _connection(packet._connection), m_receivedTime(packet.m_receivedTime) { }
WorldPacket(WorldPacket const& right) = default;
explicit WorldPacket(std::vector<uint8>&& buffer, ConnectionType connection) : ByteBuffer(std::move(buffer)),
m_opcode(UNKNOWN_OPCODE), _connection(connection) { }
WorldPacket& operator=(WorldPacket const& right)
{
if (this != &right)
@@ -71,8 +72,6 @@ class WorldPacket : public ByteBuffer
return *this;
}
WorldPacket(MessageBuffer&& buffer, ConnectionType connection) : ByteBuffer(std::move(buffer)), m_opcode(UNKNOWN_OPCODE), _connection(connection) { }
void Initialize(uint32 opcode, size_t newres = 200, ConnectionType connection = CONNECTION_TYPE_DEFAULT)
{
clear();

View File

@@ -157,7 +157,7 @@ void WorldSocketProtocolInitializer::HandleDataReady()
{
try
{
ByteBuffer buffer(std::move(_packetBuffer));
ByteBuffer buffer(std::move(_packetBuffer).Release());
if (buffer.ReadString(ClientConnectionInitialize.length()) != ClientConnectionInitialize)
{
_socket->CloseSocket();
@@ -365,7 +365,7 @@ WorldSocket::ReadDataHandlerResult WorldSocket::ReadDataHandler()
return ReadDataHandlerResult::Error;
}
WorldPacket packet(std::move(_packetBuffer), GetConnectionType());
WorldPacket packet(std::move(_packetBuffer).Release(), GetConnectionType());
OpcodeClient opcode = packet.read<OpcodeClient>();
if (!opcodeTable.IsValid(opcode))
{

View File

@@ -18,8 +18,6 @@
#include "ByteBuffer.h"
#include "Errors.h"
#include "Log.h"
#include "MessageBuffer.h"
#include "Util.h"
#include <utf8.h>
#include <algorithm>
#include <sstream>
@@ -35,10 +33,6 @@ ByteBufferInvalidValueException::ByteBufferInvalidValueException(char const* typ
{
}
ByteBuffer::ByteBuffer(MessageBuffer&& buffer) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0), _storage(buffer.Move())
{
}
ByteBuffer& ByteBuffer::operator>>(float& value)
{
read(&value, 1);

View File

@@ -25,8 +25,6 @@
#include <vector>
#include <cstring>
class MessageBuffer;
// Root of ByteBuffer exception hierarchy
class TC_SHARED_API ByteBufferException : public std::exception
{
@@ -58,34 +56,32 @@ class TC_SHARED_API ByteBuffer
constexpr static size_t DEFAULT_SIZE = 0x1000;
constexpr static uint8 InitialBitPos = 8;
// constructor
ByteBuffer() : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
{
_storage.reserve(DEFAULT_SIZE);
}
// reserve/resize tag
struct Reserve { };
struct Resize { };
ByteBuffer(size_t size, Reserve) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
// constructor
explicit ByteBuffer() : ByteBuffer(DEFAULT_SIZE, Reserve{}) { }
explicit ByteBuffer(size_t size, Reserve) : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0)
{
_storage.reserve(size);
}
ByteBuffer(size_t size, Resize) : _rpos(0), _wpos(size), _bitpos(InitialBitPos), _curbitval(0)
explicit 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) = default;
ByteBuffer(MessageBuffer&& buffer);
ByteBuffer(ByteBuffer&& buf) noexcept : _rpos(buf._rpos), _wpos(buf._wpos),
_bitpos(buf._bitpos), _curbitval(buf._curbitval), _storage(std::move(buf).Release()) { }
std::vector<uint8>&& Move() noexcept
explicit ByteBuffer(std::vector<uint8>&& buffer) noexcept : _rpos(0), _wpos(buffer.size()),
_bitpos(InitialBitPos), _curbitval(0), _storage(std::move(buffer)) { }
std::vector<uint8>&& Release() && noexcept
{
_rpos = 0;
_wpos = 0;
@@ -94,19 +90,7 @@ class TC_SHARED_API ByteBuffer
return std::move(_storage);
}
ByteBuffer& operator=(ByteBuffer const& right)
{
if (this != &right)
{
_rpos = right._rpos;
_wpos = right._wpos;
_bitpos = right._bitpos;
_curbitval = right._curbitval;
_storage = right._storage;
}
return *this;
}
ByteBuffer& operator=(ByteBuffer const& right) = default;
ByteBuffer& operator=(ByteBuffer&& right) noexcept
{
@@ -116,7 +100,7 @@ class TC_SHARED_API ByteBuffer
_wpos = right._wpos;
_bitpos = right._bitpos;
_curbitval = right._curbitval;
_storage = right.Move();
_storage = std::move(right).Release();
}
return *this;