Libevent Introduction
Libevent is a lightweight, open-source, high-performance , event-triggered network library for Windows,Linux ,BSD and other platforms, internal use of select,epoll, Kqueue such as system invoke management event mechanism.
Compile the library code, the compilation script will determine os select , epoll or kqueue  
There are many open source projects that use libevent, such as memcached. Using libevent, the memcached can be adapted to a variety of operating systems. Libevent provides a thin package for the underlying asynchronous functions, the library itself does not consume too much performance, and it provides high performance by using heap sorting to manage the timer queue . Overall,libevent has some of the following features and benefits:
- Unified data Source, unified I/O events, signals and timers of these three events ;
- Portable, cross-platform support for multiple I/O multiplexing Technologies, epoll,poll,dev/poll , Select and the Kqueue and so on ;
- Support for concurrent programming to avoid race conditions;
- High performance, driven by events;
- Lightweight, focused on the web
Libevent has the following major components:
- Event Management includes a variety of IO ( Socket ), timers, signals and other events, also libevent The most widely used modules;
- Cache management refers to the Evbuffer function;
- DNS is an asynchronous DNS query function provided by libevent;
- HTTP is a lightweight http implementation of Libevent, including server and client
structure of the libevent
- Evutil Network (Basic) implementation for abstraction of different platforms
- Event,event_base is The core of Libevent, and is a non-blocking I/O based on events for different platforms provides an abstract set of interfaces
- Bufferevent The event-based core encapsulation of the Libevent . The application's read-write request is buffer-based
- Evbuffer buffers implemented for the bufferevent
- Evhttp the implementation of a simple HTTP client/server
- Evdns the implementation of a simple DNS client/server
- Evrpc A simple RPC Implementation
Source code Organizational Structure
Libevent Source code, although all in a folder below, but its code classification is quite clear, the main can be divided into header files , internal use of the header files , auxiliary functions , logs , libevent framework , Encapsulation of System I/O multiplexing mechanisms , signal Management , timed Event management , buffer management , basic data Structures , and libevent -based A few parts of the two utility libraries, some of which may be a source file.
- header File---- is mainly event.h : Event macro definition, interface function declaration, main struct Event 's statement;
- Internal Head ---- xxx-internal.h : Internal data structures and functions are not visible to the outside, in order to achieve the purpose of information hiding;
- libevent Frame---- event.c : Event code implementation of the overall framework;
- to the systemI/Opackage----of multiplexing mechanismsepoll.c: ToEpollthe package;select.c: ToSelectthe package;devpoll.c: ToDev/pollthe package;kqueue.c: ToKqueuethe package;
- timed Event Management ---- Min-heap.h : In fact, it is a time as Key the small Gan structure;
- Signal Management ---- signal.c : The processing of signal events;
- Accessibility Functions --- evutil.h and the evutil.c : Some accessibility functions, including creating Socket pair and some time operation functions: Add, subtract and compare.
- Log ---- log.h and the log.c : Log Log Functions
- Buffer Management ---- evbuffer.c and the buffr.c : libevent encapsulation of the buffer;
- BASIC Data Structure ---- Compat/sys a source file under: queue.h is a libevent implementation of basic data structures, including linked lists, doubly linked lists, queues, etc.
- Practical Network Library ---- http and the Evdns : is based on libevent implementation of http Server and asynchronous DNS Query Library;
Compiling libevent1. Download here, the latest libevent compression pack libevent-2.0.22-stable.tar.gz. Source file has a problem,evutil.c Source code to modify this part. Otherwise the use will fail .
#ifdef win32#include <winsock2.h> #include <ws2tcpip.h> #pragma comment (lib, "Ws2_32.lib") #define Win32_ Lean_and_mean#include <windows.h> #undef win32_lean_and_mean#include <io.h> #include <tchar.h># endif
2. OpenVS2012Development Tools command line,CDto thelibevent directory; type
nmake /f makefile.nmake Complete compilation, generate
- Libevent_core.lib: all core event functions and buffer function, including Event_base,evbuffer, bufferevent, utility .
- Libevent_extra.lib: provides functionality for certain protocols, including HTTP, DNS, RPC .
- Libevent.lib: This library exists for historical reasons, do not use it and may disappear later.
[Libevent] Libevent Introduction and compilation