Line data Source code
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 : /** 19 : * @file 20 : * Various declarations shared by networking code. 21 : */ 22 : 23 : #ifndef NETHOST_H 24 : #define NETHOST_H 25 : 26 : #include "lib/external_libraries/enet.h" 27 : #include "ps/CStr.h" 28 : 29 : #include <map> 30 : 31 : class CNetMessage; 32 : 33 : typedef struct _ENetPeer ENetPeer; 34 : typedef struct _ENetPacket ENetPacket; 35 : typedef struct _ENetHost ENetHost; 36 : 37 0 : struct PlayerAssignment 38 : { 39 : /** 40 : * Whether the player is currently connected and active. 41 : * (We retain information on disconnected players to support rejoining, 42 : * but don't transmit these to other clients.) 43 : */ 44 : bool m_Enabled; 45 : 46 : /// Player name 47 : CStrW m_Name; 48 : 49 : /// The player that the given host controls, or -1 if none (observer) 50 : std::int32_t m_PlayerID; 51 : 52 : /// Status - Ready or not: 0 for not ready, 1 for ready, 2 to stay ready 53 : std::uint8_t m_Status; 54 : }; 55 : 56 : typedef std::map<CStr, PlayerAssignment> PlayerAssignmentMap; // map from GUID -> assignment 57 : 58 : /** 59 : * Reasons sent by server to clients in disconnection messages. 60 : * Must be kept in sync with binaries/data/mods/public/gui/common/network.js 61 : * To avoid ambiguity, use a distinct reason for each callstack leading to a disconnect. 62 : * NDR_UNKNOWN should remain reserved to the case where it is actually not a known disconnect by the server. 63 : */ 64 : enum NetDisconnectReason 65 : { 66 : NDR_UNKNOWN = 0, 67 : NDR_CONNECTION_REQUEST_TIMED_OUT, 68 : NDR_SERVER_SHUTDOWN, 69 : NDR_INCORRECT_PROTOCOL_VERSION, 70 : NDR_SERVER_LOADING, 71 : NDR_SERVER_ALREADY_IN_GAME, 72 : NDR_KICKED, 73 : NDR_BANNED, 74 : NDR_PLAYERNAME_IN_USE, 75 : NDR_SERVER_FULL, 76 : NDR_LOBBY_AUTH_FAILED, 77 : NDR_GUID_FAILED, 78 : NDR_INCORRECT_READY_TURN_COMMANDS, 79 : NDR_INCORRECT_READY_TURN_SIMULATED, 80 : NDR_SERVER_REFUSED, 81 : NDR_STUN_PORT_FAILED, 82 : NDR_STUN_ENDPOINT_FAILED, 83 : NDR_INCORRECT_SOFTWARE_VERSION 84 : }; 85 : 86 : struct DestroyHost 87 : { 88 0 : void operator()(ENetHost* host) const noexcept 89 : { 90 0 : enet_host_destroy(host); 91 0 : } 92 : }; 93 : 94 : struct DestroyPeer 95 : { 96 0 : void operator()(ENetPeer* peer) const noexcept 97 : { 98 : // Disconnect immediately (we can't wait for acks) 99 0 : enet_peer_disconnect_now(peer, NDR_SERVER_SHUTDOWN); 100 0 : } 101 : }; 102 : 103 : class CNetHost 104 : { 105 : public: 106 : static const int DEFAULT_CHANNEL = 0; 107 : 108 : /** 109 : * Transmit a message to the given peer. 110 : * @param message message to send 111 : * @param peer peer to send to 112 : * @param peerName name of peer for debug logs 113 : * @return true on success, false on failure 114 : */ 115 : static bool SendMessage(const CNetMessage* message, ENetPeer* peer, const char* peerName); 116 : 117 : /** 118 : * Construct an ENet packet by serialising the given message. 119 : * @return NULL on failure 120 : */ 121 : static ENetPacket* CreatePacket(const CNetMessage* message); 122 : 123 : /** 124 : * Initialize ENet. 125 : * This must be called before any other networking code. 126 : */ 127 : static void Initialize(); 128 : 129 : /** 130 : * Deinitialize ENet. 131 : */ 132 : static void Deinitialize(); 133 : }; 134 : 135 : #endif // NETHOST_H