Last night Niels Provos announced the release of libevent 1.2. It comes with 2 new and useful features:
- Non-blocking DNS resolution: previously you had to write your own, hunt down someone elses or uses threads to prevent DNS lookups from blocking your app. This came just at the right time for me as I was about to integrate someone elses non-blocking solution into my app.
- Support for simple HTTP clients and servers: At first thought this may seem like unneeded bloat, and to many it might. I usually work on applications that lack a user interface and sometimes a simple http interface is very useful for debugging, or quickly adding some extra control to your application. This might even been robust enough to add a basic webservices like API to your application.
Non-blocking DNS example:
{ fprintf(stderr, ); exit(1); } { *addrs = addresses; int i; (result != 0) { printf(); exit(1); } { (i = 0; i < count; i++) { printf(, inet_ntoa(addrs[i])); } exit(0); } } { (argc != 2) usage(); event_init(); evdns_init(); evdns_resolve_ipv4(argv[1], 0, evdns_cb, NULL); event_dispatch(); (0); }
http server example
{ *buf; buf = evbuffer_new(); (buf == NULL) err(1, ); evbuffer_add_printf(buf, ); evhttp_send_reply(req, HTTP_OK, , buf); } { *buf; buf = evbuffer_new(); (buf == NULL) err(1, ); evbuffer_add_printf(buf, , evhttp_request_uri(req)); evhttp_send_reply(req, HTTP_OK, , buf); } { *httpd; event_init(); httpd = evhttp_start(, 8080); evhttp_set_cb(httpd, , root_handler, NULL); evhttp_set_gencb(httpd, generic_handler, NULL); event_dispatch(); evhttp_free(httpd); 0; }