Recently, the domestic open source library libhv is awesome-c are included, the sudden emergence of libhv library exactly what best at?
Article Directory
libhv Profile
libhv
It is a similar cross-platform libevent、libev、libuv
, non-blocking IO event loop libraries, but offers a simpler and richer API interface protocols (including http、ftp、smtp、dns、icmp
, etc.).
libhv
It has been widely practical in the company's IoT
platform, HTTP API
in service, accuracy, stability, scalability and performance are guaranteed, fully open source, ease of use.
Project Address: https://github.com/ithewei/libhv.git
code cloud images: https://gitee.com/ithewei/libhv.git
QQ technical exchange group: 739352073
libhv a school day Bowen: HTTPS: // Hewei .blog.csdn.net / article / details / 103903123
Simpler than libevent, libuv API interface
libhv source directory echo-servers in shows asio、libevent、libev、libhv、libuv、muduo、poco
seven network libraries written example of echo-server.
// libhv 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;
}
Stress test results chart:
can be found on the comparable performance of each library, but libhv interface is the easiest to use.
Embodied in:
- Provide TCP listening port to create a service, do not write socket underlying code (libev, libuv flow basis having the call
socket->bind->listen->accept
); hread_cb
,hio_write
Prototype and system callsread、write
similar, no memory and cognitive load;- IO write abnormal situation, chain scission, and the like to unify
hclose_cb
the process; - Without forcing provide application layer buffer, libhv event loop provides a default ReadBuf (
one loop per thread,one readbuf per loop
), of course, you also can callhio_set_readbuf
to provide their own buffer, to avoid the follow-upmemcpy
;
httpd provide comparable performance libhv nginx
git clone https://github.com/ithewei/libhv.git
cd libhv
make httpd curl
bin/httpd -h
bin/httpd -d
#bin/httpd -c etc/httpd.conf -s restart -d
ps aux | grep httpd
# http web service
bin/curl -v localhost:8080
# http indexof service
bin/curl -v localhost:8080/downloads/
# webbench (linux only)
make webbench
bin/webbench -c 2 -t 60 localhost:8080
libhv draws nginx's master-workers
multi-process model, the number of concurrent throughput even faint than nginx
libhv HTTP API is written in c ++ server / client easiest library
// HTTP API server示例
#include "HttpServer.h"
int http_api_echo(HttpRequest* req, HttpResponse* res) {
res->body = req->body;
return 0;
}
int main() {
HttpService service;
service.base_url = "/v1/api";
service.AddApi("/echo", HTTP_POST, http_api_echo);
http_server_t server;
server.port = 8080;
server.service = &service;
http_server_run(&server);
return 0;
}
See more usage Bowen https://hewei.blog.csdn.net/article/details/104055509
Cross-platform
libhv in Linux、Windows、MacOS
at compile the test by using a different IO multiplexing mechanism under different platforms,linux=>epoll, Windows=>IOCP,MacOS=>kqueue
libhv module division clear, high readability, it is worth learning for beginners
data structure
- array.h: Dynamic Array
- list.h: list
- queue.h: Queue
- heap.h: Heap
base
- hplatform.h: platform-specific macros
- hdef.h: macro definitions
- hversion.h: version
- hbase.h: Basic Interface
- hsysinfo.h: System Information
- hproc.h: child process / thread class
- hmath.h: math spread function
- htime.h: Time
- herr.h: error code
- hlog.h: Logs
- hmutex.h: Genlock
- hthread.h: Thread
- hsocket.h: socket operation
- hbuf.h: cache class
- hurl.h: URL escaped
- hgui.h: gui related definitions
- hstring.h: String
- hvar.h: var variables
- hobj.h: Object base class
- hfile.h: File Class
- hdir.h: ls achieve
- hscope.h: Scope RAII mechanism
- hthreadpool.h: Thread Pool
- hobjectpool.h: object pool
- ifconfig.h: ifconfig实现
utils
- hmain.h: main_ctx: arg env
- hendian.h: the size of the end
- iniparser.h: This 解析
- singleton.h: Singleton
- md5.h
- base64.h
- json.hpp
event
- hloop.h: Event Loop
iowatcher
- EVENT_SELECT
- EVENT_POLL
- EVENT_EPOLL (linux only)
- EVENT_KQUEUE (mac/bsd)
- EVENT_IOCP (windows only)
http
- http_client.h: http client
- HttpServer.h: http server
other
- hv.h: total header file
- Makefile.in: General Makefile template
libhv API list See Bowen https://hewei.blog.csdn.net/article/details/103976875 or source directory https://github.com/ithewei/libhv/tree/master/docs
If you think the library is good, please github on star
the support of it, thanks!