Files
OpenRCT2-Web/src/openrct2/network/NetworkPacket.cpp
T

114 lines
2.6 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2016-05-28 22:27:08 +01:00
#ifndef DISABLE_NETWORK
#include "NetworkPacket.h"
#include "NetworkTypes.h"
2018-06-22 23:02:47 +02:00
#include <memory>
2017-12-03 23:45:43 +01:00
namespace OpenRCT2::Network
{
Packet::Packet(Command id) noexcept
: Header{ 0, id }
2018-06-22 23:02:47 +02:00
{
}
2021-09-15 22:22:15 +02:00
uint8_t* Packet::GetData() noexcept
{
return Data.data();
}
const uint8_t* Packet::GetData() const noexcept
{
return Data.data();
}
Command Packet::GetCommand() const noexcept
{
return Header.Id;
}
void Packet::Clear() noexcept
{
BytesTransferred = 0;
BytesRead = 0;
Data.clear();
}
bool Packet::CommandRequiresAuth() const noexcept
{
switch (GetCommand())
{
case Command::ping:
case Command::auth:
case Command::token:
case Command::gameInfo:
case Command::objectsList:
case Command::scriptsHeader:
case Command::scriptsData:
case Command::mapRequest:
case Command::heartbeat:
return false;
default:
return true;
}
}
void Packet::Write(const void* bytes, size_t size)
{
const uint8_t* src = reinterpret_cast<const uint8_t*>(bytes);
Data.insert(Data.end(), src, src + size);
}
void Packet::WriteString(std::string_view s)
{
Write(s.data(), s.size());
Data.push_back(0);
}
const uint8_t* Packet::Read(size_t size)
{
if (BytesRead + size > Data.size())
{
return nullptr;
}
const uint8_t* data = Data.data() + BytesRead;
BytesRead += size;
return data;
}
std::string_view Packet::ReadString()
{
if (BytesRead >= Data.size())
return {};
const char* str = reinterpret_cast<const char*>(Data.data() + BytesRead);
size_t stringLen = 0;
while (BytesRead < Data.size() && str[stringLen] != '\0')
{
BytesRead++;
stringLen++;
}
if (str[stringLen] != '\0')
return {};
// Skip null terminator.
2017-01-12 17:45:48 +00:00
BytesRead++;
return std::string_view(str, stringLen);
}
} // namespace OpenRCT2::Network
2016-05-28 22:27:08 +01:00
#endif