libevent概括
libevent 开源。精简。跨平台(Windows、Linux、maxos、unix)。专注于网络通信。
学习的好处
学习libevent有助于提升程序设计功力,除了网络程序设计方面外,Libevent的代码里有很多有用的设计技巧和基础数据结构,比如信息隐藏、函数指针、c语言的多态支持、链表和堆等等,都有助于提升自身的程序功力。
因此一定要看Libevent的代码
程序设计不止要了解框架,很多细节之处恰恰也是事关整个系统成败的关键。只对libevent本身的框架大概了解,那或许仅仅是一知半解,不深入代码分析,就难以了解其设计的精巧之处,也就难以为自己所用。
事实上 Libevent本身就是一个典型的 Reactor模型,理解 Reactor模式是理解libevent的基石
特性
基于事件的异步通信模型
linux 认为都是文件,而libevent认为都是event(事件)
异步,并不是在函数调用的时候使用,只有满足某个条件的时候才回去使用,处理事件是由回调函数来完成的,而调用回调函数是由内核来完成的。
安装libevent
1、源码安装
去官网下载源码包
tar zxvf libevent-2.1.8-stable.tar.gz
cd ./libevent-2.1.8-stable/
./configure
make
make install
2、验证安装
cd ./sample/
gcc hello-world.c -o hello -levent
编译使用库的.c时,需要加-levent选项
运行./hello
nc查看
./hello
nc 127.1 9995 #默认9995端口
3、库默认安装在/usr/local/lib
libevent框架基本使用
1、使用流程概括
类似epoll一样,先创建一个红黑树,然后创建一个事件,再把事件放到红黑树上,再监听,用完以后释放
1. 创建 event_base (乐高底座)
struct event_base *event_base_new(void);
struct event_base *base = event_base_new();
2. 创建 事件evnet
常规事件 event --> event_new();
bufferevent --> bufferevent_socket_new();
3. 将事件 添加到 base上
int event_add(struct event *ev, const struct timeval *tv)
4. 循环监听事件满足
int event_base_dispatch(struct event_base *base);
event_base_dispatch(base);
5. 释放 event_base
event_base_free(base);
2、使用管道读写
函数具体功能以后再整理
读操作
下面有点错误,那里不是新建管道,是打开管道
源码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <event2/event.h>
// 对操作处理函数
void write_cb(evutil_socket_t fd, short what, void *arg)
{
// write管道
char buf[1024] = {0};
static int num = 0;
sprintf(buf, "hello,world-%d\n", num++);
write(fd, buf, strlen(buf)+1);
sleep(1);
}
// 写管道
int main(int argc, const char* argv[])
{
// open file
//int fd = open("myfifo", O_WRONLY | O_NONBLOCK);
int fd = open("myfifo", O_WRONLY);
if(fd == -1)
{
perror("open error");
exit(1);
}
// 写管道
struct event_base* base = NULL;
base = event_base_new();
// 创建事件
struct event* ev = NULL;
// 检测的写缓冲区是否有空间写
//ev = event_new(base, fd, EV_WRITE , write_cb, NULL);
ev = event_new(base, fd, EV_WRITE | EV_PERSIST, write_cb, NULL);
// 添加事件
event_add(ev, NULL);
// 事件循环
event_base_dispatch(base);
// 释放资源
event_free(ev);
event_base_free(base);
close(fd);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <event2/event.h>
// 对操作处理函数
void read_cb(evutil_socket_t fd, short what, void *arg)
{
// 读管道
char buf[1024] = {0};
int len = read(fd, buf, sizeof(buf));
printf("read event: %s \n", what & EV_READ ? "Yes" : "No");
printf("data len = %d, buf = %s\n", len, buf);
sleep(1);
}
// 读管道
int main(int argc, const char* argv[])
{
unlink("myfifo");
//创建有名管道
mkfifo("myfifo", 0664);
// open file
//int fd = open("myfifo", O_RDONLY | O_NONBLOCK);
int fd = open("myfifo", O_RDONLY);
if(fd == -1)
{
perror("open error");
exit(1);
}
// 创建个event_base
struct event_base* base = NULL;
base = event_base_new();
// 创建事件
struct event* ev = NULL;
ev = event_new(base, fd, EV_READ | EV_PERSIST, read_cb, NULL);
// 添加事件
event_add(ev, NULL);
// 事件循环
event_base_dispatch(base); // while(1) { epoll();}
// 释放资源
event_free(ev);
event_base_free(base);
close(fd);
return 0;
}