libevent简述

浪尽此生 提交于 2019-11-26 10:34:24

一。libevent概念

Libevent 是一个用C语言编写的、轻量级的开源高性能事件通知库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大;源代码相当精炼、易读;跨平台,支持 Windows、 Linux、 *BSD 和 Mac Os;支持多种 I/O 多路复用技术, epoll、 poll、 dev/poll、 select 和 kqueue 等;支持 I/O,定时器和信号等事件;注册事件优先级。

二。安装

1.从官网下载安装包libevent-2.1.8-stable.tar.gz

2.解压 jar zxvf libevent-2.1.8-stable.tar.gz

3.进入解压目录 

4. ./congifure

5. make

5.sudo make install

6.以上完成后会在/usr/local/lib下生成库文件,将/usr/local/lib加入到 /etc/ls.so.conf内

7.sudo ldconfig -v

 

三。基本函数

 

1.创建事件处理框架

 

2.创建事件

what的参数可以设为:

 

 

 

3.将事件加入到事件处理框架中(让事件处于未决状态)

 

4.事件处理框架循环处理事件(事件触发后执行事件的回调函数)

 

5.将事件从事件处理框架中卸下(将事件设置为非未决)

 

6.释放事件

 

 7.释放事件处理 框架

 

 

8.libevent读写管道

 //写管道 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_call_back(evutil_socket_t fd,short what,void *arg)
12 {
13     //写管道
14     char buf[BUFSIZ];
15     static int num=0;
16     sprintf(buf,"this is the %d data\n",++num);
17     write(fd,buf,sizeof(buf));
18     return ;
19 }
20 //写管道
21 int main()
22 {
23     //openfile
24     int fd=open("myfifo",O_WRONLY|O_NONBLOCK);
25     if(fd==-1)
26     {
27         perror("open err");
28         exit(1);
29     }
30     //写管道
31     struct event_base* base=NULL;
32     base=event_base_new();
33     //创建事件
34     struct event* event=NULL;
35 //检测写缓冲区是否有空间写
36         event=event_new(base,fd,EV_WRITE|EV_PERSIST,write_call_back,NULL);
37     //添加事件
38     event_add(event,NULL); //阻塞等待事件发生
39     //事件循环
40     event_base_dispatch(base);
41     //释放事件
42     event_free(event);
43     //释放框架
44     event_base_free(base);
45     close(fd);
46     return 0;
47 }

 

 //读管道 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_call_back(evutil_socket_t fd,short what,void *arg)
12 {
13     //读管道
14     char buf[BUFSIZ];
15     int len=read(fd,buf,sizeof(buf));
16     printf("buf:%s\n",buf);
17     printf("read event:%s\n",what&EV_READ?"yes":"no");
18     return ;
19 }
20 //读管道
21 int main()
22 {
23     unlink("myfifo");
24     //创建有名管道
25     mkfifo("myfifo",0664);
26 
27     //openfile
28     int fd=open("myfifo",O_RDONLY|O_NONBLOCK);
29     if(fd==-1)
30     {
31         perror("open err");
32         exit(1);
33     }
34     //读管道
35     struct event_base* base=NULL;
36     base=event_base_new();
37     //创建事件
38     struct event* event=NULL;
39         event=event_new(base,fd,EV_READ|EV_PERSIST,read_call_back,NULL);
40     //添加事件
41     event_add(event,NULL); //阻塞等待事件发生
42     //事件循环
43     event_base_dispatch(base);
44     //释放事件
45     event_free(event);
46     //释放框架
47     event_base_free(base);
48     close(fd);
49     return 0;
50 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!