3#ifndef OCPP_WEBSOCKET_BASE_HPP
4#define OCPP_WEBSOCKET_BASE_HPP
11#include <everest/timer.hpp>
13#include <ocpp/common/types.hpp>
14#include <ocpp/common/websocket/websocket_uri.hpp>
19 std::vector<OcppProtocolVersion> ocpp_versions;
22 std::optional<std::string> authorization_key;
23 int retry_backoff_random_range_s;
24 int retry_backoff_repeat_times;
25 int retry_backoff_wait_minimum_s;
26 int max_connection_attempts;
27 std::string supported_ciphers_12;
28 std::string supported_ciphers_13;
30 std::string ping_payload;
32 bool use_ssl_default_verify_paths;
33 std::optional<bool> additional_root_certificate_check;
34 std::optional<std::string> hostName;
35 bool verify_csms_common_name;
37 bool verify_csms_allow_wildcards;
38 std::optional<std::string> iface;
48 std::atomic_bool m_is_connected;
50 std::function<void(OcppProtocolVersion protocol)> connected_callback;
51 std::function<void()> disconnected_callback;
52 std::function<void(
const WebsocketCloseReason reason)> stopped_connecting_callback;
53 std::function<void(
const std::string& message)> message_callback;
54 std::function<void(ConnectionFailedReason)> connection_failed_callback;
55 std::shared_ptr<boost::asio::steady_timer> reconnect_timer;
56 std::unique_ptr<Everest::SteadyTimer> ping_timer;
57 std::mutex reconnect_mutex;
58 std::mutex connection_mutex;
59 std::atomic_int reconnect_backoff_ms;
60 std::atomic_int connection_attempts;
61 std::atomic_bool shutting_down;
62 std::atomic_bool reconnecting;
72 void log_on_fail(
const std::error_code& ec,
const boost::system::error_code& transport_ec,
const int http_status);
79 void cancel_reconnect_timer();
106 void disconnect(
const WebsocketCloseReason code);
112 virtual void close(
const WebsocketCloseReason code,
const std::string& reason) = 0;
132 virtual bool send(
const std::string& message) = 0;
Definition: websocket_uri.hpp:14
contains a websocket abstraction
Definition: websocket_base.hpp:46
void log_on_fail(const std::error_code &ec, const boost::system::error_code &transport_ec, const int http_status)
Logs websocket connection error.
Definition: websocket_base.cpp:123
void register_connection_failed_callback(const std::function< void(ConnectionFailedReason)> &callback)
register a callback that is called when the websocket could not connect with a specific reason
Definition: websocket_base.cpp:56
void register_connected_callback(const std::function< void(OcppProtocolVersion protocol)> &callback)
register a callback that is called when the websocket is connected successfully
Definition: websocket_base.cpp:39
void set_authorization_key(const std::string &authorization_key)
set the authorization_key of the connection_options
Definition: websocket_base.cpp:172
long get_reconnect_interval()
Calculates and returns the reconnect interval based on int retry_backoff_random_range_s,...
Definition: websocket_base.cpp:132
void register_stopped_connecting_callback(const std::function< void(const WebsocketCloseReason reason)> &callback)
register a callback that is called when the websocket connection has been closed and will not attempt...
Definition: websocket_base.cpp:47
bool initialized()
Indicates if the required callbacks are registered.
Definition: websocket_base.cpp:60
void disconnect(const WebsocketCloseReason code)
disconnect the websocket
Definition: websocket_base.cpp:77
void register_message_callback(const std::function< void(const std::string &message)> &callback)
register a callback that is called when the websocket receives a message
Definition: websocket_base.cpp:52
virtual void set_connection_options(const WebsocketConnectionOptions &connection_options)=0
sets this connection_options to the given connection_options and resets the connection_attempts
WebsocketBase()
Creates a new WebsocketBase object. The connection_options must be initialised with set_connection_op...
Definition: websocket_base.cpp:10
virtual void reconnect(long delay)=0
reconnect the websocket after the delay
std::optional< std::string > getAuthorizationHeader()
getter for authorization header for connection with basic authentication
Definition: websocket_base.cpp:106
virtual bool send(const std::string &message)=0
send a message over the websocket
void register_disconnected_callback(const std::function< void()> &callback)
register a callback that is called when the websocket connection is disconnected
Definition: websocket_base.cpp:43
virtual void ping()=0
send a websocket ping
void on_pong_timeout(std::string msg)
Called when a websocket pong timeout is received.
Definition: websocket_base.cpp:176
void set_websocket_ping_interval(int32_t interval_s)
starts a timer that sends a websocket ping at the given interval_s
Definition: websocket_base.cpp:162
virtual bool start_connecting()=0
Starts the connection attempts. It will init the websocket processing thread.
virtual void close(const WebsocketCloseReason code, const std::string &reason)=0
closes the websocket
bool is_connected()
indicates if the websocket is connected
Definition: websocket_base.cpp:102
Definition: websocket_base.hpp:18
bool enable_tls_keylog
If set to true enables logging of TLS secrets to the keylog_file.
Definition: websocket_base.hpp:39
std::optional< std::filesystem::path > keylog_file
Optional path to a keylog file.
Definition: websocket_base.hpp:40