当前位置:网站首页>libev库解剖(1)
libev库解剖(1)
2022-08-09 14:59:00 【Small-K】
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的实现
边栏推荐
- "Deep learning" evaluation index of target detection
- 使用NATS及其nats.c客户端简单示例用法(nats.c的API接口)
- 将类指针强制转换为void*指针进行传参的使用方法
- hugging face tutorial - Chinese translation - share a model
- Visual Studio 2019新手使用(安装并创建第一个程序详细教程)
- Heap series _0x03: heap block + malloc/new bottom layer + LFH (WinDbg analysis)
- 深入浅出最优化(5) 共轭梯度下降法
- 【Postgraduate Work Weekly】(Week 7)
- 【力扣】1995. 统计特殊四元组
- Markdown 文档生成 PDF
猜你喜欢
随机推荐
Principal Component Analysis - Applications of MATLAB in Mathematical Modeling (2nd Edition)
hugging face tutorial - Chinese translation - model summary
Vim实用技巧_7.模式匹配和查找
Postgraduate Work Weekly
TOPSIS优劣解距离法
交叉编译 CURL
【力扣】33. 搜索旋转排序数组
【Postgraduate Work Weekly】
CTF online encryption and decryption and common tools
Basic Terms of Machine Learning
杭州富阳科目三新规3号线考试攻略
Stetman读paper小记:Backdoor Learning: A Survey(Yiming Li, Yong Jiang, Zhifeng Li, Shu-Tao Xia)
【知识分享】Modbus通信协议详解
【工具使用】Keil5软件使用-进阶工程配置篇
【 graduate work weekly 】 (10 weeks)
基于MTCNN和FaceNet的实时人脸检测识别系统
#define DEBUG(format, ...) 以及 #、##、__VA_ARGS__和##__VA_ARGS__的作用
堆(heap)系列_0x05:一张图剖析堆块分配和FreeLists的联系
【Postgraduate Work Weekly】(Week 9)
Heap series _0x02: The past and present of the heap (WinDbg+Visual Studio compilation)