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