Không có mô tả

hewei 813b82ac76 add LICENSE 6 năm trước cách đây
base cd40a89b2e update 6 năm trước cách đây
cert 7f2ba3920f add WITH_OPENSSL WITH_CURL 6 năm trước cách đây
consul 3f1526c38f update 6 năm trước cách đây
crc 7be28a2805 add crc sqlite 6 năm trước cách đây
echo-servers f83d8fde98 echo-servers/benchmark 6 năm trước cách đây
etc c14ace0cf2 master_workers_run 6 năm trước cách đây
event f83d8fde98 echo-servers/benchmark 6 năm trước cách đây
examples 9219fdbd8c hloop_s add readbuf 6 năm trước cách đây
html f99101266a update 6 năm trước cách đây
http c14ace0cf2 master_workers_run 6 năm trước cách đây
misc c460e45491 update 6 năm trước cách đây
protocol a287e5317e trim 6 năm trước cách đây
scripts 61dbcafc66 add module: consul 6 năm trước cách đây
sqlite c6ae3cbcf2 update libhv 6 năm trước cách đây
unittest 9219fdbd8c hloop_s add readbuf 6 năm trước cách đây
utils c14ace0cf2 master_workers_run 6 năm trước cách đây
winbuild 6dfe4b1aaa update 6 năm trước cách đây
.clang-format c6ae3cbcf2 update libhv 6 năm trước cách đây
.gitignore 61dbcafc66 add module: consul 6 năm trước cách đây
.travis.yml cd8dfbb4b2 update 6 năm trước cách đây
LICENSE 813b82ac76 add LICENSE 6 năm trước cách đây
Makefile 8caf37bfda gmtime_fmt 6 năm trước cách đây
Makefile.in 903b18b379 update 6 năm trước cách đây
Makefile.vars 903b18b379 update 6 năm trước cách đây
README.md 12c06b145f update 6 năm trước cách đây
configure c6ae3cbcf2 update libhv 6 năm trước cách đây
hconfig.h c6ae3cbcf2 update libhv 6 năm trước cách đây
hv.h c6ae3cbcf2 update libhv 6 năm trước cách đây
main.cpp.tmpl c14ace0cf2 master_workers_run 6 năm trước cách đây

README.md

Build Status

Intro

Like libevent, libev, and libuv, libhv provides event-loop with non-blocking IO and timer, but simpler apis and richer protocols.

Features

  • cross-platform (Linux, Windows, Mac)
  • event-loop (IO, timer, idle)
  • http client/server (include https http1/x http2 grpc)
  • protocols
    • dns
    • ftp
    • smtp
  • apps
    • ls
    • ifconfig
    • ping
    • nc
    • nmap
    • nslookup
    • ftp
    • sendmail
    • httpd
    • curl

Getting Started

http server

see examples/httpd.cpp

#include "HttpServer.h"

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

int main() {
    HttpService service;
    service.base_url = "/v1/api";
    service.AddApi("/hello", HTTP_GET, http_api_hello);

    http_server_t server;
    server.port = 8080;
    server.worker_processes = 4;
    server.service = &service;
    http_server_run(&server);
    return 0;
}
git clone https://github.com/ithewei/libhv.git
cd libhv
make httpd curl

bin/httpd -s restart -d
ps aux | grep httpd

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

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

# http api service
bin/curl -v -X POST localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'

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

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

EventLoop

see examples/tcp.c examples/udp.c

// TCP 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;
}
make tcp udp nc
bin/tcp 1111
bin/nc 127.0.0.1 1111

bin/udp 2222
bin/nc -u 127.0.0.1 2222

BUILD

lib

  • make libhv
  • sudo make install

examples

  • make test # master-workers model
  • make timer # timer add/del/reset
  • make loop # event-loop(include idle, timer, io)
  • make tcp # tcp server
  • make udp # udp server
  • make nc # network client
  • make nmap # host discovery
  • make httpd # http server
  • make curl # http client

unittest

  • make unittest

compile options

compile with print debug info

  • make DEFINES=PRINT_DEBUG

compile WITH_OPENSSL

  • make DEFINES=WITH_OPENSSL

compile WITH_CURL

  • make DEFINES="WITH_CURL CURL_STATICLIB"

compile WITH_NGHTTP2

  • make DEFINES=WITH_NGHTTP2

other options

  • ENABLE_IPV6
  • WITH_WINDUMP
  • USE_MULTIMAP

echo-servers

make libhv
make webbench

# ubuntu16.04
sudo apt-get install libevent-dev libev-dev libuv1-dev libboost-dev libasio-dev libpoco-dev
# muduo install => https://github.com/chenshuo/muduo.git
make echo-servers
sudo echo-servers/benchmark.sh

echo-servers/benchmark
echo-servers

Note: The client and servers are located in the same computer, the results are random, for reference only. In general, the performance of these libraries are similar, each has its own advantages.

Module

data-structure

  • 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: 对象池

utils

  • hmain.h: main_ctx: arg env
  • hendian.h: 大小端
  • ifconfig.h: ifconfig实现
  • 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服务端

other

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

学习资料