1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-21 05:53:02 +01:00
Files
OpenRCT2/src/openrct2/network/NetworkPacket.h
ζeh Matt 8490e87eeb Close #12422: Refactor NETWORK_COMMAND to use strong enum (#12481)
* Refactor NETWORK_COMMAND to strong enum

* Refactor all uses of NETWORK_COMMAND

Closes #12422
2020-07-28 17:24:40 +01:00

72 lines
2.0 KiB
C++

/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include "../common.h"
#include "../core/DataSerialiser.h"
#include "NetworkTypes.h"
#include <memory>
#include <vector>
class NetworkPacket final
{
public:
uint16_t Size = 0;
std::shared_ptr<std::vector<uint8_t>> Data = std::make_shared<std::vector<uint8_t>>();
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();
NetworkCommand GetCommand() const;
void Clear();
bool CommandRequiresAuth();
const uint8_t* Read(size_t size);
const utf8* ReadString();
void Write(const uint8_t* bytes, size_t size);
void WriteString(const utf8* string);
template<typename T> NetworkPacket& operator>>(T& value)
{
if (BytesRead + sizeof(value) > Size)
{
value = T{};
}
else
{
T local;
std::memcpy(&local, &GetData()[BytesRead], sizeof(local));
value = ByteSwapBE(local);
BytesRead += sizeof(value);
}
return *this;
}
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));
return *this;
}
NetworkPacket& operator<<(DataSerialiser& data)
{
Write(static_cast<const uint8_t*>(data.GetStream().GetData()), data.GetStream().GetLength());
return *this;
}
};