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

A TCP client that connects to an acsf::Server and exchanges length-prefixed messages. More...

#include <client.h>

Public Member Functions

 Client (std::string ip, std::uint16_t port, std::function< void()> callBack=[](){}, bool keepAliveByHeartBeat=false, bool sendHeartBeats=true, LoggerConfig loggerConfig={})
 Creates a client. Nothing is connected until start() is called.
 ~Client ()
 Stops the client (see stop()) and releases its resources.
boost::system::error_code start ()
 Connects to the server and starts the I/O thread.
void stop ()
 Closes the connection, wakes any thread blocked in receive() and joins the I/O thread. Safe to call more than once.
std::optional< acsf::Message > receive ()
 Blocks until a message arrives from the server.
void send (std::string body)
 Queues a message to be sent to the server.

Detailed Description

A TCP client that connects to an acsf::Server and exchanges length-prefixed messages.

The client runs its own I/O thread. Call start() to connect, send() to queue outgoing messages and receive() to block until a message arrives. Timeouts and heartbeats are configured with acsf::setTimeout.

acsf::Client client("127.0.0.1", 15555);
if (!client.start()) {
client.send("hello");
if (auto msg = client.receive()) std::cout << msg->payload << "\n";
}
client.stop();
A TCP client that connects to an acsf::Server and exchanges length-prefixed messages.
Definition client.h:30
Examples
main_example.cpp.

Constructor & Destructor Documentation

◆ Client()

acsf::Client::Client ( std::string ip,
std::uint16_t port,
std::function< void()> callBack = [](){},
bool keepAliveByHeartBeat = false,
bool sendHeartBeats = true,
LoggerConfig loggerConfig = {} )
inline

Creates a client. Nothing is connected until start() is called.

Parameters
ipIP address of the server, e.g. "127.0.0.1".
portPort the server listens on.
callBackCalled when the connection closes. Defaults to doing nothing.
keepAliveByHeartBeatIf true, the connection is dropped when the peer stops sending heartbeats.
sendHeartBeatsIf true, the client sends heartbeats to the server at acsf::HEARTBEAT_RATE.
loggerConfigLogging settings. Logging is off unless LoggerConfig::enabled is true.

Member Function Documentation

◆ receive()

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

Blocks until a message arrives from the server.

Returns
The received message, or std::nullopt once the client is stopped or the connection has closed.

◆ send()

void acsf::Client::send ( std::string body)
inline

Queues a message to be sent to the server.

Parameters
bodyMessage payload. Must be non-empty and no larger than acsf::MaxMessageSize. Does nothing if the client is not connected.

◆ start()

boost::system::error_code acsf::Client::start ( )
inline

Connects to the server and starts the I/O thread.

Returns
A default (false) error_code on success. If already connected, boost::asio::error::already_connected. If the connection fails, the error from the connect attempt.

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