1 Star 0 Fork 0

llongint/libevent笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
11_event_assign.c 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
llongint 提交于 2019-04-29 10:01 . 三个客户服务器的例子
#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);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/longlongint/notes_by_libevent.git
git@gitee.com:longlongint/notes_by_libevent.git
longlongint
notes_by_libevent
libevent笔记
master

搜索帮助