-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathwebsocket_base.cpp
183 lines (148 loc) · 6.23 KB
/
websocket_base.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
#include <random>
#include <everest/logging.hpp>
#include <ocpp/common/websocket/websocket_base.hpp>
#include <websocketpp_utils/base64.hpp>
namespace ocpp {
WebsocketBase::WebsocketBase() :
m_is_connected(false),
connected_callback(nullptr),
closed_callback(nullptr),
message_callback(nullptr),
reconnect_timer(nullptr),
connection_attempts(1),
reconnect_backoff_ms(0),
shutting_down(false),
reconnecting(false) {
set_connection_options_base(connection_options);
this->ping_timer = std::make_unique<Everest::SteadyTimer>();
const auto auth_key = connection_options.authorization_key;
if (auth_key.has_value() and auth_key.value().length() < 16) {
EVLOG_warning << "AuthorizationKey with only " << auth_key.value().length()
<< " characters has been configured";
}
}
WebsocketBase::~WebsocketBase() {
this->cancel_reconnect_timer();
}
void WebsocketBase::set_connection_options_base(const WebsocketConnectionOptions& connection_options) {
this->connection_options = connection_options;
}
void WebsocketBase::register_connected_callback(const std::function<void(const int security_profile)>& callback) {
this->connected_callback = callback;
}
void WebsocketBase::register_disconnected_callback(const std::function<void()>& callback) {
this->disconnected_callback = callback;
}
void WebsocketBase::register_closed_callback(const std::function<void(const WebsocketCloseReason reason)>& callback) {
this->closed_callback = callback;
}
void WebsocketBase::register_message_callback(const std::function<void(const std::string& message)>& callback) {
this->message_callback = callback;
}
void WebsocketBase::register_connection_failed_callback(const std::function<void(ConnectionFailedReason)>& callback) {
this->connection_failed_callback = callback;
}
bool WebsocketBase::initialized() {
if (this->connected_callback == nullptr) {
EVLOG_error << "Not properly initialized: please register connected callback.";
return false;
}
if (this->closed_callback == nullptr) {
EVLOG_error << "Not properly initialized: please closed_callback.";
return false;
}
if (this->message_callback == nullptr) {
EVLOG_error << "Not properly initialized: please register message callback.";
return false;
}
return true;
}
void WebsocketBase::disconnect(const WebsocketCloseReason code) {
if (!this->initialized()) {
EVLOG_error << "Cannot disconnect a websocket that was not initialized";
return;
}
{
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
if (code == WebsocketCloseReason::Normal) {
this->shutting_down = true;
}
if (this->reconnect_timer) {
this->reconnect_timer.get()->cancel();
}
}
if (this->ping_timer) {
this->ping_timer->stop();
}
EVLOG_info << "Disconnecting websocket...";
this->close(code, "");
}
bool WebsocketBase::is_connected() {
return this->m_is_connected;
}
std::optional<std::string> WebsocketBase::getAuthorizationHeader() {
std::optional<std::string> auth_header = std::nullopt;
const auto authorization_key = this->connection_options.authorization_key;
if (authorization_key.has_value()) {
EVLOG_debug << "AuthorizationKey present, encoding authentication header";
std::string plain_auth_header =
this->connection_options.csms_uri.get_chargepoint_id() + ":" + authorization_key.value();
// TODO (ioan): replace with libevse-security usage
auth_header.emplace(std::string("Basic ") + ocpp::base64_encode(plain_auth_header));
EVLOG_debug << "Basic Auth header: " << auth_header.value();
}
return auth_header;
}
void WebsocketBase::log_on_fail(const std::error_code& ec, const boost::system::error_code& transport_ec,
const int http_status) {
EVLOG_error << "Failed to connect to websocket server"
<< ", error_code: " << ec.value() << ", reason: " << ec.message()
<< ", HTTP response code: " << http_status << ", category: " << ec.category().name()
<< ", transport error code: " << transport_ec.value()
<< ", Transport error category: " << transport_ec.category().name();
}
long WebsocketBase::get_reconnect_interval() {
// We need to add 1 to the repeat times since the first try is already connection_attempt 1
if (this->connection_attempts > (this->connection_options.retry_backoff_repeat_times + 1)) {
return this->reconnect_backoff_ms;
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(0, this->connection_options.retry_backoff_random_range_s);
int random_number = distr(gen);
if (this->connection_attempts == 1) {
this->reconnect_backoff_ms = (this->connection_options.retry_backoff_wait_minimum_s + random_number) * 1000;
return this->reconnect_backoff_ms;
}
this->reconnect_backoff_ms = this->reconnect_backoff_ms * 2 + (random_number * 1000);
return this->reconnect_backoff_ms;
}
void WebsocketBase::cancel_reconnect_timer() {
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
if (this->reconnect_timer) {
this->reconnect_timer.get()->cancel();
}
this->reconnect_timer = nullptr;
}
void WebsocketBase::set_websocket_ping_interval(int32_t interval_s) {
if (this->ping_timer) {
this->ping_timer->stop();
}
if (interval_s > 0) {
this->ping_timer->interval([this]() { this->ping(); }, std::chrono::seconds(interval_s));
}
this->connection_options.ping_interval_s = interval_s;
}
void WebsocketBase::set_authorization_key(const std::string& authorization_key) {
this->connection_options.authorization_key = authorization_key;
}
void WebsocketBase::on_pong_timeout(std::string msg) {
if (!this->reconnecting) {
EVLOG_info << "Reconnecting because of a pong timeout after " << this->connection_options.pong_timeout_s << "s";
this->reconnecting = true;
this->close(WebsocketCloseReason::GoingAway, "Pong timeout");
}
}
} // namespace ocpp