R-Type 0.1
Loading...
Searching...
No Matches
NetworkServer.hpp
1#pragma once
2
8#include <asio/io_context.hpp>
9#include <atomic>
10#include <cstdint>
11#include <functional>
12#include <memory>
13#include <thread>
14#include <unordered_map>
15#include <vector>
16
17#include "../connection/Connection.hpp"
18#include "../metrics/Metrics.hpp"
19#include "../protocol/MessageParser.hpp"
20#include "../transport/UdpTransport.hpp"
21
31public:
33 using ConnectionCallback = std::function<void(std::uint32_t connectionId)>;
36 std::function<void(std::uint32_t connectionId, MessageId type,
37 const std::vector<std::uint8_t> &payload)>;
38
43 NetworkServer(std::uint16_t port);
44
49
53 void start();
54
58 void stop();
59
65 void send(std::uint32_t connectionId,
66 const std::vector<std::uint8_t> &payload);
67
73
79
84 void setOnMessage(MessageCallback callback);
85
86private:
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);
94 void sendWelcome(Connection &connection);
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);
99
100 asio::io_context _io;
101 NetworkMetrics _metrics;
102 UdpTransport _transport;
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};
107
108 ConnectionCallback _onClientConnected;
109 ConnectionCallback _onClientDisconnected;
110 MessageCallback _onMessage;
111};
112
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