Like libevent and libuv, libhv provides event-loop with non-blocking IO and timer, but simpler api and richer protocols.

Overview

Build Status Platform

中文版

Intro

Like libevent, libev, and libuv, libhv provides event-loop with non-blocking IO and timer, but simpler api and richer protocols.

Features

  • cross-platform (Linux, Windows, Mac, Solaris)
  • event-loop (IO, timer, idle)
  • ENABLE_IPV6
  • ENABLE_UDS (Unix Domain Socket)
  • WITH_OPENSSL or WITH_MBEDTLS
  • http client/server (include https http1/x http2 grpc)
  • http web service, indexof service, api service (support RESTful API)
  • protocols
    • dns
    • ftp
    • smtp
  • apps
    • ifconfig
    • ping
    • nc
    • nmap
    • nslookup
    • ftp
    • sendmail
    • httpd
    • curl

Getting Started

./getting_started.sh

HTTP

http server

see examples/http_server_test.cpp

#include "HttpServer.h"

int main() {
    HttpService router;
    router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
        return resp->String("pong");
    });

    router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
        static char data[] = "0123456789";
        return resp->Data(data, 10);
    });

    router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
        return resp->Json(router.Paths());
    });

    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
        resp->content_type = req->content_type;
        resp->body = req->body;
        return 200;
    });

    http_server_t server;
    server.port = 8080;
    server.service = &router;
    http_server_run(&server);
    return 0;
}

http client

see examples/http_client_test.cpp

#include "requests.h"

int main() {
    auto resp = requests::get("http://www.example.com");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    auto resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
    if (resp == NULL) {
        printf("request failed!\n");
    } else {
        printf("%d %s\r\n", resp->status_code, resp->status_message());
        printf("%s\n", resp->body.c_str());
    }

    return 0;
}

httpd/curl

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/

# http api service
bin/curl -v localhost:8080/ping
bin/curl -v localhost:8080/echo -d "hello,world!"
bin/curl -v localhost:8080/query?page_no=1\&page_size=10
bin/curl -v localhost:8080/kv   -H "Content-Type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
bin/curl -v localhost:8080/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
bin/curl -v localhost:8080/upload -F "[email protected]"

bin/curl -v localhost:8080/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
bin/curl -v localhost:8080/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
bin/curl -v localhost:8080/test -F 'bool=1 int=123 float=3.14 string=hello'
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/group/test/user/123

benchmark

# webbench (linux only)
make webbench
bin/webbench -c 2 -t 10 http://127.0.0.1:8080/
bin/webbench -k -c 2 -t 10 http://127.0.0.1:8080/

# sudo apt install apache2-utils
ab -c 100 -n 100000 http://127.0.0.1:8080/

# sudo apt install wrk
wrk -c 100 -t 4 -d 10s http://127.0.0.1:8080/

libhv(port:8080) vs nginx(port:80) libhv-vs-nginx.png

EventLoop

see examples/tcp_echo_server.c examples/udp_echo_server.c examples/nc.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 = hloop_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 examples

bin/tcp_echo_server 1234
bin/nc 127.0.0.1 1234

bin/tcp_chat_server 1234
bin/nc 127.0.0.1 1234
bin/nc 127.0.0.1 1234

bin/httpd -s restart -d
bin/tcp_proxy_server 1234 127.0.0.1:8080
bin/curl -v 127.0.0.1:8080
bin/curl -v 127.0.0.1:1234

bin/udp_echo_server 1234
bin/nc -u 127.0.0.1 1234

BUILD

see BUILD.md

lib

  • make libhv
  • sudo make install

examples

  • make examples

unittest

  • make unittest

compile options

compile WITH_OPENSSL

Enable SSL in libhv is so easy, just only two apis:

// init ssl_ctx, see base/hssl.h
hssl_ctx_t hssl_ctx_init(hssl_ctx_init_param_t* param);

// enable ssl, see event/hloop.h
int hio_enable_ssl(hio_t* io);

https is the best example.

sudo apt install openssl libssl-dev # ubuntu
make clean
make WITH_OPENSSL=yes
# editor etc/httpd.conf => ssl = on
bin/httpd -s restart -d
bin/curl -v https://localhost:8080
curl -v https://localhost:8080 --insecure

compile WITH_CURL

make WITH_CURL=yes DEFINES="CURL_STATICLIB"

compile WITH_NGHTTP2

sudo apt install libnghttp2-dev # ubuntu
make clean
make WITH_NGHTTP2=yes
bin/httpd -d
bin/curl -v localhost:8080 --http2

other options

see config.mk

echo-servers

cd echo-servers
./build.sh
./benchmark.sh

echo-servers/benchmark
echo-servers

Issues
Releases(v1.2.6)
  • v1.2.6(Jun 26, 2022)

    • Add Channel::isWriteComplete
    • Add defaultLargeFileHander and limit_rate
    • Timer use monotonic time
    • Add TcpServer::setLoadBalance
    • Add HUrl
    • Add HttpRequest::connect_timeout
    • Add requests::downloadFile
    • Add router.Static()
    • Add class MqttClient
    • fix some bugs
    • optimize code
    Source code(tar.gz)
    Source code(zip)
  • v1.2.5(Mar 5, 2022)

    • Add mqtt client
    • Add examples mqtt_pub mqtt_sub
    • Add hssl_ctx_new, hssl_ctx_free, hio_new_ssl_ctx
    • Add SSE: Content-Type: text/event-stream
    • Add build for iOS
    • eventfd > pipe > socketpair
    Source code(tar.gz)
    Source code(zip)
  • v1.2.4(Dec 24, 2021)

    • Add hio_set_read_timeout, hio_set_write_timeout
    • Add hio_readline, hio_readstring, hio_readbytes
    • Add logger_set_format
    • Add examples/tinyhttpd
    • Add examples/tinyproxyd
    • Add examples/socks5_proxy_server
    • http_async_handler use hv::async
    • Fix HttpCookie
    Source code(tar.gz)
    Source code(zip)
  • v1.2.3(Nov 25, 2021)

  • v1.2.2(Sep 27, 2021)

  • v1.2.1(Aug 28, 2021)

    New Features:

    • Add HttpMessage::cookies
    • Support http chunked
    • Add class HttpContext
    • axios
    • WITH_GNUTLS、WITH_APPLETLS
    • hio_set_unpack
    • Add example jsonrpc
    • Add example protorpc
    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(May 28, 2021)

  • v1.1.0(Mar 31, 2021)

  • v1.0.0(Feb 28, 2021)

