xref: /freebsd/contrib/libevent/whatsnew-2.0.txt (revision c6879c6c14eedbd060ba588a3129a6c60ebbe783)
1*c43e99fdSEd MasteWhat's New In Libevent 2.0 so far:
2*c43e99fdSEd Maste
3*c43e99fdSEd Maste1. Meta-issues
4*c43e99fdSEd Maste
5*c43e99fdSEd Maste1.1. About this document
6*c43e99fdSEd Maste
7*c43e99fdSEd Maste  This document describes the key differences between Libevent 1.4 and
8*c43e99fdSEd Maste  Libevent 2.0, from a user's point of view.  It was most recently
9*c43e99fdSEd Maste  updated based on features in git master as of August 2010.
10*c43e99fdSEd Maste
11*c43e99fdSEd Maste  NOTE: I am very sure that I missed some thing on this list.  Caveat
12*c43e99fdSEd Maste  haxxor.
13*c43e99fdSEd Maste
14*c43e99fdSEd Maste1.2. Better documentation
15*c43e99fdSEd Maste
16*c43e99fdSEd Maste  There is now a book-in-progress that explains how to use Libevent and its
17*c43e99fdSEd Maste  growing pile of APIs.  As of this writing, it covers everything except the
18*c43e99fdSEd Maste  http and rpc code.  Check out the latest draft at
19*c43e99fdSEd Maste  http://www.wangafu.net/~nickm/libevent-book/ .
20*c43e99fdSEd Maste
21*c43e99fdSEd Maste2. New and Improved Event APIs
22*c43e99fdSEd Maste
23*c43e99fdSEd Maste  Many APIs are improved, refactored, or deprecated in Libevent 2.0.
24*c43e99fdSEd Maste
25*c43e99fdSEd Maste  COMPATIBILITY:
26*c43e99fdSEd Maste
27*c43e99fdSEd Maste  Nearly all existing code that worked with Libevent 1.4 should still
28*c43e99fdSEd Maste  work correctly with Libevent 2.0.  However, if you are writing new code,
29*c43e99fdSEd Maste  or if you want to port old code, we strongly recommend using the new APIs
30*c43e99fdSEd Maste  and avoiding deprecated APIs as much as possible.
31*c43e99fdSEd Maste
32*c43e99fdSEd Maste  Binaries linked against Libevent 1.4 will need to be recompiled to link
33*c43e99fdSEd Maste  against Libevent 2.0.  This is nothing new; we have never been good at
34*c43e99fdSEd Maste  preserving binary compatibility between releases.  We'll try harder in the
35*c43e99fdSEd Maste  future, though: see 2.1 below.
36*c43e99fdSEd Maste
37*c43e99fdSEd Maste2.1. New header layout for improved forward-compatibility
38*c43e99fdSEd Maste
39*c43e99fdSEd Maste  Libevent 2.0 has a new header layout to make it easier for programmers to
40*c43e99fdSEd Maste  write good, well-supported libevent code.  The new headers are divided
41*c43e99fdSEd Maste  into three types.
42*c43e99fdSEd Maste
43*c43e99fdSEd Maste  There are *regular headers*, like event2/event.h.  These headers contain
44*c43e99fdSEd Maste  the functions that most programmers will want to use.
45*c43e99fdSEd Maste
46*c43e99fdSEd Maste  There are *backward compatibility headers*, like event2/event_compat.h.
47*c43e99fdSEd Maste  These headers contain declarations for deprecated functions from older
48*c43e99fdSEd Maste  versions of Libevent.  Documentation in these headers should suggest what's
49*c43e99fdSEd Maste  wrong with the old functions, and what functions you want to start using
50*c43e99fdSEd Maste  instead of the old ones.  Some of these functions might be removed in a
51*c43e99fdSEd Maste  future release.  New programs should generally not include these headers.
52*c43e99fdSEd Maste
53*c43e99fdSEd Maste  Finally, there are *structure headers*, like event2/event_struct.h.
54*c43e99fdSEd Maste  These headers contain definitions of some structures that Libevent has
55*c43e99fdSEd Maste  historically exposed.  Exposing them caused problems in the past,
56*c43e99fdSEd Maste  since programs that were compiled to work with one version of Libevent
57*c43e99fdSEd Maste  would often stop working with another version that changed the size or
58*c43e99fdSEd Maste  layout of some object.  We've moving them into separate headers so
59*c43e99fdSEd Maste  that programmers can know that their code is not depending on any
60*c43e99fdSEd Maste  unstable aspect of the Libvent ABI.  New programs should generally not
61*c43e99fdSEd Maste  include these headers unless they really know what they are doing, are
62*c43e99fdSEd Maste  willing to rebuild their software whenever they want to link it
63*c43e99fdSEd Maste  against a new version of Libevent, and are willing to risk their code
64*c43e99fdSEd Maste  breaking if and when data structures change.
65*c43e99fdSEd Maste
66*c43e99fdSEd Maste  Functionality that once was located in event.h is now more subdivided.
67*c43e99fdSEd Maste  The core event logic is now in event2/event.h.  The "evbuffer" functions
68*c43e99fdSEd Maste  for low-level buffer manipulation are in event2/buffer.h.  The
69*c43e99fdSEd Maste  "bufferevent" functions for higher-level buffered IO are in
70*c43e99fdSEd Maste  event2/bufferevent.h.
71*c43e99fdSEd Maste
72*c43e99fdSEd Maste  COMPATIBILITY:
73*c43e99fdSEd Maste
74*c43e99fdSEd Maste  All of the old headers (event.h, evdns.h, evhttp.h, evrpc.h, and
75*c43e99fdSEd Maste  evutil.h) will continue to work by including the corresponding new
76*c43e99fdSEd Maste  headers.  Old code should not be broken by this change.
77*c43e99fdSEd Maste
78*c43e99fdSEd Maste2.2. New thread-safe, binary-compatible, harder-to-mess-up APIs
79*c43e99fdSEd Maste
80*c43e99fdSEd Maste  Some aspects of the historical Libevent API have encouraged
81*c43e99fdSEd Maste  non-threadsafe code, or forced code built against one version of Libevent
82*c43e99fdSEd Maste  to no longer build with another.  The problems with now-deprecated APIs
83*c43e99fdSEd Maste  fell into two categories:
84*c43e99fdSEd Maste
85*c43e99fdSEd Maste     1) Dependence on the "current" event_base.  In an application with
86*c43e99fdSEd Maste        multiple event_bases, Libevent previously had a notion of the
87*c43e99fdSEd Maste        "current" event_base.  New events were linked to this base, and
88*c43e99fdSEd Maste        the caller needed to explicitly reattach them to another base.
89*c43e99fdSEd Maste        This was horribly error-prone.
90*c43e99fdSEd Maste
91*c43e99fdSEd Maste        Functions like "event_set" that worked with the "current" event_base
92*c43e99fdSEd Maste        are now deprecated but still available (see 2.1).  There are new
93*c43e99fdSEd Maste        functions like "event_assign" that take an explicit event_base
94*c43e99fdSEd Maste        argument when setting up a structure.  Using these functions will help
95*c43e99fdSEd Maste        prevent errors in your applications, and to be more threadsafe.
96*c43e99fdSEd Maste
97*c43e99fdSEd Maste     2) Structure dependence.  Applications needed to allocate 'struct
98*c43e99fdSEd Maste        event' themselves, since there was no function in Libevent to do it
99*c43e99fdSEd Maste        for them.  But since the size and contents of struct event can
100*c43e99fdSEd Maste        change between libevent versions, this created binary-compatibility
101*c43e99fdSEd Maste        nightmares.  All structures of this kind are now isolated in
102*c43e99fdSEd Maste        _struct.h header (see 2.1), and there are new allocate-and-
103*c43e99fdSEd Maste        initialize functions you can use instead of the old initialize-only
104*c43e99fdSEd Maste        functions.  For example, instead of malloc and event_set, you
105*c43e99fdSEd Maste        can use event_new().
106*c43e99fdSEd Maste
107*c43e99fdSEd Maste        (For people who do really want to allocate a struct event on the
108*c43e99fdSEd Maste        stack, or put one inside another structure, you can still use
109*c43e99fdSEd Maste        event2/event_compat.h.)
110*c43e99fdSEd Maste
111*c43e99fdSEd Maste   So in the case where old code would look like this:
112*c43e99fdSEd Maste
113*c43e99fdSEd Maste      #include <event.h>
114*c43e99fdSEd Maste      ...
115*c43e99fdSEd Maste      struct event *ev = malloc(sizeof(struct event));
116*c43e99fdSEd Maste      /* This call will cause a buffer overrun if you compile with one version
117*c43e99fdSEd Maste         of Libevent and link dynamically against another. */
118*c43e99fdSEd Maste      event_set(ev, fd, EV_READ, cb, NULL);
119*c43e99fdSEd Maste      /* If you forget this call, your code will break in hard-to-diagnose
120*c43e99fdSEd Maste         ways in the presence of multiple event bases. */
121*c43e99fdSEd Maste      event_set_base(ev, base);
122*c43e99fdSEd Maste
123*c43e99fdSEd Maste   New code will look more like this:
124*c43e99fdSEd Maste
125*c43e99fdSEd Maste     #include <event2/event.h>
126*c43e99fdSEd Maste     ...
127*c43e99fdSEd Maste     struct event *ev;
128*c43e99fdSEd Maste     ev = event_new(base, fd, EV_READ, cb, NULL);
129*c43e99fdSEd Maste
130*c43e99fdSEd Maste2.3. Overrideable allocation functions
131*c43e99fdSEd Maste
132*c43e99fdSEd Maste  If you want to override the allocation functions used by libevent
133*c43e99fdSEd Maste  (for example, to use a specialized allocator, or debug memory
134*c43e99fdSEd Maste  issues, or so on), you can replace them by calling
135*c43e99fdSEd Maste  event_set_mem_functions.  It takes replacements for malloc(),
136*c43e99fdSEd Maste  free(), and realloc().
137*c43e99fdSEd Maste
138*c43e99fdSEd Maste  If you're going to use this facility, you need to call it _before_
139*c43e99fdSEd Maste  Libevent does any memory allocation; otherwise, Libevent may allocate some
140*c43e99fdSEd Maste  memory with malloc(), and free it with the free() function you provide.
141*c43e99fdSEd Maste
142*c43e99fdSEd Maste  You can disable this feature when you are building Libevent by passing
143*c43e99fdSEd Maste  the --disable-malloc-replacement argument to configure.
144*c43e99fdSEd Maste
145*c43e99fdSEd Maste2.4. Configurable event_base creation
146*c43e99fdSEd Maste
147*c43e99fdSEd Maste  Older versions of Libevent would always got the fastest backend
148*c43e99fdSEd Maste  available, unless you reconfigured their behavior with the environment
149*c43e99fdSEd Maste  variables EVENT_NOSELECT, EVENT_NOPOLL, and so forth.  This was annoying
150*c43e99fdSEd Maste  to programmers who wanted to pick a backend explicitly without messing
151*c43e99fdSEd Maste  with the environment.
152*c43e99fdSEd Maste
153*c43e99fdSEd Maste  Also, despite our best efforts, not every backend supports every
154*c43e99fdSEd Maste  operation we might like.  Some features (like edge-triggered events, or
155*c43e99fdSEd Maste  working with non-socket file descriptors) only work with some operating
156*c43e99fdSEd Maste  systems' fast backends.  Previously, programmers who cared about this
157*c43e99fdSEd Maste  needed to know which backends supported what.  This tended to get quite
158*c43e99fdSEd Maste  ungainly.
159*c43e99fdSEd Maste
160*c43e99fdSEd Maste  There is now an API to choose backends, either by name or by feature.
161*c43e99fdSEd Maste  Here is an example:
162*c43e99fdSEd Maste
163*c43e99fdSEd Maste      struct event_config_t *config;
164*c43e99fdSEd Maste      struct event_base *base;
165*c43e99fdSEd Maste
166*c43e99fdSEd Maste      /* Create a new configuration object. */
167*c43e99fdSEd Maste      config = event_config_new();
168*c43e99fdSEd Maste      /* We don't want to use the "select" method. */
169*c43e99fdSEd Maste      event_config_avoid_method(config, "select");
170*c43e99fdSEd Maste      /* We want a method that can work with non-socket file descriptors */
171*c43e99fdSEd Maste      event_config_require_features(config, EV_FEATURE_FDS);
172*c43e99fdSEd Maste
173*c43e99fdSEd Maste      base = event_base_new_with_config(config);
174*c43e99fdSEd Maste      if (!base) {
175*c43e99fdSEd Maste         /* There is no backend method that does what we want. */
176*c43e99fdSEd Maste         exit(1);
177*c43e99fdSEd Maste      }
178*c43e99fdSEd Maste      event_config_free(config);
179*c43e99fdSEd Maste
180*c43e99fdSEd Maste  Supported features are documented in event2/event.h
181*c43e99fdSEd Maste
182*c43e99fdSEd Maste2.5. Socket is now an abstract type
183*c43e99fdSEd Maste
184*c43e99fdSEd Maste  All APIs that formerly accepted int as a socket type now accept
185*c43e99fdSEd Maste  "evutil_socket_t".  On Unix, this is just an alias for "int" as
186*c43e99fdSEd Maste  before.  On Windows, however, it's an alias for SOCKET, which can
187*c43e99fdSEd Maste  be wider than int on 64-bit platforms.
188*c43e99fdSEd Maste
189*c43e99fdSEd Maste2.6. Timeouts and persistent events work together.
190*c43e99fdSEd Maste
191*c43e99fdSEd Maste  Previously, it wasn't useful to set a timeout on a persistent event:
192*c43e99fdSEd Maste  the timeout would trigger once, and never again.  This is not what
193*c43e99fdSEd Maste  applications tend to want.  Instead, applications tend to want every
194*c43e99fdSEd Maste  triggering of the event to re-set the timeout.  So now, if you set
195*c43e99fdSEd Maste  up an event like this:
196*c43e99fdSEd Maste       struct event *ev;
197*c43e99fdSEd Maste       struct timeval tv;
198*c43e99fdSEd Maste       ev = event_new(base, fd, EV_READ|EV_PERSIST, cb, NULL);
199*c43e99fdSEd Maste       tv.tv_sec = 1;
200*c43e99fdSEd Maste       tv.tv_usec = 0;
201*c43e99fdSEd Maste       event_add(ev, &tv);
202*c43e99fdSEd Maste
203*c43e99fdSEd Maste  The callback 'cb' will be invoked whenever fd is ready to read, OR whenever
204*c43e99fdSEd Maste  a second has passed since the last invocation of cb.
205*c43e99fdSEd Maste
206*c43e99fdSEd Maste2.7. Multiple events allowed per fd
207*c43e99fdSEd Maste
208*c43e99fdSEd Maste  Older versions of Libevent allowed at most one EV_READ event and at most
209*c43e99fdSEd Maste  one EV_WRITE event per socket, per event base.  This restriction is no
210*c43e99fdSEd Maste  longer present.
211*c43e99fdSEd Maste
212*c43e99fdSEd Maste2.8. evthread_* functions for thread-safe structures.
213*c43e99fdSEd Maste
214*c43e99fdSEd Maste  Libevent structures can now be built with locking support.  This code
215*c43e99fdSEd Maste  makes it safe to add, remove, and activate events on an event base from a
216*c43e99fdSEd Maste  different thread.  (Previously, if you wanted to write multithreaded code
217*c43e99fdSEd Maste  with Libevent, you could only an event_base or its events in one thread at
218*c43e99fdSEd Maste  a time.)
219*c43e99fdSEd Maste
220*c43e99fdSEd Maste  If you want threading support and you're using pthreads, you can just
221*c43e99fdSEd Maste  call evthread_use_pthreads().  (You'll need to link against the
222*c43e99fdSEd Maste  libevent_pthreads library in addition to libevent_core.  These functions are
223*c43e99fdSEd Maste  not in libevent_core.)
224*c43e99fdSEd Maste
225*c43e99fdSEd Maste  If you want threading support and you're using Windows, you can just
226*c43e99fdSEd Maste  call evthread_use_windows_threads().
227*c43e99fdSEd Maste
228*c43e99fdSEd Maste  If you are using some locking system besides Windows and pthreads, You
229*c43e99fdSEd Maste  can enable this on a per-event-base level by writing functions to
230*c43e99fdSEd Maste  implement mutexes, conditions, and thread IDs, and passing them to
231*c43e99fdSEd Maste  evthread_set_lock_callbacks and related functions in event2/thread.h.
232*c43e99fdSEd Maste
233*c43e99fdSEd Maste  Once locking functions are enabled, every new event_base is created with a
234*c43e99fdSEd Maste  lock.  You can prevent a single event_base from being built with a lock
235*c43e99fdSEd Maste  disabled by using the EVENT_BASE_FLAG_NOLOCK flag in its
236*c43e99fdSEd Maste  event_config.  If an event_base is created with a lock, it is safe to call
237*c43e99fdSEd Maste  event_del, event_add, and event_active on its events from any thread.  The
238*c43e99fdSEd Maste  event callbacks themselves are still all executed from the thread running
239*c43e99fdSEd Maste  the event loop.
240*c43e99fdSEd Maste
241*c43e99fdSEd Maste  To make an evbuffer or a bufferevent object threadsafe, call its
242*c43e99fdSEd Maste  *_enable_locking() function.
243*c43e99fdSEd Maste
244*c43e99fdSEd Maste  The HTTP api is not currently threadsafe.
245*c43e99fdSEd Maste
246*c43e99fdSEd Maste  To build Libevent with threading support disabled, pass
247*c43e99fdSEd Maste  --disable-thread-support to the configure script.
248*c43e99fdSEd Maste
249*c43e99fdSEd Maste2.9. Edge-triggered events on some backends.
250*c43e99fdSEd Maste
251*c43e99fdSEd Maste  With some backends, it's now possible to add the EV_ET flag to an event
252*c43e99fdSEd Maste  in order to request that the event's semantics be edge-triggered.  Right
253*c43e99fdSEd Maste  now, epoll and kqueue support this.
254*c43e99fdSEd Maste
255*c43e99fdSEd Maste  The corresponding event_config feature is EV_FEATURE_ET; see 2.4 for more
256*c43e99fdSEd Maste  information.
257*c43e99fdSEd Maste
258*c43e99fdSEd Maste2.10. Better support for huge numbers of timeouts
259*c43e99fdSEd Maste
260*c43e99fdSEd Maste  The heap-based priority queue timer implementation for Libevent 1.4 is good
261*c43e99fdSEd Maste  for randomly distributed timeouts, but suboptimal if you have huge numbers
262*c43e99fdSEd Maste  of timeouts that all expire in the same amount of time after their
263*c43e99fdSEd Maste  creation.  The new event_base_init_common_timeout() logic lets you signal
264*c43e99fdSEd Maste  that a given timeout interval will be very common, and should use a linked
265*c43e99fdSEd Maste  list implementation instead of a priority queue.
266*c43e99fdSEd Maste
267*c43e99fdSEd Maste2.11. Improved debugging support
268*c43e99fdSEd Maste
269*c43e99fdSEd Maste  It's been pretty easy to forget to delete all your events before you
270*c43e99fdSEd Maste  re-initialize them, or otherwise put Libevent in an internally inconsistent
271*c43e99fdSEd Maste  state.  You can tell libevent to catch these and other common errors with
272*c43e99fdSEd Maste  the new event_enable_debug_mode() call.  Just invoke it before you do
273*c43e99fdSEd Maste  any calls to other libevent functions, and it'll catch many common
274*c43e99fdSEd Maste  event-level errors in your code.
275*c43e99fdSEd Maste
276*c43e99fdSEd Maste2.12. Functions to access all event fields
277*c43e99fdSEd Maste
278*c43e99fdSEd Maste  So that you don't have to access the struct event fields directly, Libevent
279*c43e99fdSEd Maste  now provides accessor functions to retrieve everything from an event that
280*c43e99fdSEd Maste  you set during event_new() or event_assign().
281*c43e99fdSEd Maste
282*c43e99fdSEd Maste3. Backend-specific and performance improvements.
283*c43e99fdSEd Maste
284*c43e99fdSEd Maste3.1. Change-minimization on O(1) backends
285*c43e99fdSEd Maste
286*c43e99fdSEd Maste  With previous versions of Libevent, if you called event_del() and
287*c43e99fdSEd Maste  event_add() repeatedly on a single event between trips to the backend's
288*c43e99fdSEd Maste  dispatch function, the backend might wind up making unnecessary calls or
289*c43e99fdSEd Maste  passing unnecessary data to the kernel.  The new backend logic batches up
290*c43e99fdSEd Maste  redundant adds and deletes, and performs no more operations than necessary
291*c43e99fdSEd Maste  at the kernel level.
292*c43e99fdSEd Maste
293*c43e99fdSEd Maste  This logic is on for the kqueue backend, and available (but off by
294*c43e99fdSEd Maste  default) for the epoll backend.  To turn it on for the epoll backend,
295*c43e99fdSEd Maste  set the EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST flag in the
296*c43e99fdSEd Maste  event_base_cofig, or set the EVENT_EPOLL_USE_CHANGELIST environment
297*c43e99fdSEd Maste  variable.  Doing this with epoll may result in weird bugs if you give
298*c43e99fdSEd Maste  any fds closed by dup() or its variants.
299*c43e99fdSEd Maste
300*c43e99fdSEd Maste3.2. Improved notification on Linux
301*c43e99fdSEd Maste
302*c43e99fdSEd Maste  When we need to wake the event loop up from another thread, we use
303*c43e99fdSEd Maste  an epollfd to do so, instead of a socketpair.  This is supposed to be
304*c43e99fdSEd Maste  faster.
305*c43e99fdSEd Maste
306*c43e99fdSEd Maste3.3. Windows: better support for everything
307*c43e99fdSEd Maste
308*c43e99fdSEd Maste  Bufferevents on Windows can use a new mechanism (off-by-default; see below)
309*c43e99fdSEd Maste  to send their data via Windows overlapped IO and get their notifications
310*c43e99fdSEd Maste  via the IOCP API.  This should be much faster than using event-based
311*c43e99fdSEd Maste  notification.
312*c43e99fdSEd Maste
313*c43e99fdSEd Maste  Other functions throughout the code have been fixed to work more
314*c43e99fdSEd Maste  consistently with Windows.  Libevent now builds on Windows using either
315*c43e99fdSEd Maste  mingw, or using MSVC (with nmake).  Libevent works fine with UNICODE
316*c43e99fdSEd Maste  defined, or not.
317*c43e99fdSEd Maste
318*c43e99fdSEd Maste  Data structures are a little smarter: our lookups from socket to pending
319*c43e99fdSEd Maste  event are now done with O(1) hash tables rather than O(lg n) red-black
320*c43e99fdSEd Maste  trees.
321*c43e99fdSEd Maste
322*c43e99fdSEd Maste  Unfortunately, the main Windows backend is still select()-based: from
323*c43e99fdSEd Maste  testing the IOCP backends on the mailing list, it seems that there isn't
324*c43e99fdSEd Maste  actually a way to tell for certain whether a socket is writable with IOCP.
325*c43e99fdSEd Maste  Libevent 2.1 may add a multithreaded WaitForMultipleEvents-based
326*c43e99fdSEd Maste  backend for better performance with many inactive sockets and better
327*c43e99fdSEd Maste  integration with Windows events.
328*c43e99fdSEd Maste
329*c43e99fdSEd Maste4. Improvements to evbuffers
330*c43e99fdSEd Maste
331*c43e99fdSEd Maste  Libevent has long had an "evbuffer" implementation to wrap access to an
332*c43e99fdSEd Maste  input or output memory buffer.  In previous versions, the implementation
333*c43e99fdSEd Maste  was very inefficient and lacked some desirable features.  We've made many
334*c43e99fdSEd Maste  improvements in Libevent 2.0.
335*c43e99fdSEd Maste
336*c43e99fdSEd Maste4.1. Chunked-memory internal representation
337*c43e99fdSEd Maste
338*c43e99fdSEd Maste  Previously, each evbuffer was a huge chunk of memory.  When we ran out of
339*c43e99fdSEd Maste  space in an evbuffer, we used realloc() to grow the chunk of memory.  When
340*c43e99fdSEd Maste  data was misaligned, we used memmove to move the data back to the front
341*c43e99fdSEd Maste  of the buffer.
342*c43e99fdSEd Maste
343*c43e99fdSEd Maste  Needless to say, this is a terrible interface for networked IO.
344*c43e99fdSEd Maste
345*c43e99fdSEd Maste  Now, evbuffers are implemented as a linked list of memory chunks, like
346*c43e99fdSEd Maste  most Unix kernels use for network IO.  (See Linux's skbuf interfaces,
347*c43e99fdSEd Maste  or *BSD's mbufs).  Data is added at the end of the linked list and
348*c43e99fdSEd Maste  removed from the front, so that we don't ever need realloc huge chunks
349*c43e99fdSEd Maste  or memmove the whole buffer contents.
350*c43e99fdSEd Maste
351*c43e99fdSEd Maste  To avoid excessive calls to read and write, we use the readv/writev
352*c43e99fdSEd Maste  interfaces (or WSASend/WSARecv on Windows) to do IO on multiple chunks at
353*c43e99fdSEd Maste  once with a single system call.
354*c43e99fdSEd Maste
355*c43e99fdSEd Maste  COMPATIBILITY NOTE:
356*c43e99fdSEd Maste  The evbuffer struct is no longer exposed in a header.  The code here is
357*c43e99fdSEd Maste  too volatile to expose an official evbuffer structure, and there was never
358*c43e99fdSEd Maste  any means provided to create an evbuffer except via evbuffer_new which
359*c43e99fdSEd Maste  heap-allocated the buffer.
360*c43e99fdSEd Maste
361*c43e99fdSEd Maste  If you need access to the whole buffer as a linear chunk of memory, the
362*c43e99fdSEd Maste  EVBUFFER_DATA() function still works.  Watch out, though: it needs to copy
363*c43e99fdSEd Maste  the buffer's contents in a linear chunk before you can use it.
364*c43e99fdSEd Maste
365*c43e99fdSEd Maste4.2. More flexible readline support
366*c43e99fdSEd Maste
367*c43e99fdSEd Maste  The old evbuffer_readline() function (which accepted any sequence of
368*c43e99fdSEd Maste  CR and LF characters as a newline, and which couldn't handle lines
369*c43e99fdSEd Maste  containing NUL characters), is now deprecated.  The preferred
370*c43e99fdSEd Maste  function is evbuffer_readln(), which supports a variety of
371*c43e99fdSEd Maste  line-ending styles, and which can return the number of characters in
372*c43e99fdSEd Maste  the line returned.
373*c43e99fdSEd Maste
374*c43e99fdSEd Maste  You can also call evbuffer_search_eol() to find the end of a line
375*c43e99fdSEd Maste  in an evbuffer without ever extracting the line.
376*c43e99fdSEd Maste
377*c43e99fdSEd Maste4.3. Support for file-based IO in evbuffers.
378*c43e99fdSEd Maste
379*c43e99fdSEd Maste  You can now add chunks of a file into a evbuffer, and Libevent will have
380*c43e99fdSEd Maste  your OS use mapped-memory functionality, sendfile, or splice to transfer
381*c43e99fdSEd Maste  the data without ever copying it to userspace.  On OSs where this is not
382*c43e99fdSEd Maste  supported, Libevent just loads the data.
383*c43e99fdSEd Maste
384*c43e99fdSEd Maste  There are probably some bugs remaining in this code.  On some platforms
385*c43e99fdSEd Maste  (like Windows), it just reads the relevant parts of the file into RAM.
386*c43e99fdSEd Maste
387*c43e99fdSEd Maste4.4. Support for zero-copy ("scatter/gather") writes in evbuffers.
388*c43e99fdSEd Maste
389*c43e99fdSEd Maste  You can add a piece of memory to an evbuffer without copying it.
390*c43e99fdSEd Maste  Instead, Libevent adds a new element to the evbuffer's linked list of
391*c43e99fdSEd Maste  chunks with a pointer to the memory you supplied.  You can do this
392*c43e99fdSEd Maste  either with a reference-counted chunk (via evbuffer_add_reference), or
393*c43e99fdSEd Maste  by asking Libevent for a pointer to its internal vectors (via
394*c43e99fdSEd Maste  evbuffer_reserve_space or evbuffer_peek()).
395*c43e99fdSEd Maste
396*c43e99fdSEd Maste4.5. Multiple callbacks per evbuffer
397*c43e99fdSEd Maste
398*c43e99fdSEd Maste  Previously, you could only have one callback active on an evbuffer at a
399*c43e99fdSEd Maste  time.  In practice, this meant that if one part of Libevent was using an
400*c43e99fdSEd Maste  evbuffer callback to notice when an internal evbuffer was reading or
401*c43e99fdSEd Maste  writing data, you couldn't have your own callback on that evbuffer.
402*c43e99fdSEd Maste
403*c43e99fdSEd Maste  Now, you can now use the evbuffer_add_cb() function to add a callback that
404*c43e99fdSEd Maste  does not interfere with any other callbacks.
405*c43e99fdSEd Maste
406*c43e99fdSEd Maste  The evbuffer_setcb() function is now deprecated.
407*c43e99fdSEd Maste
408*c43e99fdSEd Maste4.6. New callback interface
409*c43e99fdSEd Maste
410*c43e99fdSEd Maste  Previously, evbuffer callbacks were invoked with the old size of the
411*c43e99fdSEd Maste  buffer and the new size of the buffer.  This interface could not capture
412*c43e99fdSEd Maste  operations that simultaneously filled _and_ drained a buffer, or handle
413*c43e99fdSEd Maste  cases where we needed to postpone callbacks until multiple operations were
414*c43e99fdSEd Maste  complete.
415*c43e99fdSEd Maste
416*c43e99fdSEd Maste  Callbacks that are set with evbuffer_setcb still use the old API.
417*c43e99fdSEd Maste  Callbacks added with evbuffer_add_cb() use a new interface that takes a
418*c43e99fdSEd Maste  pointer to a struct holding the total number of bytes drained read and the
419*c43e99fdSEd Maste  total number of bytes written.  See event2/buffer.h for full details.
420*c43e99fdSEd Maste
421*c43e99fdSEd Maste4.7. Misc new evbuffer features
422*c43e99fdSEd Maste
423*c43e99fdSEd Maste   You can use evbuffer_remove() to move a given number of bytes from one
424*c43e99fdSEd Maste   buffer to another.
425*c43e99fdSEd Maste
426*c43e99fdSEd Maste   The evbuffer_search() function lets you search for repeated instances of
427*c43e99fdSEd Maste   a pattern inside an evbuffer.
428*c43e99fdSEd Maste
429*c43e99fdSEd Maste   You can use evbuffer_freeze() to temporarily suspend drains from or adds
430*c43e99fdSEd Maste   to a given evbuffer.  This is useful for code that exposes an evbuffer as
431*c43e99fdSEd Maste   part of its public API, but wants users to treat it as a pure source or
432*c43e99fdSEd Maste   sink.
433*c43e99fdSEd Maste
434*c43e99fdSEd Maste   There's an evbuffer_copyout() that looks at the data at the start of an
435*c43e99fdSEd Maste   evbuffer without doing a drain.
436*c43e99fdSEd Maste
437*c43e99fdSEd Maste   You can have an evbuffer defer all of its callbacks, so that rather than
438*c43e99fdSEd Maste   being invoked immediately when the evbuffer's length changes, they are
439*c43e99fdSEd Maste   invoked from within the event_loop.  This is useful when you have a
440*c43e99fdSEd Maste   complex set of callbacks that can change the length of other evbuffers,
441*c43e99fdSEd Maste   and you want to avoid having them recurse and overflow your stack.
442*c43e99fdSEd Maste
443*c43e99fdSEd Maste5. Bufferevents improvements
444*c43e99fdSEd Maste
445*c43e99fdSEd Maste   Libevent has long included a "bufferevents" structure and related
446*c43e99fdSEd Maste   functions that were useful for generic buffered IO on a TCP connection.
447*c43e99fdSEd Maste   This is what Libevent uses for its HTTP implementation.  In addition to
448*c43e99fdSEd Maste   the improvements that they get for free from the underlying evbuffer
449*c43e99fdSEd Maste   implementation above, there are many new features in Libevent 2.0's
450*c43e99fdSEd Maste   evbuffers.
451*c43e99fdSEd Maste
452*c43e99fdSEd Maste5.1. New OO implementations
453*c43e99fdSEd Maste
454*c43e99fdSEd Maste   The "bufferevent" structure is now an abstract base type with multiple
455*c43e99fdSEd Maste   implementations.  This should not break existing code, which always
456*c43e99fdSEd Maste   allocated bufferevents with bufferevent_new().
457*c43e99fdSEd Maste
458*c43e99fdSEd Maste   Current implementations of the bufferevent interface are described below.
459*c43e99fdSEd Maste
460*c43e99fdSEd Maste5.2. bufferevent_socket_new() replaces bufferevent_new()
461*c43e99fdSEd Maste
462*c43e99fdSEd Maste   Since bufferevents that use a socket are not the only kind,
463*c43e99fdSEd Maste   bufferevent_new() is now deprecated.  Use bufferevent_socket_new()
464*c43e99fdSEd Maste   instead.
465*c43e99fdSEd Maste
466*c43e99fdSEd Maste5.3. Filtered bufferevent IO
467*c43e99fdSEd Maste
468*c43e99fdSEd Maste   You can use bufferevent_filter_new() to create a bufferevent that wraps
469*c43e99fdSEd Maste   around another bufferevent and transforms data it is sending and
470*c43e99fdSEd Maste   receiving.  See test/regress_zlib.c for a toy example that uses zlib to
471*c43e99fdSEd Maste   compress data before sending it over a bufferevent.
472*c43e99fdSEd Maste
473*c43e99fdSEd Maste5.3. Linked pairs of bufferevents
474*c43e99fdSEd Maste
475*c43e99fdSEd Maste   You can use bufferevent_pair_new() to produce two linked
476*c43e99fdSEd Maste   bufferevents.  This is like using socketpair, but doesn't require
477*c43e99fdSEd Maste   system-calls.
478*c43e99fdSEd Maste
479*c43e99fdSEd Maste5.4. SSL support for bufferevents with OpenSSL
480*c43e99fdSEd Maste
481*c43e99fdSEd Maste   There is now a bufferevent type that supports SSL/TLS using the
482*c43e99fdSEd Maste   OpenSSL library.  The code for this is build in a separate
483*c43e99fdSEd Maste   library, libevent_openssl, so that your programs don't need to
484*c43e99fdSEd Maste   link against OpenSSL unless they actually want SSL support.
485*c43e99fdSEd Maste
486*c43e99fdSEd Maste   There are two ways to construct one of these bufferevents, both
487*c43e99fdSEd Maste   declared in <event2/bufferevent_ssl.h>.  If you want to wrap an
488*c43e99fdSEd Maste   SSL layer around an existing bufferevent, you would call the
489*c43e99fdSEd Maste   bufferevent_openssl_filter_new() function.  If you want to do SSL
490*c43e99fdSEd Maste   on a socket directly, call bufferevent_openssl_socket_new().
491*c43e99fdSEd Maste
492*c43e99fdSEd Maste5.5. IOCP support for bufferevents on Windows
493*c43e99fdSEd Maste
494*c43e99fdSEd Maste   There is now a bufferevents backend that supports IOCP on Windows.
495*c43e99fdSEd Maste   Supposedly, this will eventually make Windows IO much faster for
496*c43e99fdSEd Maste   programs using bufferevents.  We'll have to see; the code is not
497*c43e99fdSEd Maste   currently optimized at all.  To try it out, call the
498*c43e99fdSEd Maste   event_base_start_iocp() method on an event_base before contructing
499*c43e99fdSEd Maste   bufferevents.
500*c43e99fdSEd Maste
501*c43e99fdSEd Maste   This is tricky code; there are probably some bugs hiding here.
502*c43e99fdSEd Maste
503*c43e99fdSEd Maste5.6. Improved connect support for bufferevents.
504*c43e99fdSEd Maste
505*c43e99fdSEd Maste   You can now create a bufferevent that is not yet connected to any
506*c43e99fdSEd Maste   host, and tell it to connect, either by address or by hostname.
507*c43e99fdSEd Maste
508*c43e99fdSEd Maste   The functions to do this are bufferevent_socket_connect and
509*c43e99fdSEd Maste   bufferevent_socket_connect_hostname.
510*c43e99fdSEd Maste
511*c43e99fdSEd Maste5.7. Rate-limiting for bufferevents
512*c43e99fdSEd Maste
513*c43e99fdSEd Maste   If you need to limit the number of bytes read/written by a single
514*c43e99fdSEd Maste   bufferevent, or by a group of them, you can do this with a new set of
515*c43e99fdSEd Maste   bufferevent rate-limiting calls.
516*c43e99fdSEd Maste
517*c43e99fdSEd Maste6. Other improvements
518*c43e99fdSEd Maste
519*c43e99fdSEd Maste6.1. DNS improvements
520*c43e99fdSEd Maste
521*c43e99fdSEd Maste6.1.1. DNS: IPv6 nameservers
522*c43e99fdSEd Maste
523*c43e99fdSEd Maste   The evdns code now lets you have nameservers whose addresses are IPv6.
524*c43e99fdSEd Maste
525*c43e99fdSEd Maste6.1.2. DNS: Better security
526*c43e99fdSEd Maste
527*c43e99fdSEd Maste   Libevent 2.0 tries harder to resist DNS answer-sniping attacks than
528*c43e99fdSEd Maste   earlier versions of evdns.  See comments in the code for full details.
529*c43e99fdSEd Maste
530*c43e99fdSEd Maste   Notably, evdns now supports the "0x20 hack" to make it harder to
531*c43e99fdSEd Maste   impersonate a DNS server.  Additionally, Libevent now uses a strong
532*c43e99fdSEd Maste   internal RNG to generate DNS transaction IDs, so you don't need to supply
533*c43e99fdSEd Maste   your own.
534*c43e99fdSEd Maste
535*c43e99fdSEd Maste6.1.3. DNS: Getaddrinfo support
536*c43e99fdSEd Maste
537*c43e99fdSEd Maste   There's now an asynchronous getaddrinfo clone, evdns_getaddrinfo(),
538*c43e99fdSEd Maste   to make the results of the evdns functions more usable.  It doesn't
539*c43e99fdSEd Maste   support every feature of a typical platform getaddrinfo() yet, but it
540*c43e99fdSEd Maste   is quite close.
541*c43e99fdSEd Maste
542*c43e99fdSEd Maste   There is also a blocking evutil_getaddrinfo() declared in
543*c43e99fdSEd Maste   event2/util.h, to provide a getaddrinfo() implementation for
544*c43e99fdSEd Maste   platforms that don't have one, and smooth over the differences in
545*c43e99fdSEd Maste   various platforms implementations of RFC3493.
546*c43e99fdSEd Maste
547*c43e99fdSEd Maste   Bufferevents provide bufferevent_connect_hostname(), which combines
548*c43e99fdSEd Maste   the name lookup and connect operations.
549*c43e99fdSEd Maste
550*c43e99fdSEd Maste6.1.4. DNS: No more evdns globals
551*c43e99fdSEd Maste
552*c43e99fdSEd Maste   Like an event base, evdns operations are now supposed to use an evdns_base
553*c43e99fdSEd Maste   argument.  This makes them easier to wrap for other (more OO) languages,
554*c43e99fdSEd Maste   and easier to control the lifetime of.  The old evdns functions will
555*c43e99fdSEd Maste   still, of course, continue working.
556*c43e99fdSEd Maste
557*c43e99fdSEd Maste6.2. Listener support
558*c43e99fdSEd Maste
559*c43e99fdSEd Maste   You can now more easily automate setting up a bound socket to listen for
560*c43e99fdSEd Maste   TCP connections.  Just use the evconnlistener_*() functions in the
561*c43e99fdSEd Maste   event2/listener.h header.
562*c43e99fdSEd Maste
563*c43e99fdSEd Maste   The listener code supports IOCP on Windows if available.
564*c43e99fdSEd Maste
565*c43e99fdSEd Maste6.3. Secure RNG support
566*c43e99fdSEd Maste
567*c43e99fdSEd Maste   Network code very frequently needs a secure, hard-to-predict random number
568*c43e99fdSEd Maste   generator.  Some operating systems provide a good C implementation of one;
569*c43e99fdSEd Maste   others do not.  Libevent 2.0 now provides a consistent implementation
570*c43e99fdSEd Maste   based on the arc4random code originally from OpenBSD.  Libevent (and you)
571*c43e99fdSEd Maste   can use the evutil_secure_rng_*() functions to access a fairly secure
572*c43e99fdSEd Maste   random stream of bytes.
573*c43e99fdSEd Maste
574*c43e99fdSEd Maste6.4. HTTP
575*c43e99fdSEd Maste
576*c43e99fdSEd Maste   The evhttp uriencoding and uridecoding APIs have updated versions
577*c43e99fdSEd Maste   that behave more correctly, and can handle strings with internal NULs.
578*c43e99fdSEd Maste
579*c43e99fdSEd Maste   The evhttp query parsing and URI parsing logic can now detect errors
580*c43e99fdSEd Maste   more usefully.  Moreover, we include an actual URI parsing function
581*c43e99fdSEd Maste   (evhttp_uri_parse()) to correctly parse URIs, so as to discourage
582*c43e99fdSEd Maste   people from rolling their own ad-hoc parsing functions.
583*c43e99fdSEd Maste
584*c43e99fdSEd Maste   There are now accessor functions for the useful fields of struct http
585*c43e99fdSEd Maste   and friends; it shouldn't be necessary to access them directly any
586*c43e99fdSEd Maste   more.
587*c43e99fdSEd Maste
588*c43e99fdSEd Maste   Libevent now lets you declare support for all specified HTTP methods,
589*c43e99fdSEd Maste   including OPTIONS, PATCH, and so on.  The default list is unchanged.
590*c43e99fdSEd Maste
591*c43e99fdSEd Maste   Numerous evhttp bugs also got fixed.
592*c43e99fdSEd Maste
593*c43e99fdSEd Maste7. Infrastructure improvements
594*c43e99fdSEd Maste
595*c43e99fdSEd Maste7.1. Better unit test framework
596*c43e99fdSEd Maste
597*c43e99fdSEd Maste   We now use a unit test framework that Nick wrote called "tinytest".
598*c43e99fdSEd Maste   The main benefit from Libevent's point of view is that tests which
599*c43e99fdSEd Maste   might mess with global state can all run each in their own
600*c43e99fdSEd Maste   subprocess.  This way, when there's a bug that makes one unit test
601*c43e99fdSEd Maste   crash or mess up global state, it doesn't affect any others.
602*c43e99fdSEd Maste
603*c43e99fdSEd Maste7.2. Better unit tests
604*c43e99fdSEd Maste
605*c43e99fdSEd Maste   Despite all the code we've added, our unit tests are much better than
606*c43e99fdSEd Maste   before.  Right now, iterating over the different backends on various
607*c43e99fdSEd Maste   platforms, I'm getting between 78% and 81% test coverage, compared
608*c43e99fdSEd Maste   with less than 45% test coverage in Libevent 1.4.
609*c43e99fdSEd Maste
610