dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
SocketClient.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <functional>
13
14// Third-party includes
15#include <nlohmann/json.hpp>
16
17// Project includes
18#include "Message.hpp"
19#include <dfx-utilities/FileSystem.hpp>
21
22namespace dfx::FdWatch
23{
24class Poller;
25} // !namespace dfx::FdWatch
26
27namespace dfx::Client
28{
47{
48 DFX_PRIVATE_STATE(SocketClient, DFX_CLIENT_LOGGER())
49
50public:
52 using Callback = std::move_only_function<void (Message)>;
53
54public:
60 SocketClient(FdWatch::Poller & poller, fs::path socketPath, int version = 1);
62
63public:
65 bool isConnected() const noexcept;
68 void reconnect();
71 void disconnect();
72
74 bool willAutoReconnect() const noexcept;
76 void setAutoReconnect(bool shouldReconnect);
77
78public:
87
88public:
100 std::string sendCommand(std::string_view cmd, nlohmann::json const & params, Callback cb = nullptr, std::string_view id = {});
101
107 void sendNotification(nlohmann::json json);
108
109public:
116 static std::string generateRequestId();
117};
118} // !namespace dfx::Ctl::Shared
Macros to implement the Private State (PIMPL-like) idiom with value semantics.
#define DFX_PRIVATE_STATE(Class, logger)
Declare a private implementation state for a class.
Definition PrivateState.hpp:52
#define DFX_PRIVATE_STATE_DECLARE_RULE_OF_5(Class)
Declare Rule-of-5 special member functions using the private state pattern.
Definition PrivateState.hpp:82
Wrapper around a JSON message returned by the client protocol/API.
Definition Message.hpp:38
void disconnect()
Disconnect from the server and release underlying resources. If willAutoReconnect() returns true then...
void setAutoReconnect(bool shouldReconnect)
Enable or disable automatic reconnection.
bool willAutoReconnect() const noexcept
Return whether the client will automatically attempt to reconnect.
bool isConnected() const noexcept
Check whether the client is currently connected.
std::move_only_function< void(Message)> Callback
Callback type used to deliver a received message or a response.
Definition SocketClient.hpp:52
void reconnect()
Attempt to (re)establish the connection. Previous connection (if any) will be closed if this function...
SocketClient(FdWatch::Poller &poller, fs::path socketPath, int version=1)
Construct a socket client bound to a poller.
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 setMessageCallback(Callback cb)
Set the callback invoked for incoming messages.
void sendNotification(nlohmann::json json)
Send a one-way notification to the server.
static std::string generateRequestId()
Generate a new request identifier.
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Definition Message.hpp:21
Definition SocketClient.hpp:23
STL namespace.