2016-05-28 17:54:45 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2016-05-28 17:54:45 +01:00
|
|
|
*
|
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
|
2016-05-28 17:54:45 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-28 17:54:45 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2016-05-28 22:27:08 +01:00
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "NetworkPacket.h"
|
2016-05-28 17:54:45 +01:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "NetworkTypes.h"
|
2018-06-22 23:02:47 +02:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <memory>
|
2017-12-03 23:45:43 +01:00
|
|
|
|
2025-08-31 15:26:10 +02:00
|
|
|
namespace OpenRCT2::Network
|
2020-08-02 22:31:00 +02:00
|
|
|
{
|
2025-09-05 13:17:14 +02:00
|
|
|
Packet::Packet(Command id) noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
: Header{ 0, id }
|
2018-06-22 23:02:47 +02:00
|
|
|
{
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
uint8_t* Packet::GetData() noexcept
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2025-08-31 15:26:10 +02:00
|
|
|
return Data.data();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
const uint8_t* Packet::GetData() const noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
return Data.data();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
Command Packet::GetCommand() const noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
return Header.Id;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Packet::Clear() noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
BytesTransferred = 0;
|
|
|
|
|
BytesRead = 0;
|
|
|
|
|
Data.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
bool Packet::CommandRequiresAuth() const noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
switch (GetCommand())
|
|
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
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:
|
2025-08-31 15:26:10 +02:00
|
|
|
return false;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Packet::Write(const void* bytes, size_t size)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
const uint8_t* src = reinterpret_cast<const uint8_t*>(bytes);
|
|
|
|
|
Data.insert(Data.end(), src, src + size);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Packet::WriteString(std::string_view s)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
Write(s.data(), s.size());
|
|
|
|
|
Data.push_back(0);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
const uint8_t* Packet::Read(size_t size)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
if (BytesRead + size > Data.size())
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uint8_t* data = Data.data() + BytesRead;
|
|
|
|
|
BytesRead += size;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
std::string_view Packet::ReadString()
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
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++;
|
2025-08-31 15:26:10 +02:00
|
|
|
|
|
|
|
|
return std::string_view(str, stringLen);
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
2025-08-31 15:26:10 +02:00
|
|
|
} // namespace OpenRCT2::Network
|
2016-05-28 22:27:08 +01:00
|
|
|
|
|
|
|
|
#endif
|