标签:
1.事件的分类
- 文件可写
- 文件可读
- 超时发生
- 信号发生
- 用户触发事件
2事件的生命周期
--非 persistent
event_del(nopending) 在callback中通过event_add(重新pending)构成循环
^ ^
| |
event_new(initialized)---->event_add(pending)-->事件发生(active)-->callback被调用(stoping)-->event_free(destory)
----persistent
event_del(nopending)
^
|
event_new(initialized)-->event_add(pending)->事件发生(active)-->callback被调用(pending)和第二步构成循环->event_free(destory)
- 事件状态
EV_TIMEOUT:事件超时,应该在event_add的第二个参数设置。这个时间是相对于上一次active发生call_back调用后的时间
EV_READ:文件可读
EV_WRITE:文件可写
EV_SIGNAL:信号发生(在一个进程中只应该有一个event_base监听和处理信号,即便有多个,也只有一个会收到信号(即使信号不一样))
EV_PERSIST:保持pending状态,事件可被多次激活
Thus, if you have an event with flags EV_READ|EV_PERSIST and a timeout of five seconds, the event will become active:
EV_ET:边缘触发。(状态满足触发一次,而不是条件满足多次触发)
接口
#define EV_TIMEOUT 0x01
#define EV_READ 0x02
#define EV_WRITE 0x04
#define EV_SIGNAL 0x08
#define EV_PERSIST 0x10
#define EV_ET 0x20
typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
struct event *event_new(struct event_base *base, evutil_socket_t fd,
short what, event_callback_fn cb,
void *arg);
void event_free(struct event *event);
//把事件本身作为回调函数的参数,需要使用下面的api
void *event_self_cbarg();
//辅助宏
//超时只会被激活一次
#define evtimer_new(base, callback, arg) \
event_new((base), -1, 0, (callback), (arg))
#define evtimer_add(ev, tv) \
event_add((ev),(tv))
#define evtimer_del(ev) \
event_del(ev)
#define evtimer_pending(ev, tv_out) \
event_pending((ev), EV_TIMEOUT, (tv_out))
#define evsignal_new(base, signum, cb, arg) \
event_new(base, signum, EV_SIGNAL|EV_PERSIST, cb, arg)
struct event *hup_event;
struct event_base *base = event_base_new();
/* call sighup_function on a HUP signal */
hup_event = evsignal_new(base, SIGHUP, sighup_function, NULL);
#define evsignal_add(ev, tv) \
event_add((ev),(tv))
#define evsignal_del(ev) \
event_del(ev)
#define evsignal_pending(ev, what, tv_out) \
event_pending((ev), (what), (tv_out))
libevent学习四(Working with events)
标签:
原文地址:http://www.cnblogs.com/manziluo/p/5789636.html