2#ifndef _KURLYK_NETWORK_WORKER_HPP_INCLUDED
3#define _KURLYK_NETWORK_WORKER_HPP_INCLUDED
8#define KURLYK_HANDLE_ERROR(e, msg) \
9 ::kurlyk::core::NetworkWorker::get_instance().handle_error((e), (msg), __FILE__, __LINE__, __FUNCTION__)
20 using ErrorHandler = std::function<void(
const std::exception&,
const char*,
const char*,
int,
const char*)>;
43 const std::exception& e,
54 for (
const auto& handler : handlers) {
56 handler(e, msg, file, line, func);
70 std::exception_ptr eptr,
79 std::rethrow_exception(eptr);
80 }
catch (
const std::exception& e) {
83 const std::runtime_error unknown(
"Unknown non-std::exception");
134 if (!use_async)
return;
246 for (
auto &item : tasks_list) {
266 if (m->is_loaded())
return true;
#define KURLYK_HANDLE_ERROR(e, msg)
Interface for modules managed by NetworkWorker (e.g., HTTP, WebSocket).
void stop()
Stops the worker thread, ensuring all tasks are completed.
void notify()
Notifies the worker to begin processing requests or tasks.
bool m_is_worker_started
Flag indicating if the worker thread is started.
const bool is_loaded() const
Checks if the NetworkWorker has pending tasks or active network events.
static NetworkWorker & get_instance()
Get the singleton instance of NetworkWorker.
void add_task(std::function< void()> task)
Adds a task to the queue and notifies the worker thread.
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.
std::atomic< bool > m_shutdown
Flag indicating if shutdown has been requested.
NetworkWorker(const NetworkWorker &)=delete
Deleted copy constructor to enforce the singleton pattern.
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 process()
Processes all queued tasks and active HTTP and WebSocket requests.
std::condition_variable m_notify_condition
Condition variable for notifying the worker.
NetworkWorker & operator=(const NetworkWorker &)=delete
Deleted copy assignment operator to enforce the singleton pattern.
std::shared_future< void > m_future
Future for managing asynchronous worker execution.
std::vector< INetworkTaskManager * > m_managers
List of registered network task managers.
void process_tasks()
Processes all tasks in the task list, then clears the list.
void start(const bool use_async)
Starts the worker thread for asynchronous task processing.
bool m_notify
Flag indicating whether a notification is pending.
std::list< std::function< void()> > m_tasks_list
List of tasks queued for processing by the worker.
const bool has_pending_tasks() const
Checks if there are any pending tasks in the task list.
void register_manager(INetworkTaskManager *manager)
Registers a network task manager to be managed by the NetworkWorker.
void add_error_handler(ErrorHandler handler)
Registers a callback for handling network errors.
std::mutex m_managers_mutex
Mutex protecting access to registered managers.
~NetworkWorker()
Private destructor, ensuring the worker thread stops on destruction.
void shutdown()
Shuts down the worker, clearing all active requests and pending tasks.
std::mutex m_tasks_list_mutex
Mutex for protecting access to the task list.
std::mutex m_is_worker_started_mutex
Mutex to control worker thread initialization.
NetworkWorker()
Private constructor to enforce singleton pattern.
std::mutex m_error_handlers_mutex
Mutex guarding the error handler list.
std::function< void(const std::exception &, const char *, const char *, int, const char *)> ErrorHandler
std::mutex m_notify_mutex
Mutex for managing worker notifications.
std::vector< ErrorHandler > m_error_handlers
Collection of registered error handlers.
bool is_valid_email_id(const std::string &str)
Validates an email address format.
std::string convert_user_agent_to_sec_ch_ua(const std::string &user_agent)
Converts a User-Agent string to a sec-ch-ua header value.