![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
Event-driven JSON socket client. More...
#include <dfx-client/SocketClient.hpp>
Public Types | |
| using | Callback = std::move_only_function<void (Message)> |
| Callback type used to deliver a received message or a response. | |
Public Member Functions | |
| SocketClient (FdWatch::Poller &poller, fs::path socketPath, int version=1) | |
| Construct a socket client bound to a poller. | |
| DFX_PRIVATE_STATE_DECLARE_RULE_OF_5 (SocketClient) | |
| bool | isConnected () const noexcept |
| Check whether the client is currently connected. | |
| void | reconnect () |
| Attempt to (re)establish the connection. Previous connection (if any) will be closed if this function doesn't throw an exception. | |
| void | disconnect () |
| Disconnect from the server and release underlying resources. If willAutoReconnect() returns true then a reconnection will be immediately attempted. | |
| bool | willAutoReconnect () const noexcept |
| Return whether the client will automatically attempt to reconnect. | |
| void | setAutoReconnect (bool shouldReconnect) |
| Enable or disable automatic reconnection. | |
| void | setMessageCallback (Callback cb) |
| Set the callback invoked for incoming messages. | |
| std::string | sendCommand (std::string_view cmd, nlohmann::json const ¶ms, Callback cb=nullptr, std::string_view id={}) |
| Send a request/command to the server. | |
| void | sendNotification (nlohmann::json json) |
| Send a one-way notification to the server. | |
Static Public Member Functions | |
| static std::string | generateRequestId () |
| Generate a new request identifier. | |
Event-driven JSON socket client.
SocketClient manages a client connection to a server via a Unix socket endpoint identified by a filesystem path (see constructor). It integrates with an external event loop through dfx::FdWatch::Poller.
The client supports:
Requests and responses are wrapped as dfx::Client::Message objects on reception/dispatch.
| using dfx::Client::SocketClient::Callback = std::move_only_function<void (Message)> |
Callback type used to deliver a received message or a response.
| dfx::Client::SocketClient::SocketClient | ( | FdWatch::Poller & | poller, |
| fs::path | socketPath, | ||
| int | version = 1 ) |
Construct a socket client bound to a poller.
| poller | Event poller used to watch the underlying socket fd and dispatch callbacks. |
| socketPath | Path identifying the socket endpoint to connect to. |
| version | Protocol version to use (default: 1). |
| void dfx::Client::SocketClient::disconnect | ( | ) |
Disconnect from the server and release underlying resources. If willAutoReconnect() returns true then a reconnection will be immediately attempted.
|
static |
Generate a new request identifier.
The format/uniqueness guarantees are implementation-defined, but it is intended for use with sendCommand().
|
noexcept |
Check whether the client is currently connected.
| void dfx::Client::SocketClient::reconnect | ( | ) |
Attempt to (re)establish the connection. Previous connection (if any) will be closed if this function doesn't throw an exception.
| std::string dfx::Client::SocketClient::sendCommand | ( | std::string_view | cmd, |
| nlohmann::json const & | params, | ||
| Callback | cb = nullptr, | ||
| std::string_view | id = {} ) |
Send a request/command to the server.
This method is intended for request/response interactions. If cb is provided, it is associated with the request id and invoked once a corresponding response is received.
| cmd | Command name/string. |
| params | Parameters encoded as JSON. |
| cb | Optional callback invoked with the response Message. |
| id | Optional request identifier. If empty, an id is generated using generateRequestId() |
| void dfx::Client::SocketClient::sendNotification | ( | nlohmann::json | json | ) |
Send a one-way notification to the server.
Notifications do not expect a reply.
| json | Notification payload. |
| void dfx::Client::SocketClient::setAutoReconnect | ( | bool | shouldReconnect | ) |
Enable or disable automatic reconnection.
| void dfx::Client::SocketClient::setMessageCallback | ( | Callback | cb | ) |
Set the callback invoked for incoming messages.
This callback is used only for incomming messages which aren't associated to a command send previously using sendCommand(). Passing a null callback (default-constructed Callback) disables message delivery.
| cb | Callback to invoke when a message is received. |
|
noexcept |
Return whether the client will automatically attempt to reconnect.