|
R-Type 0.1
|
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. | |
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.
| NetworkServer::NetworkServer | ( | std::uint16_t | port | ) |
Creates a server bound to a UDP port, without starting it.
| port | Local port to listen on. |
| void NetworkServer::send | ( | std::uint32_t | connectionId, |
| const std::vector< std::uint8_t > & | payload ) |
Sends messages to a client inside a new connected packet.
| connectionId | Destination connection. |
| payload | Encoded messages, headers included. |
| void NetworkServer::setOnClientConnected | ( | ConnectionCallback | callback | ) |
Sets the function called after a successful handshake.
| callback | Receives the new connection id. |
| void NetworkServer::setOnClientDisconnected | ( | ConnectionCallback | callback | ) |
Sets the function called when a client disconnects.
| callback | Receives the closed connection id. |
| void NetworkServer::setOnMessage | ( | MessageCallback | callback | ) |
Sets the function called for every game level message.
| callback | Receives the connection id, message type and payload. |