]> git.meshlink.io Git - meshlink/blob - src/event.h
Never automatically try to bind to ports >= 32768.
[meshlink] / src / event.h
1 #ifndef MESHLINK_EVENT_H
2 #define MESHLINK_EVENT_H
3
4 /*
5     event.h -- I/O, timeout and signal event handling
6     Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "splay_tree.h"
24 #include "system.h"
25 #include <pthread.h>
26
27 #define IO_READ 1
28 #define IO_WRITE 2
29
30 typedef struct event_loop_t event_loop_t;
31
32 typedef void (*io_cb_t)(event_loop_t *loop, void *data, int flags);
33 typedef void (*timeout_cb_t)(event_loop_t *loop, void *data);
34 typedef void (*signal_cb_t)(event_loop_t *loop, void *data);
35 typedef struct timespec(*idle_cb_t)(event_loop_t *loop, void *data);
36
37 typedef struct io_t {
38         struct splay_node_t node;
39         int fd;
40         int flags;
41         io_cb_t cb;
42         void *data;
43 } io_t;
44
45 typedef struct timeout_t {
46         struct splay_node_t node;
47         struct timespec tv;
48         timeout_cb_t cb;
49         void *data;
50 } timeout_t;
51
52 typedef struct signal_t {
53         struct splay_node_t node;
54         int signum;
55 #ifdef HAVE_STDATOMIC_H
56         volatile atomic_flag set;
57 #endif
58         signal_cb_t cb;
59         void *data;
60 } signal_t;
61
62 struct event_loop_t {
63         void *data;
64
65         volatile bool running;
66         bool deletion;
67
68         struct timespec now;
69
70         splay_tree_t timeouts;
71         idle_cb_t idle_cb;
72         void *idle_data;
73         splay_tree_t ios;
74         splay_tree_t signals;
75
76         fd_set readfds;
77         fd_set writefds;
78
79         io_t signalio;
80         int pipefd[2];
81 };
82
83 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags);
84 void io_del(event_loop_t *loop, io_t *io);
85 void io_set(event_loop_t *loop, io_t *io, int flags);
86
87 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv);
88 void timeout_del(event_loop_t *loop, timeout_t *timeout);
89 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv);
90
91 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum);
92 void signal_trigger(event_loop_t *loop, signal_t *sig);
93 void signal_del(event_loop_t *loop, signal_t *sig);
94
95 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data);
96
97 void event_loop_init(event_loop_t *loop);
98 void event_loop_exit(event_loop_t *loop);
99 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) __attribute__((__warn_unused_result__));
100 void event_loop_flush_output(event_loop_t *loop);
101 void event_loop_start(event_loop_t *loop);
102 void event_loop_stop(event_loop_t *loop);
103
104 #endif