diff --git a/xapi/CMakeLists.txt b/xapi/CMakeLists.txt index d82ae17..7d56237 100644 --- a/xapi/CMakeLists.txt +++ b/xapi/CMakeLists.txt @@ -23,6 +23,7 @@ set(COMMON_FLAGS -Werror -Wpedantic -Wextra + -march=native ) if(CMAKE_BUILD_TYPE STREQUAL "Release") diff --git a/xapi/Connection.cpp b/xapi/Connection.cpp index 8914bec..9f04f7e 100644 --- a/xapi/Connection.cpp +++ b/xapi/Connection.cpp @@ -1,5 +1,6 @@ #include "Connection.hpp" #include "Exceptions.hpp" +#include namespace xapi { @@ -25,7 +26,19 @@ Connection::Connection(Connection &&other) noexcept Connection::~Connection() { - cancelAsyncOperations(); + m_cancellationSignal.emit(boost::asio::cancellation_type::all); + if (m_websocket.is_open()) + { + try + { + // Attempt a graceful WebSocket closure + m_websocket.close(boost::beast::websocket::close_code::normal); + } + catch (const boost::system::system_error &e) + { + std::cerr << "Fatal error: " << e.what() << std::endl; + } + } } boost::asio::awaitable Connection::connect(const boost::url &url) @@ -79,7 +92,7 @@ boost::asio::awaitable Connection::establishSSLConnection( boost::asio::awaitable Connection::disconnect() { - cancelAsyncOperations(); + m_cancellationSignal.emit(boost::asio::cancellation_type::all); try { co_await m_websocket.async_close(boost::beast::websocket::close_code::normal, boost::asio::use_awaitable); @@ -148,15 +161,17 @@ boost::asio::awaitable Connection::startKeepAlive(boost::asio::cancellatio const auto executor = co_await boost::asio::this_coro::executor; boost::asio::steady_timer pingTimer(executor); const auto pingInterval = std::chrono::seconds(20); + bool canceled = true; - cancellationSlot.assign([&]([[maybe_unused]] boost::asio::cancellation_type type) { + cancellationSlot.assign([&](boost::asio::cancellation_type type) { if (type == boost::asio::cancellation_type::all) { + canceled = true; pingTimer.cancel(); } }); - while (true) + while (!canceled) { try { @@ -179,14 +194,5 @@ boost::asio::awaitable Connection::startKeepAlive(boost::asio::cancellatio } } -void Connection::cancelAsyncOperations() noexcept -{ - m_cancellationSignal.emit(boost::asio::cancellation_type::all); - if (m_websocket.is_open()) - { - m_websocket.next_layer().next_layer().cancel(); - } -} - } // namespace internals } // namespace xapi diff --git a/xapi/Connection.hpp b/xapi/Connection.hpp index e3ed0e6..aeb80ef 100644 --- a/xapi/Connection.hpp +++ b/xapi/Connection.hpp @@ -97,12 +97,6 @@ class Connection : public IConnection */ boost::asio::awaitable startKeepAlive(boost::asio::cancellation_slot cancellationSlot); - /** - * @brief Cancels all pending asynchronous operations and stops the keep-alive coroutine. - * @return void. - */ - void cancelAsyncOperations() noexcept; - // SSL context, stores certificates. boost::asio::ssl::context m_sslContext;