国产开源库libhv为何能被awesome-c收录

近日,国产开源库libhvawesome-c所收录,这个异军突起的libhv库究竟有何出色之处?

libhv简介

libhv是一个跨平台的类似libevent、libev、libuv的非阻塞IO事件循环库,但提供了更加简单的API接口和更加丰富的协议(包括http、ftp、smtp、dns、icmp等)。
libhv已广泛实用在公司的IoT平台、HTTP API服务之中,正确性、稳定性、可扩展性、性能都有保证,完全开源,请放心使用。

项目地址:https://github.com/ithewei/libhv.git
码云镜像:https://gitee.com/ithewei/libhv.git
QQ技术交流群:739352073
libhv每日一学博文:https://hewei.blog.csdn.net/article/details/103903123

比libevent、libuv更简单的API接口

libhv源码目录echo-servers中展示了asio、libevent、libev、libhv、libuv、muduo、poco7个网络库编写echo-server的示例。

// libhv echo-server示例
#include "hloop.h"

void on_close(hio_t* io) {
}

void on_recv(hio_t* io, void* buf, int readbytes) {
    hio_write(io, buf, readbytes);
}

void on_accept(hio_t* io) {
    hio_setcb_close(io, on_close);
    hio_setcb_read(io, on_recv);
    hio_read(io);
}

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: cmd port\n");
        return -10;
    }
    int port = atoi(argv[1]);

    hloop_t* loop = hloop_new(0);
    hio_t* listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
    if (listenio == NULL) {
        return -20;
    }
    hloop_run(loop);
    hloop_free(&loop);
    return 0;
}

压力测试结果图:
echo-servers
可以发现各库的性能上不相上下,但libhv的接口是最简单易用的。
具体体现在:

  • 提供监听端口即可创建TCP服务,不用写socket底层代码(libev、libuv中需按流程调用socket->bind->listen->accept);
  • hread_cbhio_write原型与系统调用read、write类似,无记忆和认知负担;
  • IO读写异常、断链等情况统一到hclose_cb中处理;
  • 不用强制提供应用层buffer,libhv事件循环中提供了一个默认的readbuf(one loop per thread,one readbuf per loop),当然你也可以调用hio_set_readbuf提供自己的buffer,避免后续的memcpy

libhv提供的httpd性能媲美nginx

git clone https://github.com/ithewei/libhv.git
cd libhv
make httpd curl

bin/httpd -h
bin/httpd -d
#bin/httpd -c etc/httpd.conf -s restart -d
ps aux | grep httpd

# http web service
bin/curl -v localhost:8080

# http indexof service
bin/curl -v localhost:8080/downloads/

# webbench (linux only)
make webbench
bin/webbench -c 2 -t 60 localhost:8080

libhv-vs-nginx
libhv借鉴了nginx的master-workers多进程模型,并发数、吞吐量甚至隐隐超过nginx

libhv是c++编写HTTP API服务端/客户端最简单的库

// HTTP API server示例
#include "HttpServer.h"

int http_api_echo(HttpRequest* req, HttpResponse* res) {
    res->body = req->body;
    return 0;
}

int main() {
    HttpService service;
    service.base_url = "/v1/api";
    service.AddApi("/echo", HTTP_POST, http_api_echo);

    http_server_t server;
    server.port = 8080;
    server.service = &service;
    http_server_run(&server);
    return 0;
}

更多用法见博文https://hewei.blog.csdn.net/article/details/104055509

跨平台

libhv在Linux、Windows、MacOS下编译测试通过,在不同的平台下使用了不同的IO多路复用机制,linux=>epoll, Windows=>IOCP,MacOS=>kqueue

libhv模块划分清晰,代码可读性高,值得初学者学习

数据结构

  • array.h: 动态数组
  • list.h: 链表
  • queue.h: 队列
  • heap.h: 堆

base

  • hplatform.h: 平台相关宏
  • hdef.h: 宏定义
  • hversion.h: 版本
  • hbase.h: 基本接口
  • hsysinfo.h: 系统信息
  • hproc.h: 子进程/线程类
  • hmath.h: math扩展函数
  • htime.h: 时间
  • herr.h: 错误码
  • hlog.h: 日志
  • hmutex.h: 同步锁
  • hthread.h: 线程
  • hsocket.h: socket操作
  • hbuf.h: 缓存类
  • hurl.h: URL转义
  • hgui.h: gui相关定义
  • hstring.h: 字符串
  • hvar.h: var变量
  • hobj.h: 对象基类
  • hfile.h: 文件类
  • hdir.h: ls实现
  • hscope.h: 作用域RAII机制
  • hthreadpool.h: 线程池
  • hobjectpool.h: 对象池
  • ifconfig.h: ifconfig实现

utils

  • hmain.h: main_ctx: arg env
  • hendian.h: 大小端
  • iniparser.h: ini解析
  • singleton.h: 单例模式
  • md5.h
  • base64.h
  • json.hpp

event

  • hloop.h: 事件循环

iowatcher

  • EVENT_SELECT
  • EVENT_POLL
  • EVENT_EPOLL (linux only)
  • EVENT_KQUEUE (mac/bsd)
  • EVENT_IOCP (windows only)

http

  • http_client.h: http客户端
  • HttpServer.h: http服务端

其它

  • hv.h: 总头文件
  • Makefile.in: 通用Makefile模板

libhv API列表见博文https://hewei.blog.csdn.net/article/details/103976875或源码目录https://github.com/ithewei/libhv/tree/master/docs

如果你觉得该库不错,请githubstar支持下吧,感谢!

发布了130 篇原创文章 · 获赞 146 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/GG_SiMiDa/article/details/104920336