Kurlyk
Toggle main menu visibility
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
8
namespace
kurlyk
{
9
12
class
SimpleWebSocketWorker
{
13
public
:
14
17
static
SimpleWebSocketWorker
&
get_instance
() {
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
;
39
m_is_worker_started
=
true
;
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>;
87
io_context_t
m_io_context
;
88
work_guard_t
m_work_guard
;
89
90
std::mutex
m_is_worker_started_mutex
;
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
101
SimpleWebSocketWorker
() :
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
111
~SimpleWebSocketWorker
() {
112
stop
();
113
}
114
115
// Delete copy constructor and assignment operator to enforce singleton pattern
116
SimpleWebSocketWorker
(
const
SimpleWebSocketWorker
&) =
delete
;
117
SimpleWebSocketWorker
&
operator=
(
const
SimpleWebSocketWorker
&) =
delete
;
118
119
};
// SimpleWebSocketWorker
120
121
};
// namespace kurlyk
122
123
#endif
// _KURLYK_SIMPLE_WEB_SOCKET_WORKER_HPP_INCLUDED
kurlyk::SimpleWebSocketWorker::work_guard_t
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > work_guard_t
Definition
SimpleWebSocketWorker.hpp:83
kurlyk::SimpleWebSocketWorker::m_io_context
io_context_t m_io_context
Definition
SimpleWebSocketWorker.hpp:87
kurlyk::SimpleWebSocketWorker::~SimpleWebSocketWorker
~SimpleWebSocketWorker()
Private destructor.
Definition
SimpleWebSocketWorker.hpp:111
kurlyk::SimpleWebSocketWorker::operator=
SimpleWebSocketWorker & operator=(const SimpleWebSocketWorker &)=delete
kurlyk::SimpleWebSocketWorker::m_work_guard
work_guard_t m_work_guard
Definition
SimpleWebSocketWorker.hpp:88
kurlyk::SimpleWebSocketWorker::notify
void notify()
Notifies the worker to check for pending tasks.
Definition
SimpleWebSocketWorker.hpp:29
kurlyk::SimpleWebSocketWorker::stop
void stop()
Stops the worker thread and waits for it to finish.
Definition
SimpleWebSocketWorker.hpp:67
kurlyk::SimpleWebSocketWorker::SimpleWebSocketWorker
SimpleWebSocketWorker(const SimpleWebSocketWorker &)=delete
kurlyk::SimpleWebSocketWorker::start
void start()
Starts the worker thread if it is not already running.
Definition
SimpleWebSocketWorker.hpp:36
kurlyk::SimpleWebSocketWorker::m_notify_condition
std::condition_variable m_notify_condition
Condition variable for notifying worker.
Definition
SimpleWebSocketWorker.hpp:94
kurlyk::SimpleWebSocketWorker::m_is_worker_started
bool m_is_worker_started
Flag indicating if worker is started.
Definition
SimpleWebSocketWorker.hpp:91
kurlyk::SimpleWebSocketWorker::m_notify
bool m_notify
Flag indicating if notification is pending.
Definition
SimpleWebSocketWorker.hpp:95
kurlyk::SimpleWebSocketWorker::get_instance
static SimpleWebSocketWorker & get_instance()
Get the singleton instance of SimpleWebSocketWorker.
Definition
SimpleWebSocketWorker.hpp:17
kurlyk::SimpleWebSocketWorker::SimpleWebSocketWorker
SimpleWebSocketWorker()
Private constructor to prevent instantiation.
Definition
SimpleWebSocketWorker.hpp:101
kurlyk::SimpleWebSocketWorker::io_context_t
std::shared_ptr< SimpleWeb::io_context > io_context_t
Definition
SimpleWebSocketWorker.hpp:86
kurlyk::SimpleWebSocketWorker::m_notify_mutex
std::mutex m_notify_mutex
Mutex for notifying worker.
Definition
SimpleWebSocketWorker.hpp:93
kurlyk::SimpleWebSocketWorker::m_future
std::shared_future< void > m_future
Definition
SimpleWebSocketWorker.hpp:97
kurlyk::SimpleWebSocketWorker::get_io_context
std::shared_ptr< SimpleWeb::io_context > get_io_context()
Provides access to the I/O context for WebSocket operations.
Definition
SimpleWebSocketWorker.hpp:24
kurlyk::SimpleWebSocketWorker::m_shutdown
std::atomic< bool > m_shutdown
Definition
SimpleWebSocketWorker.hpp:98
kurlyk::SimpleWebSocketWorker::m_is_worker_started_mutex
std::mutex m_is_worker_started_mutex
Mutex for controlling worker start.
Definition
SimpleWebSocketWorker.hpp:90
SimpleWeb
Definition
SocketClient.hpp:30
kurlyk
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
std
Enables use of ClientError with std::error_code.
Definition
ClientErrorCategory.hpp:69
include
kurlyk
websocket
client
SimpleWeb
SimpleWebSocketWorker.hpp
Generated by
1.17.0