Jazz2::Multiplayer::NetworkManagerBase class

Allows to create generic network clients and servers.

Wraps the ENet (and optional WebSocket) transport to run either a client or a server on a background thread, tracks connected peers and the connection state, and dispatches connect, disconnect and received-packet events to an INetworkHandler. Provides the low-level send and kick primitives that NetworkManager builds the game-specific session on top of.

Base classes

class Death::IDisposable
Base interface for releasing resources on object destruction.

Derived classes

class NetworkManager
Manages game-specific network connections.

Public types

struct ConnectionStatistics
Quality of the connection to the server.

Public static functions

static auto AddressToString(const struct in_addr& address, std::uint16_t port = 0) -> String
Converts the specified IPv4 endpoint to the string representation.
static auto AddressToString(const ENetAddress& address, bool includePort = false) -> String
Converts the specified ENet address to the string representation.
static auto IsAddressValid(StringView address) -> bool
Returns true if the specified string representation of the address is valid.
static auto IsDomainValid(StringView domain) -> bool
Returns true if the specified string representation of the domain name is valid.
static auto TrySplitAddressAndPort(StringView input, StringView& address, std::uint16_t& port) -> bool
Attempts to split the specified endpoint into address and port.
static auto SetAdhocMode(bool enabled) -> bool
Switches the transport between Wi-Fi (infrastructure) and ad hoc (local wireless) mode.
static auto IsAdhocMode() -> bool
Whether the transport is in ad hoc mode (see SetAdhocMode()).
static auto ReasonToString(Reason reason) -> const char*
Converts the specified reason to the string representation.

Constructors, destructors, conversion operators

NetworkManagerBase()
Creates a new instance.
~NetworkManagerBase()
NetworkManagerBase(const NetworkManagerBase&) deleted

Public functions

auto operator=(const NetworkManagerBase&) -> NetworkManagerBase& deleted
void CreateClient(INetworkHandler* handler, StringView endpoints, std::uint16_t defaultPort, std::uint32_t clientData) virtual
Creates a client connection to a remote server.
auto CreateServer(INetworkHandler* handler, std::uint16_t port) -> bool virtual
Creates a server that accepts incoming connections.
void Dispose() virtual
Disposes all active connections.
auto GetState() const -> NetworkState
Returns state of network connection.
auto GetRoundTripTimeMs() const -> std::uint32_t
Returns mean round trip time to the server, in milliseconds.
auto GetRoundTripTimeMs(const Peer& peer) const -> std::uint32_t
Returns mean round trip time to the server for specified peer, in milliseconds.
auto GetConnectionStatistics(ConnectionStatistics& stats) const -> bool
Returns the quality of the connection to the server.
auto GetServerEndpoints() const -> Array<String>
Returns all IPv4 and IPv6 addresses along with ports of the server.
auto GetServerPort() const -> std::uint16_t
Returns port of the server.
void SendTo(const Peer& peer, NetworkChannel channel, std::uint8_t packetType, ArrayView<const std::uint8_t> data)
Sends a packet to a given peer.
void SendTo(Function<bool(const Peer&)>&& predicate, NetworkChannel channel, std::uint8_t packetType, ArrayView<const std::uint8_t> data)
Sends a packet to all connected peers that match a given predicate.
void SendTo(AllPeersT, NetworkChannel channel, std::uint8_t packetType, ArrayView<const std::uint8_t> data)
Sends a packet to all connected peers or the remote server peer.
void Kick(const Peer& peer, Reason reason)
Kicks a given peer from the server.
auto AddressToString(const Peer& peer) const -> String
Converts the endpoint of the specified peer to the string representation.

Protected functions

void CreateLocalSession(INetworkHandler* handler)
Puts the manager into a socket-less local session state.
auto OnPeerConnected(const Peer& peer, std::uint32_t clientData) -> ConnectionResult virtual
Called when a peer connects to the local server or the local client connects to a server.
void OnPeerDisconnected(const Peer& peer, Reason reason) virtual
Called when a peer disconnects from the local server or the local client disconnects from a server.

Constants

static std::uint32_t MaxPeerCount constexpr
Maximum connected peer count.
static std::uint16_t DefaultPort constexpr
Default port used when an endpoint doesn't specify one.

Function documentation

static bool Jazz2::Multiplayer::NetworkManagerBase::SetAdhocMode(bool enabled)

Switches the transport between Wi-Fi (infrastructure) and ad hoc (local wireless) mode.

bool Jazz2::Multiplayer::NetworkManagerBase::GetConnectionStatistics(ConnectionStatistics& stats) const

Returns the quality of the connection to the server.

Returns false unless connected to a server as a client. ENet measures all of it; the WebSocket transport runs over TCP, which neither loses packets nor has the jitter measured, so only the round trip time is there.

void Jazz2::Multiplayer::NetworkManagerBase::Kick(const Peer& peer, Reason reason)

Kicks a given peer from the server.

The disconnect of the peer is then reported with this reason, even though the transport doesn't carry it back (an ENet peer the server disconnects comes back with no reason at all). The first kick counts, a repeated one before the peer is gone doesn't change it.

void Jazz2::Multiplayer::NetworkManagerBase::CreateLocalSession(INetworkHandler* handler) protected

Puts the manager into a socket-less local session state.

Used for local splitscreen multiplayer: no socket is bound and no background thread is started, but the manager reports NetworkState::Local so the game session layer treats it as an authoritative server. Sending packets is a no-op in this state.