Bomberman Multiplayer
Authoritative multiplayer networking layer for Bomberman.
Loading...
Searching...
No Matches
NetCommon.h File Reference

Shared client/server wire contract for the multiplayer protocol. More...

#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string_view>
#include "Const.h"
#include "Sim/PowerupConfig.h"

Go to the source code of this file.

Classes

struct  bomberman::net::PacketHeader
 Packet metadata prefix present on every wire message. More...
 
struct  bomberman::net::MsgHello
 Hello payload sent by client during handshake. More...
 
struct  bomberman::net::MsgWelcome
 Welcome payload sent by server in response to Hello. More...
 
struct  bomberman::net::MsgReject
 Reject payload sent by server in response to Hello on failure. More...
 
struct  bomberman::net::MsgLevelInfo
 Round-start payload sent reliably by the server when a match begins. More...
 
struct  bomberman::net::MsgLobbyReady
 Client-authored desired ready state for its currently accepted lobby seat. More...
 
struct  bomberman::net::MsgMatchLoaded
 Client acknowledgement that the gameplay scene for one round start is loaded. More...
 
struct  bomberman::net::MsgMatchStart
 Explicit reliable start edge sent once all current match participants have loaded. More...
 
struct  bomberman::net::MsgMatchCancelled
 Explicit pre-start cancel edge sent when the current round start is aborted back to lobby. More...
 
struct  bomberman::net::MsgMatchResult
 Reliable end-of-match result edge sent before the automatic return to lobby. More...
 
struct  bomberman::net::MsgLobbyState
 Passive lobby seat snapshot sent reliably by the server. More...
 
struct  bomberman::net::MsgLobbyState::SeatEntry
 
struct  bomberman::net::MsgInput
 Batched input payload sent by client each simulation tick. More...
 
struct  bomberman::net::MsgCell
 Compact tile cell payload reused by reliable gameplay events. More...
 
struct  bomberman::net::MsgSnapshot
 Snapshot payload broadcast by the server to all clients. More...
 
struct  bomberman::net::MsgSnapshot::PlayerEntry
 
struct  bomberman::net::MsgSnapshot::BombEntry
 
struct  bomberman::net::MsgSnapshot::PowerupEntry
 
struct  bomberman::net::MsgCorrection
 Owner-only correction payload sent by the server. More...
 
struct  bomberman::net::MsgBombPlaced
 Reliable discrete bomb-placement event sent by the server. More...
 
struct  bomberman::net::MsgExplosionResolved
 Reliable authoritative explosion-resolution event sent by the server. More...
 

Namespaces

namespace  bomberman::net
 Shared multiplayer protocol types and transport-facing wire helpers.
 

Enumerations

enum class  bomberman::net::EChannel : uint8_t {
  ControlReliable = 0 , GameplayReliable = 1 , InputUnreliable = 2 , SnapshotUnreliable = 3 ,
  CorrectionUnreliable = 4
}
 ENet channel identifiers used by the current protocol. More...
 
enum class  bomberman::net::EMsgType : uint8_t {
  bomberman::net::Invalid = 0x00 , bomberman::net::Hello = 0x01 , bomberman::net::Welcome = 0x02 , bomberman::net::Reject = 0x03 ,
  bomberman::net::LevelInfo = 0x04 , bomberman::net::LobbyState = 0x05 , bomberman::net::LobbyReady = 0x06 , bomberman::net::MatchLoaded = 0x07 ,
  bomberman::net::MatchStart = 0x08 , bomberman::net::MatchCancelled = 0x09 , bomberman::net::MatchResult = 0x0A , bomberman::net::Input = 0x10 ,
  bomberman::net::Snapshot = 0x11 , bomberman::net::Correction = 0x12 , bomberman::net::BombPlaced = 0x13 , bomberman::net::ExplosionResolved = 0x14
}
 Message type identifiers used in packet headers. More...
 

Functions

constexpr int8_t bomberman::net::buttonsToMoveX (uint8_t buttons)
 Derives horizontal movement {-1, 0, 1} from a button bitmask.
 
constexpr int8_t bomberman::net::buttonsToMoveY (uint8_t buttons)
 Derives vertical movement {-1, 0, 1} from a button bitmask.
 
constexpr std::string_view bomberman::net::channelName (uint8_t id)
 Returns a human-readable name for a channel ID.
 
bool bomberman::net::isValidMsgType (uint8_t raw)
 Checks whether a raw byte value corresponds to a valid EMsgType.
 
constexpr std::string_view bomberman::net::msgTypeName (EMsgType type)
 Returns a human-readable name for a protocol message type.
 
