8#include <asio/io_context.hpp>
14#include <unordered_map>
17#include "../connection/Connection.hpp"
18#include "../metrics/Metrics.hpp"
19#include "../protocol/MessageParser.hpp"
20#include "../transport/UdpTransport.hpp"
36 std::function<void(std::uint32_t connectionId,
MessageId type,
37 const std::vector<std::uint8_t> &payload)>;
65 void send(std::uint32_t connectionId,
66 const std::vector<std::uint8_t> &payload);
87 void handleHandshake(
const asio::ip::udp::endpoint &endpoint,
88 const std::vector<std::uint8_t> &data);
89 void handleConnectedPacket(
const asio::ip::udp::endpoint &endpoint,
90 const std::vector<std::uint8_t> &data);
91 Connection *findConnectionByEndpoint(
const asio::ip::udp::endpoint &endpoint);
92 void handleDatagram(
const asio::ip::udp::endpoint &endpoint,
93 const std::vector<std::uint8_t> &data);
95 void doSend(std::uint32_t connectionId,
96 const std::vector<std::uint8_t> &payload);
97 void handleMessage(std::uint32_t connectionId,
const ParsedMessage &message);
98 void disconnectClient(std::uint32_t connectionId);
100 asio::io_context _io;
103 std::thread _networkThread;
104 std::unordered_map<std::uint32_t, std::unique_ptr<Connection>> _connections;
105 std::uint32_t _nextId = 1;
106 std::atomic_bool _running{
false};
State the server keeps for one connected client.
Definition Connection.hpp:18
UDP server that runs the handshake, tracks connections and dispatches messages.
Definition NetworkServer.hpp:30
void setOnMessage(MessageCallback callback)
Sets the function called for every game level message.
Definition NetworkServer.cpp:231
~NetworkServer()
Stops the server and joins the network thread.
Definition NetworkServer.cpp:21
NetworkServer(std::uint16_t port)
Creates a server bound to a UDP port, without starting it.
Definition NetworkServer.cpp:18
void start()
Starts receiving datagrams on the network thread.
Definition NetworkServer.cpp:23
void send(std::uint32_t connectionId, const std::vector< std::uint8_t > &payload)
Sends messages to a client inside a new connected packet.
Definition NetworkServer.cpp:171
std::function< void(std::uint32_t connectionId)> ConnectionCallback
Callback receiving the id of a connection.
Definition NetworkServer.hpp:33
void setOnClientConnected(ConnectionCallback callback)
Sets the function called after a successful handshake.
Definition NetworkServer.cpp:223
std::function< void(std::uint32_t connectionId, MessageId type, const std::vector< std::uint8_t > &payload)> MessageCallback
Callback receiving a message that the network layer does not handle.
Definition NetworkServer.hpp:35
void stop()
Closes the socket and joins the network thread.
Definition NetworkServer.cpp:37
void setOnClientDisconnected(ConnectionCallback callback)
Sets the function called when a client disconnects.
Definition NetworkServer.cpp:227
Asynchronous UDP socket wrapper used by the server.
Definition UdpTransport.hpp:21
std::uint16_t MessageId
Numeric identifier of a message type.
Definition MessageHeader.hpp:14
Traffic and latency counters of a network endpoint.
Definition Metrics.hpp:14
Message extracted from a connected packet.
Definition MessageParser.hpp:17