The event object provides a set of methods to handle event-driven sockets
in the stack. When Duda I/O is started, it creates a fixed number of worker threads
where each one is capable to receive a high number of incoming client connections, each
thread have a separated main event loop based in the Linux epoll(7) interface.
The methods presented here, allows to register your own file descriptors or sockets into the main event loop for the active worker in question. Once the file descriptor is registered, you are responsible for it deletion. You can define different callbacks for the event desired, when handling events and callbacks, the following modes are available:
DUDA_EVENT_READ: some data is available for a read operation on the socket.
DUDA_EVENT_WRITE: the socket is ready for write operations.
DUDA_EVENT_RW: the socket is ready for read or write operations.
DUDA_EVENT_SLEEP: the socket events are disable, socket in sleep mode.
DUDA_EVENT_WAKEUP: wake up a sleeping socket.
Besides the callbacks and handlers, this interface also support notifications. When a worker is created, Duda also creates a notification interface for that main loop event, internally this is done through the Linux pipe(2) system call. So if you create your own threads and wants to send some notification to the default workers, you can issue it using the method event->signal(). The signaling system only allow to distribute unsigned 64 bits values (uint64_t).
Register a new socket or file descriptor into the worker event loop and associate proper event handlers or callbacks.
int add(int sockfd, int init_mode, int behavior, int (*cb_on_read) (int, void *), int (*cb_on_write) (int, void *), int (*cb_on_error) (int, void *), int (*cb_on_close) (int, void *), int (*cb_on_timeout) (int, void *), void *data);Parameters
It creates a file descriptor that will be used to receive events emited by the event->signal() method. This call is useful to be used in customized threads that have their own polling loop that want to get notifications as core threads do.
int create_signal_fd();Parameters None Return: Upon successful completion it returns the file descriptor that works in read-only mode.
Delete a registered event handler from the worker events queue.
int delete(int sockfd);Parameters
Define a callback function inside the web service to be triggered for when a signal is emited through the signal() method. This is a function that must be called inside duda_main().
void duda_event_set_signal_callback(void (*func) (int, uint64_t));Parameters
When an event is registered through the add method, internally an event handler is created. It stores the references for socket and the callbacks for each type of event. This method allows to find a specific event_handler through the given socket file descriptor.
struct duda_event_handler *lookup(int socket);Parameters
For a given socket file descriptor, alter the event handler mode and behavior.
int mode(int sockfd, int mode, int behavior);Parameters
Send a notification signal to each worker thread. Upon receiving this signal on each worker, the defined callback through the function duda_event_set_callback() from duda_main(), will be triggered.
int signal(uint64_t val);Parameters