dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Client::SocketClient Class Reference

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 &params, 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.

Detailed Description

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.

Member Typedef Documentation

◆ Callback

using dfx::Client::SocketClient::Callback = std::move_only_function<void (Message)>

Callback type used to deliver a received message or a response.

Constructor & Destructor Documentation

◆ SocketClient()

dfx::Client::SocketClient::SocketClient ( FdWatch::Poller & poller,
fs::path socketPath,
int version = 1 )

Construct a socket client bound to a poller.

Parameters
pollerEvent poller used to watch the underlying socket fd and dispatch callbacks.
socketPathPath identifying the socket endpoint to connect to.
versionProtocol version to use (default: 1).

Member Function Documentation

◆ disconnect()

void dfx::Client::SocketClient::disconnect ( )

Disconnect from the server and release underlying resources. If willAutoReconnect() returns true then a reconnection will be immediately attempted.

◆ generateRequestId()

std::string dfx::Client::SocketClient::generateRequestId ( )
static

Generate a new request identifier.

The format/uniqueness guarantees are implementation-defined, but it is intended for use with sendCommand().

Returns
A newly generated request id as a string.

◆ isConnected()

bool dfx::Client::SocketClient::isConnected ( ) const
noexcept

Check whether the client is currently connected.

◆ reconnect()

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.

◆ sendCommand()

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.

Parameters
cmdCommand name/string.
paramsParameters encoded as JSON.
cbOptional callback invoked with the response Message.
idOptional request identifier. If empty, an id is generated using generateRequestId()
Returns
The request identifier used for this command.

◆ sendNotification()

void dfx::Client::SocketClient::sendNotification ( nlohmann::json json)

Send a one-way notification to the server.

Notifications do not expect a reply.

Parameters
jsonNotification payload.

◆ setAutoReconnect()

void dfx::Client::SocketClient::setAutoReconnect ( bool shouldReconnect)

Enable or disable automatic reconnection.

◆ setMessageCallback()

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.

Parameters
cbCallback to invoke when a message is received.

◆ willAutoReconnect()

bool dfx::Client::SocketClient::willAutoReconnect ( ) const
noexcept

Return whether the client will automatically attempt to reconnect.


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