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 "NetworkConnection.h"
|
2016-05-28 17:54:45 +01:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
#include "../Diagnostic.h"
|
|
|
|
|
#include "../core/Diagnostics.hpp"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "../core/String.hpp"
|
|
|
|
|
#include "../localisation/Formatting.h"
|
|
|
|
|
#include "../platform/Platform.h"
|
2025-02-24 04:43:06 -05:00
|
|
|
#include "Network.h"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "Socket.h"
|
2016-05-28 17:54:45 +01:00
|
|
|
|
2024-12-11 21:27:08 +02:00
|
|
|
#include <sfl/small_vector.hpp>
|
|
|
|
|
|
2025-08-31 15:26:10 +02:00
|
|
|
namespace OpenRCT2::Network
|
|
|
|
|
{
|
2025-09-05 12:53:42 +02:00
|
|
|
static constexpr size_t kDisconnectReasonBufSize = 256;
|
2025-12-03 00:06:39 +02:00
|
|
|
static constexpr size_t kBufferSize = 1024 * 128; // 128 KiB.
|
|
|
|
|
static constexpr size_t kNoDataTimeout = 40; // Seconds.
|
2024-12-11 21:18:36 +02:00
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
Connection::Connection() noexcept
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2025-12-03 00:06:39 +02:00
|
|
|
_lastReceiveTime = Platform::GetTicks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Connection::update()
|
|
|
|
|
{
|
|
|
|
|
if (!IsValid())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
receiveData();
|
|
|
|
|
SendQueuedData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Connection::receiveData()
|
|
|
|
|
{
|
|
|
|
|
uint8_t buffer[kBufferSize];
|
|
|
|
|
size_t bytesRead = 0;
|
|
|
|
|
|
|
|
|
|
ReadPacket status = Socket->ReceiveData(buffer, sizeof(buffer), &bytesRead);
|
|
|
|
|
if (status == ReadPacket::disconnected)
|
|
|
|
|
{
|
|
|
|
|
Disconnect();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == ReadPacket::success)
|
|
|
|
|
{
|
|
|
|
|
_lastReceiveTime = Platform::GetTicks();
|
|
|
|
|
_inboundBuffer.insert(_inboundBuffer.end(), buffer, buffer + bytesRead);
|
|
|
|
|
}
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
2020-08-02 23:34:02 +02:00
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
ReadPacket Connection::readPacket()
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2025-12-03 00:06:39 +02:00
|
|
|
uint32_t magic = 0;
|
2020-08-02 23:34:02 +02:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
// Check if we have enough data for the magic.
|
|
|
|
|
if (_inboundBuffer.size() < sizeof(magic))
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2025-12-03 00:06:39 +02:00
|
|
|
return ReadPacket::moreData;
|
|
|
|
|
}
|
2025-08-31 15:26:10 +02:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
// Read magic.
|
|
|
|
|
std::memcpy(&magic, _inboundBuffer.data(), sizeof(magic));
|
2025-08-31 15:26:10 +02:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
size_t totalPacketLength = 0;
|
|
|
|
|
size_t headerSize = 0;
|
2016-06-01 22:58:21 +01:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
magic = Convert::NetworkToHost(magic);
|
|
|
|
|
if (magic == PacketHeader::kMagic)
|
|
|
|
|
{
|
|
|
|
|
// New format.
|
|
|
|
|
auto& header = InboundPacket.Header;
|
|
|
|
|
std::memcpy(&header, _inboundBuffer.data(), sizeof(header));
|
|
|
|
|
|
|
|
|
|
header.magic = magic;
|
|
|
|
|
header.version = Convert::NetworkToHost(header.version);
|
|
|
|
|
header.size = Convert::NetworkToHost(header.size);
|
|
|
|
|
header.id = Convert::NetworkToHost(header.id);
|
|
|
|
|
|
|
|
|
|
headerSize = sizeof(header);
|
|
|
|
|
totalPacketLength = sizeof(header) + header.size;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Legacy format.
|
|
|
|
|
PacketLegacyHeader header{};
|
|
|
|
|
std::memcpy(&header, _inboundBuffer.data(), sizeof(header));
|
2025-08-31 15:26:10 +02:00
|
|
|
|
|
|
|
|
// Normalise values.
|
|
|
|
|
header.Size = Convert::NetworkToHost(header.Size);
|
|
|
|
|
header.Id = ByteSwapBE(header.Id);
|
|
|
|
|
|
|
|
|
|
// NOTE: For compatibility reasons for the master server we need to remove sizeof(Header.Id) from the size.
|
|
|
|
|
// Previously the Id field was not part of the header rather part of the body.
|
2025-12-03 00:06:39 +02:00
|
|
|
// We correct the size to have only the length of the body.
|
|
|
|
|
if (header.Size < sizeof(header.Id))
|
|
|
|
|
{
|
|
|
|
|
// This is a malformed packet, disconnect.
|
|
|
|
|
LOG_INFO(
|
|
|
|
|
"Received malformed packet (size: %u) from {%s}, disconnecting.", header.Size,
|
|
|
|
|
Socket->GetIpAddress().c_str());
|
2025-08-31 15:26:10 +02:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
Disconnect();
|
|
|
|
|
return ReadPacket::disconnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header.Size -= sizeof(header.Id);
|
|
|
|
|
|
|
|
|
|
// Fill in new header format.
|
|
|
|
|
InboundPacket.Header.magic = PacketHeader::kMagic;
|
|
|
|
|
InboundPacket.Header.size = header.Size;
|
|
|
|
|
InboundPacket.Header.id = header.Id;
|
|
|
|
|
|
|
|
|
|
headerSize = sizeof(header);
|
|
|
|
|
totalPacketLength = sizeof(header) + header.Size;
|
|
|
|
|
|
|
|
|
|
_isLegacyProtocol = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_inboundBuffer.size() < totalPacketLength)
|
|
|
|
|
{
|
|
|
|
|
InboundPacket.BytesTransferred = _inboundBuffer.size();
|
|
|
|
|
return ReadPacket::moreData;
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
2020-08-02 23:34:02 +02:00
|
|
|
|
2025-08-31 15:26:10 +02:00
|
|
|
// Read packet body.
|
2025-12-03 00:06:39 +02:00
|
|
|
InboundPacket.BytesTransferred = totalPacketLength;
|
|
|
|
|
InboundPacket.Write(_inboundBuffer.data() + headerSize, totalPacketLength - headerSize);
|
2019-01-29 16:35:19 +01:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
// Remove read data from buffer.
|
|
|
|
|
_inboundBuffer.erase(_inboundBuffer.begin(), _inboundBuffer.begin() + totalPacketLength);
|
2019-01-29 16:35:19 +01:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
RecordPacketStats(InboundPacket, false);
|
2025-08-31 15:26:10 +02:00
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
return ReadPacket::success;
|
2025-08-31 15:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
static sfl::small_vector<uint8_t, 512> serializePacket(bool legacyProtocol, const Packet& packet)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
sfl::small_vector<uint8_t, 512> buffer;
|
|
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
if (legacyProtocol)
|
|
|
|
|
{
|
|
|
|
|
// NOTE: For compatibility reasons for the master server we need to add sizeof(Header.Id) to the size.
|
|
|
|
|
// Previously the Id field was not part of the header rather part of the body.
|
|
|
|
|
const auto bodyLength = packet.Data.size() + sizeof(PacketLegacyHeader::Id);
|
|
|
|
|
|
|
|
|
|
Guard::Assert(bodyLength <= std::numeric_limits<uint16_t>::max(), "Packet size too large");
|
|
|
|
|
|
|
|
|
|
PacketLegacyHeader header{};
|
|
|
|
|
header.Size = static_cast<uint16_t>(bodyLength);
|
|
|
|
|
header.Size = Convert::HostToNetwork(header.Size);
|
|
|
|
|
header.Id = ByteSwapBE(packet.Header.id);
|
|
|
|
|
|
|
|
|
|
buffer.insert(
|
|
|
|
|
buffer.end(), reinterpret_cast<uint8_t*>(&header), reinterpret_cast<uint8_t*>(&header) + sizeof(header));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PacketHeader header{};
|
|
|
|
|
header.magic = Convert::HostToNetwork(PacketHeader::kMagic);
|
|
|
|
|
header.version = Convert::HostToNetwork(PacketHeader::kVersion);
|
|
|
|
|
header.size = Convert::HostToNetwork(static_cast<uint32_t>(packet.Data.size()));
|
|
|
|
|
header.id = Convert::HostToNetwork(packet.Header.id);
|
|
|
|
|
|
|
|
|
|
buffer.insert(
|
|
|
|
|
buffer.end(), reinterpret_cast<uint8_t*>(&header), reinterpret_cast<uint8_t*>(&header) + sizeof(header));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 15:26:10 +02:00
|
|
|
buffer.insert(buffer.end(), packet.Data.begin(), packet.Data.end());
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Connection::QueuePacket(const Packet& packet, bool front)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
if (AuthStatus == Auth::ok || !packet.CommandRequiresAuth())
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
2025-12-03 00:06:39 +02:00
|
|
|
const auto payload = serializePacket(_isLegacyProtocol, packet);
|
2025-08-31 15:26:10 +02:00
|
|
|
if (front)
|
|
|
|
|
{
|
|
|
|
|
_outboundBuffer.insert(_outboundBuffer.begin(), payload.begin(), payload.end());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_outboundBuffer.insert(_outboundBuffer.end(), payload.begin(), payload.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RecordPacketStats(packet, true);
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-02 23:34:02 +02:00
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Connection::Disconnect() noexcept
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2025-08-31 15:26:10 +02:00
|
|
|
ShouldDisconnect = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
bool Connection::IsValid() const
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
return !ShouldDisconnect && Socket->GetStatus() == SocketStatus::connected;
|
2025-08-31 15:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Connection::SendQueuedData()
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
if (_outboundBuffer.empty())
|
2016-05-28 17:54:45 +01:00
|
|
|
{
|
2025-08-31 15:26:10 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto bytesSent = Socket->SendData(_outboundBuffer.data(), _outboundBuffer.size());
|
|
|
|
|
|
|
|
|
|
if (bytesSent > 0)
|
|
|
|
|
{
|
|
|
|
|
_outboundBuffer.erase(_outboundBuffer.begin(), _outboundBuffer.begin() + bytesSent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 00:06:39 +02:00
|
|
|
bool Connection::ReceivedDataRecently() const noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
2025-09-05 12:53:42 +02:00
|
|
|
constexpr auto kTimeoutMs = kNoDataTimeout * 1000;
|
2025-12-03 00:06:39 +02:00
|
|
|
|
|
|
|
|
const auto timeSinceLastRecv = Platform::GetTicks() - _lastReceiveTime;
|
|
|
|
|
if (timeSinceLastRecv > kTimeoutMs)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-12-03 00:06:39 +02:00
|
|
|
|
2025-08-31 15:26:10 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
const utf8* Connection::GetLastDisconnectReason() const noexcept
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
return this->_lastDisconnectReason.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Connection::SetLastDisconnectReason(std::string_view src)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
_lastDisconnectReason = src;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Connection::SetLastDisconnectReason(const StringId string_id, void* args)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
2025-09-05 12:53:42 +02:00
|
|
|
char buffer[kDisconnectReasonBufSize];
|
|
|
|
|
FormatStringLegacy(buffer, kDisconnectReasonBufSize, string_id, args);
|
2025-08-31 15:26:10 +02:00
|
|
|
SetLastDisconnectReason(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 13:17:14 +02:00
|
|
|
void Connection::RecordPacketStats(const Packet& packet, bool sending)
|
2025-08-31 15:26:10 +02:00
|
|
|
{
|
|
|
|
|
uint32_t packetSize = static_cast<uint32_t>(packet.BytesTransferred);
|
2025-08-31 16:02:43 +02:00
|
|
|
StatisticsGroup trafficGroup;
|
2025-08-31 15:26:10 +02:00
|
|
|
|
|
|
|
|
switch (packet.GetCommand())
|
|
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
case Command::gameAction:
|
|
|
|
|
trafficGroup = StatisticsGroup::Commands;
|
2025-08-31 15:26:10 +02:00
|
|
|
break;
|
2025-08-31 16:02:43 +02:00
|
|
|
case Command::map:
|
|
|
|
|
trafficGroup = StatisticsGroup::MapData;
|
2025-08-31 15:26:10 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2025-08-31 16:02:43 +02:00
|
|
|
trafficGroup = StatisticsGroup::Base;
|
2025-08-31 15:26:10 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sending)
|
|
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
stats.bytesSent[EnumValue(trafficGroup)] += packetSize;
|
|
|
|
|
stats.bytesSent[EnumValue(StatisticsGroup::Total)] += packetSize;
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
stats.bytesReceived[EnumValue(trafficGroup)] += packetSize;
|
|
|
|
|
stats.bytesReceived[EnumValue(StatisticsGroup::Total)] += packetSize;
|
2016-05-28 17:54:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-03 00:06:39 +02:00
|
|
|
|
|
|
|
|
Command Connection::getPendingPacketCommand() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return InboundPacket.GetCommand();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Connection::getPendingPacketSize() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return InboundPacket.Header.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Connection::getPendingPacketAvailable() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return InboundPacket.BytesTransferred;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 15:26:10 +02:00
|
|
|
} // namespace OpenRCT2::Network
|
2019-01-29 16:35:19 +01:00
|
|
|
|
2016-05-28 22:27:08 +01:00
|
|
|
#endif
|