事件库之Libev

2019-10-10  本文已影响0人  炎鸿

一、libev理解

二、libev用法

简单示例

#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;
}

三、常用函数

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)

// 回调函数都是这种类型
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实现非阻塞网络通信实例

#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;
}
#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;
}
上一篇 下一篇

猜你喜欢

热点阅读