Skip to content

Commit 72cbc5a

Browse files
maaikezMaaike Zijderveld
and
Maaike Zijderveld
authored
Disconnect websocket also when it is not connected. (#418)
* Disconnect websocket also when it is not connected. Add some more checks. Cancel timer in websocket destructor. --------- Signed-off-by: Maaike Zijderveld <m.zijderveld@alfen.nl> Signed-off-by: Maaike <maaike@iolar.nl> Co-authored-by: Maaike Zijderveld <m.zijderveld@alfen.nl>
1 parent d4c9bf4 commit 72cbc5a

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

lib/ocpp/common/websocket/websocket_base.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ WebsocketBase::WebsocketBase() :
2828
}
2929

3030
WebsocketBase::~WebsocketBase() {
31+
this->cancel_reconnect_timer();
3132
}
3233

3334
void WebsocketBase::set_connection_options_base(const WebsocketConnectionOptions& connection_options) {
@@ -77,12 +78,18 @@ void WebsocketBase::disconnect(websocketpp::close::status::value code) {
7778
EVLOG_error << "Cannot disconnect a websocket that was not initialized";
7879
return;
7980
}
80-
if (code == websocketpp::close::status::normal) {
81-
this->shutting_down = true;
82-
}
83-
if (this->reconnect_timer) {
84-
this->reconnect_timer.get()->cancel();
81+
82+
{
83+
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
84+
if (code == websocketpp::close::status::normal) {
85+
this->shutting_down = true;
86+
}
87+
88+
if (this->reconnect_timer) {
89+
this->reconnect_timer.get()->cancel();
90+
}
8591
}
92+
8693
if (this->ping_timer) {
8794
this->ping_timer->stop();
8895
}

lib/ocpp/common/websocket/websocket_plain.cpp

+15-13
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ bool WebsocketPlain::connect() {
5151
websocket_thread.reset(new websocketpp::lib::thread(&client::run, &this->ws_client));
5252

5353
this->reconnect_callback = [this](const websocketpp::lib::error_code& ec) {
54-
EVLOG_info << "Reconnecting to plain websocket at uri: " << this->connection_options.csms_uri.string()
55-
<< " with security profile: " << this->connection_options.security_profile;
56-
57-
// close connection before reconnecting
58-
if (this->m_is_connected) {
59-
try {
60-
EVLOG_info << "Closing websocket connection before reconnecting";
61-
this->ws_client.close(this->handle, websocketpp::close::status::normal, "");
62-
} catch (std::exception& e) {
63-
EVLOG_error << "Error on plain close: " << e.what();
54+
if (!this->shutting_down) {
55+
EVLOG_info << "Reconnecting to plain websocket at uri: " << this->connection_options.csms_uri.string()
56+
<< " with security profile: " << this->connection_options.security_profile;
57+
58+
// close connection before reconnecting
59+
if (this->m_is_connected) {
60+
try {
61+
EVLOG_info << "Closing websocket connection before reconnecting";
62+
this->ws_client.close(this->handle, websocketpp::close::status::normal, "");
63+
} catch (std::exception& e) {
64+
EVLOG_error << "Error on plain close: " << e.what();
65+
}
6466
}
65-
}
6667

67-
this->cancel_reconnect_timer();
68-
this->connect_plain();
68+
this->cancel_reconnect_timer();
69+
this->connect_plain();
70+
}
6971
};
7072

7173
this->connect_plain();

lib/ocpp/common/websocket/websocket_tls.cpp

+16-13
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ void WebsocketTLS::set_connection_options(const WebsocketConnectionOptions& conn
132132

133133
this->connection_options.csms_uri.set_secure(true);
134134
}
135+
135136
bool WebsocketTLS::connect() {
136137
if (!this->initialized()) {
137138
return false;
@@ -151,21 +152,23 @@ bool WebsocketTLS::connect() {
151152
websocketpp::lib::placeholders::_1, this->connection_options.security_profile));
152153

153154
this->reconnect_callback = [this](const websocketpp::lib::error_code& ec) {
154-
EVLOG_info << "Reconnecting to TLS websocket at uri: " << this->connection_options.csms_uri.string()
155-
<< " with security profile: " << this->connection_options.security_profile;
156-
157-
// close connection before reconnecting
158-
if (this->m_is_connected) {
159-
try {
160-
EVLOG_info << "Closing websocket connection before reconnecting";
161-
this->wss_client.close(this->handle, websocketpp::close::status::normal, "");
162-
} catch (std::exception& e) {
163-
EVLOG_error << "Error on TLS close: " << e.what();
155+
if (!this->shutting_down) {
156+
EVLOG_info << "Reconnecting to TLS websocket at uri: " << this->connection_options.csms_uri.string()
157+
<< " with security profile: " << this->connection_options.security_profile;
158+
159+
// close connection before reconnecting
160+
if (this->m_is_connected) {
161+
try {
162+
EVLOG_info << "Closing websocket connection before reconnecting";
163+
this->wss_client.close(this->handle, websocketpp::close::status::normal, "");
164+
} catch (std::exception& e) {
165+
EVLOG_error << "Error on TLS close: " << e.what();
166+
}
164167
}
165-
}
166168

167-
this->cancel_reconnect_timer();
168-
this->connect_tls();
169+
this->cancel_reconnect_timer();
170+
this->connect_tls();
171+
}
169172
};
170173

171174
this->connect_tls();

lib/ocpp/v201/charge_point.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,8 @@ void ChargePoint::connect_websocket() {
210210

211211
void ChargePoint::disconnect_websocket(websocketpp::close::status::value code) {
212212
if (this->websocket != nullptr) {
213-
if (this->websocket->is_connected()) {
214-
this->disable_automatic_websocket_reconnects = true;
215-
this->websocket->disconnect(code);
216-
}
213+
this->disable_automatic_websocket_reconnects = true;
214+
this->websocket->disconnect(code);
217215
}
218216
}
219217

0 commit comments

Comments
 (0)