libevent Brief

One. libevent concept

Libevent is a C language, lightweight, open source library of high-performance event notification, mainly in the following highlights: event-driven (event-driven), high performance; lightweight, focused on the network, as so bloated ACE huge; source code is quite refined, easy to read; cross-platform support for Windows, Linux, * BSD and Mac Os; support a variety of I / O multiplexing, epoll, poll, dev / poll, select and kqueue etc; support I / O, timers and signal events such as; registration event priority.

two. installation

1. From the official website to download the installation package libevent-2.1.8-stable.tar.gz

2. Extract jar zxvf libevent-2.1.8-stable.tar.gz

3. Go to the extracted directory 

4. ./congifure

5. make

5.sudo make install

6. After completion of the above will generate libraries in / usr / local / lib, the / usr / local / lib added to the /etc/ls.so.conf

7.sudo ldconfig -v

 

three. Basic functions

 

1. Create an event-handling framework

 

2. Create an event

what parameters can be set:

 

 

 

3. The event is added to the event handler framework (so that the event is pending)

 

4. Event processing cycle event processing framework (callback function event after event trigger)

 

5. Remove events from the event handler in the frame (set to a non-pending event)

 

6. Release Event

 

 7. Release Event Processing Framework

 

 

8.libevent read and write pipeline

 //写管道
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 }

 

Guess you like

Origin www.cnblogs.com/sclu/p/11318153.html
Recommended