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的更多相关文章

  1. linux下libevent安装

    wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz tar –xzvf libevent-1.4.13-stable.tar.gz ...

  2. 总结libevent安装方法

    1.先用:ls -al /usr/lib | grep libevent  查看是否已安装,如果已安装且版本低于1.3,则先通过:rpm -e libevent -nodeps 进行卸载. 2.下载l ...

  3. 定时器管理:nginx的红黑树和libevent的堆

    libevent 发生超时后, while循环一次从堆顶del timer——直到最新调整的最小堆顶不是超时事件为止,(实际是del event),但是会稍后把这个timeout的 event放到ac ...

  4. [译]libev和libevent的设计差异

    本文译自what's the difference between libev and libevent? 作者是libev作者 [问]两个库都是为异步io调度而设计,在Linux上都是使用epoll ...

  5. Libevent的IO复用技术和定时事件原理

    Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易 ...

  6. Libevent初探

    Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易 ...

  7. PHP写的异步高并发服务器,基于libevent

    PHP写的异步高并发服务器,基于libevent 博客分类: PHP PHPFPSocketLinuxQQ  本文章于2013年11月修改. swoole已使用C重写作为PHP扩展来运行.项目地址:h ...

  8. libevent在windows平台下通过vs进行编译

    1.vs中新建一个静态库项目 2.配置头文件目录,将./compat../include../WIN32-Code三个目录添加到文件目录中 3.用记事本打开Makefile.nmake文件,可以看到里 ...

  9. 基于Libevent的HTTP Server

    简单的Http Server 使用Libevent内置的http相关接口,可以很容易的构建一个Http Server,一个简单的Http Server如下: #include <event2/e ...

  10. windows下编译及使用libevent

    Libevent官网:http://libevent.org/ windows 7下编译: 编译环境: windows 7 + VS2010 (1)解压libevent到F:\libevent\lib ...

随机推荐

  1. discuz数据库表

    http://faq.comsenz.com/library/database/x3/x3_index.htm    discuz数据库表字典 Discuz X3各数据库表用途 pre_common_ ...

  2. IOS-synthesize和dynamic的异同(转)

    一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...

  3. 6.5 为什么Android用Java不用c实现?

    C/C++过于底层,开发者要花很多的经历对C/C++的语言研究清楚,例如C/C++的内存机制,如果稍不注意,就会忘了开启或者释放.而Java的GC会自动处理这些,省去了很多的时间让开发者专注于自己的业 ...

  4. android firmware 利用UDP socket发送Magic Packet--python版本

    android firmware 利用UDP socket发送Magic Packet--python版本 #!/usr/bin/python import sys, time from struct ...

  5. UISwitch(开关控件)、UISegmentedControl(分段控件)

    一.UISwitch 1.初始化 UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)];   2.设 ...

  6. C++语法之-------strcpy,memcpy,memset

    1.strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#i nclude 功能:把src所指由NULL结束的字符串复制到dest所指的数组 ...

  7. SR触发器和JK触发器

    SR触发器(电平触发器) 基本RS触发器的逻辑方程为:Q(n+1)=一S+RQ(n); 约束方程:R+S=1; 根据上述两个式子得到它的四种输入与输出的关系: 1.当R端有效(0),S端无效时(1), ...

  8. iOS URL 编码

    一.iOS 中的NSURL编码 iOS 中,NSURL 的基本样式是 scheme://username:password@host:port/path?query#fragment RFC 1738 ...

  9. php redis 代码实例

    <?phpheader("Content-type:text/html;charset=utf8");$redis = new redis();$redis ->con ...

  10. C语言 线性表 链式表结构 实现

    一个单链式实现的线性表 mList (GCC编译). /** * @brief 线性表的链式实现 (单链表) * @author wid * @date 2013-10-21 * * @note 若代 ...