代码拉取完成,页面将自动刷新
#include <event2/event.h>
#include <stdlib.h>
/* When we allocate an event_pair in memory, we’ll actually allocate
* more space at the end of the structure. We define some macros
* to make accessing those events less error-prone.
*/
struct event_pair {
evutil_socket_t fd;
};
/* Macro: yield the struct event ’offset’ bytes from the start of ’p’ */
#define EVENT_AT_OFFSET(p, offset) \
((struct event * ) ( ((char * )(p)) + (offset) ))
/* Macro: yield the read event of an event_pair */
#define READEV_PTR(pair) \
EVENT_AT_OFFSET((pair), sizeof(struct event_pair))
/* Macro: yield the write event of an event_pair */
#define WRITEEV_PTR(pair) \
EVENT_AT_OFFSET((pair), \
sizeof(struct event_pair)+event_get_struct_event_size())
/* Macro: yield the actual size to allocate for an event_pair */
#define EVENT_PAIR_SIZE() \
(sizeof(struct event_pair)+2 * event_get_struct_event_size())
void cb_func(evutil_socket_t fd, short what, void* arg);
struct event_pair* event_pair_new(struct event_base * base, evutil_socket_t fd)
{
struct event_pair* p = malloc(EVENT_PAIR_SIZE());
if (!p) return NULL;
p->fd = fd;
event_assign(READEV_PTR(p), base, fd, EV_READ|EV_PERSIST, cb_func, p);
event_assign(WRITEEV_PTR(p), base, fd, EV_WRITE|EV_PERSIST, cb_func, p);
return p;
}
int main()
{
struct event_base* base = event_base_new();
struct event_pair* event=event_pair_new(base,fileno(stderr));
struct timeval five_ses = {5,0};
event_add(READEV_PTR(event),&five_ses);
event_base_dispatch(base);
return 0;
}
void cb_func(evutil_socket_t fd, short what, void* arg)
{
const char* data = arg;
printf("Got an event on socket %d:%s%s%s%s [%s]\n",
(int) fd,
(what&EV_TIMEOUT) ? " timeout" : "",
(what&EV_READ) ? " read" : "",
(what&EV_WRITE) ? " write" : "",
(what&EV_SIGNAL) ? " signal" : "",
data);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。