Owner
ithewei
C/C++
ithewei
Full-featured high-performance event loop loosely modelled after libevent

libev is a high-performance event loop/event model with lots of features. (see benchmark at http://libev.schmorp.de/bench.html) ABOUT Homepage:

Paul Bohm 1.3k Jun 20, 2022
Header-only, event based, tiny and easy to use libuv wrapper in modern C++ - now available as also shared/static library!

Do you have a question that doesn't require you to open an issue? Join the gitter channel. If you use uvw and you want to say thanks or support the pr

Michele Caini 1.5k Jun 24, 2022
Event loop friendly C++ actor micro-framework

Rotor rotor is event loop friendly C++ actor micro framework, github gitee features minimalistic loop agnostic core erlang-like hierarchical superviso

Ivan Baidakou 218 Jun 23, 2022
KBMS is a C++11 high performance network framework based on libevent.

KBMS is a C++11 high performance network framework based on libevent. It is light and easy to deploy. At present, it mainly supports HTTP protocol.

Ke Technologies 28 Apr 12, 2022
Native C++ coroutines for libevent

libevent-async libevent-async is a C++ wrapper around the libevent API. It wraps commonly-used objects in classes and provides coroutine support for m

Martin Michelsen 2 Jan 8, 2022
The Telegram Bot API provides an HTTP API for creating Telegram Bots.

The Telegram Bot API provides an HTTP API for creating Telegram Bots.

Telegram Library 1.6k Jun 25, 2022
Encapsulates the two protocols of OpenVpn and Ikev2, you only need to enter the server IP and port number to realize the connection and status display, and the specific situation of the connection can be displayed at the same time。

NewVpnCore 封装了OpenVpn和Ikev2两种协议,只需要输入服务器IP和端口号即可实现连接和状态显示,同时可以显示连接的具体情况。 UniteVpn Core(第一版) 1. 模块说明 unitevpn:封装了vpn的操作和统一不同协议信息的模块 ikev2:IKEV2协议的源码 op

ZFashion 3 Jun 8, 2022
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution

CppServer Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and

Ivan Shynkarenka 848 Jun 26, 2022
We use Clash as the backend proxy, which supports Shadowsocks(R), V2Ray, and Trojan protocols.

We use Clash as the backend proxy, which supports Shadowsocks(R), V2Ray, and Trojan protocols.

Dr. Incognito 1.2k Jun 25, 2022
A modern C++ network library for developing high performance network services in TCP/UDP/HTTP protocols.

evpp Introduction 中文说明 evpp is a modern C++ network library for developing high performance network services using TCP/UDP/HTTP protocols. evpp provid

Qihoo 360 3k Jun 27, 2022
RPC based on C++ Workflow. Supports Baidu bRPC, Tencent tRPC, thrift protocols.

中文版入口 SRPC Introduction SRPC is an RPC system developed by Sogou. Its main features include: Base on Sogou C++ Workflow, with the following features:

C++ Workflow Project and Ecosystem 1.3k Jun 20, 2022
Event-driven network library for multi-threaded Linux server in C++11

Muduo is a multithreaded C++ network library based on the reactor pattern. http://github.com/chenshuo/muduo Copyright (c) 2010, Shuo Chen. All righ

Shuo Chen 11.5k Jun 19, 2022
About Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐

Hello-World About Add any Program in any language you like or add a hello world Program ❣️ if you like give us ⭐ Give this Project a Star ⭐ If you lik

Lokesh Jangid 16 Jan 22, 2022
A special version of Packet Batch that utilizes AF_XDP Linux sockets (this should be faster than the standard version, but not as fast as the DPDK).

Packet Batch (AF_XDP) Description This is a special version of Packet Batch that utilizes AF_XDP sockets instead of AF_PACKETv3 (which is what the sta

Packet Batch 9 Feb 24, 2022
A simple networking library for C++. Designed with games in mind, but can be used for other things

A simple networking library for C++. Designed with games in mind, but can be used for other things. Tested to work on Android and Windows.

Matthew 5 Nov 14, 2019
Webdav-client-cpp - C++ WebDAV Client provides easy and convenient to work with WebDAV-servers.

WebDAV Client Package WebDAV Client provides easy and convenient to work with WebDAV-servers: Yandex.Disk Dropbox Google Drive Box 4shared ownCloud ..

Cloud Polis 100 Apr 13, 2022
Level up your Beat Saber experience on Quest! AnyTweaks provides various tweaks to help boost your experience on Quest, such as Bloom, FPS Counter and more.

Need help/support? Ask in one of BSMG's support channels for Quest, or join my Discord server! AnyTweaks Level up your Beat Saber experience on Quest!

kaitlyn~ 10 Jun 19, 2022
GNOME Shell provides core user interface functions for the GNOME 3 desktop

GNOME Shell GNOME Shell provides core user interface functions for the GNOME 3 desktop, like switching to windows and launching applications. GNOME Sh

Muqtadir 3 Oct 18, 2021