|
|
6 yıl önce | |
|---|---|---|
| base | 6 yıl önce | |
| cert | 6 yıl önce | |
| consul | 6 yıl önce | |
| crc | 6 yıl önce | |
| echo-servers | 6 yıl önce | |
| etc | 6 yıl önce | |
| event | 6 yıl önce | |
| examples | 6 yıl önce | |
| html | 6 yıl önce | |
| http | 6 yıl önce | |
| misc | 6 yıl önce | |
| protocol | 6 yıl önce | |
| scripts | 6 yıl önce | |
| sqlite | 6 yıl önce | |
| unittest | 6 yıl önce | |
| utils | 6 yıl önce | |
| winbuild | 6 yıl önce | |
| .clang-format | 6 yıl önce | |
| .gitignore | 6 yıl önce | |
| .travis.yml | 6 yıl önce | |
| LICENSE | 6 yıl önce | |
| Makefile | 6 yıl önce | |
| Makefile.in | 6 yıl önce | |
| Makefile.vars | 6 yıl önce | |
| README.md | 6 yıl önce | |
| configure | 6 yıl önce | |
| hconfig.h | 6 yıl önce | |
| hv.h | 6 yıl önce | |
| main.cpp.tmpl | 6 yıl önce |
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 localhost:8080/v1/api/hello
bin/curl -v localhost:8080/v1/api/echo -d "hello,world!"
bin/curl -v localhost:8080/v1/api/query?page_no=1&page_size=10
bin/curl -v localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/v1/api/kv -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/v1/api/mp -F "file=@LICENSE"
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/v1/api/group/test/user/123
# 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
libhv combines OpenSSL perfectly, something almost all asynchronous IO network libraries don't do. And enable SSL in libhv is so easy, just only two apis:
int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file); // init global SSL_CTX, see
base/ssl_ctx.h
int hio_enable_ssl(hio_t* io); // enable ssl, see event/hloop.h
https is the best example.
sudo apt-get install openssl libssl-dev # ubuntu
make clean
make libhv httpd curl DEFINES=WITH_OPENSSL
# editor etc/httpd.conf => ssl = on
bin/httpd -d
bin/curl -v https://localhost:8080
curl -v https://localhost:8080 --insecure
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
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.
739352073,欢迎加群讨论