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

Commit f3ac73f

Browse files
authored
Merge pull request #1 from MPogotsky/develop
Coredump fix & performance improvements
2 parents 1e7e49a + 87ac14f commit f3ac73f

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

xapi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(COMMON_FLAGS
2323
-Werror
2424
-Wpedantic
2525
-Wextra
26+
-march=native
2627
)
2728

2829
if(CMAKE_BUILD_TYPE STREQUAL "Release")

xapi/Connection.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Connection.hpp"
22
#include "Exceptions.hpp"
3+
#include <iostream>
34

45
namespace xapi
56
{
@@ -25,7 +26,19 @@ Connection::Connection(Connection &&other) noexcept
2526

2627
Connection::~Connection()
2728
{
28-
cancelAsyncOperations();
29+
m_cancellationSignal.emit(boost::asio::cancellation_type::all);
30+
if (m_websocket.is_open())
31+
{
32+
try
33+
{
34+
// Attempt a graceful WebSocket closure
35+
m_websocket.close(boost::beast::websocket::close_code::normal);
36+
}
37+
catch (const boost::system::system_error &e)
38+
{
39+
std::cerr << "Fatal error: " << e.what() << std::endl;
40+
}
41+
}
2942
}
3043

3144
boost::asio::awaitable<void> Connection::connect(const boost::url &url)
@@ -79,7 +92,7 @@ boost::asio::awaitable<void> Connection::establishSSLConnection(
7992

8093
boost::asio::awaitable<void> Connection::disconnect()
8194
{
82-
cancelAsyncOperations();
95+
m_cancellationSignal.emit(boost::asio::cancellation_type::all);
8396
try
8497
{
8598
co_await m_websocket.async_close(boost::beast::websocket::close_code::normal, boost::asio::use_awaitable);
@@ -148,15 +161,17 @@ boost::asio::awaitable<void> Connection::startKeepAlive(boost::asio::cancellatio
148161
const auto executor = co_await boost::asio::this_coro::executor;
149162
boost::asio::steady_timer pingTimer(executor);
150163
const auto pingInterval = std::chrono::seconds(20);
164+
bool canceled = true;
151165

152-
cancellationSlot.assign([&]([[maybe_unused]] boost::asio::cancellation_type type) {
166+
cancellationSlot.assign([&](boost::asio::cancellation_type type) {
153167
if (type == boost::asio::cancellation_type::all)
154168
{
169+
canceled = true;
155170
pingTimer.cancel();
156171
}
157172
});
158173

159-
while (true)
174+
while (!canceled)
160175
{
161176
try
162177
{
@@ -179,14 +194,5 @@ boost::asio::awaitable<void> Connection::startKeepAlive(boost::asio::cancellatio
179194
}
180195
}
181196

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-
191197
} // namespace internals
192198
} // namespace xapi

xapi/Connection.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ class Connection : public IConnection
9797
*/
9898
boost::asio::awaitable<void> startKeepAlive(boost::asio::cancellation_slot cancellationSlot);
9999

100-
/**
101-
* @brief Cancels all pending asynchronous operations and stops the keep-alive coroutine.
102-
* @return void.
103-
*/
104-
void cancelAsyncOperations() noexcept;
105-
106100
// SSL context, stores certificates.
107101
boost::asio::ssl::context m_sslContext;
108102

0 commit comments

Comments
 (0)