• Libevent库的介绍以及使用示例


    Chapter1 Libevent库的介绍以及使用示例

    原文链接:https://blog.csdn.net/m0_54355780/article/details/121684841

    1、Libevent概述

    Libevent是一个I/O框架库,具有如下特点:
    ①跨平台支持。
    ②统一事件源。Libevent对I/O事件,信号和定时事件提供统一的处理
    线程安全。Libevent使用libevent_pthreads库来提供线程安全支持
    ④基于Reactor模式实现(即主线程负责事件的产生,其余线程负责对事件的处理)

    2、Libevent使用模型

    首先注册事件,需要有描述符fd,事件以及回调函数fun。然后交给libevent,libevent通过底层封装的I/O复用方法来进行事件循环的检测,最后调用回调函数进行处理

    所以对于用户只需要进行:
    ①调用libevent实例
    ②注册事件
    ③启动事件循环
    对于调用回调函数,这个由与用户之前在注册事件的时候已经写好了回调函数,所以最后通过libevent进行处理

    3、Libevent支持的事件类型

    #define EV_TIMEOUT    //定时事件
    #define EV_READ       //可读事件
    #define EV_WRITE      //可写事件
    #define EV_SIGNAL     //信号事件
    #define EV_PERSIST    //永久事件
    /*边沿触发事件,需要I/O复用系统调用支持,比如epoll*/
    #define EV_ET  
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、示例一:简单使用Libevent注册信号事件以及定时事件

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    void signal_cb(int fd,short event,void* arg)
    {
        if(event&EV_SIGNAL)
        {
            printf("sig=%d\n",fd);
        }
    }
    void timeout_cb(int fd,short event,void* arg)
    {
        if(event&EV_TIMEOUT)
        {
            printf("time out\n");
        }
    }
    int main()
    {
        //调用libevent示例
        struct event_base* base=event_init();
    
        //注册信号事件
        struct event* signal_event=evsignal_new(base,SIGINT,signal_cb,NULL);
        event_add(signal_event,NULL);
        //注册超时事件
        struct timeval tv = {2,0};
        struct event* timeout_event=evtimer_new(base,timeout_cb,NULL);
        event_add(timeout_event,&tv);
    
        //启动事件循环
        event_base_dispatch(base);
    
        //free
        event_free(signal_event);
        event_free(timeout_event);
        event_base_free(base);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    由于上述代码中并没有将注册的事件变为永久事件,因此一次之后就结束了
    所以程序运行结果如下:
    在这里插入图片描述

    5、 示例二:Libevent实现TCP服务器

    服务器端:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    //创建监听套接字
    int socket_init()
    {
        int sockfd=socket(AF_INET,SOCK_STREAM,0);
        if(sockfd==-1)
        {
            return -1;
        }
    
        struct sockaddr_in saddr;
        memset(&saddr,0,sizeof(saddr));
        saddr.sin_family=AF_INET;
        saddr.sin_port=htons(6000);
        saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
        int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
        if(res==-1)
        {
            return -1;
        }
    
        res=listen(sockfd,5);
        if(res==-1)
        {
            return -1;
        }
    
        return sockfd;
    }
    void recv_cb(int fd,short event,void* arg)
    {
        if(event&EV_READ)
        {
            char buff[1024]={0};
            int n=recv(fd,buff,1024,0);
            if(n<=0)
            {
                struct event** p_cev=(struct event**)arg;
                event_free(*p_cev);
                free(p_cev);
                close(fd);
                printf("client close\n");
                return ;
            }
            printf("recv:%s\n",buff);
            send(fd,"ok",2,0);
        }
    }
    void accept_cb(int fd,short event,void* arg)
    {
        struct event_base* base=(struct event_base*)arg;
        
        if(event&EV_READ)
        {
            struct sockaddr_in caddr;
            int len=sizeof(caddr);
            int c=accept(fd,(struct sockaddr*)&caddr,&len);
            if(c<0)
            {
                return ;
            }
            printf("accept c=%d\n",c);
    
            struct event** p_cev=(struct event**)malloc(sizeof(struct event*));
            if(p_cev==NULL)
            {
                return ;
            }
            *p_cev=event_new(base,c,EV_READ|EV_PERSIST,recv_cb,p_cev);
            if(*p_cev==NULL)
            {
                close(c);
                return ;
            }
            
            event_add(*p_cev,NULL);
        }
    }
    int main()
    {
        struct event_base* base=event_init();
    
        int sockfd=socket_init();
        assert(sockfd!=-1);
    
        struct event* sock_ev=event_new(base,sockfd,EV_READ|EV_PERSIST,accept_cb,base);
        event_add(sock_ev,NULL);
    
        event_base_dispatch(base);
    
        event_free(sock_ev);
        event_base_free(base);
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    客户端:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    int main()
    {
        int sockfd=socket(AF_INET,SOCK_STREAM,0);
        assert(sockfd!=-1);
    
        struct sockaddr_in saddr;
        memset(&saddr,0,sizeof(saddr));
        saddr.sin_family=AF_INET;
        saddr.sin_port=htons(6000);
        saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    
        int res=connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
        assert(res!=-1);
    
        while(1)
        {
            printf("please input:\n");
            char buff[1024]={0};
            fgets(buff,1024,stdin);
            if(strncmp(buff,"end",3)==0)
            {
                break;
            }
            int n=send(sockfd,buff,strlen(buff),0);
            if(n<=0)
            {
                printf("send error\n");
                break;
            }
            memset(buff,0,1024);
            n=recv(sockfd,buff,1024,0);
            if(n<=0)
            {
                printf("recv error\n");
            }
            printf("buff=%s\n",buff);
        }
        close(sockfd);
        exit(0);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    程序运行结果:
    在这里插入图片描述

    Chapter2 Libevent库的介绍与应用

    https://blog.csdn.net/XXXTENTAC1ON/article/details/122280764

    Chapter2 Libevent学习

    https://blog.csdn.net/qq_50615776/article/details/130874522?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-130874522-blog-122280764.235%5Ev38%5Epc_relevant_sort_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-130874522-blog-122280764.235%5Ev38%5Epc_relevant_sort_base3&utm_relevant_index=2

  • 相关阅读:
    UsernameAuthenticationFilter授权成功后调用AuthenticationSuccessHandler时的解析
    大型 APP 的性能优化思路
    SQL注入类型判断
    HDMI接口含义
    软件测试的调用接口怎么调用,逻辑是什么?
    博流RISC-V芯片BL616开发环境搭建
    小型功率放大器的设计与制作——功率放大电路的关键问题
    vite如何兼容低版本浏览器
    【python】python面向对象——类的使用
    动态整数多重集的高效实现与操作
  • 原文地址:https://blog.csdn.net/m0_46577050/article/details/133656111