struct event_base {

const struct eventop *evsel;

void *evbase;

int event_count; /* counts number of total events */

int event_count_active; /* counts number of active events */


int event_gotterm; /* Set to terminate loop */


/* active event management */

struct event_list **activequeues;

int nactivequeues;


struct event_list eventqueue;

struct timeval event_tv;


RB_HEAD(event_tree, event) timetree;

};

struct eventop {

char *name;

void *(*init)(void);

int (*add)(void *, struct event *);

int (*del)(void *, struct event *);

int (*recalc)(struct event_base *, void *, int);

int (*dispatch)(struct event_base *, void *, struct timeval *);

void (*dealloc)(void *);

};

struct eventop 结构体内部主要存放事件的操作函数,由于libevent支持 /dev/pollkqueue(2)event portsPOSIX select(2)Windows select()poll(2), and epoll(4).多平台网络IO,所以根据不同平台,也定义了不同eventop对象,它们被统一放入结构体指针数组eventops[]里。

/* In order of preference */

const struct eventop *eventops[] = {

#ifdef HAVE_EVENT_PORTS

&evportops,

#endif

#ifdef HAVE_WORKING_KQUEUE

&kqops,

#endif

#ifdef HAVE_EPOLL

&epollops,

#endif

#ifdef HAVE_DEVPOLL

&devpollops,

#endif

#ifdef HAVE_RTSIG

&rtsigops,

#endif

#ifdef HAVE_POLL

&pollops,

#endif

#ifdef HAVE_SELECT

&selectops,

#endif

#ifdef WIN32

&win32ops,

#endif

NULL

};


/* EVLIST_X_ Private space: 0x1000-0xf000 */

#define EVLIST_ALL (0xf000 | 0x9f)


#define EV_TIMEOUT 0x01

#define EV_READ 0x02

#define EV_WRITE 0x04

#define EV_SIGNAL 0x08

#define EV_PERSIST 0x10 /* Persistant event */

libevent运用二进制形式,区分5种事件类型。event_add 函数用于添加超时、读、写、信号事件。超时事件由程序内部主动触发。超时事件的元素存在一个红黑树中。