2023-07-21 11:21:16 +08:00
|
|
|
#ifndef IOCONTEXT_H
|
|
|
|
#define IOCONTEXT_H
|
|
|
|
|
|
|
|
#include "Singleton.h"
|
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
class IoContext {
|
|
|
|
public:
|
|
|
|
enum class Mode {
|
|
|
|
Synchronous,
|
|
|
|
Asynchronous,
|
|
|
|
};
|
|
|
|
friend class Amass::Singleton<IoContext, Amass::LocalInstance>;
|
|
|
|
std::shared_ptr<boost::asio::io_context> ioContext() const { return m_ioContext; }
|
|
|
|
template <Mode mode = Mode::Asynchronous>
|
|
|
|
void run() {
|
|
|
|
if constexpr (mode == Mode::Asynchronous) {
|
|
|
|
m_thread = std::thread(&IoContext::runIoContext, this);
|
|
|
|
} else {
|
|
|
|
runIoContext();
|
|
|
|
}
|
|
|
|
}
|
2024-09-07 21:54:50 +08:00
|
|
|
void stop();
|
2023-07-21 11:21:16 +08:00
|
|
|
~IoContext();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <typename... Args>
|
|
|
|
IoContext(Args &&... args) : m_ioContext(std::make_shared<boost::asio::io_context>(std::forward<Args>(args)...)) {}
|
|
|
|
void runIoContext();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::thread m_thread;
|
|
|
|
std::shared_ptr<boost::asio::io_context> m_ioContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // IOCONTEXT_H
|