Bomberman Multiplayer
Authoritative multiplayer networking layer for Bomberman.
Loading...
Searching...
No Matches
bomberman::net::NetClient Class Reference

ENet client connection and protocol endpoint. More...

#include <NetClient.h>

Classes

struct  ClientLiveStats
 Live multiplayer/network HUD state updated during gameplay. More...
 
struct  GameplayEvent
 One reliable gameplay event dequeued in original receive order. More...
 
struct  Impl
 

Public Member Functions

 NetClient ()
 Constructs an idle client with no active transport.
 
 ~NetClient () noexcept
 Disconnects locally if needed and releases ENet resources.
 
 NetClient (const NetClient &)=delete
 Non-copyable.
 
NetClientoperator= (const NetClient &)=delete
 
 NetClient (NetClient &&)=delete
 Non-movable.
 
NetClientoperator= (NetClient &&)=delete
 
void beginConnect (const std::string &host, uint16_t port, std::string_view playerName)
 Starts a non-blocking connect attempt.
 
bool disconnectBlocking ()
 Attempts a blocking graceful disconnect, then releases local transport resources.
 
void disconnectAsync ()
 Starts a non-blocking graceful disconnect for an active session.
 
void cancelConnect ()
 Cancels an in-progress connect or handshake attempt.
 
void pumpNetwork (uint16_t timeoutMs=0)
 Pumps ENet events for this client host.
 
std::optional< uint32_t > sendInput (uint8_t buttons)
 Records a button bitmask and queues a batched input packet.
 
bool sendLobbyReady (bool ready)
 Sends an authoritative lobby ready-state request for the local accepted seat.
 
bool sendMatchLoaded (uint32_t matchId)
 Acknowledges that the gameplay scene for matchId has been constructed locally.
 
void flushOutgoing () const
 Flushes any queued outgoing ENet packets immediately.
 
bool isConnected () const
 Returns true when an active session is connected.
 
EConnectState connectState () const
 Returns the current connection state.
 
const std::optional< MsgReject::EReason > & lastRejectReason () const
 Returns the last explicit server reject reason, if any.
 
void setDiagnosticsConfig (bool enabled, bool predictionEnabled, bool remoteSmoothingEnabled)
 Configures client diagnostics behavior for future connect sessions.
 
ClientDiagnosticsclientDiagnostics ()
 Returns the client diagnostics recorder.
 
const ClientDiagnosticsclientDiagnostics () const
 Returns the client diagnostics recorder.
 
void updateLiveTransportStats (uint32_t rttMs, uint32_t rttVarianceMs, uint32_t lossPermille, uint32_t lastSnapshotTick, uint32_t lastCorrectionTick, uint32_t snapshotAgeMs, uint32_t gameplaySilenceMs)
 Updates the live transport portion of the multiplayer HUD state.
 
void updateLivePredictionStats (bool predictionActive, bool recoveryActive, uint32_t correctionCount, uint32_t mismatchCount, uint32_t lastCorrectionDeltaQ, uint32_t maxPendingInputDepth)
 Updates the live prediction portion of the multiplayer HUD state.
 
const ClientLiveStatsliveStats () const
 Returns the current live multiplayer HUD state.
 
uint8_t playerId () const
 Returns the server-assigned player id, or NetClient::kInvalidPlayerId before connect.
 
uint16_t serverTickRate () const
 Returns negotiated server tick rate. Valid only after a successful handshake.
 
bool tryGetLatestLobbyState (MsgLobbyState &out) const
 Copies the newest cached lobby state for the current session.
 
uint32_t lobbySilenceMs () const
 Returns milliseconds since the last authoritative lobby-state update.
 
bool consumePendingLevelInfo (MsgLevelInfo &out)
 Consumes the newest unhandled round-start LevelInfo for the current session.
 
bool tryGetLatestSnapshot (MsgSnapshot &out) const
 Copies the newest cached snapshot for the current session.
 
uint32_t lastSnapshotTick () const
 Returns the server tick of the newest cached snapshot, or 0 if none is cached.
 
bool tryGetLatestCorrection (MsgCorrection &out) const
 Copies the newest cached owner correction for the current session.
 
uint32_t lastCorrectionTick () const
 Returns the server tick of the newest cached correction, or 0 if none is cached.
 
bool tryDequeueGameplayEvent (GameplayEvent &out)
 Pops the oldest pending reliable gameplay event for the current session.
 
bool hasBrokenGameplayEventStream () const
 Returns true once the reliable gameplay-event stream can no longer be trusted.
 
uint32_t gameplaySilenceMs () const
 Returns milliseconds since the last snapshot or correction.
 
bool tryGetLatestMatchStart (MsgMatchStart &out) const
 Copies the newest cached match-start timing edge for the current session.
 
bool hasMatchStarted (uint32_t matchId) const
 Returns true after the server has explicitly started matchId for this session.
 
bool isMatchCancelled (uint32_t matchId) const
 Returns true after the server has explicitly cancelled matchId back to the lobby.
 
bool tryGetLatestMatchResult (MsgMatchResult &out) const
 Copies the newest cached authoritative match result for the current session.
 
bool tryGetMapSeed (uint32_t &outSeed) const
 Returns the cached map seed from the current session's latest LevelInfo.
 

Static Public Attributes

static constexpr uint8_t kInvalidPlayerId = 0xFF
 Sentinel value indicating no player id has been assigned yet.
 

Detailed Description

ENet client connection and protocol endpoint.

