Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
e8c92ba7 | 3 | * Copyright (C) 2004, 2007 Richard Kettlewell |
460b9539 | 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, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | ||
21 | #ifndef EVENT_H | |
22 | #define EVENT_H | |
23 | ||
24 | typedef struct ev_source ev_source; | |
25 | ||
26 | struct rusage; | |
27 | struct sink; | |
28 | ||
29 | ev_source *ev_new(void); | |
30 | /* create a new event loop */ | |
31 | ||
32 | int ev_run(ev_source *ev); | |
33 | /* run an event loop. If any callback returns nonzero then that value | |
34 | * is returned. If an error occurs then -1 is returned and @error@ is | |
35 | * called. */ | |
36 | ||
37 | /* file descriptors ***********************************************************/ | |
38 | ||
39 | typedef enum { | |
40 | ev_read, | |
41 | ev_write, | |
42 | ev_except, | |
43 | ||
44 | ev_nmodes | |
45 | } ev_fdmode; | |
46 | ||
47 | typedef int ev_fd_callback(ev_source *ev, int fd, void *u); | |
48 | /* signature for fd callback functions */ | |
49 | ||
50 | int ev_fd(ev_source *ev, | |
51 | ev_fdmode mode, | |
52 | int fd, | |
53 | ev_fd_callback *callback, | |
e8c92ba7 RK |
54 | void *u, |
55 | const char *what); | |
460b9539 | 56 | /* register a callback on a file descriptor */ |
57 | ||
58 | int ev_fd_cancel(ev_source *ev, | |
59 | ev_fdmode mode, | |
60 | int fd); | |
61 | /* cancel a callback on a file descriptor */ | |
62 | ||
63 | int ev_fd_disable(ev_source *ev, | |
64 | ev_fdmode mode, | |
65 | int fd); | |
66 | /* temporarily disable callbacks on a file descriptor */ | |
67 | ||
68 | int ev_fd_enable(ev_source *ev, | |
69 | ev_fdmode mode, | |
70 | int fd); | |
71 | /* re-enable callbacks on a file descriptor */ | |
72 | ||
73 | /* timeouts *******************************************************************/ | |
74 | ||
75 | typedef int ev_timeout_callback(ev_source *ev, | |
76 | const struct timeval *now, | |
77 | void *u); | |
78 | /* signature for timeout callback functions */ | |
79 | ||
80 | typedef void *ev_timeout_handle; | |
81 | ||
82 | int ev_timeout(ev_source *ev, | |
83 | ev_timeout_handle *handlep, | |
84 | const struct timeval *when, | |
85 | ev_timeout_callback *callback, | |
86 | void *u); | |
87 | /* register a timeout callback. If @handlep@ is not a null pointer then a | |
88 | * handle suitable for ev_timeout_cancel() below is returned through it. */ | |
89 | ||
90 | int ev_timeout_cancel(ev_source *ev, | |
91 | ev_timeout_handle handle); | |
92 | /* cancel a timeout callback */ | |
93 | ||
94 | /* signals ********************************************************************/ | |
95 | ||
96 | typedef int ev_signal_callback(ev_source *ev, | |
97 | int sig, | |
98 | void *u); | |
99 | /* signature for signal callback functions */ | |
100 | ||
101 | int ev_signal(ev_source *ev, | |
102 | int sig, | |
103 | ev_signal_callback *callback, | |
104 | void *u); | |
105 | /* register a signal callback */ | |
106 | ||
107 | int ev_signal_cancel(ev_source *ev, | |
108 | int sig); | |
109 | /* cancel a signal callback */ | |
110 | ||
111 | void ev_signal_atfork(ev_source *ev); | |
112 | /* unhandle and unblock handled signals - call after calling fork and | |
113 | * then setting @exitfn@ */ | |
114 | ||
115 | /* child processes ************************************************************/ | |
116 | ||
117 | typedef int ev_child_callback(ev_source *ev, | |
118 | pid_t pid, | |
119 | int status, | |
120 | const struct rusage *rusage, | |
121 | void *u); | |
122 | /* signature for child wait callbacks */ | |
123 | ||
124 | int ev_child_setup(ev_source *ev); | |
125 | /* must be called exactly once before @ev_child@ */ | |
126 | ||
127 | int ev_child(ev_source *ev, | |
128 | pid_t pid, | |
129 | int options, | |
130 | ev_child_callback *callback, | |
131 | void *u); | |
132 | /* register a child callback. @options@ must be 0 or WUNTRACED. */ | |
133 | ||
134 | int ev_child_cancel(ev_source *ev, | |
135 | pid_t pid); | |
136 | /* cancel a child callback. */ | |
137 | ||
138 | /* socket listeners ***********************************************************/ | |
139 | ||
140 | typedef int ev_listen_callback(ev_source *ev, | |
141 | int newfd, | |
142 | const struct sockaddr *remote, | |
143 | socklen_t rlen, | |
144 | void *u); | |
145 | /* callback when a connection arrives. */ | |
146 | ||
147 | int ev_listen(ev_source *ev, | |
148 | int fd, | |
149 | ev_listen_callback *callback, | |
e8c92ba7 RK |
150 | void *u, |
151 | const char *what); | |
460b9539 | 152 | /* register a socket listener callback. @bind@ and @listen@ should |
153 | * already have been called. */ | |
154 | ||
155 | int ev_listen_cancel(ev_source *ev, | |
156 | int fd); | |
157 | /* cancel a socket listener callback */ | |
158 | ||
159 | /* buffered writer ************************************************************/ | |
160 | ||
161 | typedef struct ev_writer ev_writer; | |
162 | ||
163 | typedef int ev_error_callback(ev_source *ev, | |
164 | int fd, | |
165 | int errno_value, | |
166 | void *u); | |
167 | /* called when an error occurs on a writer. Called with @errno_value@ | |
168 | * of 0 when finished. */ | |
169 | ||
170 | ev_writer *ev_writer_new(ev_source *ev, | |
171 | int fd, | |
172 | ev_error_callback *callback, | |
e8c92ba7 RK |
173 | void *u, |
174 | const char *what); | |
460b9539 | 175 | /* create a new buffered writer, writing to @fd@. Calls @error@ if an |
176 | * error occurs. */ | |
177 | ||
178 | int ev_writer_close(ev_writer *w); | |
179 | /* close a writer (i.e. promise not to write to it any more) */ | |
180 | ||
181 | int ev_writer_cancel(ev_writer *w); | |
182 | /* cancel a writer */ | |
183 | ||
184 | int ev_writer_flush(ev_writer *w); | |
185 | /* attempt to flush the buffer */ | |
186 | ||
187 | struct sink *ev_writer_sink(ev_writer *w) attribute((const)); | |
188 | /* return a sink for the writer - use this to actually write to it */ | |
189 | ||
190 | /* buffered reader ************************************************************/ | |
191 | ||
192 | typedef struct ev_reader ev_reader; | |
193 | ||
194 | typedef int ev_reader_callback(ev_source *ev, | |
195 | ev_reader *reader, | |
196 | int fd, | |
197 | void *ptr, | |
198 | size_t bytes, | |
199 | int eof, | |
200 | void *u); | |
201 | /* Called when data is read or an error occurs. @ptr@ and @bytes@ | |
202 | * indicate the amount of data available. @eof@ will be 1 at eof. */ | |
203 | ||
204 | ev_reader *ev_reader_new(ev_source *ev, | |
205 | int fd, | |
206 | ev_reader_callback *callback, | |
207 | ev_error_callback *error_callback, | |
e8c92ba7 RK |
208 | void *u, |
209 | const char *what); | |
460b9539 | 210 | /* register a new reader. @callback@ will be called whenever data is |
211 | * available. */ | |
212 | ||
213 | void ev_reader_buffer(ev_reader *r, size_t nbytes); | |
214 | /* specify a buffer size *case */ | |
215 | ||
216 | void ev_reader_consume(ev_reader *r, size_t nbytes); | |
217 | /* consume @nbytes@ bytes. */ | |
218 | ||
219 | int ev_reader_cancel(ev_reader *r); | |
220 | /* cancel a reader */ | |
221 | ||
222 | int ev_reader_disable(ev_reader *r); | |
223 | /* disable reading */ | |
224 | ||
225 | int ev_reader_incomplete(ev_reader *r); | |
226 | /* callback didn't fully process buffer, but would like another | |
227 | * callback (used where processing more would block too long) */ | |
228 | ||
229 | int ev_reader_enable(ev_reader *r); | |
230 | /* enable reading. If there is unconsumed data then you get a | |
231 | * callback next time round the event loop even if nothing new has | |
232 | * been read. | |
233 | * | |
234 | * The idea is in your read callback you come across a line (or | |
235 | * whatever) that can't be processed immediately. So you set up | |
236 | * processing and disable reading. Later when you finish processing | |
237 | * you re-enable. You'll automatically get another callback pretty | |
238 | * much direct from the event loop (not from inside ev_reader_enable) | |
239 | * so you can handle the next line (or whatever) if the whole thing | |
240 | * has in fact already arrived. | |
241 | */ | |
242 | ||
243 | #endif /* EVENT_H */ | |
244 | ||
245 | /* | |
246 | Local Variables: | |
247 | c-basic-offset:2 | |
248 | comment-column:40 | |
249 | fill-column:79 | |
250 | End: | |
251 | */ |