Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetProtocol.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef NETPROTOCOL_H
19#define NETPROTOCOL_H
20
21#include "network/NetMessage.h"
22#include "ps/Mod.h"
23#include "ps/Pyrogenesis.h"
24
25#include <optional>
26#include <string>
27#include <type_traits>
28#include <vector>
29
30/**
31 * Report the peer if we didn't receive a packet after this time (milliseconds).
32 */
33inline constexpr std::uint32_t NETWORK_WARNING_TIMEOUT{2000};
34
36{
37 std::string componentType;
38 std::string clientComponent;
39 std::string serverComponent;
40};
41
42template <typename Message>
43Message CreateHandshake() {
44 Message handshake;
45
46 if constexpr (std::is_same_v<Message, CCliHandshakeMessage>)
47 handshake.m_MagicResponse = PS_PROTOCOL_MAGIC_RESPONSE;
48 else
49 handshake.m_Magic = PS_PROTOCOL_MAGIC;
50
51 handshake.m_ProtocolVersion = PS_PROTOCOL_VERSION;
52 handshake.m_EngineVersion = PS_SERIALIZATION_VERSION;
53
54 for (const Mod::ModData* mod : Mod::Instance().GetEnabledModsData())
55 {
56 if (mod->m_IgnoreInCompatibilityChecks)
57 continue;
58
59 typename Message::S_m_EnabledMods enabledMod;
60 enabledMod.m_Name = mod->m_Name;
61 enabledMod.m_Version = mod->m_Version;
62 handshake.m_EnabledMods.push_back(enabledMod);
63 }
64
65 return handshake;
66}
67
68std::optional<HandshakeError> CheckHandshake(const CSrvHandshakeMessage& serverMessage, const CCliHandshakeMessage& clientMessage);
69
70#endif
#define PS_PROTOCOL_MAGIC
Definition: NetMessages.h:28
#define PS_PROTOCOL_MAGIC_RESPONSE
Definition: NetMessages.h:29
#define PS_PROTOCOL_VERSION
Definition: NetMessages.h:30
constexpr std::uint32_t NETWORK_WARNING_TIMEOUT
Report the peer if we didn't receive a packet after this time (milliseconds).
Definition: NetProtocol.h:33
Message CreateHandshake()
Definition: NetProtocol.h:43
std::optional< HandshakeError > CheckHandshake(const CSrvHandshakeMessage &serverMessage, const CCliHandshakeMessage &clientMessage)
Definition: NetProtocol.cpp:36
#define PS_SERIALIZATION_VERSION
Definition: build_version.h:64
static Mod & Instance()
Definition: Mod.cpp:138
const std::vector< const Mod::ModData * > GetEnabledModsData() const
Get a list of the enabled mod's data (intended for compatibility checks).
Definition: Mod.cpp:195
Definition: NetProtocol.h:36
std::string serverComponent
Definition: NetProtocol.h:39
std::string clientComponent
Definition: NetProtocol.h:38
std::string componentType
Definition: NetProtocol.h:37
Parsed mod.json data for C++ usage.
Definition: Mod.h:41