当前位置:   article > 正文

libevent、libev等网络库介绍_gear-lib

gear-lib

一、介绍

关注内容:

  • 是否跨平台
  • 是否简单易用
  • 性能、稳定性、资料
  • 至少要有如下demo:TCP Server/Clinet、UDP Server/Client、定时器、线程池

二、libevent

疑问:具备哪些功能、核心原理、是否跨平台、是否线程安全、demo

Libevent 是一个用C语言编写的、轻量级的开源高性能事件通知库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大;源代码相当精炼、易读;跨平台,支持 Windows、 Linux、 *BSD 和 Mac Os;支持多种 I/O 多路复用技术, epoll、 poll、 dev/poll、 select 和 kqueue 等;支持 I/O,定时器和信号等事件;注册事件优先级

其他资料

libevent 快速上手(简单使用)

原文:Libevent使用例子,从简单到复杂

下载版本:2.1.12-stable

系统环境:Ubuntu 20.04 (win10 linux 子系统)

编译方式:cmake

编译报错:没指定 openssl 库路径 (https://www.cnblogs.com/pandamohist/p/14227440.html

三、libev

其他内容:

  • 个人开发者维护、当前貌似已停止维护
  • 问题:未掌握核心

典型watcher有: ev_io(IO可读可写观察器),ev_signal(信号处理观察器),ev_timer(定时器),
ev_periodic(周期任务处理),ev_child(子进程状态变化观察器),ev_stat(文件属性变化观察器)。
ev_fork(创建的进程时的观察器),ev_async(异步调用观察器),ev_cleanup(event
loop退出时触发事件),ev_prepare(每次event loop之前事件),ev_check(每次event
loop之后事件),ev_idle(每次event loop空闲触发事件)

1. ev_timer

ev_timer_init(ev,cb,after,repeat); 参数说明:
     ev:ev_timer结构
     cb:超时回调函数
     after:多少秒之后调用回调函数
     repeat:第一次触发回调后每多少秒再次重复触发回调,0-执行一次

其他特性

  • 若上次执行未结束,到下个周期后不会执行(实践会超周期时间)
  • 若设定周期时间5秒,回调执行3秒,内部会自动适配回调耗时时间。(会缩短休眠时间来满足重复间隔)
  • 秒级定时器
  • 是否受校时影响?
  • List item
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/prctl.h>      //for prctl

#include <string.h>
#include <time.h>
#include <ev.h>

static void ev_timer_cb (struct ev_loop *loop, ev_timer *w, int revents)
{
    prctl(PR_SET_NAME, "libev_ev_timer");
    time_t now = time(NULL);
    printf("do_timer %s ", (char *) ctime(&now));
    sleep(3);
    // ev_timer_stop(loop, w);  //达到某种条件退出回调
}
void test_ev_timer()
{
    struct ev_loop *loop = EV_DEFAULT;
    ev_timer t1;
    ev_timer_init (&t1, ev_timer_cb, 2, 5.);    //创建2秒后开始执行、每1秒执行一次的定时器.
    ev_timer_start (loop, &t1);
    // now wait for events to arrive
    ev_run (loop, 0);
}

int main() {
    //ev_timer
    test_ev_timer();
    return 0;
}
/*
gcc mydemo.c -I../include -L. -lev
*/
  • 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

四、libhv

五、gear-lib 和 libgevent

注意事项

  • 使用比较简单,若只使用libgevent仅需包含几个.h和.c原文件即可。
    • IO检测和读写
    • 定时器
  • 使用者比较少,几乎多年没更新。
# 只能绑定C接口(C++要中间转换下)
struct gevent_base *base = gevent_base_create()
struct gevent *event = gevent_create(fd, ev_in, NULL, NULL, arg);
gevent_add(base , &event);
gevent_base_loop_start(base);
  • 1
  • 2
  • 3
  • 4
  • 5

// test_libgevent.c

/******************************************************************************
 * Copyright (C) 2014-2018 Zhifeng Gong <gozfree@163.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libraries; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 ******************************************************************************/
#include "libgevent.h"
#include <liblog.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#include <signal.h>

struct gevent_base *evbase = NULL;

static void on_input(int fd, void *arg)
{
    logi("on_input fd = %d\n", fd);
}

static int foo(void)
{
    evbase = gevent_base_create();
    if (!evbase) {
        printf("gevent_base_create failed!\n");
        return -1;
    }
    struct gevent *event_2000 = gevent_timer_create(2000, TIMER_PERSIST, on_input, NULL);
    if (!event_2000) {
        printf("gevent_create failed!\n");
        return -1;
    }
    struct gevent *event_1500 = gevent_timer_create(1500, TIMER_PERSIST, on_input, NULL);
    if (!event_1500) {
        printf("gevent_create failed!\n");
        return -1;
    }
    if (-1 == gevent_add(evbase, event_2000)) {
        printf("gevent_add failed!\n");
        return -1;
    }
    if (-1 == gevent_add(evbase, event_1500)) {
        printf("gevent_add failed!\n");
        return -1;
    }
    gevent_base_loop(evbase);
    gevent_del(evbase, event_1500);
    gevent_del(evbase, event_2000);
    gevent_base_destroy(evbase);

    return 0;
}

static void sigint_handler(int sig)
{
    gevent_base_loop_break(evbase);
}

void signal_init()
{
    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, sigint_handler);
}

int main(int argc, char **argv)
{
    signal_init();
    foo();
    printf("xxx\n");
    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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/742795
推荐阅读
相关标签
  

闽ICP备14008679号