Simplify usage of NetworkPacket::Data

This commit is contained in:
Matt 2020-08-02 22:07:47 +02:00
parent aa11773da3
commit 224efdbbdf
3 changed files with 29 additions and 33 deletions

View File

@ -51,13 +51,13 @@ int32_t NetworkConnection::ReadPacket()
{
return NETWORK_READPACKET_DISCONNECTED;
}
InboundPacket.Data->resize(InboundPacket.Size);
InboundPacket.Data.resize(InboundPacket.Size);
}
}
else
{
// read packet data
if (InboundPacket.Data->capacity() > 0)
if (InboundPacket.Data.capacity() > 0)
{
void* buffer = &InboundPacket.GetData()[InboundPacket.BytesTransferred - sizeof(InboundPacket.Size)];
size_t bufferLength = sizeof(InboundPacket.Size) + InboundPacket.Size - InboundPacket.BytesTransferred;
@ -88,7 +88,7 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet)
std::vector<uint8_t> tosend;
tosend.reserve(sizeof(sizen) + packet.Size);
tosend.insert(tosend.end(), reinterpret_cast<uint8_t*>(&sizen), reinterpret_cast<uint8_t*>(&sizen) + sizeof(sizen));
tosend.insert(tosend.end(), packet.Data->begin(), packet.Data->end());
tosend.insert(tosend.end(), packet.Data.begin(), packet.Data.end());
const void* buffer = &tosend[packet.BytesTransferred];
size_t bufferSize = tosend.size() - packet.BytesTransferred;
@ -106,15 +106,15 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet)
return sendComplete;
}
void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool front)
void NetworkConnection::QueuePacket(NetworkPacket&& packet, bool front)
{
if (AuthStatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
if (AuthStatus == NETWORK_AUTH_OK || !packet.CommandRequiresAuth())
{
packet->Size = static_cast<uint16_t>(packet->Data->size());
packet.Size = static_cast<uint16_t>(packet.Data.size());
if (front)
{
// If the first packet was already partially sent add new packet to second position
if (!_outboundPackets.empty() && _outboundPackets.front()->BytesTransferred > 0)
if (!_outboundPackets.empty() && _outboundPackets.front().BytesTransferred > 0)
{
auto it = _outboundPackets.begin();
it++; // Second position
@ -134,9 +134,9 @@ void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool
void NetworkConnection::SendQueuedPackets()
{
while (!_outboundPackets.empty() && SendPacket(*_outboundPackets.front()))
while (!_outboundPackets.empty() && SendPacket(_outboundPackets.front()))
{
_outboundPackets.remove(_outboundPackets.front());
_outboundPackets.pop_front();
}
}

View File

@ -15,35 +15,32 @@
# include <memory>
std::unique_ptr<NetworkPacket> NetworkPacket::Allocate()
{
return std::make_unique<NetworkPacket>();
}
std::unique_ptr<NetworkPacket> NetworkPacket::Duplicate(NetworkPacket& packet)
{
return std::make_unique<NetworkPacket>(packet);
}
uint8_t* NetworkPacket::GetData()
{
return &(*Data)[0];
return Data.data();
}
const uint8_t* NetworkPacket::GetData() const
{
return Data.data();
}
NetworkCommand NetworkPacket::GetCommand() const
{
if (Data->size() < sizeof(uint32_t))
if (Data.size() < sizeof(uint32_t))
return NetworkCommand::Invalid;
const uint32_t commandId = ByteSwapBE(*reinterpret_cast<uint32_t*>(&(*Data)[0]));
return static_cast<NetworkCommand>(commandId);
uint32_t commandId = 0;
std::memcpy(&commandId, GetData(), sizeof(commandId));
return static_cast<NetworkCommand>(ByteSwapBE(commandId));
}
void NetworkPacket::Clear()
{
BytesTransferred = 0;
BytesRead = 0;
Data->clear();
Data.clear();
}
bool NetworkPacket::CommandRequiresAuth()
@ -63,9 +60,10 @@ bool NetworkPacket::CommandRequiresAuth()
}
}
void NetworkPacket::Write(const uint8_t* bytes, size_t size)
void NetworkPacket::Write(const void* bytes, size_t size)
{
Data->insert(Data->end(), bytes, bytes + size);
const uint8_t* src = reinterpret_cast<const uint8_t*>(bytes);
Data.insert(Data.end(), src, src + size);
}
void NetworkPacket::WriteString(const utf8* string)

View File

@ -20,14 +20,13 @@ class NetworkPacket final
{
public:
uint16_t Size = 0;
std::shared_ptr<std::vector<uint8_t>> Data = std::make_shared<std::vector<uint8_t>>();
std::vector<uint8_t> Data;
size_t BytesTransferred = 0;
size_t BytesRead = 0;
static std::unique_ptr<NetworkPacket> Allocate();
static std::unique_ptr<NetworkPacket> Duplicate(NetworkPacket& packet);
uint8_t* GetData();
const uint8_t* GetData() const;
NetworkCommand GetCommand() const;
void Clear();
@ -36,7 +35,7 @@ public:
const uint8_t* Read(size_t size);
const utf8* ReadString();
void Write(const uint8_t* bytes, size_t size);
void Write(const void* bytes, size_t size);
void WriteString(const utf8* string);
template<typename T> NetworkPacket& operator>>(T& value)
@ -58,8 +57,7 @@ public:
template<typename T> NetworkPacket& operator<<(T value)
{
T swapped = ByteSwapBE(value);
uint8_t* bytes = reinterpret_cast<uint8_t*>(&swapped);
Data->insert(Data->end(), bytes, bytes + sizeof(value));
Write(&swapped, sizeof(T));
return *this;
}