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