constexpr EChannel bomberman::net::expectedChannelFor (EMsgType type)
 Returns the required ENet channel for a protocol message type.
 
constexpr bool bomberman::net::isValidPlayerMask (const uint32_t mask)
 Returns true when the given player-id bitmask uses only valid player bits.
 
constexpr std::size_t bomberman::net::explosionBlastCellOffset (std::size_t index)
 Returns the payload offset of the explosion blast-cell entry at index.
 
constexpr std::size_t bomberman::net::explosionDestroyedBrickOffset (std::size_t index)
 Returns the payload offset of the explosion destroyed-brick entry at index.
 
constexpr bool bomberman::net::isValidTileCell (const uint8_t col, const uint8_t row)
 Returns true when the given cell lies within the current tile-map bounds.
 
constexpr uint16_t bomberman::net::tileCellKey (const uint8_t col, const uint8_t row)
 Packs one tile cell into a monotonic key for ordering and deduplication.
 
constexpr bool bomberman::net::isExpectedChannelFor (EMsgType type, uint8_t channelId)
 Returns true when a message was received on its expected ENet channel.
 
constexpr std::size_t bomberman::net::expectedPayloadSize (EMsgType type)
 Returns the exact expected payload size for a message type.
 
constexpr void bomberman::net::writeU16LE (uint8_t *out, uint16_t value)
 Writes a 16-bit value using little-endian encoding.
 
constexpr void bomberman::net::writeU32LE (uint8_t *out, uint32_t value)
 Writes a 32-bit value using little-endian encoding.
 
constexpr uint16_t bomberman::net::readU16LE (const uint8_t *in)
 Reads a 16-bit value encoded as little-endian.
 
constexpr uint32_t bomberman::net::readU32LE (const uint8_t *in)
 Reads a 32-bit value encoded as little-endian.
 
constexpr std::size_t bomberman::net::boundedStrLen (const char *s, const std::size_t maxBytes)
 Returns string length capped to maxBytes for bounded C strings.
 
constexpr std::size_t bomberman::net::snapshotPlayerOffset (std::size_t index)
 Returns the payload offset of the snapshot player entry at index.
 
constexpr std::size_t bomberman::net::snapshotBombOffset (std::size_t index)
 Returns the payload offset of the snapshot bomb entry at index.
 
constexpr std::size_t bomberman::net::snapshotPowerupOffset (std::size_t index)
 Returns the payload offset of the snapshot powerup entry at index.
 
void bomberman::net::setHelloName (MsgHello &hello, std::string_view name)
 Sets MsgHello::name from string_view with truncation and zero padding.
 
void bomberman::net::setHelloName (MsgHello &hello, const char *name)
 C-string overload of setHelloName.
 
constexpr bool bomberman::net::isValidLobbySeatFlags (const uint8_t flags)
 Returns true when one lobby-seat flag field contains only known bits.
 
constexpr bool bomberman::net::isValidLobbyPhase (const uint8_t rawPhase)
 Returns true when one lobby-state phase field contains a known encoding.
 
void bomberman::net::setLobbySeatName (MsgLobbyState::SeatEntry &seat, std::string_view name)
 Sets one lobby-seat display name with truncation and zero padding.
 
void bomberman::net::setMatchResultWinnerName (MsgMatchResult &matchResult, std::string_view name)
 Sets one match-result winner display name with truncation and zero padding.
 
std::string_view bomberman::net::lobbySeatName (const MsgLobbyState::SeatEntry &seat)
 Returns the visible lobby-seat display name.
 
std::string_view bomberman::net::matchResultWinnerName (const MsgMatchResult &matchResult)
 Returns the visible winner display name carried by one match-result payload.
 
constexpr bool bomberman::net::lobbySeatIsOccupied (const MsgLobbyState::SeatEntry &seat)
 Returns true when one lobby seat is currently occupied.
 
constexpr bool bomberman::net::lobbySeatIsReady (const MsgLobbyState::SeatEntry &seat)
 Returns true when one occupied lobby seat is marked ready.
 
constexpr bool bomberman::net::lobbyCountdownActive (const MsgLobbyState &lobbyState)
 Returns true when the authoritative lobby is in the visible pre-match countdown phase.
 
void bomberman::net::serializeHeader (const PacketHeader &header, uint8_t *out) noexcept
 Serializes PacketHeader into kPacketHeaderSize bytes.
 
bool bomberman::net::deserializeHeader (const uint8_t *in, std::size_t inSize, PacketHeader &outHeader)
 Deserializes and validates PacketHeader.
 
void bomberman::net::serializeMsgHello (const MsgHello &hello, uint8_t *out) noexcept
 Serializes MsgHello to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgHello (const uint8_t *in, std::size_t inSize, MsgHello &outHello)
 Deserializes MsgHello from fixed-size wire payload.
 
