Wrapper functions for epoll to manage event-based communication. More...
#include "epollops.h"
#include "logging.h"
#include <inttypes.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
Go to the source code of this file.
Macros | |
#define | MAX_EPOLL_EVENTS 512 |
The maximum number of events that can be responded to in a single call to epoll_wait(). More... | |
Functions | |
int | enable_epoll (int epoll_fd, int new_fd, uint32_t events) |
Enables a file descriptor which has already been aded to an epoll instance. More... | |
int | add_to_epoll (int epoll_fd, int new_fd, uint32_t events, bool oneshot) |
Adds a file descriptor to an epoll instance. More... | |
static int | accept_new_connection (int socketfd, int epoll_fd, int oneshot) |
Accepts a new connection and adds it to the epoll instance. More... | |
int | epoll_loop (int socket_fd, int epoll_fd, int batch_size, int timeout, bool oneshot, int(*connection_handler)(int, void *), int(*accept_handler)(int, void *), void *data) |
The event-based loop for epoll_wait. More... | |
int | init_epoll (int socket_fd) |
Initializes a new instance of an epoll file descriptor and adds a socket to it, listening for input on that socket. More... | |
Wrapper functions for epoll to manage event-based communication.
Definition in file epollops.c.
#define MAX_EPOLL_EVENTS 512 |
The maximum number of events that can be responded to in a single call to epoll_wait().
NOT the maximum number of events that can be stored in the epoll.
Definition at line 40 of file epollops.c.
|
static |
Accepts a new connection and adds it to the epoll instance.
socketfd | The new file descriptor to accept and add to the epoll |
epoll_fd | The epoll file descriptor |
oneshot | Whether the new connection should have EPOLLONESHOT enabled by default |
If GET_NAME_INFO is defined, prints out the source of the connection
Definition at line 87 of file epollops.c.
int add_to_epoll | ( | int | epoll_fd, |
int | new_fd, | ||
uint32_t | events, | ||
bool | oneshot | ||
) |
Adds a file descriptor to an epoll instance.
epoll_fd | Epoll file descriptor |
new_fd | File descriptor to add to the epoll instance |
events | EPOLLIN &/| EPOLLOUT |
oneshot | Whether to enable EPOLLONESHOT on the added fd |
Definition at line 59 of file epollops.c.
int enable_epoll | ( | int | epoll_fd, |
int | new_fd, | ||
uint32_t | events | ||
) |
Enables a file descriptor which has already been aded to an epoll instance.
Or's EPOLLONESHOT with events so the event will only be responded to once
epoll_fd | Epoll file descriptor |
new_fd | File descriptor to enable in the epoll instance |
events | EPOLLIN &/| EPOLLOUT |
Definition at line 42 of file epollops.c.
int epoll_loop | ( | int | socket_fd, |
int | epoll_fd, | ||
int | batch_size, | ||
int | timeout, | ||
bool | oneshot, | ||
int(*)(int, void *) | connection_handler, | ||
int(*)(int, void *) | accept_handler, | ||
void * | data | ||
) |
The event-based loop for epoll_wait.
Loops (epolling), and accepts new connections when they are available on socket_fd. Calls the provided functions on socket activity (connection_handler), and on new connections (accept_handler). NOTE: On first connect, only calls connection_handler
on EPOLLIN activity.
socket_fd | Socket on which to accept new connections, or -1 if no accept is necessary |
epoll_fd | Fd of epoll instance |
batch_size | Number of connections to process in a row before exiting the loop or -1 for NA |
timeout | Epoll timeout. -1 for no timeout. |
connection_handler | Calls this function with args (int fd, void *data) on fd activity |
accept_handler | Calls this function with args (int fd, void *data) on new connection NULL for no callback or N/A |
data | Data to be passed through to handler functions |
Definition at line 132 of file epollops.c.
int init_epoll | ( | int | socket_fd | ) |
Initializes a new instance of an epoll file descriptor and adds a socket to it, listening for input on that socket.
socket_fd | Socket on which to listen for new connections. -1 if creating epoll without socket. |
Definition at line 182 of file epollops.c.