libev的安装
(注意,libev仅支持linux)
从github下载源码 源码
进入源码目录,安装指令三连:
./configure
make
make install
这样就安装完成了,默认安装在/usr/local/lib
在编写程序时,引用<ev.h>, 并在编译时,指定 -lev 选项就可以了
从示例程序看架构
// a single header file is required
#include <ev.h>
#include <cstdio>
#include <cstdlib>
// every watcher type has its own typedef'd struct
// with the name ev_<type>;
ev_io stdin_watcher;
ev_timer timeout_watcher;
// all watcher callbacks have a similar signature
// this callback is called when data is readable on stdin
static void
stdin_cb (EV_P_ struct ev_io *w, int revents)
{
puts ("stdin ready");
// for one-shot events, one must manually stop the watcher
// with its corresponding stop function.
ev_io_stop (EV_A_ w);
// this causes all nested ev_loop's to stop iterating
ev_unloop (EV_A_ EVUNLOOP_ALL);
}
// another callback, this time for a time-out
static void
timeout_cb (EV_P_ struct ev_timer *w, int revents)
{
puts ("timeout");
// this causes the innermost ev_loop to stop iterating
ev_unloop (EV_A_ EVUNLOOP_ONE);
}
int
main (void)
{
// use the default event loop unless you have special needs
struct ev_loop *loop = ev_default_loop (0);
// initialise an io watcher, then start it
// this one will watch for stdin to become readable
ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
ev_io_start (loop, &stdin_watcher);
// initialise a timer watcher, then start it
// simple non-repeating 5.5 second timeout
ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
ev_timer_start (loop, &timeout_watcher);
// now wait for events to arrive
ev_loop (loop, 0);
// unloop was called, so exit
return 0;
}
上述代码为官方提供的一段示例程序,从中我们可以一窥使用libev的基本框架
-
定义事件监听结构和回调函数
libev提供了多种事件类型,每种事件类型有其相应的事件监听结构,官方称其为watcher
ev_io stdin_watcher; ev_timer timeout_watcher;
代码中先是声明了ev_io 和 ev_timer两种事件监听结构,io即io事件,timer即定时器事件
static void stdin_cb (EV_P_ struct ev_io *w, int revents) { puts ("stdin ready"); // for one-shot events, one must manually stop the watcher // with its corresponding stop function. ev_io_stop (EV_A_ w); // this causes all nested ev_loop's to stop iterating ev_unloop (EV_A_ EVUNLOOP_ALL); } // another callback, this time for a time-out static void timeout_cb (EV_P_ struct ev_timer *w, int revents) { puts ("timeout"); // this causes the innermost ev_loop to stop iterating ev_unloop (EV_A_ EVUNLOOP_ONE); }
继而是两种事件的回调函数,即发生事件后调用的函数。
回调函数的结构:
void cb_name(struct ev_loop *loop, xx_watcher *w, int revents);
有三个参数,xx_watcher指的是某种事件监听结构,那种事件的回调就用那种事件的监听结构,比如
void stdin_cb(struct ev_loop *loop, ev_io *w, int revents);
基本io的回调函数,就用ev_io
示例中的EV_P_其实就是 struct ev_loop* 的宏
-
初始化事件循环
// use the default event loop unless you have special needs struct ev_loop *loop = ev_default_loop (0);
-
初始化事件并绑定回调函数
// initialise an io watcher, then start it // this one will watch for stdin to become readable ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); ev_io_start (loop, &stdin_watcher); // initialise a timer watcher, then start it // simple non-repeating 5.5 second timeout ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); ev_timer_start (loop, &timeout_watcher);
-
开始事件循环,等待事件
// now wait for events to arrive ev_loop (loop, 0);
所以,总体来看,libev的整体架构就是,将事件和事件回调函数相绑定,然后开启事件循环,等待事件到来,事件到来后调用相应的回调函数,然后继续循环。
这是一个典型的异步非阻塞框架,接下来我们深入源码,分析下libev的实现