Skip to content

Commit

Permalink
Removed the blocking function in connect() method of libwebsockets
Browse files Browse the repository at this point in the history
Signed-off-by: Piet Gömpel <[email protected]>
  • Loading branch information
Pietfried committed Nov 29, 2024
1 parent cb4aa5b commit 8aa5605
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 36 deletions.
34 changes: 1 addition & 33 deletions lib/ocpp/common/websocket/websocket_libwebsockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,6 @@ bool WebsocketLibwebsockets::connect() {
empty.swap(recv_message_queue);
}

bool timeouted = false;
bool connected = false;

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

Expand All @@ -780,37 +777,8 @@ bool WebsocketLibwebsockets::connect() {
// advance we will have a dead-lock
this->recv_message_thread = std::make_unique<std::thread>(&WebsocketLibwebsockets::recv_loop, this);

// Wait until connect or timeout
timeouted = !conn_cv.wait_for(lock, std::chrono::seconds(60), [&]() {
return !local_data->is_connecting() && EConnectionState::INITIALIZE != local_data->get_state();
});

connected = (local_data->get_state() == EConnectionState::CONNECTED);
}

if (!connected) {
EVLOG_info << "Connect failed with state: " << (int)local_data->get_state() << " Timeouted: " << timeouted;

// If we timeouted the on_conn_fail was not dispatched, since it did not had the chance
if (timeouted && local_data->get_state() != EConnectionState::ERROR) {
EVLOG_error << "Conn failed with timeout, without disconnect dispatch, dispatching manually.";
on_conn_fail();
}

// Interrupt and drop the connection data
local_data->do_interrupt();

// Also interrupt the latest conenction, if it was set by a parallel thread
auto local = conn_data;

if (local != nullptr) {
local->do_interrupt();
}

conn_data.reset();
}

return (connected);
return true;
}

void WebsocketLibwebsockets::reconnect(long delay) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ocpp/v16/charge_point_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ WebsocketConnectionOptions ChargePointImpl::get_ws_connection_options() {
void ChargePointImpl::connect_websocket() {
if (!this->websocket->is_connected()) {
this->init_websocket();
this->websocket_timer.timeout([this]() { this->websocket->connect(); }, std::chrono::seconds(0));
this->websocket->connect();
}
}

Expand Down Expand Up @@ -1073,7 +1073,7 @@ bool ChargePointImpl::start(const std::map<int, ChargePointStatus>& connector_st
this->bootreason = bootreason;
this->init_state_machine(connector_status_map);
this->init_websocket();
std::thread([this]() { this->websocket->connect(); }).detach();
this->websocket->connect();
// push transaction messages including SecurityEventNotification.req onto the message queue
this->message_queue->get_persisted_messages_from_db(this->configuration->getDisableSecurityEventNotifications());
this->boot_notification();
Expand Down
2 changes: 1 addition & 1 deletion lib/ocpp/v201/connectivity_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void ConnectivityManager::connect(std::optional<int32_t> configuration_slot_opt)
// After the websocket gets closed a reconnect will be triggered
this->websocket->disconnect(WebsocketCloseReason::ServiceRestart);
} else {
std::thread([this]() { this->try_connect_websocket(); }).detach();
this->try_connect_websocket();
}
}

Expand Down

0 comments on commit 8aa5605

Please sign in to comment.