|
|
%!s(int64=6) %!d(string=hai) anos | |
|---|---|---|
| base | %!s(int64=6) %!d(string=hai) anos | |
| cert | %!s(int64=6) %!d(string=hai) anos | |
| consul | %!s(int64=6) %!d(string=hai) anos | |
| crc | %!s(int64=6) %!d(string=hai) anos | |
| echo-servers | %!s(int64=6) %!d(string=hai) anos | |
| etc | %!s(int64=6) %!d(string=hai) anos | |
| event | %!s(int64=6) %!d(string=hai) anos | |
| examples | %!s(int64=6) %!d(string=hai) anos | |
| html | %!s(int64=6) %!d(string=hai) anos | |
| http | %!s(int64=6) %!d(string=hai) anos | |
| misc | %!s(int64=6) %!d(string=hai) anos | |
| protocol | %!s(int64=6) %!d(string=hai) anos | |
| scripts | %!s(int64=6) %!d(string=hai) anos | |
| sqlite | %!s(int64=6) %!d(string=hai) anos | |
| unittest | %!s(int64=6) %!d(string=hai) anos | |
| utils | %!s(int64=6) %!d(string=hai) anos | |
| winbuild | %!s(int64=6) %!d(string=hai) anos | |
| .clang-format | %!s(int64=6) %!d(string=hai) anos | |
| .gitignore | %!s(int64=6) %!d(string=hai) anos | |
| .travis.yml | %!s(int64=6) %!d(string=hai) anos | |
| Makefile | %!s(int64=6) %!d(string=hai) anos | |
| Makefile.in | %!s(int64=6) %!d(string=hai) anos | |
| Makefile.vars | %!s(int64=6) %!d(string=hai) anos | |
| README.md | %!s(int64=6) %!d(string=hai) anos | |
| configure | %!s(int64=6) %!d(string=hai) anos | |
| hconfig.h | %!s(int64=6) %!d(string=hai) anos | |
| hv.h | %!s(int64=6) %!d(string=hai) anos | |
| main.cpp.tmpl | %!s(int64=6) %!d(string=hai) anos |
Like libevent, libev, and libuv,
libhv provides event-loop with non-blocking IO and timer,
but simpler apis and richer protocols.
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)

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
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
libhv QQ群739352073,欢迎加群讨论