Skip to content

Commit

Permalink
Fixed reconnect attempt, fixed sys error on timer dtor
Browse files Browse the repository at this point in the history
Signed-off-by: AssemblyJohn <[email protected]>
  • Loading branch information
AssemblyJohn committed Feb 2, 2024
1 parent fbc95ee commit d43e21c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
2 changes: 1 addition & 1 deletion include/ocpp/common/websocket/websocket_tls_tpm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class WebsocketTlsTPM final : public WebsocketBase {
std::shared_ptr<EvseSecurity> evse_security;

// Connection related data
std::unique_ptr<Everest::SteadyTimer> reconnect_timer_tpm;
Everest::SteadyTimer reconnect_timer_tpm;
std::unique_ptr<std::thread> websocket_thread;
std::shared_ptr<ConnectionData> conn_data;
std::condition_variable conn_cv;
Expand Down
66 changes: 31 additions & 35 deletions lib/ocpp/common/websocket/websocket_tls_tpm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,25 @@ bool WebsocketTlsTPM::connect() {
this->recv_message_thread->join();
}

// Bind reconnect callback
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) {
EVLOG_info << "Closing websocket connection before reconnecting";
this->close(websocketpp::close::status::abnormal_close, "Reconnect");
}

{
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
this->reconnect_timer_tpm.stop();
}

this->connect();
};

std::unique_lock<std::mutex> lock(connection_mutex);

// Release other threads
Expand Down Expand Up @@ -517,31 +536,12 @@ bool WebsocketTlsTPM::connect() {
this->conn_data.reset();
}

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) {
EVLOG_info << "Closing websocket connection before reconnecting";
this->close(websocketpp::close::status::abnormal_close, "Reconnect");
}

{
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
if (this->reconnect_timer_tpm) {
this->reconnect_timer_tpm->stop();
}
this->reconnect_timer_tpm = nullptr;
}

this->connect();
};

return (connected);
}

void WebsocketTlsTPM::reconnect(std::error_code reason, long delay) {
EVLOG_info << "Attempting TLS TPM reconnect with reason: " << reason << " and delay:" << delay;

if (this->shutting_down) {
EVLOG_info << "Not reconnecting because the websocket is being shutdown.";
return;
Expand All @@ -553,27 +553,19 @@ void WebsocketTlsTPM::reconnect(std::error_code reason, long delay) {
this->close(websocketpp::close::status::abnormal_close, "Reconnect");
}

if (!this->reconnect_timer_tpm) {
EVLOG_info << "Reconnecting in: " << delay << "ms"
<< ", attempt: " << this->connection_attempts;
EVLOG_info << "Reconnecting in: " << delay << "ms"
<< ", attempt: " << this->connection_attempts;

this->reconnect_timer_tpm = std::make_unique<Everest::SteadyTimer>(
[this]() { this->reconnect_callback(websocketpp::lib::error_code()); });
this->reconnect_timer_tpm->timeout(std::chrono::seconds(delay));
} else {
EVLOG_info << "Reconnect timer already running";
}
this->reconnect_timer_tpm.timeout([this]() { this->reconnect_callback(websocketpp::lib::error_code()); },
std::chrono::milliseconds(delay));
}

void WebsocketTlsTPM::close(websocketpp::close::status::value code, const std::string& reason) {
EVLOG_info << "Closing TLS TPM websocket with reason: " << reason;

{
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
if (this->reconnect_timer_tpm) {
this->reconnect_timer_tpm->stop();
}
this->reconnect_timer_tpm = nullptr;
this->reconnect_timer_tpm.stop();
}

if (conn_data) {
Expand Down Expand Up @@ -617,6 +609,7 @@ void WebsocketTlsTPM::on_conn_fail() {
if (this->m_is_connected) {
this->disconnected_callback();
}

this->m_is_connected = false;
this->connection_attempts += 1;

Expand Down Expand Up @@ -842,7 +835,10 @@ int WebsocketTlsTPM::process_callback(void* wsi_ptr, int callback_reason, void*
EVLOG_info << "Conn interrupted/closed, closing socket!";

// Set the normal reason if we are interrupted
lws_close_reason(data->get_conn(), LWS_CLOSE_STATUS_NORMAL, NULL, 0);
if (data->get_conn()) {
lws_close_reason(data->get_conn(), LWS_CLOSE_STATUS_NORMAL, NULL, 0);
}

return LWS_CLOSE_SOCKET_RESPONSE_MESSAGE;
}

Expand Down

0 comments on commit d43e21c

Please sign in to comment.