R-Type 0.1
Loading...
Searching...
No Matches
NetworkServer Class Reference

UDP server that runs the handshake, tracks connections and dispatches messages. More...

#include <server/NetworkServer.hpp>

Public Types

using ConnectionCallback = std::function<void(std::uint32_t connectionId)>
 Callback receiving the id of a connection.
 
using MessageCallback
 Callback receiving a message that the network layer does not handle.
 

Public Member Functions

 NetworkServer (std::uint16_t port)
 Creates a server bound to a UDP port, without starting it.
 
 ~NetworkServer ()
 Stops the server and joins the network thread.
 
void start ()
 Starts receiving datagrams on the network thread.
 
void stop ()
 Closes the socket and joins the network thread.
 
void send (std::uint32_t connectionId, const std::vector< std::uint8_t > &payload)
 Sends messages to a client inside a new connected packet.
 
void setOnClientConnected (ConnectionCallback callback)
 Sets the function called after a successful handshake.
 
void setOnClientDisconnected (ConnectionCallback callback)
 Sets the function called when a client disconnects.
 
void setOnMessage (MessageCallback callback)
 Sets the function called for every game level message.
 

Detailed Description

UDP server that runs the handshake, tracks connections and dispatches messages.

Network I/O runs on a dedicated thread, so every callback is invoked from that thread and the caller must synchronize any data it shares with the game loop.

Member Typedef Documentation

◆ MessageCallback

Initial value:
std::function<void(std::uint32_t connectionId, MessageId type,
const std::vector<std::uint8_t> &payload)>
std::uint16_t MessageId
Numeric identifier of a message type.
Definition MessageHeader.hpp:14

Callback receiving a message that the network layer does not handle.

Constructor & Destructor Documentation

◆ NetworkServer()

NetworkServer::NetworkServer ( std::uint16_t port)

Creates a server bound to a UDP port, without starting it.

Parameters
portLocal port to listen on.

Member Function Documentation

◆ send()

void NetworkServer::send ( std::uint32_t connectionId,
const std::vector< std::uint8_t > & payload )

Sends messages to a client inside a new connected packet.

Parameters
connectionIdDestination connection.
payloadEncoded messages, headers included.

◆ setOnClientConnected()

void NetworkServer::setOnClientConnected ( ConnectionCallback callback)

Sets the function called after a successful handshake.

Parameters
callbackReceives the new connection id.

◆ setOnClientDisconnected()

void NetworkServer::setOnClientDisconnected ( ConnectionCallback callback)

Sets the function called when a client disconnects.

Parameters
callbackReceives the closed connection id.

◆ setOnMessage()

void NetworkServer::setOnMessage ( MessageCallback callback)

Sets the function called for every game level message.

Parameters
callbackReceives the connection id, message type and payload.