|
|
6 năm trước cách đây | |
|---|---|---|
| base | 6 năm trước cách đây | |
| cert | 6 năm trước cách đây | |
| consul | 6 năm trước cách đây | |
| crc | 6 năm trước cách đây | |
| echo-servers | 6 năm trước cách đây | |
| etc | 6 năm trước cách đây | |
| event | 6 năm trước cách đây | |
| examples | 6 năm trước cách đây | |
| html | 6 năm trước cách đây | |
| http | 6 năm trước cách đây | |
| misc | 6 năm trước cách đây | |
| protocol | 6 năm trước cách đây | |
| scripts | 6 năm trước cách đây | |
| sqlite | 6 năm trước cách đây | |
| unittest | 6 năm trước cách đây | |
| utils | 6 năm trước cách đây | |
| winbuild | 6 năm trước cách đây | |
| .clang-format | 6 năm trước cách đây | |
| .gitignore | 6 năm trước cách đây | |
| .travis.yml | 6 năm trước cách đây | |
| Makefile | 6 năm trước cách đây | |
| Makefile.in | 6 năm trước cách đây | |
| Makefile.vars | 6 năm trước cách đây | |
| README.md | 6 năm trước cách đây | |
| configure | 6 năm trước cách đây | |
| hconfig.h | 6 năm trước cách đây | |
| hv.h | 6 năm trước cách đây | |
| main.cpp.tmpl | 6 năm trước cách đây |
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,欢迎加群讨论