libeventブリーフ

A. libeventコンセプト

その肥大化ACEとして、ネットワークに焦点を当てた、軽量、;イベント・ドリブン(イベント駆動型)、高パフォーマンス:Libeventは、C言語、高性能なイベント通知の軽量、オープンソースのライブラリ、主に以下のハイライトであります巨大な、ソースコードは、読みやすい、非常に洗練されて、WindowsやLinux、* BSDとMac OSのためのクロスプラットフォームのサポート、I / Oの多重化、ファイルディスクリプタ、世論調査、DEV /投票、選択とkqueueのなどの様々なサポート、サポートI / O、タイマ、等の信号イベント、登録イベントの優先順位。

II。インストール

インストールパッケージlibevent-2.1.8-stable.tar.gzをダウンロードする公式サイトから1

2.エキスジャーzxvfのlibevent-2.1.8-stable.tar.gz

3.抽出したディレクトリに移動します 

4. ./congifure

5.メイク

5.sudo make installを

6. / usr / local / libに上記生成されたライブラリが完了した後、は/ usr / local / libに/etc/ls.so.confに添加します

7.sudoにldconfig -v

 

III。基本的な機能

 

1.イベント処理フレームワークを作成します。

 

2.イベントを作成します。

どのようなパラメータを設定することができます。

 

 

 

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 }

 

おすすめ

転載: www.cnblogs.com/sclu/p/11318153.html