2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2007 Richard Kettlewell
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.
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.
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
24 #include <sys/socket.h>
26 typedef struct ev_source ev_source;
31 ev_source *ev_new(void);
32 /* create a new event loop */
34 int ev_run(ev_source *ev);
35 /* run an event loop. If any callback returns nonzero then that value
36 * is returned. If an error occurs then -1 is returned and @error@ is
39 /* file descriptors ***********************************************************/
49 typedef int ev_fd_callback(ev_source *ev, int fd, void *u);
50 /* signature for fd callback functions */
52 int ev_fd(ev_source *ev,
55 ev_fd_callback *callback,
58 /* register a callback on a file descriptor */
60 int ev_fd_cancel(ev_source *ev,
63 /* cancel a callback on a file descriptor */
65 int ev_fd_disable(ev_source *ev,
68 /* temporarily disable callbacks on a file descriptor */
70 int ev_fd_enable(ev_source *ev,
73 /* re-enable callbacks on a file descriptor */
75 void ev_report(ev_source *ev);
77 /* timeouts *******************************************************************/
79 typedef int ev_timeout_callback(ev_source *ev,
80 const struct timeval *now,
82 /* signature for timeout callback functions */
84 typedef void *ev_timeout_handle;
86 int ev_timeout(ev_source *ev,
87 ev_timeout_handle *handlep,
88 const struct timeval *when,
89 ev_timeout_callback *callback,
91 /* register a timeout callback. If @handlep@ is not a null pointer then a
92 * handle suitable for ev_timeout_cancel() below is returned through it. */
94 int ev_timeout_cancel(ev_source *ev,
95 ev_timeout_handle handle);
96 /* cancel a timeout callback */
98 /* signals ********************************************************************/
100 typedef int ev_signal_callback(ev_source *ev,
103 /* signature for signal callback functions */
105 int ev_signal(ev_source *ev,
107 ev_signal_callback *callback,
109 /* register a signal callback */
111 int ev_signal_cancel(ev_source *ev,
113 /* cancel a signal callback */
115 void ev_signal_atfork(ev_source *ev);
116 /* unhandle and unblock handled signals - call after calling fork and
117 * then setting @exitfn@ */
119 /* child processes ************************************************************/
121 typedef int ev_child_callback(ev_source *ev,
124 const struct rusage *rusage,
126 /* signature for child wait callbacks */
128 int ev_child_setup(ev_source *ev);
129 /* must be called exactly once before @ev_child@ */
131 int ev_child(ev_source *ev,
134 ev_child_callback *callback,
136 /* register a child callback. @options@ must be 0 or WUNTRACED. */
138 int ev_child_cancel(ev_source *ev,
140 /* cancel a child callback. */
142 /* socket listeners ***********************************************************/
144 typedef int ev_listen_callback(ev_source *ev,
146 const struct sockaddr *remote,
149 /* callback when a connection arrives. */
151 int ev_listen(ev_source *ev,
153 ev_listen_callback *callback,
156 /* register a socket listener callback. @bind@ and @listen@ should
157 * already have been called. */
159 int ev_listen_cancel(ev_source *ev,
161 /* cancel a socket listener callback */
163 /* buffered writer ************************************************************/
165 typedef struct ev_writer ev_writer;
167 /** @brief Error callback for @ref ev_reader and @ref ev_writer
168 * @param ev Event loop
169 * @param errno_value Errno value (might be 0)
170 * @param u As passed to ev_writer_new() or ev_reader_new()
171 * @return 0 on success, non-0 on error
173 * This is called for a writer in the following situations:
174 * - on error, with @p errno_value != 0
175 * - when all buffered data has been written, with @p errno_value = 0
176 * - after called ev_writer_cancel(), with @p errno_value = 0
178 * It is called for a reader only on error, with @p errno_value != 0.
180 typedef int ev_error_callback(ev_source *ev,
184 ev_writer *ev_writer_new(ev_source *ev,
186 ev_error_callback *callback,
189 /* create a new buffered writer, writing to @fd@. Calls @error@ if an
192 int ev_writer_time_bound(ev_writer *ev,
194 int ev_writer_space_bound(ev_writer *ev,
195 int new_space_bound);
197 int ev_writer_close(ev_writer *w);
198 /* close a writer (i.e. promise not to write to it any more) */
200 int ev_writer_cancel(ev_writer *w);
201 /* cancel a writer */
203 int ev_writer_flush(ev_writer *w);
204 /* attempt to flush the buffer */
206 struct sink *ev_writer_sink(ev_writer *w) attribute((const));
207 /* return a sink for the writer - use this to actually write to it */
209 /* buffered reader ************************************************************/
211 typedef struct ev_reader ev_reader;
213 /** @brief Called when data is available to read
214 * @param ev Event loop
215 * @param reader Reader
216 * @param fd File descriptor we read from
217 * @param ptr Pointer to first byte
218 * @param bytes Number of bytes available
219 * @param eof True if EOF has been detected
220 * @param u As passed to ev_reader_new()
221 * @return 0 on succes, non-0 on error
223 * This callback should call ev_reader_consume() to indicate how many bytes you
224 * actually used. If you do not call it then it is assumed no bytes were
227 * If having consumed some number of bytes it is not possible to do any further
228 * processing until more data is available then the callback can just return.
229 * Note that this is not allowed if @p eof was set.
231 * If on the other hand it would be possible to do more processing immediately
232 * with the bytes available, but this is undesirable for some other reason,
233 * then ev_reader_incomplete() should be called. This will arrange a further
234 * callback in the very near future even if no more bytes are read.
236 typedef int ev_reader_callback(ev_source *ev,
243 ev_reader *ev_reader_new(ev_source *ev,
245 ev_reader_callback *callback,
246 ev_error_callback *error_callback,
249 /* register a new reader. @callback@ will be called whenever data is
252 void ev_reader_buffer(ev_reader *r, size_t nbytes);
253 /* specify a buffer size */
255 void ev_reader_consume(ev_reader *r
257 /* consume @nbytes@ bytes. */
259 int ev_reader_cancel(ev_reader *r);
260 /* cancel a reader */
262 int ev_reader_disable(ev_reader *r);
263 /* disable reading */
265 int ev_reader_incomplete(ev_reader *r);
266 /* callback didn't fully process buffer, but would like another
267 * callback (used where processing more would block too long) */
269 int ev_reader_enable(ev_reader *r);
270 /* enable reading. If there is unconsumed data then you get a
271 * callback next time round the event loop even if nothing new has
274 * The idea is in your read callback you come across a line (or
275 * whatever) that can't be processed immediately. So you set up
276 * processing and disable reading. Later when you finish processing
277 * you re-enable. You'll automatically get another callback pretty
278 * much direct from the event loop (not from inside ev_reader_enable)
279 * so you can handle the next line (or whatever) if the whole thing
280 * has in fact already arrived.
283 int ev_tie(ev_reader *r, ev_writer *w);