01. 介绍与体验
名称由来
libhv
是一个类似于libevent、libev、libuv
的跨平台网络库,提供了带非阻塞IO和定时器的事件循环。 libhv的名称也正是继承此派,寓意高性能的事件循环High-performance event loop library
。
libhv能干什么
- 编写跨平台c/c++程序;
- 基于TCP/UDP/SSL开发自定义协议网络程序;
- 编写HTTP客户端/服务端程序;
- 编写WebSocket客户端/服务端程序;
- 学习实践网络编程;
libhv和libevent、libev、libuv有什么不同
- libevent
- 最为古老、有历史包袱,bufferevent虽为精妙,却也难以上手;
- libev
- 可以说是libevent的简化版,代码极为精简,但宏定义用的过多,代码可读性不强,且在Windows上实现不佳;
- libuv
- 是nodejs的c底层库,最先也是由libevent+对Windows IOCP支持,后来才改写自成一体,同时实现了管道、文件的异步读写,很强大,但结构体比较多,封装比较深;
- libhv
- 本身是参考了libevent、libev、libuv的实现思路,它们的核心都是事件循环(即在一个事件循环中处理IO、定时器等事件),但提供的接口最为精简,API接近原生系统调用,最容易上手;
- 总结
- 具体这几个库的写法比较见echo-servers,可见libhv是最简单的;
- 此外libhv支持心跳、转发、拆包、多线程安全write和close等特性,提供了HTTP、WebSocket等协议实现;
- 当然这几个库的性能是接近的,都将非阻塞IO多路复用用到了极致;
体验
linux与mac下的用户可直接执行./getting_started.sh
脚本,即可体验使用libhv编写的http服务端httpd
与http客户端curl
的便利之处。
运行效果如下:
$ ./getting_started.sh
Welcome to libhv!
Press any key to run ...
bin/httpd -c etc/httpd.conf -s restart -d
httpd start/running
hw 83110 0.0 0.0 5100160 588 ?? S 4:14下午 0:00.00 httpd: worker process
hw 83109 0.0 0.0 4951680 580 ?? S 4:14下午 0:00.00 httpd: worker process
hw 83108 0.0 0.0 4820608 572 ?? S 4:14下午 0:00.00 httpd: worker process
hw 83107 0.0 0.0 4689536 600 ?? S 4:14下午 0:00.00 httpd: worker process
hw 83103 0.0 0.0 4950656 808 ?? Ss 4:14下午 0:00.00 httpd: master process
bin/curl -v localhost:8080
GET / HTTP/1.1
Accept: */*
Connection: keep-alive
Content-Length: 0
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 130
Content-Type: text/html
Date: Fri, 05 Feb 2021 08:14:14 GMT
Etag: "5fc1057e-82"
Last-Modified: Fri, 27 Nov 2020 13:56:14 GMT
Server: httpd/1.21.2.5
<!DOCTYPE html>
<html>
<head>
<title>httpd</title>
</head>
<body>
<center><h1>Welcome to httpd!</h1></center>
</body>
</html>
httpd与curl代码均可在examples目录下找到,是完整的命令行程序。
$ bin/httpd -h
Usage: httpd [hvc:ts:dp:]
Options:
-h|--help Print this information
-v|--version Print version
-c|--confile <confile> Set configure file, default etc/{program}.conf
-t|--test Test configure file and exit
-s|--signal <signal> Send <signal> to process,
<signal>=[start,stop,restart,status,reload]
-d|--daemon Daemonize
-p|--port <port> Set listen port
$ bin/curl -h
Usage: curl [hVvX:H:d:F:n:] url
Options:
-h|--help Print this message.
-V|--version Print version.
-v|--verbose Show verbose infomation.
-X|--method Set http method.
-H|--header Add http headers, -H "Content-Type:application/json Accept:*/*"
-d|--data Set http body.
-F|--form Set http form, -F "name1=content;name2=@filename"
-n|--count Send request count, used for test keep-alive
--http2 Use http2
--grpc Use grpc over http2
Examples:
curl -v localhost:8080
curl -v localhost:8080/hello
curl -v localhost:8080/query?page_no=1&page_size=10
curl -v localhost:8080/echo -d 'hello,world!'
curl -v localhost:8080/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
curl -v localhost:8080/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
curl -v localhost:8080/form -F 'file=@filename'
当然你也可以通过浏览器访问,地址栏输入:
- 127.0.0.1:8080/
- 127.0.0.1:8080/downloads/
- 127.0.0.1:8080/ping
链接到当前文件 0
没有文件链接到当前文件