一、介绍
- libevent
- libev
- libuv
- Gear-Lib
- libhv 国产网络库
- 网络库libevent、libev、libuv、libhv对比
- libevent、libev框架介绍
- Libev和LibEvent
- 如果用开源网络库 libevent libev libuv asio muduo ace 选哪个?
- libuv 和 libev的对比
- 跨平台网络通信与服务器框架 acl 3.2.0 发布,acl_cpp 是基于 acl 库的 C++ 库
关注内容:
- 是否跨平台
- 是否简单易用
- 性能、稳定性、资料
- 至少要有如下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 快速上手(简单使用)
下载版本: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
*/
四、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);
// 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;
}