]> git.meshlink.io Git - meshlink/blob - src/event.h
Replace __TINC with __MESHLINK in the include guards.
[meshlink] / src / event.h
1 /*
2     event.h -- I/O and timeout event handling
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef __MESHLINK_EVENT_H__
21 #define __MESHLINK_EVENT_H__
22
23 #include "splay_tree.h"
24
25 #define IO_READ 1
26 #define IO_WRITE 2
27
28 typedef struct event_loop_t event_loop_t;
29
30 typedef void (*io_cb_t)(event_loop_t *loop, void *data, int flags);
31 typedef void (*timeout_cb_t)(event_loop_t *loop, void *data);
32
33 typedef struct io_t {
34         int fd;
35         int flags;
36         io_cb_t cb;
37         void *data;
38         struct splay_node_t node;
39 } io_t;
40
41 typedef struct timeout_t {
42         struct timeval tv;
43         timeout_cb_t cb;
44         void *data;
45         struct splay_node_t node;
46 } timeout_t;
47
48 struct event_loop_t {
49         fd_set readfds;
50         fd_set writefds;
51
52         volatile bool running;
53         struct timeval now;
54         
55         splay_tree_t ios;
56         splay_tree_t timeouts;
57
58         void *data;
59 };
60
61 extern void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags);
62 extern void io_del(event_loop_t *loop, io_t *io);
63 extern void io_set(event_loop_t *loop, io_t *io, int flags);
64
65 extern void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv);
66 extern void timeout_del(event_loop_t *loop, timeout_t *timeout);
67 extern void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv);
68
69 extern void event_loop_init(event_loop_t *loop);
70 extern void event_loop_exit(event_loop_t *loop);
71 extern bool event_loop_run(event_loop_t *loop);
72 extern void event_loop_flush_output(event_loop_t *loop);
73 extern void event_loop_stop(event_loop_t *loop);
74
75 #endif