void bomberman::net::serializeMsgWelcome (const MsgWelcome &welcome, uint8_t *out) noexcept
 Serializes MsgWelcome to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgWelcome (const uint8_t *in, std::size_t inSize, MsgWelcome &outWelcome)
 Deserializes MsgWelcome from fixed-size wire payload.
 
void bomberman::net::serializeMsgReject (const MsgReject &reject, uint8_t *out) noexcept
 Serializes MsgReject to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgReject (const uint8_t *in, std::size_t inSize, MsgReject &outReject)
 Deserializes MsgReject from fixed-size wire payload.
 
void bomberman::net::serializeMsgLevelInfo (const MsgLevelInfo &info, uint8_t *out) noexcept
 Serializes MsgLevelInfo to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgLevelInfo (const uint8_t *in, std::size_t inSize, MsgLevelInfo &outInfo)
 Deserializes MsgLevelInfo from fixed-size wire payload.
 
constexpr bool bomberman::net::isValidLobbyReadyValue (const uint8_t ready)
 Returns true when one lobby-ready value uses a known wire encoding.
 
void bomberman::net::serializeMsgLobbyReady (const MsgLobbyReady &ready, uint8_t *out) noexcept
 Serializes MsgLobbyReady to a fixed-size wire payload.
 
bool bomberman::net::deserializeMsgLobbyReady (const uint8_t *in, std::size_t inSize, MsgLobbyReady &outReady)
 Deserializes MsgLobbyReady from a fixed-size wire payload.
 
void bomberman::net::serializeMsgMatchLoaded (const MsgMatchLoaded &loaded, uint8_t *out) noexcept
 Serializes MsgMatchLoaded to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgMatchLoaded (const uint8_t *in, std::size_t inSize, MsgMatchLoaded &outLoaded)
 Deserializes MsgMatchLoaded from fixed-size wire payload.
 
void bomberman::net::serializeMsgMatchStart (const MsgMatchStart &matchStart, uint8_t *out) noexcept
 Serializes MsgMatchStart to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgMatchStart (const uint8_t *in, std::size_t inSize, MsgMatchStart &outMatchStart)
 Deserializes MsgMatchStart from fixed-size wire payload.
 
void bomberman::net::serializeMsgMatchCancelled (const MsgMatchCancelled &cancelled, uint8_t *out) noexcept
 Serializes MsgMatchCancelled to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgMatchCancelled (const uint8_t *in, std::size_t inSize, MsgMatchCancelled &outCancelled)
 Deserializes MsgMatchCancelled from fixed-size wire payload.
 
void bomberman::net::serializeMsgMatchResult (const MsgMatchResult &matchResult, uint8_t *out) noexcept
 Serializes MsgMatchResult to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgMatchResult (const uint8_t *in, std::size_t inSize, MsgMatchResult &outMatchResult)
 Deserializes MsgMatchResult from fixed-size wire payload.
 
void bomberman::net::serializeMsgLobbyState (const MsgLobbyState &lobbyState, uint8_t *out) noexcept
 Serializes MsgLobbyState to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgLobbyState (const uint8_t *in, std::size_t inSize, MsgLobbyState &outLobbyState)
 Deserializes MsgLobbyState from fixed-size wire payload.
 
void bomberman::net::serializeMsgInput (const MsgInput &input, uint8_t *out) noexcept
 Serializes MsgInput to its fixed-size wire payload.
 
bool bomberman::net::deserializeMsgInput (const uint8_t *in, std::size_t inSize, MsgInput &outInput)
 Deserializes MsgInput from fixed-size wire payload.
 
void bomberman::net::serializeMsgSnapshot (const MsgSnapshot &snap, uint8_t *out) noexcept
 Serializes MsgSnapshot to its fixed-size wire payload.
 
bool bomberman::net::deserializeMsgSnapshot (const uint8_t *in, std::size_t inSize, MsgSnapshot &outSnap)
 Deserializes MsgSnapshot from fixed-size wire payload.
 
void bomberman::net::serializeMsgCorrection (const MsgCorrection &corr, uint8_t *out) noexcept
 Serializes MsgCorrection to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgCorrection (const uint8_t *in, std::size_t inSize, MsgCorrection &outCorr)
 Deserializes MsgCorrection from fixed-size wire payload.
 
void bomberman::net::serializeMsgBombPlaced (const MsgBombPlaced &bombPlaced, uint8_t *out) noexcept
 Serializes MsgBombPlaced to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgBombPlaced (const uint8_t *in, std::size_t inSize, MsgBombPlaced &outBombPlaced)
 Deserializes MsgBombPlaced from fixed-size wire payload.
 
