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 <a href="http://www.lluca.com.ar/~lucax/repos/eventxx/">get
42 * the most up to date sources</a> from the
43 * <a href="http://www.darcs.net/">darcs</a>.
45 * You can also take a look the the <a
46 * href="http://auriga.wearlab.de/~alb/darcsweb/">darcsweb</a> interface to see
47 * the <a href="http://www.llucax.com.ar/~luca/repos/darcsweb/?r=eventxx">latest
53 * This wrapper was designed to be used just like libevent, but with C++ style
54 * syntax sugar (or poison, depends on your point of view ;) and goodies. The
55 * main difference to libevent is you always have to instance a
56 * eventxx::dispatcher to get an event loop. There is no implicit global event
57 * loop. This adds just an extra line of code for single threaded applications
58 * and makes things much more simple. See eventxx::dispatcher documentation for
61 * You can use use the same plain functions callbacks or the other kind of
62 * function objects (see @link events @endlink section for details on event
65 * eventxx uses @link exceptions @endlink to report errors. All functions has
66 * exception specifications, so it's easy to find out what to expect. See
67 * exceptions section for more detail.
69 * A timespec abstraction is provided in eventxx::time for convenient argument
70 * passing. Even more, it's a timespec itself, with some convenient methods for
71 * accessing the timespec attributes in a more C++ way. And even more, eventxx
72 * is such a direct mapping that all eventxx::event's are libevent event structs
73 * too, so theoretically you can pass a eventxx::event to libevent C functions
74 * without much trouble. eventxx::dispatcher is the only class that is not
75 * derived from libevent struct (event_base) because this struct it's not
76 * defined on the libevent header (just declared).
78 * Maybe you shouldn't know this implementation details to keep the abstraction,
79 * but this is a basic design goal of this wrapper so there is not much chance
80 * that this changes in the future (but use this knowledge with care, you are
92 * eventxx::dispatcher& d;
94 * handler(eventxx::dispatcher& d): d(d), i(0) {}
95 * void operator() (int signum, short event)
97 * if (i < 5) std::cout << "keep going...\n";
100 * std::cout << "done!\n";
106 * void sighandler(int signum, short event, void* data)
108 * int& i = *static_cast< int* >(data);
109 * std::cout << ++i << " interrupts, ";
114 * eventxx::dispatcher d;
116 * eventxx::csignal sigev(SIGINT, sighandler, &h.i);
117 * eventxx::signal< handler > e(SIGINT, h);
125 * You can see some more examples on the test directory of the distribution or
126 * on the examples related page.
131 * This library was not widely used yet, it lack some testing. Because templates
132 * are not even compiled when they are used, you can't be surprised if you catch
133 * a piece of code that didn't got even compiled yet because the lack of
134 * testing. The library has no support for buffered events yet either. It
135 * doesn't support the http stuff, and probably never will because that has
136 * nothing to do with event handling.
138 * If you notice this lib leaks memory, don't blame me, blame libevent :)
139 * libevent has a known bug on event_base_free() that makes it assert always, so
140 * event_base_free() it's unusable, unless you patch your libevent (for example,
142 * href="http://monkeymail.org/archives/libevent-users/2006-April/000141.html">patch</a>
143 * written by Mark D. Anderson and who knows why it's not still applied. If you
144 * do so, you can compile your programs with -DEVENT_BASE_FREE_FIX so
145 * event_base_free() gets called.
147 * That said, I think it's pretty usable anyways. If something is broken it
148 * would be really easy to fix it because is just a simple wrapper around
149 * libevent. So, please try it out, and if you have any problems,
150 * <a href="mailto:llucax+eventxx@gmail.com">drop me an
151 * e-mail</a> and and I'll fix it ASAP (or provide a patch and you will be my
154 * Patches to support buffered events are welcome too.
157 * @author Leandro Lucarella <llucax+eventxx@gmail.com>
162 * This program is under the BOLA license (see
163 * http://auriga.wearlab.de/~alb/bola/ for more info or the
164 * <a href="http://www.llucax.com.ar/~luca/repos/eventxx/LICENSE">LICENSE</a>
169 /** @example c-way.cpp
171 * This is a simple example illustrating the usage with C-like callback
175 /** @example functor-way.cpp
177 * This is a simple example illustrating the usage with function object
181 /** @example mixed-way.cpp
183 * This is a simple example illustrating the usage with a mix of C-like callbacks
184 * and function object callbacks.
187 /** @example bench.cpp
189 * This is a benchmark example, extracted from libevent and ported to eventxx.
192 /** @example prio-test.cpp
194 * This is a priority usage example.
197 /** @example test-time.cpp
199 * This is a timer usage example.
202 /** @example test-weof.cpp
204 * This is some kind of test of EOF ported from libevent.
207 /** @example trivial.cpp
209 * This is the most trivial example.
213 * Namespace for all symbols libevent C++ wrapper defines.
219 // All libevent C API symbols and other internal stuff goes here.
226 /** @defgroup exceptions Exceptions
228 * eventxx makes a heavy use of exceptions. Each function has it's exceptions
229 * specified, so it's very easy to find out what exceptions to expect.
231 * Exceptions are mostly thrown when there is a programming error. So if you get
232 * an exception check your code.
238 * Base class for all libevent exceptions.
240 struct exception: public std::exception
246 * Invalid event exception.
248 * This exception is thrown when passing an invalid event to a function, the
249 * reason is given in the what() description but it usually means that the you
250 * are making some restricted operation with an active event.
252 * If you hit this exception, you probably got a programming error.
254 struct invalid_event: public std::invalid_argument, public exception
258 * Creates an invalid event exception with a reason.
260 * @param what Reason why the event is invalid).
262 explicit invalid_event(const std::string& what) throw():
263 std::invalid_argument(what)
267 }; // struct invalid_event
271 * Invalid priority exception.
273 * This exception is thrown when passing an invalid priority to a function. This
274 * usually means you don't have enough priority queues in your dispatcher, so
275 * you should have allocated more in the constructor.
277 * If you hit this exception, you probably got a programming error.
279 * @see dispatcher::dispatcher(int) to allocate more priority queues.
281 struct invalid_priority: public std::invalid_argument, public exception
285 * Creates an invalid priority exception with a reason.
287 * @param what Reason why the priority is invalid).
289 explicit invalid_priority(const std::string& what
290 = "invalid priority value") throw():
291 std::invalid_argument(what)
295 }; // struct invalid_priority
300 /// Miscellaneous constants
303 DEFAULT_PRIORITY = -1, ///< Default priority (the middle value).
304 ONCE = EVLOOP_ONCE, ///< Loop just once.
305 NONBLOCK = EVLOOP_NONBLOCK ///< Don't block the event loop.
309 /// C function used as callback in the C API.
310 typedef void (*ccallback_type)(int, short, void*);
314 * Time used for timeout values.
316 * This timeout is compose of seconds and microseconds.
318 struct time: ::timeval
322 * Creates a new time with @p sec seconds and @p usec microseconds.
324 * @param sec Number of seconds.
325 * @param usec Number of microseconds.
327 time(long sec = 0l, long usec = 0l) throw()
328 { tv_sec = sec; tv_usec = usec; }
331 * Gets the number of seconds.
333 * @return Number of seconds.
335 long sec() const throw() { return tv_sec; };
338 * Gets the number of microseconds.
340 * @return Number of microseconds.
342 long usec() const throw() { return tv_usec; };
345 * Sets the number of seconds.
347 * @param s Number of seconds.
349 void sec(long s) throw() { tv_sec = s; };
352 * Sets the number of microseconds.
354 * @param u Number of microseconds.
356 void usec(long u) throw() { tv_usec = u; };
361 /** @defgroup events Events
363 * There are many ways to specify how to handle an event. You can use use the
364 * same plain functions callbacks (see eventxx::cevent, eventxx::ctimer and
365 * eventxx::csignal) like in C or the other kind of more advanced, stateful
366 * function objects (see eventxx::event, eventxx::timer and eventxx::signal
367 * templates). The former are just typedef'ed specialization of the later.
369 * All events derive from a plain class (not template) eventxx::basic_event, one
370 * of the main utilities of it (besides containing common code ;) is to be used
373 * Please see each class documentation for details and examples.
380 * There are 4 kind of events: eventxx::TIMEOUT, eventxx::READ, eventxx::WRITE
381 * or eventxx::SIGNAL. eventxx::PERSIST is not an event, is an event modifier
382 * flag, that tells eventxx that this event should live until dispatcher::del()
383 * is called. You can use, for example:
385 * eventxx::event(fd, eventxx::READ | eventxx::PERSIST, ...);
390 TIMEOUT = EV_TIMEOUT, ///< Timeout event.
391 READ = EV_READ, ///< Read event.
392 WRITE = EV_WRITE, ///< Write event.
393 SIGNAL = EV_SIGNAL, ///< Signal event.
394 PERSIST = EV_PERSIST ///< Not really an event, is an event modifier.
398 * Basic event from which all events derive.
400 * All events derive from this class, so it's useful for use in containers,
403 * std::list< eventxx::basic_event* > events;
406 struct basic_event: internal::event
410 * Checks if there is an event pending.
412 * @param ev Type of event to check.
414 * @return true if there is a pending event, false if not.
416 bool pending(type ev) const throw()
418 // HACK libevent don't use const
419 return event_pending(const_cast< basic_event* >(this), ev, 0);
423 * Timeout of the event.
425 * @return Timeout of the event.
427 time timeout() const throw()
430 // HACK libevent don't use const
431 event_pending(const_cast< basic_event* >(this), EV_TIMEOUT, &tv);
436 * Sets the event's priority.
438 * @param priority New event priority.
440 * @pre The event must be added to some dispatcher.
442 * @see dispatcher::dispatcher(int)
444 void priority(int priority) const throw(invalid_event, invalid_priority)
446 if (ev_flags & EVLIST_ACTIVE)
447 throw invalid_event("can't change the priority of an "
449 // HACK libevent don't use const
450 if (event_priority_set(const_cast< basic_event* >(this),
452 throw invalid_priority();
456 * Event's file descriptor.
458 * @return Event's file descriptor.
460 int fd() const throw()
462 return EVENT_FD(this);
465 /// @note This is an abstract class, you can't instantiate it.
467 basic_event() throw() {}
468 basic_event(const basic_event&);
469 basic_event& operator= (const basic_event&);
471 }; // struct basic_event
475 * Generic event object.
477 * This object stores all the information about an event, including a callback
478 * functor, which is called when the event is fired. The template parameter
479 * must be a functor (callable object or function) that can take 2 parameters:
480 * an integer (the file descriptor of the fired event) and an event::type (the
481 * type of event being fired).
482 * There is a specialized version of this class which takes as the template
483 * parameter a C function with the ccallback_type signature, just like C
486 * @see eventxx::event< ccallback_type >
488 template < typename F >
489 struct event: basic_event
493 * Creates a new event.
495 * @param fd File descriptor to monitor for events.
496 * @param ev Type of events to monitor (see eventxx::type).
497 * @param handler Callback functor.
499 event(int fd, short ev, F& handler) throw()
501 event_set(this, fd, ev, &wrapper,
502 reinterpret_cast< void* >(&handler));
507 static void wrapper(int fd, short ev, void* h)
509 F& handler = *reinterpret_cast< F* >(h);
510 // Hackish, but this way the handler can get a clean
512 handler(fd, *reinterpret_cast< type* >(&ev));
515 }; // struct event< F >
519 * This is the specialization of eventxx::event for C-style callbacks.
521 * @see eventxx::event
524 struct event< ccallback_type >: basic_event
528 * Creates a new event.
530 * @param fd File descriptor to monitor for events.
531 * @param ev Type of events to monitor (see eventxx::type).
532 * @param handler C-style callback function.
533 * @param arg Arbitrary pointer to pass to the handler as argument.
535 event(int fd, short ev, ccallback_type handler, void* arg) throw()
537 event_set(this, fd, ev, handler, arg);
543 }; // struct event< ccallback_type >
547 * Timer event object.
549 * This is just a special case of event that is fired only when a timeout is
550 * reached. It's just a shortcut to:
552 * event(-1, 0, handler);
555 * @note This event can't eventxx::PERSIST.
556 * @see timer< ccallback_type >
558 template < typename F >
559 struct timer: event< F >
563 * Creates a new timer event.
565 * @param handler Callback functor.
567 timer(F& handler) throw()
569 evtimer_set(this, &event< F >::wrapper,
570 reinterpret_cast< void* >(&handler));
573 }; // struct timer< F >
577 * This is the specialization of eventxx::timer for C-style callbacks.
579 * @note This event can't eventxx::PERSIST.
583 struct timer< ccallback_type >: event< ccallback_type >
587 * Creates a new timer event.
589 * @param handler C-style callback function.
590 * @param arg Arbitrary pointer to pass to the handler as argument.
592 timer(ccallback_type handler, void* arg) throw()
594 evtimer_set(this, handler, arg);
597 }; // struct timer< ccallback_type >
601 * Signal event object.
603 * This is just a special case of event that is fired when a signal is raised
604 * (instead of a file descriptor being active). It's just a shortcut to:
606 * event(signum, eventxx::SIGNAL, handler);
609 * @note This event always eventxx::PERSIST.
610 * @see signal< ccallback_type >
612 template < typename F >
613 struct signal: event< F >
617 * Creates a new signal event.
619 * @param signum Signal number to monitor.
620 * @param handler Callback functor.
622 signal(int signum, F& handler) throw()
624 signal_set(this, signum, &event< F >::wrapper,
625 reinterpret_cast< void* >(&handler));
629 * Event's signal number.
631 * @return Event's signal number.
635 return EVENT_SIGNAL(this);
638 }; // struct signal<F>
642 * This is the specialization of eventxx::signal for C-style callbacks.
644 * @note This event always eventxx::PERSIST.
648 struct signal< ccallback_type >: event< ccallback_type >
652 * Creates a new signal event.
654 * @param signum Signal number to monitor.
655 * @param handler C-style callback function.
656 * @param arg Arbitrary pointer to pass to the handler as argument.
658 signal(int signum, ccallback_type handler, void* arg) throw()
660 signal_set(this, signum, handler, arg);
664 * Event's signal number.
666 * @return Event's signal number.
670 return EVENT_SIGNAL(this);
673 }; // struct signal< ccallback_type >
676 /// Shortcut to C-style event.
677 typedef eventxx::event< ccallback_type > cevent;
679 /// Shortcut to C-style timer.
680 typedef eventxx::timer< ccallback_type > ctimer;
682 /// Shortcut to C-style signal handler.
683 typedef eventxx::signal< ccallback_type > csignal;
692 * This class is the responsible for looping and dispatching events. Every time
693 * you need an event loop you should create an instance of this class.
695 * You can @link dispatcher::add add @endlink events to the dispatcher, and you
696 * can @link dispatcher::del remove @endlink them later or you can @link
697 * dispatcher::add_once add events to be processed just once @endlink. You can
698 * @link dispatcher::dispatch loop once or forever @endlink (well, of course you
699 * can break that forever removing all the events or by @link dispatcher::exit
700 * exiting the loop @endlink).
706 * Creates a default dispatcher (with just 1 priority).
708 * @see dispatcher(int) if you want to create a dispatcher with more
713 _event_base = static_cast< internal::event_base* >(internal::event_init());
717 * Creates a dispatcher with npriorities priorities.
719 * @param npriorities Number of priority queues to use.
721 dispatcher(int npriorities) throw(std::bad_alloc)
723 _event_base = static_cast< internal::event_base* >(internal::event_init());
724 if (!_event_base) throw std::bad_alloc();
725 // Can't fail because there is no way that it has active events
726 internal::event_base_priority_init(_event_base, npriorities);
729 #ifdef EVENT_BASE_FREE_FIX
730 ~dispatcher() throw() { event_base_free(_event_base); }
732 #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()."
736 * Adds an event to the dispatcher.
738 * @param e Event to add.
739 * @param priority Priority of the event.
741 void add(basic_event& e, int priority = DEFAULT_PRIORITY)
742 throw(invalid_priority)
744 internal::event_base_set(_event_base, &e);
745 if (priority != DEFAULT_PRIORITY
746 && internal::event_priority_set(&e, priority))
747 throw invalid_priority();
748 internal::event_add(&e, 0);
752 * Adds an event to the dispatcher with a timeout.
754 * The event is fired when there is activity on e or when to has elapsed,
755 * whatever come first.
757 * @param e Event to add.
759 * @param priority Priority of the event.
761 void add(basic_event& e, const time& to,
762 int priority = DEFAULT_PRIORITY)
763 throw(invalid_priority)
765 internal::event_base_set(_event_base, &e);
766 if (priority != DEFAULT_PRIORITY
767 && internal::event_priority_set(&e, priority))
768 throw invalid_priority();
769 internal::event_add(&e, const_cast< time* >(&to)); // XXX HACK libevent don't use const
773 * Adds a temporary event.
775 * Adds a temporary event, without the need of instantiating a new event
776 * object. Events added this way can't eventxx::PERSIST.
778 * @param fd File descriptor to monitor for events.
779 * @param ev Type of events to monitor.
780 * @param handler Callback function.
782 template < typename F >
783 void add_once(int fd, type ev, F& handler)
785 internal::event_once(fd, ev, &dispatcher::wrapper< F >,
786 reinterpret_cast< void* >(&handler), 0);
790 * Adds a temporary event to with a C-style callback.
792 * Adds a temporary event, without the need of instantiating a new event
793 * object. Events added this way can't eventxx::PERSIST.
795 * @param fd File descriptor to monitor for events.
796 * @param ev Type of events to monitor.
797 * @param handler Callback function.
798 * @param arg Arbitrary pointer to pass to the handler as argument.
800 void add_once(int fd, type ev, ccallback_type handler, void* arg)
802 internal::event_once(fd, ev, handler, arg, 0);
806 * Adds a temporary event.
808 * Adds a temporary event, without the need of instantiating a new event
809 * object. Events added this way can't eventxx::PERSIST.
811 * @param fd File descriptor to monitor for events.
812 * @param ev Type of events to monitor.
813 * @param handler Callback function.
816 template < typename F >
817 void add_once(int fd, type ev, F& handler, const time& to)
819 internal::event_once(fd, ev, &dispatcher::wrapper< F >,
820 reinterpret_cast< void* >(&handler),
821 const_cast< time* >(&to)); // XXX HACK libevent don't use const
825 * Adds a temporary event with a C-style callback.
827 * Adds a temporary event, without the need of instantiating a new event
828 * object. Events added this way can't eventxx::PERSIST.
830 * @param fd File descriptor to monitor for events.
831 * @param ev Type of events to monitor.
832 * @param handler Callback function.
833 * @param arg Arbitrary pointer to pass to the handler as argument.
836 void add_once(int fd, type ev, ccallback_type handler, void* arg, const time& to)
838 internal::event_once(fd, ev, handler, arg, const_cast< time* >(&to)); // XXX HACK libevent don't use const
842 * Adds a temporary timer.
844 * Adds a temporary timer, without the need of instantiating a new timer
847 * @param handler Callback function.
848 * @param to Timer's timeout.
850 template < typename F >
851 void add_once_timer(F& handler, const time& to)
853 internal::event_once(-1, EV_TIMEOUT, &dispatcher::wrapper< F >,
854 reinterpret_cast< void* >(&handler),
855 const_cast< time* >(&to)); // XXX HACK libevent don't use const
859 * Adds a temporary timer with a C-style callback.
861 * Adds a temporary timer, without the need of instantiating a new timer
864 * @param handler Callback function.
865 * @param arg Arbitrary pointer to pass to the handler as argument.
866 * @param to Timer's timeout.
868 void add_once_timer(ccallback_type handler, void* arg, const time& to)
870 internal::event_once(-1, EV_TIMEOUT, handler, arg, const_cast< time* >(&to)); // XXX HACK libevent don't use const
876 * The event e will be no longer monitored by this dispatcher.
878 * @param e Event to remove.
880 void del(basic_event& e) throw()
882 internal::event_del(&e);
886 * Main dispatcher loop.
888 * This function takes the control of the program, waiting for an event
889 * and calling its callbacks when it's fired. It only returns under
891 * - exit() was called.
892 * - All events were del()eted.
893 * - Another internal error.
894 * - eventxx::ONCE flag was set.
895 * - eventxx::NONBLOCK flag was set.
897 * @param flags If eventxx::ONCE is specified, then just one event is
898 * processed, if eventxx::NONBLOCK is specified, then this
899 * function returns even if there are no pending events.
901 int dispatch(int flags = 0) // TODO throw(exception)
903 return internal::event_base_loop(_event_base, flags);
907 * Exit the dispatch() loop.
909 * @param to If a timeout is given, the loop exits after the specified
912 int exit(const time& to = time())
914 // XXX HACK libevent don't use const
915 return internal::event_base_loopexit(_event_base,
916 const_cast< time* >(&to));
920 internal::event_base* _event_base;
921 template < typename F >
922 static void wrapper(int fd, type ev, void* h)
924 F& handler = *reinterpret_cast< F* >(h);
925 handler(fd, *reinterpret_cast< type* >(&ev));
928 }; // struct dispatcher
933 #endif // _EVENTXX_HPP_
935 // vim: set filetype=cpp :