Skip to content

Commit

Permalink
Disconnect websocket also when it is not connected. (#418)
Browse files Browse the repository at this point in the history
* Disconnect websocket also when it is not connected. Add some more checks. Cancel timer in websocket destructor.

---------

Signed-off-by: Maaike Zijderveld <[email protected]>
Signed-off-by: Maaike <[email protected]>
Co-authored-by: Maaike Zijderveld <[email protected]>
  • Loading branch information
maaikez and Maaike Zijderveld authored Feb 23, 2024
1 parent d4c9bf4 commit 72cbc5a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
17 changes: 12 additions & 5 deletions lib/ocpp/common/websocket/websocket_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ WebsocketBase::WebsocketBase() :
}

WebsocketBase::~WebsocketBase() {
this->cancel_reconnect_timer();
}

void WebsocketBase::set_connection_options_base(const WebsocketConnectionOptions& connection_options) {
Expand Down Expand Up @@ -77,12 +78,18 @@ void WebsocketBase::disconnect(websocketpp::close::status::value code) {
EVLOG_error << "Cannot disconnect a websocket that was not initialized";
return;
}
if (code == websocketpp::close::status::normal) {
this->shutting_down = true;
}
if (this->reconnect_timer) {
this->reconnect_timer.get()->cancel();

{
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
if (code == websocketpp::close::status::normal) {
this->shutting_down = true;
}

if (this->reconnect_timer) {
this->reconnect_timer.get()->cancel();
}
}

if (this->ping_timer) {
this->ping_timer->stop();
}
Expand Down
28 changes: 15 additions & 13 deletions lib/ocpp/common/websocket/websocket_plain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,23 @@ bool WebsocketPlain::connect() {
websocket_thread.reset(new websocketpp::lib::thread(&client::run, &this->ws_client));

this->reconnect_callback = [this](const websocketpp::lib::error_code& ec) {
EVLOG_info << "Reconnecting to plain websocket at uri: " << this->connection_options.csms_uri.string()
<< " with security profile: " << this->connection_options.security_profile;

// close connection before reconnecting
if (this->m_is_connected) {
try {
EVLOG_info << "Closing websocket connection before reconnecting";
this->ws_client.close(this->handle, websocketpp::close::status::normal, "");
} catch (std::exception& e) {
EVLOG_error << "Error on plain close: " << e.what();
if (!this->shutting_down) {
EVLOG_info << "Reconnecting to plain websocket at uri: " << this->connection_options.csms_uri.string()
<< " with security profile: " << this->connection_options.security_profile;

// close connection before reconnecting
if (this->m_is_connected) {
try {
EVLOG_info << "Closing websocket connection before reconnecting";
this->ws_client.close(this->handle, websocketpp::close::status::normal, "");
} catch (std::exception& e) {
EVLOG_error << "Error on plain close: " << e.what();
}
}
}

this->cancel_reconnect_timer();
this->connect_plain();
this->cancel_reconnect_timer();
this->connect_plain();
}
};

this->connect_plain();
Expand Down
29 changes: 16 additions & 13 deletions lib/ocpp/common/websocket/websocket_tls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void WebsocketTLS::set_connection_options(const WebsocketConnectionOptions& conn

this->connection_options.csms_uri.set_secure(true);
}

bool WebsocketTLS::connect() {
if (!this->initialized()) {
return false;
Expand All @@ -151,21 +152,23 @@ bool WebsocketTLS::connect() {
websocketpp::lib::placeholders::_1, this->connection_options.security_profile));

this->reconnect_callback = [this](const websocketpp::lib::error_code& ec) {
EVLOG_info << "Reconnecting to TLS websocket at uri: " << this->connection_options.csms_uri.string()
<< " with security profile: " << this->connection_options.security_profile;

// close connection before reconnecting
if (this->m_is_connected) {
try {
EVLOG_info << "Closing websocket connection before reconnecting";
this->wss_client.close(this->handle, websocketpp::close::status::normal, "");
} catch (std::exception& e) {
EVLOG_error << "Error on TLS close: " << e.what();
if (!this->shutting_down) {
EVLOG_info << "Reconnecting to TLS websocket at uri: " << this->connection_options.csms_uri.string()
<< " with security profile: " << this->connection_options.security_profile;

// close connection before reconnecting
if (this->m_is_connected) {
try {
EVLOG_info << "Closing websocket connection before reconnecting";
this->wss_client.close(this->handle, websocketpp::close::status::normal, "");
} catch (std::exception& e) {
EVLOG_error << "Error on TLS close: " << e.what();
}
}
}

this->cancel_reconnect_timer();
this->connect_tls();
this->cancel_reconnect_timer();
this->connect_tls();
}
};

this->connect_tls();
Expand Down
6 changes: 2 additions & 4 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,8 @@ void ChargePoint::connect_websocket() {

void ChargePoint::disconnect_websocket(websocketpp::close::status::value code) {
if (this->websocket != nullptr) {
if (this->websocket->is_connected()) {
this->disable_automatic_websocket_reconnects = true;
this->websocket->disconnect(code);
}
this->disable_automatic_websocket_reconnects = true;
this->websocket->disconnect(code);
}
}

Expand Down

0 comments on commit 72cbc5a

Please sign in to comment.