1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-21 23:03:04 +01:00

Move network units to OpenRCT2::Network namespace

This commit is contained in:
Aaron van Geffen
2025-08-31 15:26:10 +02:00
parent d6e9f3d205
commit 6ea5959b2b
28 changed files with 7396 additions and 7311 deletions

View File

@@ -24,16 +24,13 @@
#include <chrono>
#include <discord_rpc.h>
using namespace OpenRCT2;
namespace
namespace OpenRCT2::Network
{
using namespace std::chrono_literals;
constexpr const char* kApplicationID = "378612438200877056";
constexpr const char* kSteamAppID = nullptr;
constexpr auto kRefreshInterval = 5.0s;
} // namespace
static void OnReady([[maybe_unused]] const DiscordUser* request)
{
@@ -149,5 +146,6 @@ void DiscordService::RefreshPresence() const
Discord_UpdatePresence(&discordPresence);
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -15,6 +15,8 @@
#include <limits>
namespace OpenRCT2::Network
{
class DiscordService final
{
private:
@@ -29,5 +31,6 @@ public:
private:
void RefreshPresence() const;
};
} // namespace OpenRCT2::Network
#endif

View File

@@ -19,12 +19,6 @@
#include <string_view>
#include <vector>
constexpr uint16_t kNetworkDefaultPort = 11753;
constexpr uint16_t kNetworkLanBroadcastPort = 11754;
constexpr const char* kNetworkLanBroadcastMsg = "openrct2.server.query";
constexpr const char* kMasterServerURL = "https://servers.openrct2.io";
constexpr uint16_t kMaxServerDescriptionLength = 256;
struct Peep;
struct CoordsXYZ;
@@ -36,6 +30,14 @@ namespace OpenRCT2::GameActions
class Result;
} // namespace OpenRCT2::GameActions
namespace OpenRCT2::Network
{
constexpr uint16_t kNetworkDefaultPort = 11753;
constexpr uint16_t kNetworkLanBroadcastPort = 11754;
constexpr const char* kNetworkLanBroadcastMsg = "openrct2.server.query";
constexpr const char* kMasterServerURL = "https://servers.openrct2.io";
constexpr uint16_t kMaxServerDescriptionLength = 256;
enum class NetworkPermission : uint32_t;
void NetworkReconnect();
@@ -118,3 +120,4 @@ void NetworkAppendServerLog(const utf8* text);
[[nodiscard]] NetworkStats NetworkGetStats();
[[nodiscard]] NetworkServerState NetworkGetServerState();
[[nodiscard]] json_t NetworkGetServerInfoAsJson();
} // namespace OpenRCT2::Network

View File

@@ -16,6 +16,8 @@
#include <algorithm>
namespace OpenRCT2::Network
{
NetworkPermission NetworkActions::FindCommand(GameCommand command)
{
auto it = std::find_if(Actions.begin(), Actions.end(), [&command](NetworkAction const& action) {
@@ -265,5 +267,6 @@ const std::array<NetworkAction, static_cast<size_t>(NetworkPermission::Count)> N
},
},
};
} // namespace OpenRCT2::Network
#endif

View File

@@ -16,6 +16,8 @@
#include <string>
#include <vector>
namespace OpenRCT2::Network
{
enum class NetworkPermission : uint32_t
{
Chat,
@@ -61,3 +63,4 @@ public:
static NetworkPermission FindCommand(GameCommand command);
static NetworkPermission FindCommandByPermissionName(const std::string& permission_name);
};
} // namespace OpenRCT2::Network

View File

