4 #include <sys/types.h> // timeval
5 #include <stdexcept> // std::exception, std::invalid_argument,
6 // std::runtime_error, std::bad_alloc
10 * @section Introduction
12 * The <a href="http://monkey.org/~provos/libevent/">libevent</a> API provides
13 * a mechanism to execute a callback function when a specific event occurs on
14 * a file descriptor or after a timeout has been reached. Furthermore, libevent
15 * also support callbacks due to signals or regular timeouts.
17 * libevent is meant to replace the event loop found in event driven network
18 * servers. An application just needs to call dispatcher::dispatch() and then
19 * add or remove events dynamically without having to change the event loop.
21 * Currently, libevent supports /dev/poll, kqueue(2), select(2), poll(2) and
22 * epoll(4). It also has experimental support for real-time signals. The
23 * internal event mechanism is completely independent of the exposed event API,
24 * and a simple update of libevent can provide new functionality without having
25 * to redesign the applications. As a result, Libevent allows for portable
26 * application development and provides the most scalable event notification
27 * mechanism available on an operating system. Libevent should compile on Linux,
28 * *BSD, Mac OS X, Solaris and Windows.
30 * This is a simple, direct, one-header inline C++ wrapper for libevent. Yes,
31 * it's just one header file, so if you want to use it you can just copy the
32 * file to your project and you are set.
34 * It's designed to be as close to use to libevent (without compromising modern
35 * C++ programming techniques) and efficiency (since all implementation is
36 * trivial and inline, theoretically, it imposes no overhead at all).
38 * Please, visit the <a href="http://www.llucax.com.ar/~luca/eventxx/">eventxx
39 * website</a> for the latest documentation.
41 * You can always get the <a
42 * href="http://www.llucax.com.ar/~luca/eventxx/releases/eventxx.tar.gz">current
43 * release</a> from the
44 * <a href="http://www.llucax.com.ar/~luca/eventxx/releases/">release
45 * directory</a> or grab the
46 * <a href="http://www.llucax.com.ar/~luca/repos/eventxx/">most up to date
47 * sources</a> from the <a href="http://www.darcs.net/">darcs</a> repository.
49 * You can also take a look the the <a
50 * href="http://auriga.wearlab.de/~alb/darcsweb/">darcsweb</a> interface to see
51 * the <a href="http://www.llucax.com.ar/~luca/repos/darcsweb/?r=eventxx">latest
52 * changes online</a> or subscribe to its
53 * <a href="http://www.llucax.com.ar/~luca/repos/darcsweb/?r=eventxx;a=rss">RSS
54 * feed</a> to follow the development.
59 * This wrapper was designed to be used just like libevent, but with C++ style
60 * syntax sugar (or poison, depends on your point of view ;) and goodies. The
61 * main difference to libevent is you always have to instance a
62 * eventxx::dispatcher to get an event loop. There is no implicit global event
63 * loop. This adds just an extra line of code for single threaded applications
64 * and makes things much more simple. See eventxx::dispatcher documentation for
67 * You can use use the same plain functions callbacks or the other kind of
68 * function objects (see @link events @endlink section for details on event
71 * eventxx uses @link exceptions @endlink to report errors. All functions has
72 * exception specifications, so it's easy to find out what to expect. See
73 * exceptions section for more detail.
75 * A timespec abstraction is provided in eventxx::time for convenient argument
76 * passing. Even more, it's a timespec itself, with some convenient methods for
77 * accessing the timespec attributes in a more C++ way. And even more, eventxx
78 * is such a direct mapping that all eventxx::event's are libevent event structs
79 * too, so theoretically you can pass a eventxx::event to libevent C functions
80 * without much trouble. eventxx::dispatcher is the only class that is not
81 * derived from libevent struct (event_base) because this struct it's not
82 * defined on the libevent header (just declared).
84 * Maybe you shouldn't know this implementation details to keep the abstraction,
85 * but this is a basic design goal of this wrapper so there is not much chance
86 * that this changes in the future (but use this knowledge with care, you are
98 * eventxx::dispatcher& d;
100 * handler(eventxx::dispatcher& d): d(d), i(0) {}
101 * void operator() (int signum, short event)
103 * if (i < 5) std::cout << "keep going...\n";
106 * std::cout << "done!\n";
112 * void sighandler(int signum, short event, void* data)
114 * int& i = *static_cast< int* >(data);
115 * std::cout << ++i << " interrupts, ";
120 * eventxx::dispatcher d;
122 * eventxx::csignal sigev(SIGINT, sighandler, &h.i);
123 * eventxx::signal< handler > e(SIGINT, h);
131 * You can see some more examples on the test directory of the distribution or
132 * on the examples related page.
137 * This library was not widely used yet, it lack some testing. Because templates
138 * are not even compiled when they are used, you can't be surprised if you catch
139 * a piece of code that didn't got even compiled yet because the lack of
140 * testing. The library has no support for buffered events yet either. It
141 * doesn't support the http stuff, and probably never will because that has
142 * nothing to do with event handling.
144 * If you notice this lib leaks memory, don't blame me, blame libevent :)
145 * libevent has a known bug on event_base_free() that makes it assert always, so
146 * event_base_free() it's unusable, unless you patch your libevent (for example,
148 * href="http://monkeymail.org/archives/libevent-users/2006-April/000141.html">patch</a>
149 * written by Mark D. Anderson and who knows why it's not still applied. If you
150 * do so, you can compile your programs with -DEVENT_BASE_FREE_FIX so
151 * event_base_free() gets called.
153 * That said, I think it's pretty usable anyways. If something is broken it
154 * would be really easy to fix it because is just a simple wrapper around
155 * libevent. So, please try it out, and if you have any problems,
156 * <a href="mailto:llucax+eventxx@gmail.com">drop me an
157 * e-mail</a> and and I'll fix it ASAP (or provide a patch and you will be my
160 * Patches to support buffered events are welcome too.
163 * @author Leandro Lucarella <llucax+eventxx@gmail.com>
168 * This program is under the BOLA license (see
169 * http://auriga.wearlab.de/~alb/bola/ for more info or the
170 * <a href="http://www.llucax.com.ar/~luca/repos/eventxx/LICENSE">LICENSE</a>
175 /** @example c-way.cpp
177 * This is a simple example illustrating the usage with C-like callback
181 /** @example functor-way.cpp
183 * This is a simple example illustrating the usage with function object
187 /** @example mixed-way.cpp
189 * This is a simple example illustrating the usage with a mix of C-like callbacks
190 * and function object callbacks.
193 /** @example bench.cpp
195 * This is a benchmark example, extracted from libevent and ported to eventxx.
198 /** @example prio-test.cpp
200 * This is a priority usage example.
203 /** @example test-time.cpp
205 * This is a timer usage example ported from libevent.
208 /** @example test-eof.cpp
210 * This is some kind of test of EOF ported from libevent.
213 /** @example test-weof.cpp
215 * Another test of EOF ported from libevent.
218 /** @example trivial.cpp
220 * This is the most trivial example.
224 * Namespace for all symbols libevent C++ wrapper defines.
230 // All libevent C API symbols and other internal stuff goes here.
237 /** @defgroup exceptions Exceptions
239 * eventxx makes a heavy use of exceptions. Each function has it's exceptions
240 * specified, so it's very easy to find out what exceptions to expect.
242 * Exceptions are mostly thrown when there is a programming error. So if you get
243 * an exception check your code.
249 * Base class for all libevent exceptions.
251 struct exception: public std::exception
257 * Invalid event exception.
259 * This exception is thrown when passing an invalid event to a function, the
260 * reason is given in the what() description but it usually means that the you
261 * are making some restricted operation with an active event.
263 * If you hit this exception, you probably got a programming error.
265 struct invalid_event: public std::invalid_argument, public exception
269 * Creates an invalid event exception with a reason.
271 * @param what Reason why the event is invalid).
273 explicit invalid_event(const std::string& what) throw():
274 std::invalid_argument(what)
278 }; // struct invalid_event
282 * Invalid priority exception.
284 * This exception is thrown when passing an invalid priority to a function. This
285 * usually means you don't have enough priority queues in your dispatcher, so
286 * you should have allocated more in the constructor.
288 * If you hit this exception, you probably got a programming error.
290 * @see dispatcher::dispatcher(int) to allocate more priority queues.
292 struct invalid_priority: public std::invalid_argument, public exception
296 * Creates an invalid priority exception with a reason.
298 * @param what Reason why the priority is invalid).
300 explicit invalid_priority(const std::string& what
301 = "invalid priority value") throw():
302 std::invalid_argument(what)
306 }; // struct invalid_priority
311 /// Miscellaneous constants
314 DEFAULT_PRIORITY = -1, ///< Default priority (the middle value).
315 ONCE = EVLOOP_ONCE, ///< Loop just once.
316 NONBLOCK = EVLOOP_NONBLOCK ///< Don't block the event loop.
320 /// C function used as callback in the C API.
321 typedef void (*ccallback_type)(int, short, void*);
325 * Time used for timeout values.
327 * This timeout is compose of seconds and microseconds.
329 struct time: ::timeval
333 * Creates a new time with @p sec seconds and @p usec microseconds.
335 * @param sec Number of seconds.
336 * @param usec Number of microseconds.
338 time(long sec = 0l, long usec = 0l) throw()
339 { tv_sec = sec; tv_usec = usec; }
342 * Gets the number of seconds.
344 * @return Number of seconds.
346 long sec() const throw() { return tv_sec; };
349 * Gets the number of microseconds.
351 * @return Number of microseconds.
353 long usec() const throw() { return tv_usec; };
356 * Sets the number of seconds.
358 * @param s Number of seconds.
360 void sec(long s) throw() { tv_sec = s; };
363 * Sets the number of microseconds.
365 * @param u Number of microseconds.
367 void usec(long u) throw() { tv_usec = u; };
372 /** @defgroup events Events
374 * There are many ways to specify how to handle an event. You can use use the
375 * same plain functions callbacks (see eventxx::cevent, eventxx::ctimer and
376 * eventxx::csignal) like in C or the other kind of more advanced, stateful
377 * function objects (see eventxx::event, eventxx::timer and eventxx::signal
378 * templates). The former are just typedef'ed specialization of the later.
380 * All events derive from a plain class (not template) eventxx::basic_event, one
381 * of the main utilities of it (besides containing common code ;) is to be used
384 * Please see each class documentation for details and examples.
391 * There are 4 kind of events: eventxx::TIMEOUT, eventxx::READ, eventxx::WRITE
392 * or eventxx::SIGNAL. eventxx::PERSIST is not an event, is an event modifier
393 * flag, that tells eventxx that this event should live until dispatcher::del()
394 * is called. You can use, for example:
396 * eventxx::event(fd, eventxx::READ | eventxx::PERSIST, ...);
401 TIMEOUT = EV_TIMEOUT, ///< Timeout event.
402 READ = EV_READ, ///< Read event.
403 WRITE = EV_WRITE, ///< Write event.
404 SIGNAL = EV_SIGNAL, ///< Signal event.
405 PERSIST = EV_PERSIST ///< Not really an event, is an event modifier.
409 * Basic event from which all events derive.
411 * All events derive from this class, so it's useful for use in containers,
414 * std::list< eventxx::basic_event* > events;
417 struct basic_event: internal::event
421 * Checks if there is an event pending.
423 * @param ev Type of event to check.
425 * @return true if there is a pending event, false if not.
427 bool pending(type ev) const throw()
429 // HACK libevent don't use const
430 return event_pending(const_cast< basic_event* >(this), ev, 0);
434 * Timeout of the event.
436 * @return Timeout of the event.
438 time timeout() const throw()
441 // HACK libevent don't use const
442 event_pending(const_cast< basic_event* >(this), EV_TIMEOUT, &tv);
447 * Sets the event's priority.
449 * @param priority New event priority.
451 * @pre The event must be added to some dispatcher.
453 * @see dispatcher::dispatcher(int)
455 void priority(int priority) const throw(invalid_event, invalid_priority)
457 if (ev_flags & EVLIST_ACTIVE)
458 throw invalid_event("can't change the priority of an "
460 // HACK libevent don't use const
461 if (event_priority_set(const_cast< basic_event* >(this),
463 throw invalid_priority();
467 * Event's file descriptor.
469 * @return Event's file descriptor.
471 int fd() const throw()
473 return EVENT_FD(this);
476 /// @note This is an abstract class, you can't instantiate it.
478 basic_event() throw() {}
479 basic_event(const basic_event&);
480 basic_event& operator= (const basic_event&);
482 }; // struct basic_event
486 * Generic event object.
488 * This object stores all the information about an event, including a callback
489 * functor, which is called when the event is fired. The template parameter
490 * must be a functor (callable object or function) that can take 2 parameters:
491 * an integer (the file descriptor of the fired event) and an event::type (the
492 * type of event being fired).
493 * There is a specialized version of this class which takes as the template
494 * parameter a C function with the ccallback_type signature, just like C
497 * @see eventxx::event< ccallback_type >
499 template < typename F >
500 struct event: basic_event
504 * Creates a new event.
506 * @param fd File descriptor to monitor for events.
507 * @param ev Type of events to monitor (see eventxx::type).
508 * @param handler Callback functor.
510 event(int fd, short ev, F& handler) throw()
512 event_set(this, fd, ev, &wrapper,
513 reinterpret_cast< void* >(&handler));
518 static void wrapper(int fd, short ev, void* h)
520 F& handler = *reinterpret_cast< F* >(h);
521 // Hackish, but this way the handler can get a clean
523 handler(fd, *reinterpret_cast< type* >(&ev));
526 }; // struct event< F >
530 * This is the specialization of eventxx::event for C-style callbacks.
532 * @see eventxx::event
535 struct event< ccallback_type >: basic_event
539 * Creates a new event.
541 * @param fd File descriptor to monitor for events.
542 * @param ev Type of events to monitor (see eventxx::type).
543 * @param handler C-style callback function.
544 * @param arg Arbitrary pointer to pass to the handler as argument.
546 event(int fd, short ev, ccallback_type handler, void* arg) throw()
548 event_set(this, fd, ev, handler, arg);
554 }; // struct event< ccallback_type >
558 * Timer event object.
560 * This is just a special case of event that is fired only when a timeout is
561 * reached. It's just a shortcut to:
563 * event(-1, 0, handler);
566 * @note This event can't eventxx::PERSIST.
567 * @see timer< ccallback_type >
569 template < typename F >
570 struct timer: event< F >
574 * Creates a new timer event.
576 * @param handler Callback functor.
578 timer(F& handler) throw()
580 evtimer_set(this, &event< F >::wrapper,
581 reinterpret_cast< void* >(&handler));
584 }; // struct timer< F >
588 * This is the specialization of eventxx::timer for C-style callbacks.
590 * @note This event can't eventxx::PERSIST.
594 struct timer< ccallback_type >: event< ccallback_type >
598 * Creates a new timer event.
600 * @param handler C-style callback function.
601 * @param arg Arbitrary pointer to pass to the handler as argument.
603 timer(ccallback_type handler, void* arg) throw()
605 evtimer_set(this, handler, arg);
608 }; // struct timer< ccallback_type >
612 * Signal event object.
614 * This is just a special case of event that is fired when a signal is raised
615 * (instead of a file descriptor being active). It's just a shortcut to:
617 * event(signum, eventxx::SIGNAL, handler);
620 * @note This event always eventxx::PERSIST.
621 * @see signal< ccallback_type >
623 template < typename F >
624 struct signal: event< F >
628 * Creates a new signal event.
630 * @param signum Signal number to monitor.
631 * @param handler Callback functor.
633 signal(int signum, F& handler) throw()
635 signal_set(this, signum, &event< F >::wrapper,
636 reinterpret_cast< void* >(&handler));
640 * Event's signal number.
642 * @return Event's signal number.
646 return EVENT_SIGNAL(this);
649 }; // struct signal<F>
653 * This is the specialization of eventxx::signal for C-style callbacks.
655 * @note This event always eventxx::PERSIST.
659 struct signal< ccallback_type >: event< ccallback_type >
663 * Creates a new signal event.
665 * @param signum Signal number to monitor.
666 * @param handler C-style callback function.
667 * @param arg Arbitrary pointer to pass to the handler as argument.
669 signal(int signum, ccallback_type handler, void* arg) throw()
671 signal_set(this, signum, handler, arg);
675 * Event's signal number.
677 * @return Event's signal number.
681 return EVENT_SIGNAL(this);
684 }; // struct signal< ccallback_type >
687 /// Shortcut to C-style event.
688 typedef eventxx::event< ccallback_type > cevent;
690 /// Shortcut to C-style timer.
691 typedef eventxx::timer< ccallback_type > ctimer;
693 /// Shortcut to C-style signal handler.
694 typedef eventxx::signal< ccallback_type > csignal;
703 * This class is the responsible for looping and dispatching events. Every time
704 * you need an event loop you should create an instance of this class.
706 * You can @link dispatcher::add add @endlink events to the dispatcher, and you
707 * can @link dispatcher::del remove @endlink them later or you can @link
708 * dispatcher::add_once add events to be processed just once @endlink. You can
709 * @link dispatcher::dispatch loop once or forever @endlink (well, of course you
710 * can break that forever removing all the events or by @link dispatcher::exit
711 * exiting the loop @endlink).
717 * Creates a default dispatcher (with just 1 priority).
719 * @see dispatcher(int) if you want to create a dispatcher with more
724 _event_base = static_cast< internal::event_base* >(internal::event_init());
728 * Creates a dispatcher with npriorities priorities.
730 * @param npriorities Number of priority queues to use.
732 dispatcher(int npriorities) throw(std::bad_alloc)
734 _event_base = static_cast< internal::event_base* >(internal::event_init());
735 if (!_event_base) throw std::bad_alloc();
736 // Can't fail because there is no way that it has active events
737 internal::event_base_priority_init(_event_base, npriorities);
740 #ifdef EVENT_BASE_FREE_FIX
741 ~dispatcher() throw() { event_base_free(_event_base); }
743 #warning "The dispatcher class *will* leak memory because of a libevent bug, see http://www.mail-archive.com/libevent-users@monkey.org/msg00110.html for more info an a patch. If you already have this patch, please -DEVENT_BASE_FREE_FIX to your compiler to make this message disappear and really free the dispatcher memory using event_base_free()."
747 * Adds an event to the dispatcher.
749 * @param e Event to add.
750 * @param priority Priority of the event.
752 void add(basic_event& e, int priority = DEFAULT_PRIORITY)
753 throw(invalid_priority)
755 internal::event_base_set(_event_base, &e);
756 if (priority != DEFAULT_PRIORITY
757 && internal::event_priority_set(&e, priority))
758 throw invalid_priority();
759 internal::event_add(&e, 0);
763 * Adds an event to the dispatcher with a timeout.
765 * The event is fired when there is activity on e or when to has elapsed,
766 * whatever come first.
768 * @param e Event to add.
770 * @param priority Priority of the event.
772 void add(basic_event& e, const time& to,
773 int priority = DEFAULT_PRIORITY)
774 throw(invalid_priority)
776 internal::event_base_set(_event_base, &e);
777 if (priority != DEFAULT_PRIORITY
778 && internal::event_priority_set(&e, priority))
779 throw invalid_priority();
780 internal::event_add(&e, const_cast< time* >(&to)); // XXX HACK libevent don't use const
784 * Adds a temporary event.
786 * Adds a temporary event, without the need of instantiating a new event
787 * object. Events added this way can't eventxx::PERSIST.
789 * @param fd File descriptor to monitor for events.
790 * @param ev Type of events to monitor.
791 * @param handler Callback function.
793 template < typename F >
794 void add_once(int fd, type ev, F& handler)
796 internal::event_once(fd, ev, &dispatcher::wrapper< F >,
797 reinterpret_cast< void* >(&handler), 0);
801 * Adds a temporary event to with a C-style callback.
803 * Adds a temporary event, without the need of instantiating a new event
804 * object. Events added this way can't eventxx::PERSIST.
806 * @param fd File descriptor to monitor for events.
807 * @param ev Type of events to monitor.
808 * @param handler Callback function.
809 * @param arg Arbitrary pointer to pass to the handler as argument.
811 void add_once(int fd, type ev, ccallback_type handler, void* arg)
813 internal::event_once(fd, ev, handler, arg, 0);
817 * Adds a temporary event.
819 * Adds a temporary event, without the need of instantiating a new event
820 * object. Events added this way can't eventxx::PERSIST.
822 * @param fd File descriptor to monitor for events.
823 * @param ev Type of events to monitor.
824 * @param handler Callback function.
827 template < typename F >
828 void add_once(int fd, type ev, F& handler, const time& to)
830 internal::event_once(fd, ev, &dispatcher::wrapper< F >,
831 reinterpret_cast< void* >(&handler),
832 const_cast< time* >(&to)); // XXX HACK libevent don't use const
836 * Adds a temporary event with a C-style callback.
838 * Adds a temporary event, without the need of instantiating a new event
839 * object. Events added this way can't eventxx::PERSIST.
841 * @param fd File descriptor to monitor for events.
842 * @param ev Type of events to monitor.
843 * @param handler Callback function.
844 * @param arg Arbitrary pointer to pass to the handler as argument.
847 void add_once(int fd, type ev, ccallback_type handler, void* arg, const time& to)
849 internal::event_once(fd, ev, handler, arg, const_cast< time* >(&to)); // XXX HACK libevent don't use const
853 * Adds a temporary timer.
855 * Adds a temporary timer, without the need of instantiating a new timer
858 * @param handler Callback function.
859 * @param to Timer's timeout.
861 template < typename F >
862 void add_once_timer(F& handler, const time& to)
864 internal::event_once(-1, EV_TIMEOUT, &dispatcher::wrapper< F >,
865 reinterpret_cast< void* >(&handler),
866 const_cast< time* >(&to)); // XXX HACK libevent don't use const
870 * Adds a temporary timer with a C-style callback.
872 * Adds a temporary timer, without the need of instantiating a new timer
875 * @param handler Callback function.
876 * @param arg Arbitrary pointer to pass to the handler as argument.
877 * @param to Timer's timeout.
879 void add_once_timer(ccallback_type handler, void* arg, const time& to)
881 internal::event_once(-1, EV_TIMEOUT, handler, arg, const_cast< time* >(&to)); // XXX HACK libevent don't use const
887 * The event e will be no longer monitored by this dispatcher.
889 * @param e Event to remove.
891 void del(basic_event& e) throw()
893 internal::event_del(&e);
897 * Main dispatcher loop.
899 * This function takes the control of the program, waiting for an event
900 * and calling its callbacks when it's fired. It only returns under
902 * - exit() was called.
903 * - All events were del()eted.
904 * - Another internal error.
905 * - eventxx::ONCE flag was set.
906 * - eventxx::NONBLOCK flag was set.
908 * @param flags If eventxx::ONCE is specified, then just one event is
909 * processed, if eventxx::NONBLOCK is specified, then this
910 * function returns even if there are no pending events.
912 int dispatch(int flags = 0) // TODO throw(exception)
914 return internal::event_base_loop(_event_base, flags);
918 * Exit the dispatch() loop.
920 * @param to If a timeout is given, the loop exits after the specified
923 int exit(const time& to = time())
925 // XXX HACK libevent don't use const
926 return internal::event_base_loopexit(_event_base,
927 const_cast< time* >(&to));
931 internal::event_base* _event_base;
932 template < typename F >
933 static void wrapper(int fd, type ev, void* h)
935 F& handler = *reinterpret_cast< F* >(h);
936 handler(fd, *reinterpret_cast< type* >(&ev));
939 }; // struct dispatcher
944 #endif // _EVENTXX_HPP_
946 // vim: set filetype=cpp :