Simplify signal handling.
[tinc] / src / event.h
1 #ifndef TINC_EVENT_H
2 #define TINC_EVENT_H
3
4 /*
5     event.h -- I/O, timeout and signal event handling
6     Copyright (C) 2012-2013 Guus Sliepen <guus@tinc-vpn.org>
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 "system.h"
24 #include "splay_tree.h"
25
26 #define IO_READ 1
27 #define IO_WRITE 2
28
29 typedef void (*io_cb_t)(void *data, int flags);
30 typedef void (*timeout_cb_t)(void *data);
31 typedef void (*signal_cb_t)(void *data);
32
33 typedef struct io_t {
34         int fd;
35         int flags;
36 #ifdef HAVE_MINGW
37         WSAEVENT event;
38 #endif
39         io_cb_t cb;
40         void *data;
41         splay_node_t node;
42 } io_t;
43
44 typedef struct timeout_t {
45         struct timeval tv;
46         timeout_cb_t cb;
47         void *data;
48         splay_node_t node;
49 } timeout_t;
50
51 typedef struct signal_t {
52         int signum;
53         signal_cb_t cb;
54         void *data;
55 } signal_t;
56
57 extern struct timeval now;
58
59 extern void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags);
60 #ifdef HAVE_MINGW
61 extern void io_add_event(io_t *io, io_cb_t cb, void *data, WSAEVENT event);
62 #endif
63 extern void io_del(io_t *io);
64 extern void io_set(io_t *io, int flags);
65
66 extern void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv);
67 extern void timeout_del(timeout_t *timeout);
68 extern void timeout_set(timeout_t *timeout, struct timeval *tv);
69
70 extern void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum);
71 extern void signal_del(signal_t *sig);
72
73 extern bool event_loop(void);
74 extern void event_exit(void);
75
76 #endif