void bomberman::net::serializeMsgExplosionResolved (const MsgExplosionResolved &explosion, uint8_t *out) noexcept
 Serializes MsgExplosionResolved to fixed-size wire payload.
 
bool bomberman::net::deserializeMsgExplosionResolved (const uint8_t *in, std::size_t inSize, MsgExplosionResolved &outExplosion)
 Deserializes MsgExplosionResolved from fixed-size wire payload.
 
std::array< uint8_t, kPacketHeaderSize+kMsgHelloSize > bomberman::net::makeHelloPacket (const MsgHello &hello)
 Builds a full Hello packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgHelloSize > bomberman::net::makeHelloPacket (std::string_view name, uint16_t protocolVersion)
 Convenience overload building Hello from name and version.
 
std::array< uint8_t, kPacketHeaderSize+kMsgWelcomeSize > bomberman::net::makeWelcomePacket (const MsgWelcome &welcome)
 Builds a full Welcome packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgRejectSize > bomberman::net::makeRejectPacket (const MsgReject &reject)
 Builds a full Reject packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgLevelInfoSize > bomberman::net::makeLevelInfoPacket (const MsgLevelInfo &info)
 Builds a full LevelInfo packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgLobbyReadySize > bomberman::net::makeLobbyReadyPacket (const MsgLobbyReady &ready)
 Builds a full LobbyReady packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgLobbyReadySize > bomberman::net::makeLobbyReadyPacket (const bool ready)
 Convenience overload building LobbyReady directly from the desired ready state.
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchLoadedSize > bomberman::net::makeMatchLoadedPacket (const MsgMatchLoaded &loaded)
 Builds a full MatchLoaded packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchLoadedSize > bomberman::net::makeMatchLoadedPacket (const uint32_t matchId)
 Convenience overload building MatchLoaded directly from a match identifier.
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchStartSize > bomberman::net::makeMatchStartPacket (const MsgMatchStart &matchStart)
 Builds a full MatchStart packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchStartSize > bomberman::net::makeMatchStartPacket (const uint32_t matchId, const uint32_t goShowServerTick, const uint32_t unlockServerTick)
 Convenience overload building MatchStart directly from match timing values.
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchCancelledSize > bomberman::net::makeMatchCancelledPacket (const MsgMatchCancelled &cancelled)
 Builds a full MatchCancelled packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchCancelledSize > bomberman::net::makeMatchCancelledPacket (const uint32_t matchId)
 Convenience overload building MatchCancelled directly from a match identifier.
 
std::array< uint8_t, kPacketHeaderSize+kMsgMatchResultSize > bomberman::net::makeMatchResultPacket (const MsgMatchResult &matchResult)
 Builds a full MatchResult packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgLobbyStateSize > bomberman::net::makeLobbyStatePacket (const MsgLobbyState &lobbyState)
 Builds a full LobbyState packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgInputSize > bomberman::net::makeInputPacket (const MsgInput &input)
 Builds a full Input packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgSnapshotSize > bomberman::net::makeSnapshotPacket (const MsgSnapshot &snap)
 Builds a full Snapshot packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgCorrectionSize > bomberman::net::makeCorrectionPacket (const MsgCorrection &corr)
 Builds a full Correction packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgBombPlacedSize > bomberman::net::makeBombPlacedPacket (const MsgBombPlaced &bombPlaced)
 Builds a full BombPlaced packet (header + payload).
 
std::array< uint8_t, kPacketHeaderSize+kMsgExplosionResolvedSize > bomberman::net::makeExplosionResolvedPacket (const MsgExplosionResolved &explosion)
 Builds a full ExplosionResolved packet (header + payload).
 

Variables

constexpr uint16_t bomberman::net::kProtocolVersion = 9
 
constexpr uint16_t bomberman::net::kDefaultServerPort = 12345
 Default server port used by both client and server.
 
constexpr std::size_t bomberman::net::kMaxPacketSize = 1400
 Upper packet size bound (below typical 1500-byte MTU).
 
constexpr uint32_t bomberman::net::kInputLogEveryN = 30
 Input log sampling interval on client and server.
 
constexpr uint32_t bomberman::net::kSnapshotLogEveryN = 30
 Snapshot log sampling interval on client and server.
 
constexpr uint32_t bomberman::net::kPeerPingIntervalMs = 500
 Transport heartbeat interval for connected peers.
 
constexpr uint32_t bomberman::net::kPeerTimeoutLimit = 8
 Consecutive timeout limit before ENet drops a peer.
 
