qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::io::async::event::signal< _SIG > Struct Template Reference

Event for handling system signals asynchronously. More...

#include <signal.h>

Inheritance diagram for qb::io::async::event::signal< _SIG >:
Collaboration diagram for qb::io::async::event::signal< _SIG >:

Public Types

using base_t = base<ev::sig>
 Base type alias for base<ev::sig>.
Public Types inherited from qb::io::async::event::base< ev::sig >
using ev_t
 Alias for the underlying libev event watcher type.

Public Member Functions

 signal (ev::loop_ref loop)
 Constructor.
Public Member Functions inherited from qb::io::async::event::base< ev::sig >
 base (ev::loop_ref loop)
 Constructor.

Additional Inherited Members

Public Attributes inherited from qb::io::async::event::base< ev::sig >
IRegisteredKernelEvent_interface
 Pointer to the kernel event interface responsible for handling this event.
int _revents
 Stores the event flags (e.g., EV_READ, EV_WRITE) received from libev when the event triggers.

Detailed Description

template<int _SIG = -1>
struct qb::io::async::event::signal< _SIG >

Event for handling system signals asynchronously.

This template class extends qb::io::async::event::base<ev::sig> and thus wraps an ev::sig watcher from libev. It is used to watch for specific system signals (e.g., SIGINT, SIGTERM) and trigger callbacks when they occur. The signal to watch is typically specified as a template parameter.

Template Parameters
_SIGThe signal number to watch (e.g., SIGINT, SIGTERM). If -1 (the default for the specialized version), the signal number must be set dynamically using the set() method of the underlying ev::sig watcher.

Usage Example:

#include <csignal> // For SIGINT
// Define a handler for SIGINT
class InterruptHandler : public qb::io::async::listener::IRegisteredKernelEvent { // Or an actor, etc.
public:
qb::io::async::event::signal<SIGINT> sigint_watcher;
InterruptHandler(ev::loop_ref loop) : sigint_watcher(loop) {
// Register this handler with the listener for the sigint_watcher
// In a real scenario, this would be done via listener::current.registerEvent(*this, ... for the watcher)
// For simplicity, assume registration happens elsewhere or use a base class like qb::io::async::io
sigint_watcher.set<&InterruptHandler::on_signal_event_cb>(this); // Simplified libev callback setup
sigint_watcher.start();
}
// This is a simplified libev-style callback, not the qb::io::async::io on() signature
void on_signal_event_cb(ev::sig &watcher, int revents) {
if (watcher.signum == SIGINT) {
LOG_INFO("SIGINT received, shutting down gracefully...");
// application_is_running = false; // Signal main loop to exit
watcher.loop.break_loop(ev::ALL); // Stop the event loop
}
}
// If using qb::io::async::io or similar base, you'd implement:
// void on(qb::io::async::event::signal<SIGINT>& event) {
// LOG_INFO("SIGINT received via qb event system, signum: " << event.signum);
// // application_is_running = false;
// event.loop.break_loop(ev::ALL);
// }
};
#define LOG_INFO(X)
Info-level log macro (no-op if QB_STDOUT_LOG is not defined)
Definition io.h:229

Constructor & Destructor Documentation

◆ signal()

template<int _SIG = -1>
qb::io::async::event::signal< _SIG >::signal ( ev::loop_ref loop)
inlineexplicit

Constructor.

Creates a signal watcher for the specified signal number (if provided as template argument).

Parameters
loopReference to the libev event loop (ev::loop_ref) this watcher will be associated with.