Kurlyk
Loading...
Searching...
No Matches
kurlyk::core::NetworkWorker Class Reference

Singleton worker that manages asynchronous network operations, including HTTP requests and WebSocket events. More...

#include <NetworkWorker.hpp>

Public Types

using ErrorHandler = std::function<void(const std::exception&, const char*, const char*, int, const char*)>
 

Public Member Functions

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.
 

Static Public Member Functions

static NetworkWorkerget_instance ()
 Get the singleton instance of NetworkWorker.
 

Private Member Functions

 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.
 
NetworkWorkeroperator= (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.
 

Private Attributes

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::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.
 
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< ErrorHandlerm_error_handlers
 Collection of registered error handlers.
 

Detailed Description

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.

Member Typedef Documentation

◆ ErrorHandler

using kurlyk::core::NetworkWorker::ErrorHandler = std::function<void(const std::exception&, const char*, const char*, int, const char*)>

Definition at line 20 of file NetworkWorker.hpp.

Constructor & Destructor Documentation

◆ NetworkWorker() [1/2]

kurlyk::core::NetworkWorker::NetworkWorker ( )
inlineprivate

Private constructor to enforce singleton pattern.

Definition at line 221 of file NetworkWorker.hpp.

◆ ~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.

Member Function Documentation

◆ add_error_handler()

void kurlyk::core::NetworkWorker::add_error_handler ( ErrorHandler handler)
inline

Registers a callback for handling network errors.

Parameters
handlerFunction 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
taskA 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

Get the singleton instance of NetworkWorker.

Returns
Reference to the singleton instance.

Definition at line 24 of file NetworkWorker.hpp.

◆ 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
eThe thrown exception.
msgAdditional context message.
fileSource file where the error occurred.
lineLine number where the error occurred.
funcFunction 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
eptrThe captured exception.
msgCustom message for context.
fileFile where the error occurred.
lineLine number where the error occurred.
funcFunction 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=()

NetworkWorker & kurlyk::core::NetworkWorker::operator= ( const NetworkWorker & )
privatedelete

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()

void kurlyk::core::NetworkWorker::register_manager ( INetworkTaskManager * manager)
inline

Registers a network task manager to be managed by the NetworkWorker.

Parameters
managerPointer to a manager implementing INetworkTaskManager. Must remain valid during its lifetime.

Definition at line 99 of file NetworkWorker.hpp.

◆ 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_asyncIndicates 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.

Member Data Documentation

◆ m_error_handlers

std::vector<ErrorHandler> kurlyk::core::NetworkWorker::m_error_handlers
private

Collection of registered error handlers.

Definition at line 217 of file NetworkWorker.hpp.

◆ m_error_handlers_mutex

std::mutex kurlyk::core::NetworkWorker::m_error_handlers_mutex
private

Mutex guarding the error handler list.

Definition at line 216 of file NetworkWorker.hpp.

◆ 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

Flag indicating if the worker thread is started.

Definition at line 211 of file NetworkWorker.hpp.

◆ m_is_worker_started_mutex

std::mutex kurlyk::core::NetworkWorker::m_is_worker_started_mutex
private

Mutex to control worker thread initialization.

Definition at line 210 of file NetworkWorker.hpp.

◆ m_managers

std::vector<INetworkTaskManager*> kurlyk::core::NetworkWorker::m_managers
private

List of registered network task managers.

Definition at line 215 of file NetworkWorker.hpp.

◆ m_managers_mutex

std::mutex kurlyk::core::NetworkWorker::m_managers_mutex
mutableprivate

Mutex protecting access to registered managers.

Definition at line 214 of file NetworkWorker.hpp.

◆ 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

Condition variable for notifying the worker.

Definition at line 208 of file NetworkWorker.hpp.

◆ m_notify_mutex

std::mutex kurlyk::core::NetworkWorker::m_notify_mutex
private

Mutex for managing worker notifications.

Definition at line 207 of file NetworkWorker.hpp.

◆ m_shutdown

std::atomic<bool> kurlyk::core::NetworkWorker::m_shutdown = ATOMIC_VAR_INIT(false)
private

Flag indicating if shutdown has been requested.

Definition at line 206 of file NetworkWorker.hpp.

◆ 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

Mutex for protecting access to the task list.

Definition at line 212 of file NetworkWorker.hpp.


The documentation for this class was generated from the following file: