From d43e21c5c261e0b4013727f7e685b9137f414098 Mon Sep 17 00:00:00 2001 From: AssemblyJohn Date: Fri, 2 Feb 2024 13:00:12 +0200 Subject: [PATCH] Fixed reconnect attempt, fixed sys error on timer dtor Signed-off-by: AssemblyJohn --- .../common/websocket/websocket_tls_tpm.hpp | 2 +- .../common/websocket/websocket_tls_tpm.cpp | 66 +++++++++---------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/include/ocpp/common/websocket/websocket_tls_tpm.hpp b/include/ocpp/common/websocket/websocket_tls_tpm.hpp index 49b7720975..67709a382f 100644 --- a/include/ocpp/common/websocket/websocket_tls_tpm.hpp +++ b/include/ocpp/common/websocket/websocket_tls_tpm.hpp @@ -73,7 +73,7 @@ class WebsocketTlsTPM final : public WebsocketBase { std::shared_ptr evse_security; // Connection related data - std::unique_ptr reconnect_timer_tpm; + Everest::SteadyTimer reconnect_timer_tpm; std::unique_ptr websocket_thread; std::shared_ptr conn_data; std::condition_variable conn_cv; diff --git a/lib/ocpp/common/websocket/websocket_tls_tpm.cpp b/lib/ocpp/common/websocket/websocket_tls_tpm.cpp index 12a875f5cb..614886966c 100644 --- a/lib/ocpp/common/websocket/websocket_tls_tpm.cpp +++ b/lib/ocpp/common/websocket/websocket_tls_tpm.cpp @@ -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 lk(this->reconnect_mutex); + this->reconnect_timer_tpm.stop(); + } + + this->connect(); + }; + std::unique_lock lock(connection_mutex); // Release other threads @@ -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 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; @@ -553,16 +553,11 @@ 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( - [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) { @@ -570,10 +565,7 @@ void WebsocketTlsTPM::close(websocketpp::close::status::value code, const std::s { std::lock_guard 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) { @@ -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; @@ -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; }