1.创建 eevent_base

  struct event_base *base = event_base_new();

2.创建 事件event

  struct event *ev;

  struct event *event_new(struct event_base *base,evutil_socket_t fd,short what,event_callback_fn cb;  void *arg);

  base: event_base_new()返回值。

  fd:绑定到event上的文件描述符

  what:对应的事件(r、w、e)

    EV_READ           一次 读事件

    EV_WRITE         一次 写事件

    EV_PERSIST     持续触发。结合 event_base_dispatch 函数使用,生效。

    cb:一旦事件满足监听条件,回调的函数。

    typedef void (*event_callback_fn)(evutil_socket_t fd,  short,  void *)

    arg:回调的函数的参数。

    返回值:成功创建的event

    

 3.添加事件到 event_base

   int event_add(struct event *ev, const struct timeval *tv);

   ev: event_new() 的返回值。

   tv:NULL

   从event_base上摘下事件 【了解】

   int event_del(struct event *ev);

   ev: event_new() 的返回值。

 4.销毁事件

  int event_free(struct event *ev);

  ev: event_new() 的返回值。

操作管道代码:

read管道代码

 1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <event2/event.h>
9
10 // 对操作处理函数
11 void read_cb(evutil_socket_t fd, short what, void *arg)
12 {
13 // 读管道
14 char buf[1024] = {0};
15
16 int len = read(fd, buf, sizeof(buf));
17
18 printf("read event: %s \n", what & EV_READ ? "Yes" : "No");
19 printf("data len = %d, buf = %s\n", len, buf);
20
21 sleep(1);
22 }
23
24
25 // 读管道
26 int main(int argc, const char* argv[])
27 {
28 unlink("myfifo");
29
30 //创建有名管道
31 mkfifo("myfifo", 0664);
32
33 // open file
34 //int fd = open("myfifo", O_RDONLY | O_NONBLOCK);
35 int fd = open("myfifo", O_RDONLY);
36 if(fd == -1)
37 {
38 perror("open error");
39 exit(1);
40 }
41
42 // 创建个event_base
43 struct event_base* base = NULL;
44 base = event_base_new();
45
46 // 创建事件
47 struct event* ev = NULL;
48 ev = event_new(base, fd, EV_READ | EV_PERSIST, read_cb, NULL);
49
50 // 添加事件
51 event_add(ev, NULL);
52
53 // 事件循环
54 event_base_dispatch(base); // while(1) { epoll();}
55
56 // 释放资源
57 event_free(ev);
58 event_base_free(base);
59 close(fd);
60
61 return 0;
62 }

write管道代码

 1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <event2/event.h>
9
10 // 对操作处理函数
11 void write_cb(evutil_socket_t fd, short what, void *arg)
12 {
13 // write管道
14 char buf[1024] = {0};
15
16 static int num = 0;
17 sprintf(buf, "hello,world-%d\n", num++);
18 write(fd, buf, strlen(buf)+1);
19
20 sleep(1);
21 }
22
23
24 // 写管道
25 int main(int argc, const char* argv[])
26 {
27 // open file
28 //int fd = open("myfifo", O_WRONLY | O_NONBLOCK);
29 int fd = open("myfifo", O_WRONLY);
30 if(fd == -1)
31 {
32 perror("open error");
33 exit(1);
34 }
35
36 // 写管道
37 struct event_base* base = NULL;
38 base = event_base_new();
39
40 // 创建事件
41 struct event* ev = NULL;
42 // 检测的写缓冲区是否有空间写
43 //ev = event_new(base, fd, EV_WRITE , write_cb, NULL);
44 ev = event_new(base, fd, EV_WRITE | EV_PERSIST, write_cb, NULL);
45
46 // 添加事件
47 event_add(ev, NULL);
48
49 // 事件循环
50 event_base_dispatch(base);
51
52 // 释放资源
53 event_free(ev);
54 event_base_free(base);
55 close(fd);
56
57 return 0;
58 }

未决和非未决:

  非未决: 没有资格被处理

  未决: 有资格被处理,但尚未被处理

  event_new --> event ---> 非未决 --> event_add --> 未决 --> dispatch() && 监听事件被触发 --> 激活态

  --> 执行回调函数 --> 处理态 --> 非未决 event_add && EV_PERSIST --> 未决 --> event_del --> 非未决

