libevent源码分析(2)--2.1.8--结构体 struct event和struct event_callback

来源:互联网 发布:苹果电脑视频剪辑软件 编辑:程序博客网 时间:2024/06/06 00:43

一、event_callback结构体

struct event_callback {     //下一个回调事件    TAILQ_ENTRY(event_callback) evcb_active_next;     //回调事件的状态标识,具体为:     //           #define EVLIST_TIMEOUT 0x01 // event在time堆中,min_heap     //           #define EVLIST_INSERTED 0x02 // event在已注册事件链表中,event_base的queue中     //           #define EVLIST_SIGNAL 0x04 // 未见使用     //           #define EVLIST_ACTIVE 0x08 // event在激活链表中,event_base的active_queue中     //           #define EVLIST_INTERNAL 0x10 // 内部使用标记     //           #define EVLIST_ACTIVE_LATER 0x20 event在下一次激活链表中     //           #define EVLIST_INIT     0x80 // event已被初始化      //           #define EVLIST_ALL          0xff // 主要用于判断事件状态的合法性    short evcb_flags;     // 回调函数的优先级,越小优先级越高    ev_uint8_t evcb_pri;    /* smaller numbers are higher priority */     // 执行不同的回调函数/** @name Event closure codes    Possible values for evcb_closure in struct event_callback    @{ *//** A regular event. Uses the evcb_callback callback *///表明是常规的读写事件,执行evcb_callback回调#define EV_CLOSURE_EVENT 0/** A signal event. Uses the evcb_callback callback */// 表明是信号,执行evcb_callback回调#define EV_CLOSURE_EVENT_SIGNAL 1/** A persistent non-signal event. Uses the evcb_callback callback */// 表明是永久性的非信号事件,使用evcb_callback回调#define EV_CLOSURE_EVENT_PERSIST 2/** A simple callback. Uses the evcb_selfcb callback. */// 表明是简单的回调,使用evcb_selfcb回调#define EV_CLOSURE_CB_SELF 3/** A finalizing callback. Uses the evcb_cbfinalize callback. */// 表明是结束型回调,使用evcb_cbfinalize回调#define EV_CLOSURE_CB_FINALIZE 4/** A finalizing event. Uses the evcb_evfinalize callback. */// 表明是结束事件,使用evcb_evfinalize回调#define EV_CLOSURE_EVENT_FINALIZE 5/** A finalizing event that should get freed after. Uses the evcb_evfinalize * callback. */// 正在结束的事件后面应该释放;使用evcb_evfinalize回调#define EV_CLOSURE_EVENT_FINALIZE_FREE 6/** @} */    ev_uint8_t evcb_closure;    /* allows us to adopt for different types of events */     // 允许我们自动适配不同类型的回调事件    union {        void (*evcb_callback)(evutil_socket_t, short, void *);        void (*evcb_selfcb)(struct event_callback *, void *);        void (*evcb_evfinalize)(struct event *, void *);        void (*evcb_cbfinalize)(struct event_callback *, void *);    } evcb_cb_union;     // 回调参数    void *evcb_arg;};




二、event结构体

struct event {     // event的回调函数,被event_base调用     // 具体定义在上面     // 以下为一些常用的宏定义     // #define ev_pri ev_evcallback.evcb_pri     // #define ev_flags ev_evcallback.evcb_flags     // #define ev_closure ev_evcallback.evcb_closure     // #define ev_callback ev_evcallback.evcb_cb_union.evcb_callback     // #define ev_arg ev_evcallback.evcb_arg    struct event_callback ev_evcallback;    /* for managing timeouts */     // 用来管理超时事件    union {         // 公用超时队列        TAILQ_ENTRY(event) ev_next_with_common_timeout;          // min_heap最小堆索引        int min_heap_idx;    } ev_timeout_pos;     // 如果是I/O事件,ev_fd为文件描述符;如果是信号,ev_fd为信号    evutil_socket_t ev_fd;     // libevent句柄,每个事件都会保存一份句柄    struct event_base *ev_base;     // 用共用体来同时表现IO事件和信号     // 以下为一些方便调用的宏定义     /* mutually exclusive */     // #define ev_signal_next    ev_.ev_signal.ev_signal_next     // #define ev_io_next    ev_.ev_io.ev_io_next     // #define ev_io_timeout    ev_.ev_io.ev_timeout     /* used only by signals */     // #define ev_ncalls    ev_.ev_signal.ev_ncalls     // #define ev_pncalls    ev_.ev_signal.ev_pncalls    union {        /* used for io events */        struct {          // 下一个io事件            LIST_ENTRY (event) ev_io_next;          // 事件超时时间(既可以是相对时间,也可以是绝对时间)            struct timeval ev_timeout;        } ev_io;        /* used by signal events */        struct {          // 下一个信号            LIST_ENTRY (event) ev_signal_next;          // 信号准备激活时,调用ev_callback的次数            short ev_ncalls;            /* Allows deletes in callback */          // 通常指向 ev_ncalls或者NULL            short *ev_pncalls;        } ev_signal;    } ev_;     // 事件类型,它可以是以下三种类型,其中io事件和信号无法同时成立     // io事件: EV_READ,EV_WRITE     // 定时事件:EV_TIMEOUT     // 信号:EV_SIGNAL     // 以下是辅助选项,可以和任何事件同时成立     // EV_PERSIST,表明是一个永久事件,表示执行完毕不会移除,如不加,则执行完毕之后会自动移除     // EV_ET: 边沿触发,如果后台方法可用的话,就可以使用;注意区分水平触发     // EV_FINALIZE:删除事件时就不会阻塞了,不会等到回调函数执行完毕;为了在多线程中安全使用,需要使用     // event_finalize()或者event_free_finalize()     // EV_CLOSED: 可以自动监测关闭的连接,然后放弃读取未完的数据,但是不是所有后台方法都支持这个选项    short ev_events;     // 记录当前激活事件的类型    short ev_res;        /* result passed to event callback */     // 保存事件的超时时间    struct timeval ev_timeout;};





阅读全文
0 0
原创粉丝点击