1 Star 0 Fork 0

llongint/libevent笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
12_priority.c 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
llongint 提交于 2019-04-29 10:01 . 三个客户服务器的例子
#include <event2/event.h>
void read_cb(evutil_socket_t, short, void* );
void write_cb(evutil_socket_t, short, void* );
void main_loop(evutil_socket_t fd)
{
struct event* important, * unimportant;
struct event_base* base;
struct timeval five_seconds = {5,0};
base = event_base_new();
event_base_priority_init(base, 2);
/* Now base has priority 0, and priority 1 */
important = event_new(base, fd, EV_WRITE|EV_PERSIST, write_cb, NULL);
unimportant = event_new(base, fd, EV_READ|EV_PERSIST, read_cb, NULL);
event_priority_set(important, 0);
event_priority_set(unimportant, 1);
/* Now, whenever the fd is ready for writing, the write callback will
happen before the read callback. The read callback won’t happen at
all until the write callback is no longer active.
*/
event_add(important, &five_seconds);
event_add(important, NULL);
event_base_dispatch(base);
}
int main()
{
main_loop(fileno(stdin));
return 0;
}
void read_cb(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);
}
void write_cb(evutil_socket_t fd, short what, void* arg)
{
read_cb(fd, what, arg);
}
马建仓 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

搜索帮助