NetworkManager class
#include <Jazz2/Multiplayer/NetworkManager.h>
Manages game-specific network connections.
Extends NetworkManagerBase with the game session layer: holds the ServerConfiguration, owns the per-peer PeerDescriptor map (including retained descriptors for reconnecting players), drives LAN ServerDiscovery and the optional WebhookClient, and overrides the connect/disconnect hooks to enforce server policy.
Base classes
- class NetworkManagerBase
- Allows to create generic network clients and servers.
Public static variables
-
static std::
uint32_t TransportProtocolVersion constexpr - Version of the transport a client connects with and a server lets in, see AcceptServerPeer().
-
static std::
uint32_t MaxPlayerNameLength constexpr - Longest name of a player in bytes.
Public static functions
- static auto CreateDefaultServerConfiguration() -> ServerConfiguration
- Creates a default server configuration from the default template file.
- static auto LoadServerConfigurationFromFile(StringView path) -> ServerConfiguration
- Loads a server configuration from the specified file.
- static void ApplyPlaylistEntry(ServerConfiguration& serverConfig, const PlaylistEntry& playlistEntry)
- Overrides the per-round rules of the configuration with the values of a playlist entry.
- static auto GameModeToString(MpGameMode mode) -> StringView
- Converts MpGameMode to the string representation.
- static auto StringToGameMode(StringView value) -> MpGameMode
- Converts the non-localized string representation back to MpGameMode.
-
static auto UuidToString(StaticArrayView<Uuid::
Size, Uuid:: Type> uuid) -> String - Converts UUID to the string representation.
Constructors, destructors, conversion operators
- NetworkManager()
- Creates a new instance.
- ~NetworkManager()
- NetworkManager(const NetworkManager&) deleted
Public functions
- auto operator=(const NetworkManager&) -> NetworkManager& deleted
-
void CreateClient(INetworkHandler* handler,
StringView endpoints,
std::
uint16_t defaultPort, std:: uint32_t clientData) override - Creates a client connection to a remote server.
- auto CreateServer(INetworkHandler* handler, ServerConfiguration&& serverConfig) -> bool virtual
- Creates a server that accepts incoming connections.
- auto CreateLocalServer(INetworkHandler* handler, ServerConfiguration&& serverConfig) -> bool
- Creates a socket-less local server for local splitscreen multiplayer.
- void Dispose() override
- Disposes all active connections.
- auto GetServerConfiguration() const -> ServerConfiguration&
- Returns server configuration.
-
auto GetPeerCount() -> std::
uint32_t - Returns connected peer count.
-
auto GetPeers() -> LockedPtr<const HashMap<Peer, std::
shared_ptr<PeerDescriptor>>, Spinlock> - Returns all connected peers.
-
auto GetPeerDescriptor(LocalPeerT) -> std::
shared_ptr<PeerDescriptor> - Returns session peer descriptor for the local peer.
-
auto GetPeerDescriptor(LocalPeerT,
std::
int32_t index) -> std:: shared_ptr<PeerDescriptor> - Returns the session peer descriptor for the local splitscreen player at the given index.
-
auto AddLocalPlayer(std::
int32_t index) -> std:: shared_ptr<PeerDescriptor> - Registers (or returns the existing) descriptor for a local splitscreen player.
-
auto GetPeerDescriptor(const Peer& peer) -> std::
shared_ptr<PeerDescriptor> - Returns session peer descriptor for the specified connected remote peer.
-
auto ReclaimDisconnectedPeer(StaticArrayView<Uuid::
Size, Uuid:: Type> uuid) -> std:: shared_ptr<PeerDescriptor> - Tries to reclaim a recently disconnected peer's descriptor by unique player ID.
- void ClearDisconnectedPeerStates()
- Clears the restorable state of all retained disconnected peers (e.g., when a new round starts).
- auto HasInboundConnections() const -> bool
- Returns
trueif there are any inbound connections. - void RefreshServerConfiguration()
- Reloads server configuration from the source file.
-
void SetStatusProvider(std::
weak_ptr<IServerStatusProvider> statusProvider) - Sets status provider.
- auto GetWebhook() const -> WebhookClient*
- Returns the webhook client, or
nullptrif no webhook is configured. -
auto AcceptServerPeer(const Peer& peer,
std::
uint32_t clientData) -> ConnectionResult - Decides whether a peer that connects to the server is let in, as every server does.
-
auto HandleServerPacket(const Peer& peer,
std::
uint8_t packetType, ArrayView<const std:: uint8_t> data) -> bool - Handles what every server handles before its level handler, called from INetworkHandler::
OnPacketReceived().
Protected functions
-
auto OnPeerConnected(const Peer& peer,
std::
uint32_t clientData) -> ConnectionResult override - Called when a peer connects to the local server or the local client connects to a server.
- void OnPeerDisconnected(const Peer& peer, Reason reason) override
- Called when a peer disconnects from the local server or the local client disconnects from a server.
Function documentation
static ServerConfiguration Jazz2:: Multiplayer:: NetworkManager:: CreateDefaultServerConfiguration()
Creates a default server configuration from the default template file.
This method reads a JSON configuration file described in ServerConfiguration. If the JSON contains a "$include" directive, it recursively loads the referenced files.
static ServerConfiguration Jazz2:: Multiplayer:: NetworkManager:: LoadServerConfigurationFromFile(StringView path)
Loads a server configuration from the specified file.
This method reads a JSON configuration file described in ServerConfiguration. If the JSON contains a "$include" directive, it recursively loads the referenced files.
static void Jazz2:: Multiplayer:: NetworkManager:: ApplyPlaylistEntry(ServerConfiguration& serverConfig,
const PlaylistEntry& playlistEntry)
Overrides the per-round rules of the configuration with the values of a playlist entry.
Every per-round property of PlaylistEntry is copied except PlaylistEntry::
bool Jazz2:: Multiplayer:: NetworkManager:: CreateLocalServer(INetworkHandler* handler,
ServerConfiguration&& serverConfig)
Creates a socket-less local server for local splitscreen multiplayer.
Behaves like a server (authoritative game state, NetworkState::
std:: shared_ptr<PeerDescriptor> Jazz2:: Multiplayer:: NetworkManager:: GetPeerDescriptor(LocalPeerT,
std:: int32_t index)
Returns the session peer descriptor for the local splitscreen player at the given index.
Index zero is the default local peer (same as GetPeerDescriptor(LocalPeerT)). Returns nullptr if no such local player has been registered with AddLocalPlayer().
std:: shared_ptr<PeerDescriptor> Jazz2:: Multiplayer:: NetworkManager:: AddLocalPlayer(std:: int32_t index)
Registers (or returns the existing) descriptor for a local splitscreen player.
The descriptor is inserted into the peer map under a synthetic local key (see Peer::Local) so the shared game-mode logic iterates it like any other player, while its PeerDescriptor::
std:: shared_ptr<PeerDescriptor> Jazz2:: Multiplayer:: NetworkManager:: ReclaimDisconnectedPeer(StaticArrayView<Uuid:: Size, Uuid:: Type> uuid)
Tries to reclaim a recently disconnected peer's descriptor by unique player ID.
Lets a reconnecting player resume. Returns the retained descriptor (with its restorable state), or nullptr if there is none within the reconnect window.
ConnectionResult Jazz2:: Multiplayer:: NetworkManager:: AcceptServerPeer(const Peer& peer,
std:: uint32_t clientData)
Decides whether a peer that connects to the server is let in, as every server does.
Checks the version of the transport the client connects with and whether the server is full. Meant to be called from INetworkHandler::
bool Jazz2:: Multiplayer:: NetworkManager:: HandleServerPacket(const Peer& peer,
std:: uint8_t packetType,
ArrayView<const std:: uint8_t> data)
Handles what every server handles before its level handler, called from INetworkHandler::
Answers a ping and authenticates a peer by its authentication packet — the protocol version, the player name, bans, the whitelist, the password and the 3rd party authentication — and gives a player that reconnects back what it had. Returns true if the packet was handled completely (answered, or the peer was kicked), false if the level handler should have it too.