1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-21 22:13:07 +01:00

Fix name warnings for NetworkPacket

This commit is contained in:
Ted John
2017-01-12 17:45:48 +00:00
parent 18597bacb1
commit 50d0d69815
4 changed files with 45 additions and 55 deletions

View File

@@ -44,11 +44,11 @@ NetworkConnection::~NetworkConnection()
int NetworkConnection::ReadPacket()
{
if (InboundPacket.transferred < sizeof(InboundPacket.size))
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Size))
{
// read packet size
void * buffer = &((char*)&InboundPacket.size)[InboundPacket.transferred];
size_t bufferLength = sizeof(InboundPacket.size) - InboundPacket.transferred;
void * buffer = &((char*)&InboundPacket.Size)[InboundPacket.BytesTransferred];
size_t bufferLength = sizeof(InboundPacket.Size) - InboundPacket.BytesTransferred;
size_t readBytes;
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
if (status != NETWORK_READPACKET_SUCCESS)
@@ -56,24 +56,24 @@ int NetworkConnection::ReadPacket()
return status;
}
InboundPacket.transferred += readBytes;
if (InboundPacket.transferred == sizeof(InboundPacket.size))
InboundPacket.BytesTransferred += readBytes;
if (InboundPacket.BytesTransferred == sizeof(InboundPacket.Size))
{
InboundPacket.size = Convert::NetworkToHost(InboundPacket.size);
if (InboundPacket.size == 0) // Can't have a size 0 packet
InboundPacket.Size = Convert::NetworkToHost(InboundPacket.Size);
if (InboundPacket.Size == 0) // Can't have a size 0 packet
{
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.transferred - sizeof(InboundPacket.size)];
size_t bufferLength = sizeof(InboundPacket.size) + InboundPacket.size - InboundPacket.transferred;
void * buffer = &InboundPacket.GetData()[InboundPacket.BytesTransferred - sizeof(InboundPacket.Size)];
size_t bufferLength = sizeof(InboundPacket.Size) + InboundPacket.Size - InboundPacket.BytesTransferred;
size_t readBytes;
NETWORK_READPACKET status = Socket->ReceiveData(buffer, bufferLength, &readBytes);
if (status != NETWORK_READPACKET_SUCCESS)
@@ -81,9 +81,9 @@ int NetworkConnection::ReadPacket()
return status;
}
InboundPacket.transferred += readBytes;
InboundPacket.BytesTransferred += readBytes;
}
if (InboundPacket.transferred == sizeof(InboundPacket.size) + InboundPacket.size)
if (InboundPacket.BytesTransferred == sizeof(InboundPacket.Size) + InboundPacket.Size)
{
_lastPacketTime = SDL_GetTicks();
return NETWORK_READPACKET_SUCCESS;
@@ -94,20 +94,20 @@ int NetworkConnection::ReadPacket()
bool NetworkConnection::SendPacket(NetworkPacket& packet)
{
uint16 sizen = Convert::HostToNetwork(packet.size);
uint16 sizen = Convert::HostToNetwork(packet.Size);
std::vector<uint8> tosend;
tosend.reserve(sizeof(sizen) + packet.size);
tosend.reserve(sizeof(sizen) + packet.Size);
tosend.insert(tosend.end(), (uint8*)&sizen, (uint8*)&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.transferred];
size_t bufferSize = tosend.size() - packet.transferred;
const void * buffer = &tosend[packet.BytesTransferred];
size_t bufferSize = tosend.size() - packet.BytesTransferred;
size_t sent = Socket->SendData(buffer, bufferSize);
if (sent > 0)
{
packet.transferred += sent;
packet.BytesTransferred += sent;
}
if (packet.transferred == tosend.size())
if (packet.BytesTransferred == tosend.size())
{
return true;
}
@@ -118,11 +118,11 @@ void NetworkConnection::QueuePacket(std::unique_ptr<NetworkPacket> packet, bool
{
if (AuthStatus == NETWORK_AUTH_OK || !packet->CommandRequiresAuth())
{
packet->size = (uint16)packet->data->size();
packet->Size = (uint16)packet->Data->size();
if (front)
{
// If the first packet was already partially sent add new packet to second position
if (_outboundPackets.size() > 0 && _outboundPackets.front()->transferred > 0)
if (_outboundPackets.size() > 0 && _outboundPackets.front()->BytesTransferred > 0)
{
auto it = _outboundPackets.begin();
it++; // Second position

View File

@@ -19,14 +19,6 @@
#include "NetworkTypes.h"
#include "NetworkPacket.h"
NetworkPacket::NetworkPacket()
: size(0)
, data(std::make_shared<std::vector<uint8>>())
, transferred(0)
, read(0)
{
}
std::unique_ptr<NetworkPacket> NetworkPacket::Allocate()
{
return std::unique_ptr<NetworkPacket>(new NetworkPacket); // change to make_unique in c++14
@@ -39,14 +31,14 @@ std::unique_ptr<NetworkPacket> NetworkPacket::Duplicate(NetworkPacket &packet)
uint8 * NetworkPacket::GetData()
{
return &(*data)[0];
return &(*Data)[0];
}
uint32 NetworkPacket::GetCommand()
{
if (data->size() >= sizeof(uint32))
if (Data->size() >= sizeof(uint32))
{
return ByteSwapBE(*(uint32 *)(&(*data)[0]));
return ByteSwapBE(*(uint32 *)(&(*Data)[0]));
}
else
{
@@ -56,9 +48,9 @@ uint32 NetworkPacket::GetCommand()
void NetworkPacket::Clear()
{
transferred = 0;
read = 0;
data->clear();
BytesTransferred = 0;
BytesRead = 0;
Data->clear();
}
bool NetworkPacket::CommandRequiresAuth()
@@ -77,7 +69,7 @@ bool NetworkPacket::CommandRequiresAuth()
void NetworkPacket::Write(const uint8 * bytes, size_t size)
{
data->insert(data->end(), bytes, bytes + size);
Data->insert(Data->end(), bytes, bytes + size);
}
void NetworkPacket::WriteString(const utf8 * string)
@@ -87,32 +79,32 @@ void NetworkPacket::WriteString(const utf8 * string)
const uint8 * NetworkPacket::Read(size_t size)
{
if (read + size > NetworkPacket::size)
if (BytesRead + size > NetworkPacket::Size)
{
return nullptr;
}
else
{
uint8 * data = &GetData()[read];
read += size;
uint8 * data = &GetData()[BytesRead];
BytesRead += size;
return data;
}
}
const utf8 * NetworkPacket::ReadString()
{
char * str = (char *)&GetData()[read];
char * str = (char *)&GetData()[BytesRead];
char * strend = str;
while (read < size && *strend != 0)
while (BytesRead < Size && *strend != 0)
{
read++;
BytesRead++;
strend++;
}
if (*strend != 0)
{
return nullptr;
}
read++;
BytesRead++;
return str;
}

View File

@@ -24,16 +24,14 @@
class NetworkPacket final
{
public:
uint16 size;
std::shared_ptr<std::vector<uint8>> data;
size_t transferred;
size_t read;
uint16 Size = 0;
std::shared_ptr<std::vector<uint8>> Data = std::make_shared<std::vector<uint8>>();
size_t BytesTransferred = 0;
size_t BytesRead = 0;
static std::unique_ptr<NetworkPacket> Allocate();
static std::unique_ptr<NetworkPacket> Duplicate(NetworkPacket& packet);
NetworkPacket();
uint8 * GetData();
uint32 GetCommand();
@@ -49,14 +47,14 @@ public:
template <typename T>
NetworkPacket & operator >>(T &value)
{
if (read + sizeof(value) > size)
if (BytesRead + sizeof(value) > Size)
{
value = 0;
}
else
{
value = ByteSwapBE(*((T *)&GetData()[read]));
read += sizeof(value);
value = ByteSwapBE(*((T *)&GetData()[BytesRead]));
BytesRead += sizeof(value);
}
return *this;
}
@@ -65,7 +63,7 @@ public:
NetworkPacket & operator <<(T value) {
T swapped = ByteSwapBE(value);
uint8 * bytes = (uint8 *)&swapped;
data->insert(data->end(), bytes, bytes + sizeof(value));
Data->insert(Data->end(), bytes, bytes + sizeof(value));
return *this;
}
};

View File

@@ -1607,7 +1607,7 @@ void Network::Client_Handle_MAP(NetworkConnection& connection, NetworkPacket& pa
{
uint32 size, offset;
packet >> size >> offset;
int chunksize = (int)(packet.size - packet.read);
int chunksize = (int)(packet.Size - packet.BytesRead);
if (chunksize <= 0) {
return;
}