5 /// \brief Inline I/O event watcher classes
6 /// Copying: http://en.wikipedia.org/wiki/Copyright-free
7 /// Intended for public-domain wherever possible:
8 /// http://en.wikipedia.org/wiki/Public_domain
10 /// \author Chris Brody mailto:chris.brody@gmail.com
12 /// May be used with http://libev.schmorp.de/ or
13 /// http://monkey.org/~provos/libevent/
15 #include <sys/types.h>
18 /// I/O event callback type
19 /// @see http://monkey.org/~provos/libevent/
20 typedef void cbtype (int, short, void *);
22 /// Macro - reference to an I/O event loop
23 #define IOLOOP event_base *
25 /// Construct new I/O event loop
29 return (IOLOOP)::event_init();
32 /// Dispatch incoming I/O events & timers
33 /// (event loop with no special flags)
34 /// @param l Reference to I/O event loop
36 ioloop_dispatch(IOLOOP l)
38 ::event_base_loop(l, 0);
41 /// Event loop - dispatch incoming I/O events & timers
42 /// @param l Reference to I/O event loop
43 /// @param flags Desired EV flags for event loop
45 ioloop_loop(IOLOOP l, int flags)
47 ::event_base_loop(l, flags);
50 /// Base I/O watcher class
51 struct iowatcher : public event
55 ::event_base_set(l, this);
58 void set (int s, short ev)
60 ::event_set(this, s, (ev & (EV_READ | EV_WRITE)) ? (ev | EV_PERSIST) : ev, ev_callback, ev_arg);
65 ::event_add(this, NULL);
74 iowatcher(int s, short ev, cbtype cb, void * v = 0)
76 ::event_set(this, s, (ev & (EV_READ | EV_WRITE)) ? (ev | EV_PERSIST) : ev, cb, v);
79 iowatcher(cbtype cb, void * v = 0)
81 ::event_set(this, -1, 0, cb, v);
89 struct ioevent : public iowatcher
91 ioevent(int s, short ev, cbtype cb, void * v = 0) : iowatcher(s, ev, cb, v) { }
93 ioevent(cbtype cb, void * v = 0) : iowatcher(cb, v) { }
100 struct itimer : public iowatcher
102 itimer(cbtype cb, void * v = 0) : iowatcher(cb, v) { }
111 ::event_add(this, &tv);