void event_set(struct event *ev, int fd, short events, void (*callback)(int, short, void *), void *arg)
功能说明:
(1)event_set函数使用指定的句柄、关注的事件、事件发生时的回调函数、
回调函数的额外参数,初始化设置struct event结构对象。
ev->ev_callback = callback; ev->ev_arg = arg; ev->ev_fd = fd; ev->ev_events = events;
(2)将此event结构对象,绑定到全局current_base。
ev->ev_base = current_base;
(3)设置此event结构对象的优先级,默认为current_base中总有限级数的一半
if(current_base) ev->ev_pri = current_base->nactivequeues/2;
events参数可以是EV_READ | EV_WRITE。
#define EV_TIMEOUT 0x01 #define EV_READ 0x02 #define EV_WRITE 0x04 #define EV_SIGNAL 0x08 #define EV_PERSIST 0x10 /* Persistant event */
int event_add(struct event *ev, const struct timeval *tv);
功能:
(1)事件注册到IO事件模型,并注册到ev_base的eventqueue
struct event_base *base = ev->ev_base; res = evsel->add(evbase, ev); event_queue_insert(base, ev, EVLIST_INSERTED);
(2)如果该事件已经在活动事件队列或者超时队列中,先从中删除。
事件当前是否处于某个队列的状态保存在ev->ev_flags中,分别为:
EVLIST_TIMEOUT 0x01 位于超时队列 EVLIST_INSERTED 0x02 位于注册事件队列 EVLIST_ACTIVE 0x08 位于活动事件队列 EVLIST_INIT 0x80 初始化状态
(3)计算超时时标,并将ev加入超时队列
evutil_timeradd(&now, tv, &ev->ev_timeout); event_queue_insert(base, ev, EVLIST_TIMEOUT);
struct event { TAILQ_ENTRY (event) ev_next; TAILQ_ENTRY (event) ev_active_next; TAILQ_ENTRY (event) ev_signal_next; unsigned int min_heap_idx; /* for managing timeouts */ struct event_base *ev_base; int ev_fd; short ev_events; short ev_ncalls; short *ev_pncalls; /* Allows deletes in callback */ struct timeval ev_timeout; int ev_pri; /* smaller numbers are higher priority */ void (*ev_callback)(int, short, void *arg); void *ev_arg; int ev_res; /* result passed to event callback */ int ev_flags; };