5 : socket_(io, asio::ip::udp::endpoint(asio::ip::udp::v4(), port)),
6 gameManager_([this](uint32_t cid, const
Message &msg) {
14void Server::doReceive()
16 socket_.async_receive_from(
17 asio::buffer(recvBuffer_), remoteEndpoint_,
18 [
this](std::error_code ec, std::size_t bytesRecv) {
19 if (!ec && bytesRecv > 0) {
20 handleMessage(bytesRecv);
27void Server::handleMessage(std::size_t bytesReceived)
31 std::cout <<
"[Server] Invalid message from " << remoteEndpoint_ <<
"\n";
44 std::cout <<
"[Server] Client " << clientId
45 <<
" connected from " << remoteEndpoint_ <<
"\n";
48 std::cout <<
"[Server] Syncing " << clientId <<
" to game...\n";
49 for (
auto &packet : sync) {
54 std::cout <<
"[Server] Synced " << clientId <<
" to game.\n";
59 std::cout <<
"[Server] Client " << clientId <<
" disconnected.\n";
66 std::cout <<
"[Server] Handshaked client " << clientId <<
".\n";
84 if (ep == asio::ip::udp::endpoint()) {
85 std::cout <<
"[Server] No endpoint known for client " << clientId <<
"\n";
93 std::this_thread::sleep_for(std::chrono::milliseconds(20));
98 socket_.async_send_to(
101 [
this](std::error_code ec, std::size_t bytesSent) {
103 std::cout <<
"[Server] sendToClient error: " << ec.message() <<
"\n";
asio::ip::udp::endpoint getEndpointForId(uint32_t clientId)
Returns the endpoint for a given clientId if known, else a default-constructed endpoint.
uint32_t resolveClientID(const asio::ip::udp::endpoint &ep)
void removeClient(const asio::ip::udp::endpoint &ep)
uint32_t getGameIdForClient(uint32_t clientId) const
Get the game ID for a client, or 0 if none.
std::forward_list< std::string > syncClientToGame(uint32_t clientId)
Gets the current game state for syncing.
void removeClientFromGame(uint32_t clientId)
Remove a client from whichever game they are in.
void handleGameMessage(uint32_t gameId, uint32_t clientId, const Message &msg)
Handle a message from a client in a specific game.
Server(asio::io_context &io, unsigned short port)
Constructs the Server, binding a UDP socket to the specified port.
void sendToClient(uint32_t clientId, const Message &msg)
Sends a Message to a specific client by ID (uses clientManager to find endpoint).
std::vector< uint8_t > encodeMessage(const Message &msg)
Encodes a Message into a raw buffer (2-byte type, 2-byte length, then payload).
bool decodeMessage(const char *data, size_t length, Message &out)
Decodes a raw buffer into a Message (2-byte type, 2-byte length, then payload).
Minimal network message with a 4-byte header (type + length) and a payload.
uint8_t type
Could map to messageType in GameMessage.hpp (CONNECT, DISCONNECT, etc.)