diff --git a/src/openrct2/network/NetworkConnection.cpp b/src/openrct2/network/NetworkConnection.cpp index 270e68098b..74b09886cc 100644 --- a/src/openrct2/network/NetworkConnection.cpp +++ b/src/openrct2/network/NetworkConnection.cpp @@ -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 tosend; tosend.reserve(sizeof(sizen) + packet.Size); tosend.insert(tosend.end(), reinterpret_cast(&sizen), reinterpret_cast(&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 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(packet->Data->size()); + packet.Size = static_cast(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 packet, bool void NetworkConnection::SendQueuedPackets() { - while (!_outboundPackets.empty() && SendPacket(*_outboundPackets.front())) + while (!_outboundPackets.empty() && SendPacket(_outboundPackets.front())) { - _outboundPackets.remove(_outboundPackets.front()); + _outboundPackets.pop_front(); } } diff --git a/src/openrct2/network/NetworkPacket.cpp b/src/openrct2/network/NetworkPacket.cpp index e1e4d860c6..59a8d0865c 100644 --- a/src/openrct2/network/NetworkPacket.cpp +++ b/src/openrct2/network/NetworkPacket.cpp @@ -15,35 +15,32 @@ # include -std::unique_ptr NetworkPacket::Allocate() -{ - return std::make_unique(); -} - -std::unique_ptr NetworkPacket::Duplicate(NetworkPacket& packet) -{ - return std::make_unique(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(&(*Data)[0])); - return static_cast(commandId); + uint32_t commandId = 0; + std::memcpy(&commandId, GetData(), sizeof(commandId)); + + return static_cast(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(bytes); + Data.insert(Data.end(), src, src + size); } void NetworkPacket::WriteString(const utf8* string) diff --git a/src/openrct2/network/NetworkPacket.h b/src/openrct2/network/NetworkPacket.h index 507082e81e..6f8bd3fd9b 100644 --- a/src/openrct2/network/NetworkPacket.h +++ b/src/openrct2/network/NetworkPacket.h @@ -20,14 +20,13 @@ class NetworkPacket final { public: uint16_t Size = 0; - std::shared_ptr> Data = std::make_shared>(); + std::vector Data; size_t BytesTransferred = 0; size_t BytesRead = 0; - static std::unique_ptr Allocate(); - static std::unique_ptr 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 NetworkPacket& operator>>(T& value) @@ -58,8 +57,7 @@ public: template NetworkPacket& operator<<(T value) { T swapped = ByteSwapBE(value); - uint8_t* bytes = reinterpret_cast(&swapped); - Data->insert(Data->end(), bytes, bytes + sizeof(value)); + Write(&swapped, sizeof(T)); return *this; }