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

235 lines
7.1 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2024 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 "NetworkConnection.h"
#include "../core/String.hpp"
#include "../localisation/Formatting.h"
#include "../platform/Platform.h"
#include "Socket.h"
#include "network.h"
2024-12-11 21:27:08 +02:00
#include <sfl/small_vector.hpp>
using namespace OpenRCT2;
static constexpr size_t kNetworkDisconnectReasonBufSize = 256;
static constexpr size_t kNetworkBufferSize = (1024 * 64) - 1; // 64 KiB, maximum packet size.
#ifndef DEBUG
static constexpr size_t kNetworkNoDataTimeout = 20; // Seconds.
#endif
static_assert(kNetworkBufferSize <= std::numeric_limits<uint16_t>::max(), "kNetworkBufferSize too big, uint16_t is max.");
2022-02-10 05:57:25 +09:00
NetworkConnection::NetworkConnection() noexcept
{
ResetLastPacketTime();
}
NetworkReadPacket NetworkConnection::ReadPacket()
{
2020-08-02 23:34:02 +02:00
size_t bytesRead = 0;
// Read packet header.
auto& header = InboundPacket.Header;
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Header))
{
2020-08-02 23:34:02 +02:00
const size_t missingLength = sizeof(header) - InboundPacket.BytesTransferred;
uint8_t* buffer = reinterpret_cast<uint8_t*>(&InboundPacket.Header);
NetworkReadPacket status = Socket->ReceiveData(buffer, missingLength, &bytesRead);
if (status != NetworkReadPacket::Success)
{
2016-06-01 22:58:21 +01:00
return status;
}
2016-11-13 20:17:49 +01:00
2020-08-02 23:34:02 +02:00
InboundPacket.BytesTransferred += bytesRead;
if (InboundPacket.BytesTransferred < sizeof(InboundPacket.Header))
{
2020-08-02 23:34:02 +02:00
// If still not enough data for header, keep waiting.
return NetworkReadPacket::MoreData;
}
2020-08-02 23:34:02 +02:00
// Normalise values.
header.Size = Convert::NetworkToHost(header.Size);
header.Id = ByteSwapBE(header.Id);
2020-08-05 16:19:57 +02:00
// 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.
header.Size -= std::min<uint16_t>(header.Size, sizeof(header.Id));
2020-08-02 23:34:02 +02:00
// Fall-through: Read rest of packet.
}
2020-08-02 23:34:02 +02:00
// Read packet body.
{
// NOTE: BytesTransfered includes the header length, this will not underflow.
2020-08-02 23:34:02 +02:00
const size_t missingLength = header.Size - (InboundPacket.BytesTransferred - sizeof(header));
uint8_t buffer[kNetworkBufferSize];
2020-08-02 23:34:02 +02:00
if (missingLength > 0)
{
NetworkReadPacket status = Socket->ReceiveData(buffer, std::min(missingLength, kNetworkBufferSize), &bytesRead);
if (status != NetworkReadPacket::Success)
{
2016-06-01 22:58:21 +01:00
return status;
}
2016-06-01 22:58:21 +01:00
2020-08-02 23:34:02 +02:00
InboundPacket.BytesTransferred += bytesRead;
InboundPacket.Write(buffer, bytesRead);
}
2020-08-02 23:34:02 +02:00
if (InboundPacket.Data.size() == header.Size)
{
2020-08-02 23:34:02 +02:00
// Received complete packet.
2022-02-18 21:57:00 +01:00
_lastPacketTime = Platform::GetTicks();
2019-01-29 16:35:19 +01:00
RecordPacketStats(InboundPacket, false);
return NetworkReadPacket::Success;
}
}
2020-08-02 23:34:02 +02:00
return NetworkReadPacket::MoreData;
}
2024-12-11 21:27:08 +02:00
static sfl::small_vector<uint8_t, 512> serializePacket(const NetworkPacket& packet)
{
2020-08-05 16:19:57 +02:00
// 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(packet.Header.Id);
Guard::Assert(bodyLength <= std::numeric_limits<uint16_t>::max(), "Packet size too large");
auto header = packet.Header;
header.Size = static_cast<uint16_t>(bodyLength);
2020-08-02 23:34:02 +02:00
header.Size = Convert::HostToNetwork(header.Size);
header.Id = ByteSwapBE(header.Id);
2024-12-11 21:27:08 +02:00
sfl::small_vector<uint8_t, 512> buffer;
buffer.reserve(sizeof(header) + packet.Data.size());
2020-08-02 23:34:02 +02:00
buffer.insert(buffer.end(), reinterpret_cast<uint8_t*>(&header), reinterpret_cast<uint8_t*>(&header) + sizeof(header));
buffer.insert(buffer.end(), packet.Data.begin(), packet.Data.end());
return buffer;
}
void NetworkConnection::QueuePacket(const NetworkPacket& packet, bool front)
{
if (AuthStatus == NetworkAuth::Ok || !packet.CommandRequiresAuth())
{
const auto payload = serializePacket(packet);
if (front)
{
_outboundBuffer.insert(_outboundBuffer.begin(), payload.begin(), payload.end());
}
else
{
_outboundBuffer.insert(_outboundBuffer.end(), payload.begin(), payload.end());
}
RecordPacketStats(packet, true);
}
}
2022-02-10 05:57:25 +09:00
void NetworkConnection::Disconnect() noexcept
{
ShouldDisconnect = true;
}
bool NetworkConnection::IsValid() const
{
return !ShouldDisconnect && Socket->GetStatus() == SocketStatus::Connected;
}
void NetworkConnection::SendQueuedData()
{
// Send queued packets.
if (_outboundBuffer.empty())
{
return;
}
size_t sent = Socket->SendData(_outboundBuffer.data(), _outboundBuffer.size());
if (sent > 0)
{
_outboundBuffer.erase(_outboundBuffer.begin(), _outboundBuffer.begin() + sent);
}
}
2022-02-10 05:57:25 +09:00
void NetworkConnection::ResetLastPacketTime() noexcept
{
2022-02-18 21:57:00 +01:00
_lastPacketTime = Platform::GetTicks();
}
2022-02-10 05:57:25 +09:00
bool NetworkConnection::ReceivedPacketRecently() const noexcept
{
#ifndef DEBUG
constexpr auto kTimeoutMs = kNetworkNoDataTimeout * 1000;
if (Platform::GetTicks() > _lastPacketTime + kTimeoutMs)
{
return false;
}
#endif
return true;
}
2022-02-10 05:57:25 +09:00
const utf8* NetworkConnection::GetLastDisconnectReason() const noexcept
{
return this->_lastDisconnectReason.c_str();
}
void NetworkConnection::SetLastDisconnectReason(std::string_view src)
{
_lastDisconnectReason = src;
}
2022-07-31 14:22:58 +02:00
void NetworkConnection::SetLastDisconnectReason(const StringId string_id, void* args)
{
char buffer[kNetworkDisconnectReasonBufSize];
OpenRCT2::FormatStringLegacy(buffer, kNetworkDisconnectReasonBufSize, string_id, args);
2016-05-28 19:03:57 +01:00
SetLastDisconnectReason(buffer);
}
2016-05-28 22:27:08 +01:00
2019-01-29 16:35:19 +01:00
void NetworkConnection::RecordPacketStats(const NetworkPacket& packet, bool sending)
{
2020-04-22 17:09:29 +02:00
uint32_t packetSize = static_cast<uint32_t>(packet.BytesTransferred);
NetworkStatisticsGroup trafficGroup;
2019-01-29 16:35:19 +01:00
switch (packet.GetCommand())
{
case NetworkCommand::GameAction:
trafficGroup = NetworkStatisticsGroup::Commands;
2019-01-29 16:35:19 +01:00
break;
case NetworkCommand::Map:
trafficGroup = NetworkStatisticsGroup::MapData;
2019-01-29 16:35:19 +01:00
break;
default:
trafficGroup = NetworkStatisticsGroup::Base;
break;
2019-01-29 16:35:19 +01:00
}
if (sending)
{
Stats.bytesSent[EnumValue(trafficGroup)] += packetSize;
Stats.bytesSent[EnumValue(NetworkStatisticsGroup::Total)] += packetSize;
2019-01-29 16:35:19 +01:00
}
else
{
Stats.bytesReceived[EnumValue(trafficGroup)] += packetSize;
Stats.bytesReceived[EnumValue(NetworkStatisticsGroup::Total)] += packetSize;
2019-01-29 16:35:19 +01:00
}
}
2016-05-28 22:27:08 +01:00
#endif