一、libev理解
-
libev
核心是一个事件循环(ev_loop
),一个事件循环就是一个不停在循环运行的事件 -
libev
通过分配和注册监控器(watcher
)对多个不同类型事件进行监听,任意监听的事件被触发时,进行相应事件回调操作。
二、libev用法
- 定义一个事件循环事件驱动器(
struct ev_loop
)和想要监控事件的事件监控器(ev_XXX
); - 初始化事件监控器绑定事件回调函数(
ev_init
)和设置相应事件或参数(ev_XXX_set
); - 挂载事件监控器到事件驱动器上(
ev_XXX_start
); - 事件驱动器带着事件监控器一起Happy(
ev_run
)。
简单示例
#include <ev.h>
#include <stdio.h>
#include <signal.h>
#include <sys/unistd.h>
ev_io io_w;
ev_timer timer_w;
ev_signal signal_w;
static void io_action(struct ev_loop *main_loop,ev_io *io_w,int e)
{
int rst;
char buf[1024] = {'\0'};
puts("in io cb\n");
read(STDIN_FILENO,buf,sizeof(buf));
buf[1023] = '\0';
printf("Read in a string %s \n",buf);
ev_io_stop(main_loop,io_w);
}
static void timer_action(struct ev_loop *main_loop,ev_timer *timer_w,int e)
{
puts("in tiemr cb \n");
ev_timer_stop(main_loop,timer_w);
}
static void signal_action(struct ev_loop *main_loop,ev_signal signal_w,int e)
{
puts("in signal cb \n");
ev_signal_stop(main_loop,signal_w);
ev_break(main_loop,EVBREAK_ALL);
}
int main(int argc ,char *argv[])
{
struct ev_loop *main_loop = ev_default_loop(0);
ev_init(&io_w,io_action);
ev_io_set(&io_w,STDIN_FILENO,EV_READ);
ev_init(&timer_w,timer_action);
ev_timer_set(&timer_w,2,0);
ev_init(&signal_w,signal_action);
ev_signal_set(&signal_w,SIGINT);
ev_io_start(main_loop,&io_w);
ev_timer_start(main_loop,&timer_w);
ev_signal_start(main_loop,&signal_w);
ev_run(main_loop,0);
return 0;
}
三、常用函数
-
ev_loop构建
我们默认使用EV_DEFAULT
类型的loop,使用一下语句来创建:
struct ev_loop *loop = EV_DEFAULT;
其中EV_DEFAULT
宏的定义:ev_default_loop(EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV)
返回一个最基础的ev_loop
,并自动完成它的初始化,注意,如果程序中已经执行过该创建,将直接返回之前的创建。除此之外,更多自定义loop,可以使用该函数:struct ev_loop*ev_loop_new (unsigned int flags)
让ev_loop run起来
使用函数ev_run(loop, int flags)
flags
的作用:用于设置ev_loop
的运行方式,通常设置为0
,表示该ev_loop
在所有watcher结束后停止,也可以手动break,官方鼓励手动break。除了0之外,还有一些选择,如EVRUN_NOWAIT、EVRUN_ONCE
(详见官方文档)。ev_loop的停止
如上所述在flags
设置为0的情况下,停止主要靠全部watcher停止或者手动break(ev_break (loop,how)
)。
其中how
代表停止方式:
EVBREAK_ONE
:停止最久远的那个ev_run
EVBREAK_ALL
:停止所有的ev_run
ev_TYPE对应不同类型的事件监控,
libev
定义了如下类型的watcher
:
ev_io, ev_timer, ev_signal
, // 基础事件
ev_periodic, ev_child, ev_stat, ev_idle, ev_prepare, ev_check, ev_embed, ev_fork, ev_cleanup, ev_async
共有的标准化函数主要如下:
// 回调函数都是这种类型
typedef void (*)(struct ev_loop *loop, ev_TYPE*watcher, int revents) callback;
// 初始化watcher
ev_init (ev_TYPE *watcher, callback);
// 设置watcher
ev_TYPE_set (ev_TYPE *watcher, [args]);
// 这个函数综合了前两个函数功能
ev_TYPE_init (ev_TYPE *watcher, callback, [args]);
// 在ev_loop中注册watcher
ev_TYPE_start (loop, ev_TYPE *watcher);
// 在ev_loop中注销watcher
ev_TYPE_stop (loop, ev_TYPE *watcher);
// 设置watcher优先级,值域为[-2,2],大而优先
ev_set_priority (ev_TYPE *watcher, int priority);
// 这个做跨线程通知非常有用,相当于触发了某个事件
ev_feed_event (loop, ev_TYPE *watcher, int revents);
// watcher是否active
bool ev_is_active (ev_TYPE *watcher);
// watcher是否pending
bool ev_is_pending (ev_TYPE *watcher);
// 清除watcher pending状态并且返回事件
int ev_clear_pending (loop, ev_TYPE *watcher);
四、libev实现非阻塞网络通信实例
- server端
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ev.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#define PORT 9999
#define BUFFER_SIZE 1024
static int total_clients=0;
static void accept_cb(struct ev_loop *loop,struct ev_io *watcher,int revents)
{
int client_sd;
struct ev_io *w_client = (struct ev_io*)malloc(sizeof(struct ev_io));
if(EV_ERROR & revents){
printf("error event in accept");
return ;
}
client_sd = accept(watcher->fd,NULL,NULL);
if(client_sd < 0){
printf("accept error");
return;
}
total_clients++;
printf("successfully connected with client.\n");
printf("%d client connected .\n",total_clients);
ev_io_init(w_client,read_cb,client_sd,EV_READ);
ev_io_start(loop,w_client);
}
static void read_cb(struct ev_loop *loop,struct ev_io *watcher,int revents)
{
int read;
char buffer[BUFFER_SIZE];
if(EV_ERROR & revents){
printf("error event in read");
return;
}
read = recv(watcher->fd,buffer,BUFFER_SIZE,0);
if(read == 0){
ev_io_stop(loop,watcher);
free(watcher);
perror("peer might closing");
total_clients--;
printf("%d client connected .\n",total_clients);
return;
}
else{
buffer[read] = '\0';
printf("get the message: %s\n",buffer);
}
send(watcher->fd,buffer,read,0);
bzero(buffer,read);
}
int main(void)
{
int sd;
struct sockaddr_in addr;
struct ev_io socket_accept;
struct ev_loop *loop=ev_default_loop(0);
if((sd = socket(AF_INET,SOCK_STREAM,0))<0){
printf("socket error");
return -1;
}
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sd,(struct sockaddr*)&addr,sizeof(addr))!=0){
printf("bind error");
}
if(listen(sd,0)<0){
printf("listen error");
return -1;
}
ev_io_init(&socket_accept,accept_cb,sd,EV_READ);
ev_io_start(loop,&socket_accept);
while(1){
ev_loop(loop,0);
}
return 0;
}
- client端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 9999
#define BUFFER_SIZE 1024
int main()
{
int sd;
struct sockaddr_in addr;
char buffer[BUFFER_SIZE];
if((sd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error");
return -1;
}
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(connect(sd,(struct sockaddr *)&addr,sizeof(addr))<0)
{
perror("connect error");
return -1;
}
while(strcmp(buffer,"q")!=0)
{
scanf("%s",buffer);
send(sd,buffer,strlen(buffer),0);
recv(sd,buffer,BUFFER_SIZE,0);
printf("message:%s\n",buffer);
}
return 0;
}