Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect websocket also when it is not connected. #418

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if WebsocketBase::disconnect is called just after this line? Will the code after this line still run and is that a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean to add it there? Or in the 'else'? I do not totally understand what you mean.

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
Loading