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.
|
|
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.
|
|
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 18 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 227 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 31 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 90 of file NetworkWorker.hpp.
◆ get_instance()
static NetworkWorker & kurlyk::core::NetworkWorker::get_instance |
( |
| ) |
|
|
inlinestatic |
◆ 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 42 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 69 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 254 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 263 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 119 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 109 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 240 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 197 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 130 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 182 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 205 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.
Definition at line 209 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 213 of file NetworkWorker.hpp.
◆ m_tasks_list_mutex
std::mutex kurlyk::core::NetworkWorker::m_tasks_list_mutex |
|
mutableprivate |
The documentation for this class was generated from the following file: