1
0

Тайлбар байхгүй

hewei.it b457fef203 http_async_handler(HttpRequestPtr, HttpResponseWriterPtr) 4 жил өмнө
base 3e15658ee2 sleep => hv_sleep 4 жил өмнө
cert 7f2ba3920f add WITH_OPENSSL WITH_CURL 6 жил өмнө
cmake fd52ad0e13 move cpp to cpputil, let base event pure c 4 жил өмнө
consul 0b6e86d208 consul support ipv6 4 жил өмнө
cpputil 3e15658ee2 sleep => hv_sleep 4 жил өмнө
docs 3e15658ee2 sleep => hv_sleep 4 жил өмнө
echo-servers ce483e03a0 fix bugs 4 жил өмнө
etc 7b2bdb200d http server support both http and https 4 жил өмнө
event 5e003585a3 HV_EXPORT hio_getcb_xxx 4 жил өмнө
evpp 5e003585a3 HV_EXPORT hio_getcb_xxx 4 жил өмнө
examples 3e15658ee2 sleep => hv_sleep 4 жил өмнө
html fd52ad0e13 move cpp to cpputil, let base event pure c 4 жил өмнө
http b457fef203 http_async_handler(HttpRequestPtr, HttpResponseWriterPtr) 4 жил өмнө
misc c460e45491 update 6 жил өмнө
protocol 3e15658ee2 sleep => hv_sleep 4 жил өмнө
scripts 30317590c7 ./configure --option 4 жил өмнө
unittest 3e15658ee2 sleep => hv_sleep 4 жил өмнө
util fd52ad0e13 move cpp to cpputil, let base event pure c 4 жил өмнө
.clang-format f085d888f0 update 4 жил өмнө
.gitattributes a91aad6f5e update 4 жил өмнө
.gitignore 30317590c7 ./configure --option 4 жил өмнө
.travis.yml a12be753c2 make evpp 4 жил өмнө
BUILD.md 4fc08fe253 ./configure --with-openssl 4 жил өмнө
CMakeLists.txt fd52ad0e13 move cpp to cpputil, let base event pure c 4 жил өмнө
LICENSE 813b82ac76 add LICENSE 6 жил өмнө
Makefile 9ce06f59f9 ./configure --prefix=PREFIX --incdir=INSTALL_INCDIR --libdir=INSTALL_LIBDIR 4 жил өмнө
Makefile.in fd52ad0e13 move cpp to cpputil, let base event pure c 4 жил өмнө
Makefile.vars 9ce06f59f9 ./configure --prefix=PREFIX --incdir=INSTALL_INCDIR --libdir=INSTALL_LIBDIR 4 жил өмнө
README-CN.md a5eada9162 Add user case: hvloop 4 жил өмнө
README.md 238eff8190 update 4 жил өмнө
TREE.md fd52ad0e13 move cpp to cpputil, let base event pure c 4 жил өмнө
config.ini 9ce06f59f9 ./configure --prefix=PREFIX --incdir=INSTALL_INCDIR --libdir=INSTALL_LIBDIR 4 жил өмнө
config.mk 9ce06f59f9 ./configure --prefix=PREFIX --incdir=INSTALL_INCDIR --libdir=INSTALL_LIBDIR 4 жил өмнө
configure 737e901072 update 4 жил өмнө
getting_started.sh 238eff8190 update 4 жил өмнө
hconfig.h e037e6b6d8 EVPP_HEADERS, check_option, benchmark 4 жил өмнө
hconfig.h.in e037e6b6d8 EVPP_HEADERS, check_option, benchmark 4 жил өмнө
hexport.h 27cfd65570 update 5 жил өмнө
hv.h a969840bce rm ununsed files 5 жил өмнө

README-CN.md

English | 中文

libhv

Latest Version Build Status Platform

libhv是一个类似于libevent、libev、libuv的跨平台网络库,提供了更简单的接口和更丰富的协议。

✨ 特征

  • 跨平台(Linux, Windows, Mac, Solaris)
  • 高性能事件循环(网络IO事件、定时器事件、空闲事件)
  • TCP/UDP服务端/客户端/代理
  • SSL/TLS加密通信(WITH_OPENSSL or WITH_MBEDTLS)
  • HTTP服务端/客户端(https http1/x http2 grpc)
  • HTTP文件服务、目录服务、API服务(支持RESTful)
  • WebSocket服务端/客户端

⌛️ 构建

BUILD.md

libhv提供了以下构建方式:

1、通过Makefile:

./configure
make
sudo make install

2、通过cmake:

mkdir build
cd build
cmake ..
cmake --build .

3、通过vcpkg:

vcpkg install libhv

4、通过xmake:

xrepo install libhv

⚡️ 入门与体验

运行脚本./getting_started.sh:

# 下载编译
git clone https://github.com/ithewei/libhv.git
cd libhv
make

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

# 文件服务
bin/curl -v localhost:8080

# 目录服务
bin/curl -v localhost:8080/downloads/

# API服务
bin/curl -v localhost:8080/ping
bin/curl -v localhost:8080/echo -d "hello,world!"
bin/curl -v localhost:8080/query?page_no=1\&page_size=10
bin/curl -v localhost:8080/kv   -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
bin/curl -v localhost:8080/upload -F "file=@LICENSE"

bin/curl -v localhost:8080/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
bin/curl -v localhost:8080/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
bin/curl -v localhost:8080/test -F 'bool=1 int=123 float=3.14 string=hello'
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/group/test/user/123

HTTP

HTTP服务端

examples/http_server_test.cpp

#include "HttpServer.h"

int main() {
    HttpService router;
    router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
        return resp->String("pong");
    });

    router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
        static char data[] = "0123456789";
        return resp->Data(data, 10);
    });

    router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
        return resp->Json(router.Paths());
    });

    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
        resp->content_type = req->content_type;
        resp->body = req->body;
        return 200;
    });

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

HTTP客户端

examples/http_client_test.cpp

#include "requests.h"

int main() {
    auto resp = requests::get("http://www.example.com");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    return 0;
}

HTTP压测

# webbench (linux only)
make webbench
bin/webbench -c 2 -t 10 http://127.0.0.1:8080/
bin/webbench -k -c 2 -t 10 http://127.0.0.1:8080/

# sudo apt install apache2-utils
ab -c 100 -n 100000 http://127.0.0.1:8080/

# sudo apt install wrk
wrk -c 100 -t 4 -d 10s http://127.0.0.1:8080/

libhv(port:8080) vs nginx(port:80) libhv-vs-nginx.png

🍭 示例

c版本

c++版本

🥇 性能测试

cd echo-servers
./build.sh
./benchmark.sh

吞吐量:

libevent running on port 2001
libev running on port 2002
libuv running on port 2003
libhv running on port 2004
asio running on port 2005
poco running on port 2006

==============2001=====================================
[127.0.0.1:2001] 4 threads 1000 connections run 10s
total readcount=1616761 readbytes=1655563264
throughput = 157 MB/s

==============2002=====================================
[127.0.0.1:2002] 4 threads 1000 connections run 10s
total readcount=2153171 readbytes=2204847104
throughput = 210 MB/s

==============2003=====================================
[127.0.0.1:2003] 4 threads 1000 connections run 10s
total readcount=1599727 readbytes=1638120448
throughput = 156 MB/s

==============2004=====================================
[127.0.0.1:2004] 4 threads 1000 connections run 10s
total readcount=2202271 readbytes=2255125504
throughput = 215 MB/s

==============2005=====================================
[127.0.0.1:2005] 4 threads 1000 connections run 10s
total readcount=1354230 readbytes=1386731520
throughput = 132 MB/s

==============2006=====================================
[127.0.0.1:2006] 4 threads 1000 connections run 10s
total readcount=1699652 readbytes=1740443648
throughput = 165 MB/s

📚 中文资料

💎 用户案例

如果您在使用libhv,欢迎通过PR将信息提交至此列表,让更多的用户了解libhv的实际使用场景,以建立更好的网络生态。

用户 (公司名/项目名/个人联系方式) 案例 (项目简介/业务场景)
阅面科技 猎户AIoT平台设备管理、人脸检测HTTP服务、人脸搜索HTTP服务
socks5-libhv socks5代理
hvloop 类似uvloop的python异步IO事件循环