Kylin/Universal/IoContext.cpp
2024-09-07 21:54:50 +08:00

39 lines
1.1 KiB
C++

#include "IoContext.h"
#include <boost/asio/executor_work_guard.hpp>
#include <boost/scope_exit.hpp>
#include <boost/stacktrace.hpp>
IoContext::~IoContext() {
stop();
}
void IoContext::stop() {
m_ioContext->stop();
if (m_thread.joinable()) m_thread.join();
}
void IoContext::runIoContext() {
LOG(info) << "asio context started ...";
BOOST_SCOPE_EXIT(void) {
LOG(info) << "asio context exited ...";
}
BOOST_SCOPE_EXIT_END
try {
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work(m_ioContext->get_executor());
m_ioContext->run();
} catch (const boost::log::system_error &error) {
LOG(error) << error.what();
} catch (const std::out_of_range &e) {
LOG(error) << e.what();
} catch (const boost::exception &e) {
LOG(error) << boost::diagnostic_information(e);
} catch (const std::exception &e) {
#if BOOST_VERSION < 108600
LOG(error) << e.what();
#else
boost::stacktrace::stacktrace trace = boost::stacktrace::stacktrace::from_current_exception();
LOG(error) << e.what() << ", trace:\n" << trace;
#endif
}
}