Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
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
1 change: 1 addition & 0 deletions xapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set(COMMON_FLAGS
-Werror
-Wpedantic
-Wextra
-march=native
)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
Expand Down
32 changes: 19 additions & 13 deletions xapi/Connection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Connection.hpp"
#include "Exceptions.hpp"
#include <iostream>

namespace xapi
{
Expand All @@ -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<void> Connection::connect(const boost::url &url)
Expand Down Expand Up @@ -79,7 +92,7 @@ boost::asio::awaitable<void> Connection::establishSSLConnection(

boost::asio::awaitable<void> 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);
Expand Down Expand Up @@ -148,15 +161,17 @@ boost::asio::awaitable<void> 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
{
Expand All @@ -179,14 +194,5 @@ boost::asio::awaitable<void> 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
6 changes: 0 additions & 6 deletions xapi/Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ class Connection : public IConnection
*/
boost::asio::awaitable<void> 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;

Expand Down