class NetworkPeer

Represents a single peer in a network hierarchy.

Allows you to create a server that listens for incoming connections, or connect as a client to a remote server. Supports sending and receiving messages with configurable reliability and ordering guarantees.

Public

Constructors

NetworkPeer

NetworkPeer()

Methods

~NetworkPeer

~NetworkPeer()

StartServer

bool StartServer(u16 port, u32 maxConnections)

Starts the peer as a server, listening for incoming connections on the specified port.

port
Port to listen on.
maxConnections
Maximum number of simultaneous connections allowed.

Returns: True if the server started successfully, false otherwise.

Connect

ConnectionID Connect(const NetworkAddress &address)

Connects to a remote server as a client.

address
Network address of the server to connect to.

Returns: Connection ID that will be valid once connection completes, or Invalid if the connection attempt failed to start.

Disconnect

void Disconnect(ConnectionID connection)

Disconnects from a specific peer.

connection
Connection ID to disconnect.

DisconnectAll

void DisconnectAll()

Disconnects all connected peers and shuts down the peer.

PollMessages

void PollMessages(Vector<NetworkMessage> &outMessages, u32 maxMessages = 32)

Polls for new messages received from the network.

Messages are appended to the provided container.

Example:

outMessages
Container to fill with received messages. Will be cleared before filling. Users should maintain a persistent container to avoid per-frame allocations.
maxMessages
Maximum number of messages to retrieve in a single poll.

SendMessage

void SendMessage(ConnectionID connection, const u8 *data, u32 size, NetworkSendFlags flags = NetworkSendFlagBits::Reliable)

Sends a message to a specific connection.

connection
Connection ID to send to.
data
Pointer to message data. First byte should be the message type.
size
Size of the message data in bytes.
flags
Flags controlling send behavior (reliability, priority, etc.).

BroadcastMessage

void BroadcastMessage(const u8 *data, u32 size, NetworkSendFlags flags = NetworkSendFlagBits::Reliable)

Broadcasts a message to all connected peers.

data
Pointer to message data. First byte should be the message type.
size
Size of the message data in bytes.
flags
Flags controlling send behavior (reliability, priority, etc.).

GetConnectionInfo

bool GetConnectionInfo(ConnectionID connection, ConnectionInformation &outInfo) const

Retrieves connection quality information for a specific connection.

connection
Connection to query.
outInfo
Structure to fill with connection information.

Returns: True if connection info was retrieved successfully, false if connection is invalid.

IsConnected

bool IsConnected(ConnectionID connection) const

Checks if a connection is currently active.

connection
Connection to check.

Returns: True if connected, false otherwise.

GetConnectionState

ConnectionState GetConnectionState(ConnectionID connection) const

Retrieves the current state of a connection.

connection
Connection to query.

Returns: Current connection state.

FreeMessage

void FreeMessage(NetworkMessage &message)

Frees a message received via PollMessages.

Must be called for each message after processing.

message
Message to free.

Private

Fields

m

Impl * m