mirror of https://github.com/OpenRCT2/OpenRCT2.git
Simplify usage of NetworkPacket::Data
This commit is contained in:
parent
aa11773da3
commit
224efdbbdf
|
@ -51,13 +51,13 @@ int32_t NetworkConnection::ReadPacket()
|
||||||
{
|
{
|
||||||
return NETWORK_READPACKET_DISCONNECTED;
|
return NETWORK_READPACKET_DISCONNECTED;
|
||||||
}
|
}
|
||||||
InboundPacket.Data->resize(InboundPacket.Size);
|
InboundPacket.Data.resize(InboundPacket.Size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// read packet data
|
// read packet data
|
||||||
if (InboundPacket.Data->capacity() > 0)
|
if (InboundPacket.Data.capacity() > 0)
|
||||||
{
|
{
|
||||||
void* buffer = &InboundPacket.GetData()[InboundPacket.BytesTransferred - sizeof(InboundPacket.Size)];
|
void* buffer = &InboundPacket.GetData()[InboundPacket.BytesTransferred - sizeof(InboundPacket.Size)];
|
||||||
size_t bufferLength = sizeof(InboundPacket.Size) + InboundPacket.Size - InboundPacket.BytesTransferred;
|
size_t bufferLength = sizeof(InboundPacket.Size) + InboundPacket.Size - InboundPacket.BytesTransferred;
|
||||||
|
@ -88,7 +88,7 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet)
|
||||||
std::vector<uint8_t> tosend;
|
std::vector<uint8_t> tosend;
|
||||||
tosend.reserve(sizeof(sizen) + packet.Size);
|
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(), 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];
|
const void* buffer = &tosend[packet.BytesTransferred];
|
||||||
size_t bufferSize = tosend.size() - packet.BytesTransferred;
|
size_t bufferSize = tosend.size() - packet.BytesTransferred;
|
||||||
|
@ -106,15 +106,15 @@ bool NetworkConnection::SendPacket(NetworkPacket& packet)
|
||||||
return sendComplete;
|
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 (front)
|
||||||
{
|
{
|
||||||
// If the first packet was already partially sent add new packet to second position
|
// 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();
|
auto it = _outboundPackets.begin();
|
||||||
it++; // Second position
|
it++; // Second position
|
||||||
|
@ -134,9 +134,9 @@ void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool
|
||||||
|
|
||||||
void NetworkConnection::SendQueuedPackets()
|
void NetworkConnection::SendQueuedPackets()
|
||||||
{
|
{
|
||||||
while (!_outboundPackets.empty() && SendPacket(*_outboundPackets.front()))
|
while (!_outboundPackets.empty() && SendPacket(_outboundPackets.front()))
|
||||||
{
|
{
|
||||||
_outboundPackets.remove(_outboundPackets.front());
|
_outboundPackets.pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,35 +15,32 @@
|
||||||
|
|
||||||
# include <memory>
|
# 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()
|
uint8_t* NetworkPacket::GetData()
|
||||||
{
|
{
|
||||||
return &(*Data)[0];
|
return Data.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t* NetworkPacket::GetData() const
|
||||||
|
{
|
||||||
|
return Data.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkCommand NetworkPacket::GetCommand() const
|
NetworkCommand NetworkPacket::GetCommand() const
|
||||||
{
|
{
|
||||||
if (Data->size() < sizeof(uint32_t))
|
if (Data.size() < sizeof(uint32_t))
|
||||||
return NetworkCommand::Invalid;
|
return NetworkCommand::Invalid;
|
||||||
|
|
||||||
const uint32_t commandId = ByteSwapBE(*reinterpret_cast<uint32_t*>(&(*Data)[0]));
|
uint32_t commandId = 0;
|
||||||
return static_cast<NetworkCommand>(commandId);
|
std::memcpy(&commandId, GetData(), sizeof(commandId));
|
||||||
|
|
||||||
|
return static_cast<NetworkCommand>(ByteSwapBE(commandId));
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkPacket::Clear()
|
void NetworkPacket::Clear()
|
||||||
{
|
{
|
||||||
BytesTransferred = 0;
|
BytesTransferred = 0;
|
||||||
BytesRead = 0;
|
BytesRead = 0;
|
||||||
Data->clear();
|
Data.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkPacket::CommandRequiresAuth()
|
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)
|
void NetworkPacket::WriteString(const utf8* string)
|
||||||
|
|
|
@ -20,14 +20,13 @@ class NetworkPacket final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
uint16_t Size = 0;
|
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 BytesTransferred = 0;
|
||||||
size_t BytesRead = 0;
|
size_t BytesRead = 0;
|
||||||
|
|
||||||
static std::unique_ptr<NetworkPacket> Allocate();
|
|
||||||
static std::unique_ptr<NetworkPacket> Duplicate(NetworkPacket& packet);
|
|
||||||
|
|
||||||
uint8_t* GetData();
|
uint8_t* GetData();
|
||||||
|
const uint8_t* GetData() const;
|
||||||
|
|
||||||
NetworkCommand GetCommand() const;
|
NetworkCommand GetCommand() const;
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
|
@ -36,7 +35,7 @@ public:
|
||||||
const uint8_t* Read(size_t size);
|
const uint8_t* Read(size_t size);
|
||||||
const utf8* ReadString();
|
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);
|
void WriteString(const utf8* string);
|
||||||
|
|
||||||
template<typename T> NetworkPacket& operator>>(T& value)
|
template<typename T> NetworkPacket& operator>>(T& value)
|
||||||
|
@ -58,8 +57,7 @@ public:
|
||||||
template<typename T> NetworkPacket& operator<<(T value)
|
template<typename T> NetworkPacket& operator<<(T value)
|
||||||
{
|
{
|
||||||
T swapped = ByteSwapBE(value);
|
T swapped = ByteSwapBE(value);
|
||||||
uint8_t* bytes = reinterpret_cast<uint8_t*>(&swapped);
|
Write(&swapped, sizeof(T));
|
||||||
Data->insert(Data->end(), bytes, bytes + sizeof(value));
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue