A TCP client that connects to an acsf::Server and exchanges length-prefixed messages.
More...
#include <client.h>
|
| | 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.
|
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.
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.
◆ 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
-
| ip | IP address of the server, e.g. "127.0.0.1". |
| port | Port the server listens on. |
| callBack | Called when the connection closes. Defaults to doing nothing. |
| keepAliveByHeartBeat | If true, the connection is dropped when the peer stops sending heartbeats. |
| sendHeartBeats | If true, the client sends heartbeats to the server at acsf::HEARTBEAT_RATE. |
| loggerConfig | Logging settings. Logging is off unless LoggerConfig::enabled is true. |
◆ receive()
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
-
◆ 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: