Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit af86ff5

Browse files
committed
Add cleanup on Connection destruction
1 parent 6517e71 commit af86ff5

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

xapi/Connection.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Connection::Connection(Connection &&other) noexcept
2323
{
2424
}
2525

26+
Connection::~Connection()
27+
{
28+
cancelAsyncOperations();
29+
}
30+
2631
boost::asio::awaitable<void> Connection::connect(const boost::url &url)
2732
{
2833
const auto executor = co_await boost::asio::this_coro::executor;
@@ -74,10 +79,7 @@ boost::asio::awaitable<void> Connection::establishSSLConnection(
7479

7580
boost::asio::awaitable<void> Connection::disconnect()
7681
{
77-
m_cancellationSignal.emit(boost::asio::cancellation_type::all);
78-
// Cancel all pending asynchronous operations
79-
m_websocket.next_layer().next_layer().cancel();
80-
82+
cancelAsyncOperations();
8183
try
8284
{
8385
co_await m_websocket.async_close(boost::beast::websocket::close_code::normal, boost::asio::use_awaitable);
@@ -177,5 +179,14 @@ boost::asio::awaitable<void> Connection::startKeepAlive(boost::asio::cancellatio
177179
}
178180
}
179181

182+
void Connection::cancelAsyncOperations() noexcept
183+
{
184+
m_cancellationSignal.emit(boost::asio::cancellation_type::all);
185+
if (m_websocket.is_open())
186+
{
187+
m_websocket.next_layer().next_layer().cancel();
188+
}
189+
}
190+
180191
} // namespace internals
181192
} // namespace xapi

xapi/Connection.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Connection
3434
public:
3535
Connection() = delete;
3636

37-
Connection(const Connection &) = delete;
38-
Connection &operator=(const Connection &) = delete;
37+
Connection(const Connection &other) = delete;
38+
Connection &operator=(const Connection &other) = delete;
3939

4040
Connection(Connection &&other) noexcept;
4141
// Move assignment operator is not supported because of boost::beast::websocket::stream
@@ -47,7 +47,7 @@ class Connection
4747
*/
4848
explicit Connection(boost::asio::io_context &ioContext);
4949

50-
virtual ~Connection() = default;
50+
virtual ~Connection();
5151

5252
/**
5353
* @brief Asynchronously establishes secure WebSocket connection to the server.
@@ -101,6 +101,12 @@ class Connection
101101
*/
102102
boost::asio::awaitable<void> startKeepAlive(boost::asio::cancellation_slot cancellationSlot);
103103

104+
/**
105+
* @brief Cancels all pending asynchronous operations and stops the keep-alive coroutine.
106+
* @return void.
107+
*/
108+
void cancelAsyncOperations() noexcept;
109+
104110
// SSL context, stores certificates.
105111
boost::asio::ssl::context m_sslContext;
106112

0 commit comments

Comments
 (0)