Libevent库基础(1)
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 --> 非未决


浙公网安备 33010602011771号