Libevent库基础(1)的更多相关文章

  1. Libevent库基础(2)

    带缓冲区的事件 bufferevent #include <event2/bufferevent.h> read/write 两个缓冲. 借助 队列. 创建.销毁bufferevent: ...

  2. Libevent库学习笔记

    Libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,Libevent在底层select.pool.kqueue和epoll等机制基础上,封装出一致的事件接口.可 ...

  3. libevent库介绍--事件和数据缓冲

    首先在学习libevent库的使用前,我们还要从基本的了解开始,已经熟悉了epoll以及reactor,然后从event_base学习,依次学习事件event.数据缓冲Bufferevent和数据封装 ...

  4. Windows平台下libevent库的使用

    1     引子 手头上有一个使用了4个年头的HttpClient库,自己封装的,对于集成了IE浏览器的应用程序很友好.但最近想把产品扩展到Chrome和FireFox阵营,萌发了重构HttpClie ...

  5. C10K问题和Libevent库介绍

    http://blog.chinaunix.net/uid-20761674-id-75056.html 一.C10K的问题 C10K的问题在上个世纪90年代就被提出来了.大概的意思是当用户数超过1万 ...

  6. R1:创建Libevent库

    原文链接:http://www.wangafu.net/~nickm/libevent-book/Ref1_libsetup.html Setting up the Libevent library ...

  7. python linecache标准库基础学习

    #python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...

  8. C 标准库基础 IO 操作总结

    其实输入与输出对于不管什么系统的设计都是异常重要的,比如设计 C 接口函数,首先要设计好输入参数.输出参数和返回值,接下来才能开始设计具体的实现过程.C 语言标准库提供的接口功能很有限,不像 Pyth ...

  9. libevent库简单使用

    一.libevent库简介 Libevent是一个用于开发可扩展性网络服务器的基于事件驱动(event-driven)模型的网络库.Libevent有几个显著的亮点: (1)事件驱动(event-dr ...

随机推荐

  1. 关于windows服务器创建一个ps1脚本的周期性定时任务

    测试环境: Windows Server 2008 R2 Standard  & Windows Server 2012 R2 Standard 周期运行的ps脚本:Clean_up_Secu ...

  2. 解析形如(k,v)(k,v)(k,v)字符串

    有时根据需要会将map数据格式化成(k,v)(k,v)(k,v)--字符串,之后需要还原,下面代码实现了还原过程 1 void SplitString(const string& s, vec ...

  3. 深入理解TypeScript——第一章:上手篇

    怎么定义TypeScript呢? TypeScript是一个工具 是一个编译器 编译代码 TypeScript,通过它的能力,默认使用tsc命令,可以根据.ts为后缀名的文件生成一个新的js文件 2. ...

  4. 记一次公司mssql server密码频繁被改的事件

    环境描述 近期公司服务器mssql密码频繁被改,导致各种业务系统无法连接,报错.昨天来公司,发现4台数据库3台密码都变了.今天尝试着去查查是否能找到问题根源. 步骤 4台服务器3台连不上,只有64还活 ...

  5. spring整合(Junit、web)

    1.整合Junit (1)整合前的测试类代码 public class Test { public static void main(String[] args) { ApplicationConte ...

  6. Python-local variable 'raw_password' referenced before assignment

    where? 执行Python程序的时候,报这个错 why? 变量作用域问题,在分支中定义的变量,当满足条件的时候则可以正确得到变量,当不满足条件的时候则报这个错 way? 把变量从分支中抽离到分支上 ...

  7. 编程源自生活:抽象 -> 生活中的洗头问题

    设计背景: 我:头上的油揩给了手,手接触洗手液.洗手液伤头皮,这样头皮就不会和洗手液接触了. 具体执行过程描述: 1.手揩油  ->  2.取液体  3.->洗手   我:这是什么设计模式 ...

  8. P3118 [USACO15JAN]Moovie Mooving G

    P3118 [USACO15JAN]Moovie Mooving G Link 题目描述 Bessie is out at the movies. Being mischievous as alway ...

  9. K-DTree入门

    \(K-D Tree\),一种用来维护\(K\)维数据的数据结构.常用于维护各种高维的数据,或者是邻近搜索等.从另一种意义上说,实际上就是高维的二叉搜索树.对于一些常见的问题,如\(k\)远点对.三位 ...

  10. Mybatis的学习

    mybatis: 1.初识mybatis mybatis是一个数据库框架. 1.导包 <dependency> <groupId>org.mybatis</groupId ...