@@ -43,8 +43,6 @@
#include <iterator>
#include <stdexcept>
using namespace OpenRCT2;
// This string specifies which version of network stream current build uses.
// It is used for making sure only compatible builds get connected, even within
// single OpenRCT2 version.
@@ -567,7 +565,8 @@ void NetworkBase::UpdateClient()
auto intent = Intent(WindowClass::NetworkStatus);
intent.PutExtra(INTENT_EXTRA_MESSAGE, std::string{ str_resolving });
intent.PutExtra(INTENT_EXTRA_CALLBACK, []() -> void { ::GetContext()->GetNetwork().Close(); });
intent.PutExtra(
INTENT_EXTRA_CALLBACK, []() -> void { OpenRCT2::GetContext()->GetNetwork().Close(); });
ContextOpenIntent(&intent);
}
break;
@@ -582,7 +581,8 @@ void NetworkBase::UpdateClient()
auto intent = Intent(WindowClass::NetworkStatus);
intent.PutExtra(INTENT_EXTRA_MESSAGE, std::string{ str_connecting });
intent.PutExtra(INTENT_EXTRA_CALLBACK, []() -> void { ::GetContext()->GetNetwork().Close(); });
intent.PutExtra(
INTENT_EXTRA_CALLBACK, []() -> void { OpenRCT2::GetContext()->GetNetwork().Close(); });
ContextOpenIntent(&intent);
server_connect_time = Platform::GetTicks();
@@ -635,7 +635,8 @@ void NetworkBase::UpdateClient()
if (_serverConnection->GetLastDisconnectReason())
{
const char* disconnect_reason = _serverConnection->GetLastDisconnectReason();
FormatStringLegacy(str_disconnected, 256, STR_MULTIPLAYER_DISCONNECTED_WITH_REASON, &disconnect_reason);
FormatStringLegacy(
str_disconnected, 256, STR_MULTIPLAYER_DISCONNECTED_WITH_REASON, &disconnect_reason);
}
else
{
@@ -790,8 +791,8 @@ void NetworkBase::SendPacketToClients(const NetworkPacket& packet, bool front, b
{
// If marked as game command we can not send the packet to connections that are not fully connected.
// Sending the packet would cause the client to store a command that is behind the tick where he starts,
// which would be essentially never executed. The clients do not require commands before the server has not sent the
// map data.
// which would be essentially never executed. The clients do not require commands before the server has not sent
// the map data.
if (client_connection->Player == nullptr)
{
continue;
@@ -826,7 +827,8 @@ bool NetworkBase::CheckSRAND(uint32_t tick, uint32_t srand0)
std::string clientSpriteHash = checksum.ToString();
if (clientSpriteHash != storedTick.spriteHash)
{
LOG_INFO("Sprite hash mismatch, client = %s, server = %s", clientSpriteHash.c_str(), storedTick.spriteHash.c_str());
LOG_INFO(
"Sprite hash mismatch, client = %s, server = %s", clientSpriteHash.c_str(), storedTick.spriteHash.c_str());
return false;
}
}
@@ -994,8 +996,8 @@ uint8_t NetworkBase::GetGroupIDByHash(const std::string& keyhash)
else
{
LOG_WARNING(
"User %s is assigned to non-existent group %u. Assigning to default group (%u)", keyhash.c_str(), assignedGroup,
groupId);
"User %s is assigned to non-existent group %u. Assigning to default group (%u)", keyhash.c_str(),
assignedGroup, groupId);
}
}
return groupId;
@@ -1120,7 +1122,8 @@ void NetworkBase::LoadGroups()
group_list.at(0)->ActionsAllowed.fill(0xFF);
}
std::string NetworkBase::BeginLog(const std::string& directory, const std::string& midName, const std::string& filenameFormat)
std::string NetworkBase::BeginLog(
const std::string& directory, const std::string& midName, const std::string& filenameFormat)
{
utf8 filename[256];
time_t timer;
@@ -2556,7 +2559,8 @@ void NetworkBase::Client_Handle_GAMESTATE(NetworkConnection& connection, Network
{
GameStateCompareData cmpData = snapshots->Compare(serverSnapshot, *desyncSnapshot);
std::string outputPath = GetContext().GetPlatformEnvironment().GetDirectoryPath(DirBase::user, DirId::desyncLogs);
std::string outputPath = GetContext().GetPlatformEnvironment().GetDirectoryPath(
DirBase::user, DirId::desyncLogs);
Path::CreateDirectory(outputPath);
@@ -3657,7 +3661,8 @@ GameActions::Result NetworkModifyGroups(
{
if (groupId == 0)
{
return GameActions::Result(GameActions::Status::Disallowed, STR_THIS_GROUP_CANNOT_BE_MODIFIED, kStringIdNone);
return GameActions::Result(
GameActions::Status::Disallowed, STR_THIS_GROUP_CANNOT_BE_MODIFIED, kStringIdNone);
}
for (const auto& it : network.player_list)
{
@@ -3677,7 +3682,8 @@ GameActions::Result NetworkModifyGroups(
{
if (groupId == 0)
{ // can't change admin group permissions
return GameActions::Result(GameActions::Status::Disallowed, STR_THIS_GROUP_CANNOT_BE_MODIFIED, kStringIdNone);
return GameActions::Result(
GameActions::Status::Disallowed, STR_THIS_GROUP_CANNOT_BE_MODIFIED, kStringIdNone);
}
NetworkGroup* mygroup = nullptr;
NetworkPlayer* player = network.GetPlayerByID(actionPlayerId);
@@ -3780,7 +3786,8 @@ GameActions::Result NetworkKickPlayer(NetworkPlayerId_t playerId, bool isExecuti
{
// Player might be already removed by the PLAYERLIST command, need to refactor non-game commands executing too
// early.
return GameActions::Result(GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_PLAYER_NOT_FOUND);
return GameActions::Result(
GameActions::Status::InvalidParameters, STR_ERR_INVALID_PARAMETER, STR_ERR_PLAYER_NOT_FOUND);
}
if (player->Flags & NETWORK_PLAYER_FLAG_ISSERVER)
@@ -4086,7 +4093,13 @@ json_t NetworkGetServerInfoAsJson()
auto& network = GetContext()->GetNetwork();
return network.GetServerInfoAsJson();
}
#else
} // namespace OpenRCT2::Network
#else // DISABLE_NETWORK
namespace OpenRCT2::Network
{
int32_t NetworkGetMode()
{
return NETWORK_MODE_NONE;
@@ -4353,4 +4366,6 @@ json_t NetworkGetServerInfoAsJson()
{
return {};
}
} // namespace OpenRCT2::Network
#endif /* DISABLE_NETWORK */

View File

@@ -22,6 +22,8 @@ namespace OpenRCT2
struct IContext;
}
namespace OpenRCT2::Network
{
class NetworkBase : public OpenRCT2::System
{
public:
@@ -140,7 +142,8 @@ public: // Client
void Client_Send_RequestGameState(uint32_t tick);
void Client_Send_TOKEN();
void Client_Send_AUTH(
const std::string& name, const std::string& password, const std::string& pubkey, const std::vector<uint8_t>& signature);
const std::string& name, const std::string& password, const std::string& pubkey,
const std::vector<uint8_t>& signature);
void Client_Send_CHAT(const char* text);
void Client_Send_GAME_ACTION(const OpenRCT2::GameActions::GameAction* action);
void Client_Send_PING();
@@ -252,5 +255,6 @@ private: // Client Data
bool _clientMapLoaded = false;
ServerScriptsData _serverScriptsData{};
};
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -4,9 +4,12 @@
#ifndef DISABLE_NETWORK
namespace OpenRCT2::Network
{
class NetworkClient final : public NetworkBase
{
public:
};
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -19,8 +19,8 @@
#include <sfl/small_vector.hpp>
using namespace OpenRCT2;
namespace OpenRCT2::Network
{
static constexpr size_t kNetworkDisconnectReasonBufSize = 256;
static constexpr size_t kNetworkBufferSize = (1024 * 64) - 1; // 64 KiB, maximum packet size.
#ifndef DEBUG
@@ -231,5 +231,6 @@ void NetworkConnection::RecordPacketStats(const NetworkPacket& packet, bool send
Stats.bytesReceived[EnumValue(NetworkStatisticsGroup::Total)] += packetSize;
}
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -20,13 +20,15 @@
#include <string_view>
#include <vector>
class NetworkPlayer;
namespace OpenRCT2
{
struct ObjectRepositoryItem;
}
namespace OpenRCT2::Network
{
class NetworkPlayer;
class NetworkConnection final
{
public:
@@ -66,5 +68,6 @@ private:
void RecordPacketStats(const NetworkPacket& packet, bool sending);
};
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -15,8 +15,8 @@
#include "NetworkAction.h"
#include "NetworkTypes.h"
using namespace OpenRCT2;
namespace OpenRCT2::Network
{
NetworkGroup NetworkGroup::FromJson(const json_t& jsonData)
{
Guard::Assert(jsonData.is_object(), "NetworkGroup::FromJson expects parameter jsonData to be object");
@@ -129,5 +129,6 @@ bool NetworkGroup::CanPerformCommand(GameCommand command) const
}
return false;
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -16,6 +16,8 @@
#include <string>
#include <string_view>
namespace OpenRCT2::Network
{
enum class NetworkPermission : uint32_t;
class NetworkGroup final
@@ -52,3 +54,4 @@ public:
private:
std::string _name;
};
} // namespace OpenRCT2::Network

View File

@@ -19,8 +19,8 @@
#include <vector>
using namespace OpenRCT2;
namespace OpenRCT2::Network
{
NetworkKey::NetworkKey() = default;
NetworkKey::~NetworkKey() = default;
@@ -213,5 +213,6 @@ bool NetworkKey::Verify(const uint8_t* md, const size_t len, const std::vector<u
return false;
}
}
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -25,6 +25,8 @@ namespace OpenRCT2::Crypt
class RsaKey;
}
namespace OpenRCT2::Network
{
class NetworkKey final
{
public:
@@ -45,5 +47,6 @@ private:
NetworkKey(const NetworkKey&) = delete;
std::unique_ptr<OpenRCT2::Crypt::RsaKey> _key;
};
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -15,6 +15,8 @@
#include <memory>
namespace OpenRCT2::Network
{
NetworkPacket::NetworkPacket(NetworkCommand id) noexcept
: Header{ 0, id }
{
@@ -107,5 +109,6 @@ std::string_view NetworkPacket::ReadString()
return std::string_view(str, stringLen);
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -16,6 +16,8 @@
#include <sfl/small_vector.hpp>
#include <vector>
namespace OpenRCT2::Network
{
#pragma pack(push, 1)
struct PacketHeader
{
@@ -81,3 +83,4 @@ public:
size_t BytesTransferred = 0;
size_t BytesRead = 0;
};
} // namespace OpenRCT2::Network

View File

@@ -15,6 +15,8 @@
#include "../ui/WindowManager.h"
#include "NetworkPacket.h"
namespace OpenRCT2::Network
{
void NetworkPlayer::SetName(std::string_view name)
{
// 36 == 31 + strlen(" #255");
@@ -25,15 +27,15 @@ void NetworkPlayer::Read(NetworkPacket& packet)
{
auto name = packet.ReadString();
SetName(name);
packet >> Id >> Flags >> Group >> LastAction >> LastActionCoord.x >> LastActionCoord.y >> LastActionCoord.z >> MoneySpent
>> CommandsRan;
packet >> Id >> Flags >> Group >> LastAction >> LastActionCoord.x >> LastActionCoord.y >> LastActionCoord.z
>> MoneySpent >> CommandsRan;
}
void NetworkPlayer::Write(NetworkPacket& packet)
{
packet.WriteString(Name);
packet << Id << Flags << Group << LastAction << LastActionCoord.x << LastActionCoord.y << LastActionCoord.z << MoneySpent
<< CommandsRan;
packet << Id << Flags << Group << LastAction << LastActionCoord.x << LastActionCoord.y << LastActionCoord.z
<< MoneySpent << CommandsRan;
}
void NetworkPlayer::IncrementNumCommands()
@@ -49,5 +51,6 @@ void NetworkPlayer::AddMoneySpent(money64 cost)
auto* windowMgr = OpenRCT2::Ui::GetWindowManager();
windowMgr->InvalidateByNumber(WindowClass::Player, Id);
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -17,9 +17,12 @@
#include <string_view>
#include <unordered_map>
struct NetworkPacket;
struct Peep;
namespace OpenRCT2::Network
{
struct NetworkPacket;
class NetworkPlayer final
{
public:
@@ -48,3 +51,4 @@ public:
void IncrementNumCommands();
void AddMoneySpent(money64 cost);
};
} // namespace OpenRCT2::Network

View File

@@ -4,9 +4,12 @@
#ifndef DISABLE_NETWORK
namespace OpenRCT2::Network
{
class NetworkServer final : public NetworkBase
{
public:
};
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -34,8 +34,8 @@
#include <random>
#include <string>
using namespace OpenRCT2;
namespace OpenRCT2::Network
{
enum class MasterServerStatus
{
Ok = 200,
@@ -359,5 +359,6 @@ std::unique_ptr<INetworkServerAdvertiser> CreateServerAdvertiser(uint16_t port)
{
return std::make_unique<NetworkServerAdvertiser>(port);
}
} // namespace OpenRCT2::Network
#endif // DISABLE_NETWORK

View File

@@ -11,6 +11,8 @@
#include <memory>
namespace OpenRCT2::Network
{
enum class AdvertiseStatus
{
disabled,
@@ -29,3 +31,4 @@ struct INetworkServerAdvertiser
};
[[nodiscard]] std::unique_ptr<INetworkServerAdvertiser> CreateServerAdvertiser(uint16_t port);
} // namespace OpenRCT2::Network

View File

@@ -13,6 +13,8 @@
#include "../core/EnumUtils.hpp"
#include "../ride/RideTypes.h"
namespace OpenRCT2::Network
{
enum
{
SERVER_EVENT_PLAYER_JOINED,
@@ -146,3 +148,4 @@ struct NetworkStats
uint64_t bytesReceived[EnumValue(NetworkStatisticsGroup::Max)];
uint64_t bytesSent[EnumValue(NetworkStatisticsGroup::Max)];
};
} // namespace OpenRCT2::Network

View File

@@ -22,8 +22,8 @@
#include <unordered_set>
using namespace OpenRCT2;
namespace OpenRCT2::Network
{
constexpr const utf8* kUserStoreFilename = "users.json";
std::unique_ptr<NetworkUser> NetworkUser::FromJson(const json_t& jsonData)
@@ -215,5 +215,6 @@ u8string NetworkUserManager::GetStorePath()
auto& env = OpenRCT2::GetContext()->GetPlatformEnvironment();
return Path::Combine(env.GetDirectoryPath(OpenRCT2::DirBase::user), kUserStoreFilename);
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -16,6 +16,8 @@
#include <optional>
#include <unordered_map>
namespace OpenRCT2::Network
{
class NetworkUser final
{
public:
@@ -65,3 +67,4 @@ private:
static u8string GetStorePath();
};
} // namespace OpenRCT2::Network

View File

@@ -31,8 +31,8 @@
#include <numeric>
#include <optional>
using namespace OpenRCT2;
namespace OpenRCT2::Network
{
int32_t ServerListEntry::CompareTo(const ServerListEntry& other) const
{
const auto& a = *this;
@@ -155,8 +155,9 @@ void ServerList::AddOrUpdateRange(const std::vector<ServerListEntry>& entries)
{
for (auto& existsEntry : _serverEntries)
{
auto match = std::find_if(
entries.begin(), entries.end(), [&](const ServerListEntry& entry) { return existsEntry.Address == entry.Address; });
auto match = std::find_if(entries.begin(), entries.end(), [&](const ServerListEntry& entry) {
return existsEntry.Address == entry.Address;
});
if (match != entries.end())
{
// Keep favourites
@@ -264,7 +265,8 @@ bool ServerList::WriteFavourites(const std::vector<ServerListEntry>& entries) co
}
}
std::future<std::vector<ServerListEntry>> ServerList::FetchLocalServerListAsync(const INetworkEndpoint& broadcastEndpoint) const
std::future<std::vector<ServerListEntry>> ServerList::FetchLocalServerListAsync(
const INetworkEndpoint& broadcastEndpoint) const
{
auto broadcastAddress = broadcastEndpoint.GetHostname();
return std::async(std::launch::async, [broadcastAddress] {
@@ -438,5 +440,6 @@ const char* MasterServerException::what() const noexcept
static std::string localisedStatusText = LanguageGetString(StatusText);
return localisedStatusText.c_str();
}
} // namespace OpenRCT2::Network
#endif

View File

@@ -18,6 +18,8 @@
#include <string>
#include <vector>
namespace OpenRCT2::Network
{
struct INetworkEndpoint;
struct ServerListEntry
@@ -83,3 +85,4 @@ public:
const char* what() const noexcept override;
};
} // namespace OpenRCT2::Network

View File

@@ -73,6 +73,8 @@
#include "Socket.h"
namespace OpenRCT2::Network
{
constexpr auto kConnectTimeout = std::chrono::milliseconds(3000);
// RAII WSA initialisation needed for Windows
@@ -959,7 +961,8 @@ std::vector<std::unique_ptr<INetworkEndpoint>> GetBroadcastAddresses()
{
ifreq r;
strcpy(r.ifr_name, req->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 && (r.ifr_flags & IFF_BROADCAST) && ioctl(sock, SIOCGIFBRDADDR, &r) != -1)
if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 && (r.ifr_flags & IFF_BROADCAST)
&& ioctl(sock, SIOCGIFBRDADDR, &r) != -1)
{
baddresses.push_back(std::make_unique<NetworkEndpoint>(&r.ifr_broadaddr, sizeof(sockaddr)));
}
@@ -974,6 +977,8 @@ std::vector<std::unique_ptr<INetworkEndpoint>> GetBroadcastAddresses()
return baddresses;
}
} // namespace OpenRCT2::Network
namespace OpenRCT2::Convert
{
uint16_t HostToNetwork(uint16_t value)

View File

@@ -13,6 +13,8 @@
#include <string>
#include <vector>
namespace OpenRCT2::Network
{
enum class SocketStatus
{
Closed,
@@ -100,6 +102,7 @@ public:
[[nodiscard]] std::unique_ptr<ITcpSocket> CreateTcpSocket();
[[nodiscard]] std::unique_ptr<IUdpSocket> CreateUdpSocket();
[[nodiscard]] std::vector<std::unique_ptr<INetworkEndpoint>> GetBroadcastAddresses();
} // namespace OpenRCT2::Network
namespace OpenRCT2::Convert
{