This repository was archived by the owner on Apr 1, 2025. It is now read-only.
File tree 2 files changed +24
-7
lines changed
2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,11 @@ Connection::Connection(Connection &&other) noexcept
23
23
{
24
24
}
25
25
26
+ Connection::~Connection ()
27
+ {
28
+ cancelAsyncOperations ();
29
+ }
30
+
26
31
boost::asio::awaitable<void > Connection::connect (const boost::url &url)
27
32
{
28
33
const auto executor = co_await boost::asio::this_coro::executor;
@@ -74,10 +79,7 @@ boost::asio::awaitable<void> Connection::establishSSLConnection(
74
79
75
80
boost::asio::awaitable<void > Connection::disconnect ()
76
81
{
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 ();
81
83
try
82
84
{
83
85
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
177
179
}
178
180
}
179
181
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
+
180
191
} // namespace internals
181
192
} // namespace xapi
Original file line number Diff line number Diff line change @@ -34,8 +34,8 @@ class Connection
34
34
public:
35
35
Connection () = delete ;
36
36
37
- Connection (const Connection &) = delete ;
38
- Connection &operator =(const Connection &) = delete ;
37
+ Connection (const Connection &other ) = delete ;
38
+ Connection &operator =(const Connection &other ) = delete ;
39
39
40
40
Connection (Connection &&other) noexcept ;
41
41
// Move assignment operator is not supported because of boost::beast::websocket::stream
@@ -47,7 +47,7 @@ class Connection
47
47
*/
48
48
explicit Connection (boost::asio::io_context &ioContext);
49
49
50
- virtual ~Connection () = default ;
50
+ virtual ~Connection ();
51
51
52
52
/* *
53
53
* @brief Asynchronously establishes secure WebSocket connection to the server.
@@ -101,6 +101,12 @@ class Connection
101
101
*/
102
102
boost::asio::awaitable<void > startKeepAlive (boost::asio::cancellation_slot cancellationSlot);
103
103
104
+ /* *
105
+ * @brief Cancels all pending asynchronous operations and stops the keep-alive coroutine.
106
+ * @return void.
107
+ */
108
+ void cancelAsyncOperations () noexcept ;
109
+
104
110
// SSL context, stores certificates.
105
111
boost::asio::ssl::context m_sslContext;
106
112
You can’t perform that action at this time.
0 commit comments