]> 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 struct meshlink_handle;
32
33 typedef void (*io_cb_t)(event_loop_t *loop, void *data, int flags);
34 typedef void (*timeout_cb_t)(event_loop_t *loop, void *data);
35 typedef void (*signal_cb_t)(event_loop_t *loop, void *data);
36 typedef struct timespec(*idle_cb_t)(event_loop_t *loop, void *data);
37
38 typedef struct io_t {
39         struct splay_node_t node;
40         int fd;
41         int flags;
42         io_cb_t cb;
43         void *data;
44 } io_t;
45
46 typedef struct timeout_t {
47         struct splay_node_t node;
48         struct timespec tv;
49         timeout_cb_t cb;
50         void *data;
51 } timeout_t;
52
53 typedef struct signal_t {
54         struct splay_node_t node;
55         int signum;
56 #ifdef HAVE_STDATOMIC_H
57         volatile atomic_flag set;
58 #endif
59         signal_cb_t cb;
60         void *data;
61 } signal_t;
62
63 struct event_loop_t {
64         void *data;
65
66         volatile bool running;
67         bool deletion;
68
69         struct timespec now;
70
71         splay_tree_t timeouts;
72         idle_cb_t idle_cb;
73         void *idle_data;
74         splay_tree_t ios;
75         splay_tree_t signals;
76
77         fd_set readfds;
78         fd_set writefds;
79
80         io_t signalio;
81         int pipefd[2];
82 };
83
84 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags);
85 void io_del(event_loop_t *loop, io_t *io);
86 void io_set(event_loop_t *loop, io_t *io, int flags);
87
88 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv);
89 void timeout_del(event_loop_t *loop, timeout_t *timeout);
90 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv);
91
92 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum);
93 void signal_trigger(event_loop_t *loop, signal_t *sig);
94 void signal_del(event_loop_t *loop, signal_t *sig);
95
96 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data);
97
98 void event_loop_init(event_loop_t *loop);
99 void event_loop_exit(event_loop_t *loop);
100 bool event_loop_run(event_loop_t *loop, struct meshlink_handle *mesh) __attribute__((__warn_unused_result__));
101 void event_loop_flush_output(event_loop_t *loop);
102 void event_loop_start(event_loop_t *loop);
103 void event_loop_stop(event_loop_t *loop);
104
105 #endif