libamxrt  0.4.2
Ambiorix Run Time Library
amxrt_el_libevent.c File Reference
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <event2/event.h>
#include <amxrt/amxrt.h>
#include "amxrt_priv.h"

Go to the source code of this file.

Data Structures

struct  _el_data
 

Typedefs

typedef struct _el_data el_data_t
 

Functions

static void amxrt_el_free_event_source (amxc_llist_it_t *it)
 
static void amxrt_el_add_event_source (struct event *source, amxp_connection_t *con)
 
static void amxrt_el_signal_cb (UNUSED evutil_socket_t fd, UNUSED short event, UNUSED void *arg)
 
static void amxrt_el_signal_timers (UNUSED evutil_socket_t fd, UNUSED short event, UNUSED void *arg)
 
static void amxrt_el_connection_read_cb (UNUSED evutil_socket_t fd, UNUSED short flags, void *arg)
 
static void amxrt_el_connection_can_write_cb (UNUSED evutil_socket_t fd, UNUSED short flags, void *arg)
 
static void amxrt_el_amxp_signal_read_cb (UNUSED evutil_socket_t fd, UNUSED short flags, UNUSED void *arg)
 
static void amxrt_el_amxp_syssignal_read_cb (UNUSED evutil_socket_t fd, UNUSED short flags, UNUSED void *arg)
 