constexpr uint32_t bomberman::net::kPeerTimeoutMinimumMs = 5000
 Lower bound for ENet peer timeout detection.
 
constexpr uint32_t bomberman::net::kPeerTimeoutMaximumMs = 10000
 Upper bound for ENet peer timeout detection.
 
constexpr std::size_t bomberman::net::kPlayerNameMax = 16
 
constexpr uint8_t bomberman::net::kMaxPlayers = 4
 Maximum supported player count in a game instance.
 
constexpr uint8_t bomberman::net::kMaxSnapshotBombs = kMaxPlayers * 4
 Maximum bombs carried by one snapshot payload.
 
constexpr uint8_t bomberman::net::kMaxSnapshotPowerups = sim::kPowerupsPerRound
 Maximum revealed powerups carried by one snapshot payload.
 
constexpr std::size_t bomberman::net::kMaxExplosionBlastCells
 Maximum cells a cross-shaped blast can touch.
 
constexpr std::size_t bomberman::net::kMaxExplosionDestroyedBricks = kMaxExplosionBlastCells - 1u
 Upper bound for bricks one blast can destroy.
 
constexpr uint32_t bomberman::net::kFirstInputSeq = 1
 First valid input sequence number. Seq 0 means "no input received yet".
 
constexpr uint8_t bomberman::net::kMaxInputBatchSize = 16
 Maximum number of inputs in a single batched MsgInput packet.
 
constexpr uint8_t bomberman::net::kInputUp = 0x01
 
constexpr uint8_t bomberman::net::kInputDown = 0x02
 
constexpr uint8_t bomberman::net::kInputLeft = 0x04
 
constexpr uint8_t bomberman::net::kInputRight = 0x08
 
constexpr uint8_t bomberman::net::kInputBomb = 0x10
 
constexpr uint8_t bomberman::net::kInputKnownBits = kInputUp | kInputDown | kInputLeft | kInputRight | kInputBomb
 Union of all defined input bits. Used for unknown-bit rejection.
 
constexpr std::size_t bomberman::net::kChannelCount = 5
 Number of ENet channels provisioned by client and server.
 
constexpr std::size_t bomberman::net::kPacketHeaderSize
 
constexpr std::size_t bomberman::net::kMsgHelloSize
 
constexpr std::size_t bomberman::net::kMsgWelcomeSize
 
constexpr std::size_t bomberman::net::kMsgRejectSize
 
constexpr std::size_t bomberman::net::kMsgLevelInfoSize
 
constexpr std::size_t bomberman::net::kMsgLobbyReadySize
 
constexpr std::size_t bomberman::net::kMsgMatchLoadedSize
 
constexpr std::size_t bomberman::net::kMsgMatchStartSize
 
constexpr std::size_t bomberman::net::kMsgMatchCancelledSize
 
constexpr std::size_t bomberman::net::kMsgMatchResultSize
 
constexpr std::size_t bomberman::net::kMsgLobbyStateSeatSize
 
constexpr std::size_t bomberman::net::kMsgLobbyStateSize
 
constexpr std::size_t bomberman::net::kMsgInputSize
 
constexpr std::size_t bomberman::net::kMsgSnapshotSize
 
constexpr std::size_t bomberman::net::kMsgCorrectionSize
 
constexpr std::size_t bomberman::net::kMsgBombPlacedSize
 
constexpr std::size_t bomberman::net::kMsgExplosionResolvedSize
 
constexpr std::size_t bomberman::net::kSnapshotPlayersOffset
 Compile-time checks for expected wire sizes.
 
constexpr std::size_t bomberman::net::kSnapshotPlayerEntrySize
 
constexpr std::size_t bomberman::net::kSnapshotBombsOffset
 
constexpr std::size_t bomberman::net::kSnapshotBombEntrySize
 
constexpr std::size_t bomberman::net::kSnapshotPowerupsOffset
 
constexpr std::size_t bomberman::net::kSnapshotPowerupEntrySize
 
constexpr std::size_t bomberman::net::kExplosionBlastCellsOffset
 
constexpr std::size_t bomberman::net::kExplosionCellEntrySize
 
constexpr std::size_t bomberman::net::kExplosionDestroyedBricksOffset
 

Detailed Description

Shared client/server wire contract for the multiplayer protocol.

This file is the single source of truth for message ids, channels, fixed payload sizes, wire structs, serializers, and packet builders used by both client and server.

The protocol requires a strict version match during handshake.

Note
When changing wire layout, update:
  1. kProtocolVersion
  2. The affected wire size constants and static_assert checks
  3. expectedPayloadSize(...)
  4. The affected serializers and deserializers