Kurlyk
Loading...
Searching...
No Matches
SimpleWebSocketWorker.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_SIMPLE_WEB_SOCKET_WORKER_HPP_INCLUDED
3#define _KURLYK_SIMPLE_WEB_SOCKET_WORKER_HPP_INCLUDED
4
7
8namespace kurlyk {
9
13 public:
14
18 static SimpleWebSocketWorker instance;
19 return instance;
20 }
21
24 std::shared_ptr<SimpleWeb::io_context> get_io_context() {
25 return m_io_context;
26 }
27
29 void notify() {
30 std::lock_guard<std::mutex> locker(m_notify_mutex);
31 m_notify_condition.notify_one();
32 m_notify = true;
33 }
34
36 void start() {
37 std::unique_lock<std::mutex> locker(m_is_worker_started_mutex);
38 if (m_is_worker_started) return;
40 locker.unlock();
41
42 m_future = std::async(std::launch::async,
43 [this] {
44 while (!m_shutdown) {
45 std::unique_lock<std::mutex> locker(m_notify_mutex);
46 m_notify_condition.wait(locker, [this](){
47 return m_notify;
48 });
49 m_notify = false;
50 locker.unlock();
51
52 try {
53 m_io_context->run();
54 } catch (...) {}
55
56 if (m_shutdown) break;
57
58 try {
59 m_io_context->restart();
60 } catch (...) {}
61 }
62 m_work_guard.reset();
63 }).share();
64 }
65
67 void stop() {
68 std::lock_guard<std::mutex> locker(m_is_worker_started_mutex);
69 if (!m_future.valid()) return;
70 m_shutdown = true;
71 m_io_context->stop();
72 notify();
73 try {
74 m_future.wait();
75 m_future.get();
76 } catch(...) {};
77 }
78
79 private:
80# ifdef ASIO_STANDALONE
81 using work_guard_t = asio::executor_work_guard<asio::io_context::executor_type>;
82# else
83 using work_guard_t = boost::asio::executor_work_guard<boost::asio::io_context::executor_type>;
84# endif
85
86 using io_context_t = std::shared_ptr<SimpleWeb::io_context>;
89
91 bool m_is_worker_started = false;
92
93 std::mutex m_notify_mutex;
94 std::condition_variable m_notify_condition;
95 bool m_notify = false;
96
97 std::shared_future<void> m_future;
98 std::atomic<bool> m_shutdown = ATOMIC_VAR_INIT(false);
99
102 m_io_context(std::make_shared<SimpleWeb::io_context>()),
103# ifdef ASIO_STANDALONE
104 m_work_guard(asio::make_work_guard(*m_io_context)) {
105# else
106 m_work_guard(boost::asio::make_work_guard(*m_io_context)) {
107# endif
108 }
109
112 stop();
113 }
114
115 // Delete copy constructor and assignment operator to enforce singleton pattern
118
119 }; // SimpleWebSocketWorker
120
121}; // namespace kurlyk
122
123#endif // _KURLYK_SIMPLE_WEB_SOCKET_WORKER_HPP_INCLUDED
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > work_guard_t
SimpleWebSocketWorker & operator=(const SimpleWebSocketWorker &)=delete
void notify()
Notifies the worker to check for pending tasks.
void stop()
Stops the worker thread and waits for it to finish.
SimpleWebSocketWorker(const SimpleWebSocketWorker &)=delete
void start()
Starts the worker thread if it is not already running.
std::condition_variable m_notify_condition
Condition variable for notifying worker.
bool m_is_worker_started
Flag indicating if worker is started.
bool m_notify
Flag indicating if notification is pending.
static SimpleWebSocketWorker & get_instance()
Get the singleton instance of SimpleWebSocketWorker.
SimpleWebSocketWorker()
Private constructor to prevent instantiation.
std::shared_ptr< SimpleWeb::io_context > io_context_t
std::mutex m_notify_mutex
Mutex for notifying worker.
std::shared_future< void > m_future
std::shared_ptr< SimpleWeb::io_context > get_io_context()
Provides access to the I/O context for WebSocket operations.
std::mutex m_is_worker_started_mutex
Mutex for controlling worker start.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
Enables use of ClientError with std::error_code.