static void amxrt_el_slot_add_fd (UNUSED const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
 
static void amxrt_el_slot_wait_write_fd (UNUSED const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
 
static void amxrt_el_slot_remove_fd (UNUSED const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
 
static void amxrt_el_add_read_fd (amxc_llist_t *connections)
 
static void amxrt_el_remove_fd (amxc_llist_t *connections)
 
amxp_connection_t * amxrt_el_get_connection (amxc_llist_t *cons, int fd)
 
int amxrt_el_create (void)
 Creates and initializes all needed event loop components. More...
 
int amxrt_el_start (void)
 Starts the event loop. More...
 
int amxrt_el_stop (void)
 Stops the event loop. More...
 
int amxrt_el_destroy (void)
 Cleans-up the event loop components. More...
 

Variables

static struct event_base * base = NULL
 

Typedef Documentation

◆ el_data_t

typedef struct _el_data el_data_t

Function Documentation

◆ amxrt_el_add_event_source()

static void amxrt_el_add_event_source ( struct event *  source,
amxp_connection_t *  con 
)
static

Definition at line 86 of file amxrt_el_libevent.c.

86  {
87  amxrt_t* rt = amxrt_get();
88  el_data_t* el_data = (el_data_t*) calloc(1, sizeof(el_data_t));
89  el_data->read = source;
90  event_add(source, NULL);
91 
92  if(con != NULL) {
93  con->el_data = el_data;
94  }
95 
96  amxc_llist_append(&rt->event_sources, &el_data->it);
97 }
static amxrt_t rt
Definition: amxrt.c:74
PRIVATE amxrt_t * amxrt_get(void)
Definition: amxrt.c:297
amxc_llist_it_t it
struct event * read
amxc_llist_t event_sources
Definition: amxrt_priv.h:104

◆ amxrt_el_add_read_fd()

static void amxrt_el_add_read_fd ( amxc_llist_t *  connections)
static

Definition at line 219 of file amxrt_el_libevent.c.

219  {
220  struct event* source = NULL;
221 
222  amxc_llist_for_each(it, connections) {
223  amxp_connection_t* con = amxc_llist_it_get_data(it, amxp_connection_t, it);
224  source = event_new(base, con->fd, EV_READ | EV_PERSIST, amxrt_el_connection_read_cb, con);
225  amxrt_el_add_event_source(source, con);
226  }
227 }
static void amxrt_el_connection_read_cb(UNUSED evutil_socket_t fd, UNUSED short flags, void *arg)
static void amxrt_el_add_event_source(struct event *source, amxp_connection_t *con)
static struct event_base * base

◆ amxrt_el_amxp_signal_read_cb()

static void amxrt_el_amxp_signal_read_cb ( UNUSED evutil_socket_t  fd,
UNUSED short  flags,
UNUSED void *  arg 
)
static

Definition at line 134 of file amxrt_el_libevent.c.

136  {
137  amxp_signal_read();
138 }

◆ amxrt_el_amxp_syssignal_read_cb()

static void amxrt_el_amxp_syssignal_read_cb ( UNUSED evutil_socket_t  fd,
UNUSED short  flags,
UNUSED void *  arg 
)
static

Definition at line 140 of file amxrt_el_libevent.c.

142  {
143  amxp_syssig_read();
144 }

◆ amxrt_el_connection_can_write_cb()

static void amxrt_el_connection_can_write_cb ( UNUSED evutil_socket_t  fd,
UNUSED short  flags,
void *  arg 
)
static

Definition at line 119 of file amxrt_el_libevent.c.

121  {
122  amxp_connection_t* con = (amxp_connection_t*) arg;
123  el_data_t* el_data = (el_data_t*) con->el_data;
124  amxo_fd_cb_t cb = con->can_write;
125 
126  con->can_write = NULL;
127  event_del(el_data->can_write);
128  free(el_data->can_write);
129  el_data->can_write = NULL;
130 
131  cb(fd, con->priv);
132 }
struct event * can_write

◆ amxrt_el_connection_read_cb()

static void amxrt_el_connection_read_cb ( UNUSED evutil_socket_t  fd,
UNUSED short  flags,
void *  arg 
)
static

Definition at line 112 of file amxrt_el_libevent.c.

114  {
115  amxp_connection_t* con = (amxp_connection_t*) arg;
116  con->reader(fd, con->priv);
117 }

◆ amxrt_el_free_event_source()

static void amxrt_el_free_event_source ( amxc_llist_it_t *  it)
static

Definition at line 72 of file amxrt_el_libevent.c.

72  {
73  el_data_t* el_data = amxc_container_of(it, el_data_t, it);
74  if(el_data->read != NULL) {
75  event_del(el_data->read);
76  free(el_data->read);
77  }
78  if(el_data->can_write != NULL) {
79  event_del(el_data->can_write);
80  free(el_data->can_write);
81  }
82 
83  free(el_data);
84 }

◆ amxrt_el_get_connection()

amxp_connection_t* amxrt_el_get_connection ( amxc_llist_t *  cons,
int  fd 
)

Definition at line 240 of file amxrt_el_libevent.c.

240  {
241  amxp_connection_t* con = NULL;
242  amxc_llist_for_each(it, cons) {
243  con = amxc_llist_it_get_data(it, amxp_connection_t, it);
244  if(con->fd == fd) {
245  break;
246  }
247  con = NULL;
248  }
249 
250  return con;
251 }

◆ amxrt_el_remove_fd()

static void amxrt_el_remove_fd ( amxc_llist_t *  connections)
static

Definition at line 229 of file amxrt_el_libevent.c.

229  {
230  amxc_llist_for_each(it, connections) {
231  amxp_connection_t* con = amxc_llist_it_get_data(it, amxp_connection_t, it);
232  el_data_t* el_data = (el_data_t*) con->el_data;
233  if(el_data != NULL) {
234  amxc_llist_it_clean(&el_data->it, amxrt_el_free_event_source);
235  }
236  con->el_data = NULL;
237  }
238 }
static void amxrt_el_free_event_source(amxc_llist_it_t *it)

◆ amxrt_el_signal_cb()

static void amxrt_el_signal_cb ( UNUSED evutil_socket_t  fd,
UNUSED short  event,
UNUSED void *  arg 
)
static

Definition at line 99 of file amxrt_el_libevent.c.

101  {
102  amxrt_el_stop();
103 }
int amxrt_el_stop(void)
Stops the event loop.

◆ amxrt_el_signal_timers()

static void amxrt_el_signal_timers ( UNUSED evutil_socket_t  fd,
UNUSED short  event,
UNUSED void *  arg 
)
static

Definition at line 105 of file amxrt_el_libevent.c.

107  {
108  amxp_timers_calculate();
109  amxp_timers_check();
110 }

◆ amxrt_el_slot_add_fd()

static void amxrt_el_slot_add_fd ( UNUSED const char *const  sig_name,
const amxc_var_t *const  data,
UNUSED void *const  priv 
)
static

Definition at line 146 of file amxrt_el_libevent.c.

148  {
149  amxp_connection_t* con = NULL;
150  struct event* source = NULL;
151 
152  when_true(amxc_var_type_of(data) != AMXC_VAR_ID_FD, leave);
153 
154  con = amxp_connection_get(amxc_var_constcast(fd_t, data));
155  when_null(con, leave);
156 
157  source = event_new(base, con->fd, EV_READ | EV_PERSIST, amxrt_el_connection_read_cb, con);
158  amxrt_el_add_event_source(source, con);
159 
160 leave:
161  return;
162 }

◆ amxrt_el_slot_remove_fd()

static void amxrt_el_slot_remove_fd ( UNUSED const char *const  sig_name,
const amxc_var_t *const  data,
UNUSED void *const  priv 
)
static

Definition at line 192 of file amxrt_el_libevent.c.

194  {
195  amxc_llist_t* connections = amxp_connection_get_connections();
196  amxp_connection_t* con = NULL;
197 
198  when_true(amxc_var_type_of(data) != AMXC_VAR_ID_FD, leave);
199 
200  con = amxrt_el_get_connection(connections, amxc_var_constcast(fd_t, data));
201  if(con == NULL) {
202  connections = amxp_connection_get_listeners();
203  con = amxrt_el_get_connection(connections, amxc_var_constcast(fd_t, data));
204  }
205  when_null(con, leave);
206 
207  if(con->el_data != NULL) {
208  el_data_t* el_data = (el_data_t*) con->el_data;
209  if(el_data != NULL) {
210  amxc_llist_it_clean(&el_data->it, amxrt_el_free_event_source);
211  }
212  con->el_data = NULL;
213  }
214 
215 leave:
216  return;
217 }
amxp_connection_t * amxrt_el_get_connection(amxc_llist_t *cons, int fd)

◆ amxrt_el_slot_wait_write_fd()

static void amxrt_el_slot_wait_write_fd ( UNUSED const char *const  sig_name,
const amxc_var_t *const  data,
UNUSED void *const  priv 
)
static

Definition at line 164 of file amxrt_el_libevent.c.

166  {
167  amxp_connection_t* con = NULL;
168  el_data_t* el_data = NULL;
169 
170  when_true(amxc_var_type_of(data) != AMXC_VAR_ID_FD, leave);
171 
172  con = amxp_connection_get(amxc_var_constcast(fd_t, data));
173  when_null(con, leave);
174 
175  el_data = (el_data_t*) con->el_data;
176  when_null(el_data, leave);
177  if(el_data->can_write != NULL) {
178  event_del(el_data->can_write);
179  free(el_data->can_write);
180  }
181  el_data->can_write = event_new(base,
182  con->fd,
183  EV_WRITE,
185  con);
186  event_add(el_data->can_write, NULL);
187 
188 leave:
189  return;
190 }
static void amxrt_el_connection_can_write_cb(UNUSED evutil_socket_t fd, UNUSED short flags, void *arg)

Variable Documentation

◆ base

struct event_base* base = NULL
static

Definition at line 64 of file amxrt_el_libevent.c.