Skip to content

Network Server API

Tom Colas edited this page Jan 18, 2026 · 2 revisions

Network Server API

Overview

The server class provides an asynchronous UDP server with built-in reliable packet handling (SAFE protocol). It manages connected clients, message dispatching, and acknowledgment logic.

Designed for multiplayer game servers using ECS-based architectures.


Features

  • Fully UDP-based server
  • TCP-like reliability layer (SAFE)
  • Client auto-discovery
  • Thread-safe message queue
  • Broadcast and selective messaging

Class: server

Constructor

server(short port);

Creates and binds a UDP server.

Parameters

  • port – UDP port to listen on

Public Members

std::atomic_bool running;

Indicates whether the server is currently running.


send()

void send(
    const std::string &client,
    size_t id,
    uint16_t protocol,
    uint16_t type,
    uint16_t size,
    const void *data,
    uint16_t uid = 0
);

Sends a packet to a specific client.


sendAll()

void sendAll(
    size_t id,
    uint16_t protocol,
    uint16_t type,
    uint16_t size,
    const void *data
);

Broadcasts a packet to all connected clients.


sendAllExcept()

void sendAllExcept(
    const std::string &clientToExcept,
    size_t id,
    uint16_t protocol,
    uint16_t type,
    uint16_t size,
    const void *data
);

Broadcasts a packet to all clients except one.


pushMessage()

void pushMessage(
    size_t id,
    uint16_t protocol,
    uint16_t type,
    uint16_t size,
    const void *data
);

Queues a packet for later broadcast.


sendMessages()

void sendMessages();

Flushes the send queue and dispatches all queued packets.


popMessage()

bool popMessage(std::pair<std::string, data> &out);

Retrieves a received message.

Returns

  • true if a message was available
  • false otherwise

Client Management

void removeClient(std::string &client);
void clear();
void allowNewClient(bool enable);
size_t size() const;

Client Identification

Clients are identified using:

<ip>:<port>

Each client is assigned a unique SAFE UDP ID internally.


Threading Model

  • One dedicated network thread
  • Message queues protected by mutex
  • Asynchronous I/O using Boost.Asio

Example

server srv(8080);

while (srv.running) {
    std::pair<std::string, data> msg;
    while (srv.popMessage(msg)) {
        // Process message
    }
}

Notes

  • SAFE packets are retransmitted until acknowledged
  • Client connections are stateless (UDP-based)
  • Max packet size: 1024 bytes

Clone this wiki locally