libevent
libevent doc example
#include <event2/event.h> 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]",
(int) fd,
(what&EV_TIMEOUT) ? " timeout" : "",
(what&EV_READ) ? " read" : "",
(what&EV_WRITE) ? " write" : "",
(what&EV_SIGNAL) ? " signal" : "",
data);
} void main_loop(evutil_socket_t fd1, evutil_socket_t fd2)
{
struct event *ev1, *ev2;
struct timeval five_seconds = {,};
struct event_base *base = event_base_new(); /* The caller has already set up fd1, fd2 somehow, and make them
nonblocking. */ ev1 = event_new(base, fd1, EV_TIMEOUT|EV_READ|EV_PERSIST, cb_func,
(char*)"Reading event");
ev2 = event_new(base, fd2, EV_WRITE|EV_PERSIST, cb_func,
(char*)"Writing event"); event_add(ev1, &five_seconds);
event_add(ev2, NULL);
event_base_dispatch(base);
}
The event flags
- EV_TIMEOUT
-
This flag indicates an event that becomes active after a timeout elapses.
The EV_TIMEOUT flag is ignored when constructing an event: you
can either set a timeout when you add the event, or not. It is
set in the 'what' argument to the callback function when a timeout
has occurred. - EV_READ
-
This flag indicates an event that becomes active when the provided file descriptor is ready for reading.
- EV_WRITE
-
This flag indicates an event that becomes active when the provided file descriptor is ready for writing.
- EV_SIGNAL
-
Used to implement signal detection. See "Constructing signal events" below.
- EV_PERSIST
-
Indicates that the event is persistent. See "About Event Persistence" below.
- EV_ET
-
Indicates that the event should be edge-triggered, if the underlying event_base backend supports edge-triggered events. This affects the semantics of EV_READ and EV_WRITE.
若想将event当作参数传给回调函数,则可用使用event_self_cbarg函数(在2.1.1版本才引入)
#include <event2/event.h> static int n_calls = ; void cb_func(evutil_socket_t fd, short what, void *arg)
{
struct event *me = arg; printf("cb_func called %d times so far.\n", ++n_calls); if (n_calls > )
event_del(me);
} void run(struct event_base *base)
{
struct timeval one_sec = { , };
struct event *ev;
/* We're going to set up a repeating timer to get called called 100
times. */
ev = event_new(base, -, EV_PERSIST, cb_func, event_self_cbarg());
event_add(ev, &one_sec);
event_base_dispatch(base);
}reference: http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html
libevent的更多相关文章
- linux下libevent安装
wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz tar –xzvf libevent-1.4.13-stable.tar.gz ...
- 总结libevent安装方法
1.先用:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过:rpm -e libevent -nodeps 进行卸载. 2.下载l ...
- 定时器管理:nginx的红黑树和libevent的堆
libevent 发生超时后, while循环一次从堆顶del timer——直到最新调整的最小堆顶不是超时事件为止,(实际是del event),但是会稍后把这个timeout的 event放到ac ...
- [译]libev和libevent的设计差异
本文译自what's the difference between libev and libevent? 作者是libev作者 [问]两个库都是为异步io调度而设计,在Linux上都是使用epoll ...
- Libevent的IO复用技术和定时事件原理
Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易 ...
- Libevent初探
Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易 ...
- PHP写的异步高并发服务器,基于libevent
PHP写的异步高并发服务器,基于libevent 博客分类: PHP PHPFPSocketLinuxQQ 本文章于2013年11月修改. swoole已使用C重写作为PHP扩展来运行.项目地址:h ...
- libevent在windows平台下通过vs进行编译
1.vs中新建一个静态库项目 2.配置头文件目录,将./compat../include../WIN32-Code三个目录添加到文件目录中 3.用记事本打开Makefile.nmake文件,可以看到里 ...
- 基于Libevent的HTTP Server
简单的Http Server 使用Libevent内置的http相关接口,可以很容易的构建一个Http Server,一个简单的Http Server如下: #include <event2/e ...
- windows下编译及使用libevent
Libevent官网:http://libevent.org/ windows 7下编译: 编译环境: windows 7 + VS2010 (1)解压libevent到F:\libevent\lib ...
随机推荐
- Jade之Template Inheritance
Template inheritance jade支持通过关键字block和extends来实现模板继承. 比如,在layout.jade写上如下代码 html head title My Site ...
- java反射保存
前言 代码是我师父的,代码是我师父的,代码是我师父的,如有需要拿走的时候请标注 copyright by 山人Wu 记录这篇是为了加深理解,前段时间只是当做工具类来用,才有时间好好看一下,加深理解 ...
- poj 1556 The Doors
The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...
- What's New in C# 6.0
Static Types as using So, we are all quite familiar with this notion of accessing static class membe ...
- ASP.NET MVC学习之模型绑定(1)
一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...
- 在Mac/Linux/Windows上编译corefx遇到的问题及解决方法
这两天尝试在Mac/Linux/Windows三大平台上编译.NET跨平台三驾马车(coreclr/corefx/dnx)之一的corefx(.NET Core Framework),结果三个平台的编 ...
- 数据库优化实践【TSQL篇】
在前面我们介绍了如何正确使用索引,调整索引是见效最快的性能调优方法,但一般而言,调整索引只会提高查询性能.除此之外,我们还可以调整数据访问代码和TSQL,本文就介绍如何以最优的方法重构数据访问代码和T ...
- 【shell脚本】显示文件的偶数或奇数行
# Dispaly the odd line. awk 'NR%2==1' file
- 一起来花5分钟写一个PHP入门Demo
最近公司招了几个应届毕业生,他们对前端的了解还挺多,但是对后端的技术一无所知,我觉得,作为一个前端攻城狮,如果你有远大的抱负,就应该雨露均沾... 今天我就跟大家讲一讲PHP最基本的入门,至少别人问起 ...
- Linux:常用快捷键
按键 作用 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂定当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,恢复到前台为命令fg Ctrl+a 将光标移至输入行头,相 ...