20#ifndef ORES_COMMS_NET_CLIENT_HPP
21#define ORES_COMMS_NET_CLIENT_HPP
31#include <boost/asio/awaitable.hpp>
32#include <boost/asio/ssl.hpp>
33#include <boost/asio/strand.hpp>
34#include <boost/asio/ip/tcp.hpp>
35#include <boost/asio/io_context.hpp>
36#include "ores.utility/log/make_logger.hpp"
37#include "ores.eventing/service/event_bus.hpp"
38#include "ores.comms/net/client_options.hpp"
39#include "ores.comms/net/connection.hpp"
40#include "ores.comms/net/pending_request_map.hpp"
100 const std::string& event_type,
101 std::chrono::system_clock::time_point timestamp)>;
110 inline static std::string_view logger_name =
"ores.comms.net.client";
114 static auto instance = make_logger(logger_name);
121 void setup_ssl_context();
128 boost::asio::awaitable<void> perform_handshake();
136 boost::asio::awaitable<void> run_heartbeat();
146 boost::asio::awaitable<void> run_message_loop();
154 boost::asio::awaitable<void> run_reconnect_loop();
161 boost::asio::awaitable<void> perform_connection();
169 std::chrono::milliseconds calculate_backoff(std::uint32_t attempt)
const;
183 std::uint32_t next_correlation_id();
195 std::shared_ptr<eventing::service::event_bus> event_bus =
nullptr);
205 std::shared_ptr<eventing::service::event_bus> event_bus =
nullptr);
222 boost::asio::awaitable<void>
connect();
317 boost::asio::awaitable<std::expected<messaging::frame, messaging::error_code>>
329 std::expected<messaging::frame, messaging::error_code>
334 std::unique_ptr<boost::asio::io_context> io_ctx_;
335 boost::asio::any_io_executor executor_;
336 boost::asio::ssl::context ssl_ctx_;
337 std::unique_ptr<connection> conn_;
338 std::uint32_t sequence_number_;
339 std::atomic<connection_state> state_;
340 mutable std::mutex state_mutex_;
345 std::shared_ptr<eventing::service::event_bus> event_bus_;
348 std::unique_ptr<boost::asio::strand<boost::asio::any_io_executor>> write_strand_;
349 std::unique_ptr<pending_request_map> pending_requests_;
350 std::atomic<std::uint32_t> correlation_id_counter_{1};
351 std::atomic<bool> message_loop_running_{
false};
352 std::atomic<bool> reconnect_loop_running_{
false};
353 std::atomic<bool> heartbeat_loop_running_{
false};
354 std::unique_ptr<std::thread> io_thread_;
355 std::unique_ptr<boost::asio::executor_work_guard<boost::asio::io_context::executor_type>> work_guard_;
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:91
Contains the networking elements of the comms library.
Definition client.hpp:42
connection_state
Connection state for the client.
Definition client.hpp:47
@ connected
Connected and ready for requests.
@ disconnected
Not connected to server.
@ connecting
Initial connection in progress.
@ reconnecting
Lost connection, attempting to reconnect.
std::function< void()> reconnected_callback_t
Callback invoked when client successfully reconnects.
Definition client.hpp:86
std::function< void(const std::string &event_type, std::chrono::system_clock::time_point timestamp)> notification_callback_t
Callback invoked when client receives a notification from server.
Definition client.hpp:101
std::function< void()> reconnecting_callback_t
Callback invoked when client starts reconnection attempts.
Definition client.hpp:75
std::function< void()> disconnect_callback_t
Callback invoked when client detects server disconnect.
Definition client.hpp:64
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Complete frame with header and payload.
Definition frame.hpp:77
ORES protocol client.
Definition client.hpp:108
void set_notification_callback(notification_callback_t callback)
Set callback to be invoked when a notification is received.
Definition client.cpp:627
std::expected< messaging::frame, messaging::error_code > send_request_sync(messaging::frame request_frame)
Send a request frame and receive response frame (blocking version).
Definition client.cpp:827
void connect_with_retry_sync()
Connect to server with retry (blocking version).
Definition client.cpp:391
boost::asio::awaitable< void > connect_with_retry()
Connect to server with retry (async version).
Definition client.cpp:333
connection_state get_state() const
Get the current connection state.
Definition client.cpp:608
boost::asio::awaitable< std::expected< messaging::frame, messaging::error_code > > send_request(messaging::frame request_frame)
Send a request frame and receive response frame (async version).
Definition client.cpp:721
void set_reconnected_callback(reconnected_callback_t callback)
Set callback to be invoked when reconnection succeeds.
Definition client.cpp:622
void disconnect()
Disconnect from server.
Definition client.cpp:578
boost::asio::awaitable< void > connect()
Connect to server and perform handshake (async version).
Definition client.cpp:249
void connect_sync()
Connect to server and perform handshake (blocking version).
Definition client.cpp:269
void set_reconnecting_callback(reconnecting_callback_t callback)
Set callback to be invoked when reconnection starts.
Definition client.cpp:617
~client()
Destructor.
Definition client.cpp:99
bool is_connected() const
Check if client is connected.
Definition client.cpp:604
void set_disconnect_callback(disconnect_callback_t callback)
Set callback to be invoked when disconnect is detected.
Definition client.cpp:612
Configuration for the client.
Definition client_options.hpp:78