Owns the client-side ENet host and peer lifecycle, drives the async connect and handshake flow, pumps incoming packets, caches the latest session data, and sends runtime input batches during gameplay.

A session is considered connected once Welcome has been received successfully.

Member Function Documentation

◆ beginConnect()

void bomberman::net::NetClient::beginConnect ( const std::string &  host,
uint16_t  port,
std::string_view  playerName 
)

Starts a non-blocking connect attempt.

Parameters
hostServer hostname or IP address.
portServer port.
playerNamePlayer name sent in the Hello payload.

If transport setup succeeds, the client enters Connecting. Connected state means the server accepted the session and the client may enter lobby flow immediately.

Note
Use NetClient::connectState to observe progress or failure.

◆ cancelConnect()

void bomberman::net::NetClient::cancelConnect ( )

Cancels an in-progress connect or handshake attempt.

Safe to call in any state. If the client is currently Connecting or Handshaking, transport resources are released and state returns to Disconnected. Otherwise, this is a no-op.

◆ consumePendingLevelInfo()

bool bomberman::net::NetClient::consumePendingLevelInfo ( MsgLevelInfo out)

Consumes the newest unhandled round-start LevelInfo for the current session.

Returns false until a newer LevelInfo arrives. Once returned successfully, the same cached message is not returned again until a later LevelInfo replaces it.

◆ disconnectAsync()

void bomberman::net::NetClient::disconnectAsync ( )

Starts a non-blocking graceful disconnect for an active session.

If called during Connecting or Handshaking, the in-progress attempt is cancelled locally and state returns to Disconnected. Otherwise, connection state remains Disconnecting until NetClient::pumpNetwork observes completion or timeout.

Warning
Callers must continue pumping the client for completion or timeout handling.

◆ disconnectBlocking()

bool bomberman::net::NetClient::disconnectBlocking ( )

Attempts a blocking graceful disconnect, then releases local transport resources.

If called during Connecting or Handshaking, the in-progress attempt is cancelled locally and this returns false.

Warning
This function may block for up to the disconnect timeout while waiting for ENet disconnect completion.
Returns
true if the remote disconnect handshake completed before local teardown.

◆ gameplaySilenceMs()

uint32_t bomberman::net::NetClient::gameplaySilenceMs ( ) const

Returns milliseconds since the last snapshot or correction.

If gameplay traffic has not arrived yet for the current session, the timer runs from handshake completion instead.

◆ hasBrokenGameplayEventStream()

bool bomberman::net::NetClient::hasBrokenGameplayEventStream ( ) const

Returns true once the reliable gameplay-event stream can no longer be trusted.

The current client policy is fail-fast: if reliable gameplay events overflow locally, the active match should be abandoned instead of continuing with potentially divergent world state.

◆ lobbySilenceMs()

uint32_t bomberman::net::NetClient::lobbySilenceMs ( ) const

Returns milliseconds since the last authoritative lobby-state update.

If no lobby-state update has arrived yet for the current session, the timer runs from handshake completion instead.

◆ pumpNetwork()

void bomberman::net::NetClient::pumpNetwork ( uint16_t  timeoutMs = 0)

Pumps ENet events for this client host.

Evaluates connect and disconnect timeouts and dispatches any received protocol packets.

Parameters
timeoutMsMaximum wait in milliseconds. Defaults to non-blocking.

◆ sendInput()

std::optional< uint32_t > bomberman::net::NetClient::sendInput ( uint8_t  buttons)

Records a button bitmask and queues a batched input packet.

Parameters
buttonsButton bitmask (kInput* flags).
Returns
The assigned local input sequence, or std::nullopt if the client is not currently connected.

◆ sendLobbyReady()

bool bomberman::net::NetClient::sendLobbyReady ( bool  ready)

Sends an authoritative lobby ready-state request for the local accepted seat.

The server remains the source of truth; successful sends are reflected back through later LobbyState updates.

Returns
true if the request was queued successfully.

◆ sendMatchLoaded()

bool bomberman::net::NetClient::sendMatchLoaded ( uint32_t  matchId)

Acknowledges that the gameplay scene for matchId has been constructed locally.

This is a low-frequency reliable control message used only during the authoritative round-start handoff.

Returns
true if the request was queued successfully.

◆ tryDequeueGameplayEvent()

bool bomberman::net::NetClient::tryDequeueGameplayEvent ( GameplayEvent out)

Pops the oldest pending reliable gameplay event for the current session.

Returns false when no pending event is queued or the reliable gameplay-event stream has been invalidated for the current match.

◆ tryGetLatestCorrection()

bool bomberman::net::NetClient::tryGetLatestCorrection ( MsgCorrection out) const

Copies the newest cached owner correction for the current session.

Returns false if no valid correction has been received since the most recent successful connect.

◆ tryGetLatestLobbyState()

bool bomberman::net::NetClient::tryGetLatestLobbyState ( MsgLobbyState out) const

Copies the newest cached lobby state for the current session.

Returns false until at least one valid authoritative lobby-state message has been received for the current session.

◆ tryGetLatestSnapshot()

bool bomberman::net::NetClient::tryGetLatestSnapshot ( MsgSnapshot out) const

Copies the newest cached snapshot for the current session.

Returns false if no valid snapshot has been received since the most recent connect.

◆ tryGetMapSeed()

bool bomberman::net::NetClient::tryGetMapSeed ( uint32_t &  outSeed) const

Returns the cached map seed from the current session's latest LevelInfo.

The cache is cleared on disconnect or reset. In lobby-only states no LevelInfo may have been received yet.


The documentation for this class was generated from the following files: