ASIO Client Server Framework
Loading...
Searching...
No Matches
acsf::Server Class Reference

A TCP server that accepts many clients and exchanges length-prefixed messages with them. More...

#include <server.h>

Public Member Functions

 Server (std::uint16_t port, std::function< void()> callback, bool keepAliveByHeartBeat=true, bool sendHeartBeats=false, LoggerConfig loggerConfig={})
 Creates a server bound to a port on all IPv4 interfaces. Clients are not accepted until start() is called.
 ~Server ()
 Stops the server (see stop()) and releases its resources.
void start ()
 Starts accepting clients on the I/O thread.
void stop ()
 Stops accepting, closes every connection, wakes any thread blocked in receive() and joins the I/O thread.
std::optional< acsf::Message > receive ()
 Blocks until a message arrives from any client.
void send (uint64_t id, std::string body)
 Queues a message to be sent to one client.
void setTimeout (TimeoutType type, std::chrono::milliseconds duration)
 Changes one of the timeouts. Timeouts are shared by every server and client in the program.

Detailed Description

A TCP server that accepts many clients and exchanges length-prefixed messages with them.

The server runs its own I/O thread. Every client gets a numeric id (0, 1, 2, ...) in connection order. Incoming messages from all clients arrive through receive(), tagged with the sender's id in Message::isFrom. Reply to a specific client with send().

acsf::Server server(15555, []{});
server.start();
while (auto msg = server.receive()) {
server.send(msg->isFrom, "echo: " + msg->payload);
}
server.stop();
A TCP server that accepts many clients and exchanges length-prefixed messages with them.
Definition server.h:31
void start()
Starts accepting clients on the I/O thread.
Definition server.h:110
Examples
main_example.cpp.

Constructor & Destructor Documentation

◆ Server()

acsf::Server::Server ( std::uint16_t port,
std::function< void()> callback,
bool keepAliveByHeartBeat = true,
bool sendHeartBeats = false,
LoggerConfig loggerConfig = {} )
inline

Creates a server bound to a port on all IPv4 interfaces. Clients are not accepted until start() is called.

Parameters
portPort to listen on.
callbackCalled whenever a client connection closes.
keepAliveByHeartBeatIf true, a client is dropped when it stops sending heartbeats for SERVER_HEARTBEAT_TIMEOUT_MS.
sendHeartBeatsIf true, the server sends heartbeats to its clients at HEARTBEAT_RATE.
loggerConfigLogging settings. Logging is off unless LoggerConfig::enabled is true.

Member Function Documentation

◆ receive()

std::optional< acsf::Message > acsf::Server::receive ( )
inline

Blocks until a message arrives from any client.

Returns
The message (Message::isFrom holds the sender's id), or std::nullopt once the server is stopped.

◆ send()

void acsf::Server::send ( uint64_t id,
std::string body )
inline

Queues a message to be sent to one client.

Parameters
idId of the client, as found in Message::isFrom.
bodyMessage payload. Must be non-empty and no larger than acsf::MaxMessageSize. Does nothing if no client with that id is connected.

◆ setTimeout()

void acsf::Server::setTimeout ( TimeoutType type,
std::chrono::milliseconds duration )
inline

Changes one of the timeouts. Timeouts are shared by every server and client in the program.

Parameters
typeWhich timeout to change.
durationNew value in milliseconds. 0 turns that timeout off.

The documentation for this class was generated from the following file: