Singleton worker that manages asynchronous network operations, including HTTP requests and WebSocket events.
More...
#include <NetworkWorker.hpp>
|
| using | ErrorHandler = std::function<void(const std::exception&, const char*, const char*, int, const char*)> |
|
| void | add_error_handler (ErrorHandler handler) |
| | Registers a callback for handling network errors.
|
| void | handle_error (const std::exception &e, const char *msg, const char *file, int line, const char *func) |
| | Dispatches an exception to all registered error handlers.
|
| void | handle_error (std::exception_ptr eptr, const char *msg, const char *file, int line, const char *func) |
| | Handles an exception captured as exception_ptr.
|
| bool | is_worker_thread () const |
| | Returns true if the calling thread is the worker thread.
|
| void | add_task (std::function< void()> task) |
| | Adds a task to the queue and notifies the worker thread.
|
| void | register_manager (INetworkTaskManager *manager) |
| | Registers a network task manager to be managed by the NetworkWorker.
|
| void | process () |
| | Processes all queued tasks and active HTTP and WebSocket requests.
|
| void | notify () |
| | Notifies the worker to begin processing requests or tasks.
|
| void | start (const bool use_async) |
| | Starts the worker thread for asynchronous task processing.
|
| void | stop () |
| | Stops the worker thread, ensuring all tasks are completed.
|
| void | shutdown () |
| | Shuts down the worker, clearing all active requests and pending tasks.
|
|
| | NetworkWorker () |
| | Private constructor to enforce singleton pattern.
|
| | ~NetworkWorker () |
| | Private destructor, ensuring the worker thread stops on destruction.
|
| | NetworkWorker (const NetworkWorker &)=delete |
| | Deleted copy constructor to enforce the singleton pattern.
|
| NetworkWorker & | operator= (const NetworkWorker &)=delete |
| | Deleted copy assignment operator to enforce the singleton pattern.
|
| void | process_tasks () |
| | Processes all tasks in the task list, then clears the list.
|
| const bool | has_pending_tasks () const |
| | Checks if there are any pending tasks in the task list.
|
| const bool | is_loaded () const |
| | Checks if the NetworkWorker has pending tasks or active network events.
|
|
| std::shared_future< void > | m_future |
| | Future for managing asynchronous worker execution.
|
| std::atomic< bool > | m_shutdown = ATOMIC_VAR_INIT(false) |
| | Flag indicating if shutdown has been requested.
|
| std::thread::id | m_worker_thread_id |
| | ID of the async worker thread, if started.
|
| std::mutex | m_notify_mutex |
| | Mutex for managing worker notifications.
|
| std::condition_variable | m_notify_condition |
| | Condition variable for notifying the worker.
|
| bool | m_notify = false |
| | Flag indicating whether a notification is pending. Access is always guarded by m_notify_mutex; atomic is unnecessary because the mutex provides the happens-before relation.
|
| std::mutex | m_is_worker_started_mutex |
| | Mutex to control worker thread initialization.
|
| bool | m_is_worker_started = false |
| | Flag indicating if the worker thread is started.
|
| std::mutex | m_tasks_list_mutex |
| | Mutex for protecting access to the task list.
|
| std::list< std::function< void()> > | m_tasks_list |
| | List of tasks queued for processing by the worker.
|
| std::mutex | m_managers_mutex |
| | Mutex protecting access to registered managers.
|
| std::vector< INetworkTaskManager * > | m_managers |
| | List of registered network task managers.
|
| std::mutex | m_error_handlers_mutex |
| | Mutex guarding the error handler list.
|
| std::vector< ErrorHandler > | m_error_handlers |
| | Collection of registered error handlers.
|
Singleton worker that manages asynchronous network operations, including HTTP requests and WebSocket events.
The NetworkWorker is responsible for managing and processing network-related tasks in a separate thread. It supports adding tasks to a queue, notifying and starting the worker thread, and handling graceful shutdown.
Definition at line 20 of file NetworkWorker.hpp.
◆ ErrorHandler
◆ NetworkWorker() [1/2]
| kurlyk::core::NetworkWorker::NetworkWorker |
( |
| ) |
|
|
inlineprivate |
◆ ~NetworkWorker()
| kurlyk::core::NetworkWorker::~NetworkWorker |
( |
| ) |
|
|
inlineprivate |
Private destructor, ensuring the worker thread stops on destruction.
Definition at line 245 of file NetworkWorker.hpp.
◆ NetworkWorker() [2/2]
| kurlyk::core::NetworkWorker::NetworkWorker |
( |
const NetworkWorker & | | ) |
|
|
privatedelete |
Deleted copy constructor to enforce the singleton pattern.
◆ add_error_handler()
| void kurlyk::core::NetworkWorker::add_error_handler |
( |
ErrorHandler | handler | ) |
|
|
inline |
Registers a callback for handling network errors.
- Parameters
-
| handler | Function invoked when an error is dispatched. |
Definition at line 33 of file NetworkWorker.hpp.
◆ add_task()
| void kurlyk::core::NetworkWorker::add_task |
( |
std::function< void()> | task | ) |
|
|
inline |
Adds a task to the queue and notifies the worker thread.
- Parameters
-
| task | A function or lambda with no arguments to be executed by the worker. |
Definition at line 97 of file NetworkWorker.hpp.
◆ get_instance()
◆ handle_error() [1/2]
| void kurlyk::core::NetworkWorker::handle_error |
( |
const std::exception & | e, |
|
|
const char * | msg, |
|
|
const char * | file, |
|
|
int | line, |
|
|
const char * | func ) |
|
inline |
Dispatches an exception to all registered error handlers.
- Parameters
-
| e | The thrown exception. |
| msg | Additional context message. |
| file | Source file where the error occurred. |
| line | Line number where the error occurred. |
| func | Function name where the error occurred. |
Definition at line 44 of file NetworkWorker.hpp.
◆ handle_error() [2/2]
| void kurlyk::core::NetworkWorker::handle_error |
( |
std::exception_ptr | eptr, |
|
|
const char * | msg, |
|
|
const char * | file, |
|
|
int | line, |
|
|
const char * | func ) |
|
inline |
Handles an exception captured as exception_ptr.
- Parameters
-
| eptr | The captured exception. |
| msg | Custom message for context. |
| file | File where the error occurred. |
| line | Line number where the error occurred. |
| func | Function name where the error occurred. |
Definition at line 71 of file NetworkWorker.hpp.
◆ has_pending_tasks()
| const bool kurlyk::core::NetworkWorker::has_pending_tasks |
( |
| ) |
const |
|
inlineprivate |
Checks if there are any pending tasks in the task list.
- Returns
- True if there are pending tasks, otherwise false.
Definition at line 272 of file NetworkWorker.hpp.
◆ is_loaded()
| const bool kurlyk::core::NetworkWorker::is_loaded |
( |
| ) |
const |
|
inlineprivate |
Checks if the NetworkWorker has pending tasks or active network events.
Checks if there are any tasks or events in the HTTP or WebSocket manager or in the task list.
- Returns
- True if there are tasks or events to process; otherwise, false.
Definition at line 281 of file NetworkWorker.hpp.
◆ is_worker_thread()
| bool kurlyk::core::NetworkWorker::is_worker_thread |
( |
| ) |
const |
|
inline |
Returns true if the calling thread is the worker thread.
Definition at line 91 of file NetworkWorker.hpp.
◆ notify()
| void kurlyk::core::NetworkWorker::notify |
( |
| ) |
|
|
inline |
Notifies the worker to begin processing requests or tasks.
Signals the condition variable to wake up the worker thread if it is waiting, allowing tasks to be processed.
Definition at line 126 of file NetworkWorker.hpp.
◆ operator=()
Deleted copy assignment operator to enforce the singleton pattern.
◆ process()
| void kurlyk::core::NetworkWorker::process |
( |
| ) |
|
|
inline |
Processes all queued tasks and active HTTP and WebSocket requests.
Processes pending tasks in the task list and manages network requests in both HTTP and WebSocket managers.
Definition at line 116 of file NetworkWorker.hpp.
◆ process_tasks()
| void kurlyk::core::NetworkWorker::process_tasks |
( |
| ) |
|
|
inlineprivate |
Processes all tasks in the task list, then clears the list.
This method moves tasks from the task list to a local list, processes each one, and then clears the local list.
Definition at line 258 of file NetworkWorker.hpp.
◆ register_manager()
◆ shutdown()
| void kurlyk::core::NetworkWorker::shutdown |
( |
| ) |
|
|
inline |
Shuts down the worker, clearing all active requests and pending tasks.
Stops both HTTP and WebSocket managers and processes any remaining tasks in the queue.
Definition at line 214 of file NetworkWorker.hpp.
◆ start()
| void kurlyk::core::NetworkWorker::start |
( |
const bool | use_async | ) |
|
|
inline |
Starts the worker thread for asynchronous task processing.
If use_async is true, the worker runs in a separate thread, continually processing tasks and network events until stop() is called.
- Parameters
-
| use_async | Indicates whether the worker should run asynchronously. |
Definition at line 137 of file NetworkWorker.hpp.
◆ stop()
| void kurlyk::core::NetworkWorker::stop |
( |
| ) |
|
|
inline |
Stops the worker thread, ensuring all tasks are completed.
Signals the worker thread to stop and waits for it to complete all tasks before fully shutting down.
Definition at line 190 of file NetworkWorker.hpp.
◆ m_error_handlers
| std::vector<ErrorHandler> kurlyk::core::NetworkWorker::m_error_handlers |
|
private |
◆ m_error_handlers_mutex
| std::mutex kurlyk::core::NetworkWorker::m_error_handlers_mutex |
|
private |
◆ m_future
| std::shared_future<void> kurlyk::core::NetworkWorker::m_future |
|
private |
Future for managing asynchronous worker execution.
Definition at line 222 of file NetworkWorker.hpp.
◆ m_is_worker_started
| bool kurlyk::core::NetworkWorker::m_is_worker_started = false |
|
private |
◆ m_is_worker_started_mutex
| std::mutex kurlyk::core::NetworkWorker::m_is_worker_started_mutex |
|
private |
◆ m_managers
◆ m_managers_mutex
| std::mutex kurlyk::core::NetworkWorker::m_managers_mutex |
|
mutableprivate |
◆ m_notify
| bool kurlyk::core::NetworkWorker::m_notify = false |
|
private |
Flag indicating whether a notification is pending. Access is always guarded by m_notify_mutex; atomic is unnecessary because the mutex provides the happens-before relation.
Definition at line 227 of file NetworkWorker.hpp.
◆ m_notify_condition
| std::condition_variable kurlyk::core::NetworkWorker::m_notify_condition |
|
private |
◆ m_notify_mutex
| std::mutex kurlyk::core::NetworkWorker::m_notify_mutex |
|
private |
◆ m_shutdown
| std::atomic<bool> kurlyk::core::NetworkWorker::m_shutdown = ATOMIC_VAR_INIT(false) |
|
private |
◆ m_tasks_list
| std::list<std::function<void()> > kurlyk::core::NetworkWorker::m_tasks_list |
|
private |
List of tasks queued for processing by the worker.
Definition at line 231 of file NetworkWorker.hpp.
◆ m_tasks_list_mutex
| std::mutex kurlyk::core::NetworkWorker::m_tasks_list_mutex |
|
mutableprivate |
◆ m_worker_thread_id
| std::thread::id kurlyk::core::NetworkWorker::m_worker_thread_id |
|
private |
The documentation for this class was generated from the following file: