English | [中文](README-CN.md)
# libhv
[![platform](https://img.shields.io/badge/platform-linux%20%7C%20windows%20%7C%20macos-blue)](.github/workflows/CI.yml)
[![CI](https://github.com/ithewei/libhv/workflows/CI/badge.svg?branch=master)](https://github.com/ithewei/libhv/actions/workflows/CI.yml?query=branch%3Amaster)
[![benchmark](https://github.com/ithewei/libhv/workflows/benchmark/badge.svg?branch=master)](https://github.com/ithewei/libhv/actions/workflows/benchmark.yml?query=branch%3Amaster)
<br>
[![release](https://badgen.net/github/release/ithewei/libhv?icon=github)](https://github.com/ithewei/libhv/releases)
[![stars](https://badgen.net/github/stars/ithewei/libhv?icon=github)](https://github.com/ithewei/libhv/stargazers)
[![forks](https://badgen.net/github/forks/ithewei/libhv?icon=github)](https://github.com/ithewei/libhv/network/members)
[![issues](https://badgen.net/github/issues/ithewei/libhv?icon=github)](https://github.com/ithewei/libhv/issues)
[![PRs](https://badgen.net/github/prs/ithewei/libhv?icon=github)](https://github.com/ithewei/libhv/pulls)
[![license](https://badgen.net/github/license/ithewei/libhv?icon=github)](LICENSE)
<br>
[![gitee](https://badgen.net/badge/mirror/gitee/red)](https://gitee.com/libhv/libhv)
[![awesome-c](https://badgen.net/badge/icon/awesome-c/pink?icon=awesome&label&color)](https://github.com/oz123/awesome-c)
[![awesome-cpp](https://badgen.net/badge/icon/awesome-cpp/pink?icon=awesome&label&color)](https://github.com/fffaraz/awesome-cpp)
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, MacOS, BSD, Solaris, Android, iOS)
- High-performance EventLoop (IO, timer, idle, custom)
- TCP/UDP client/server/proxy
- TCP supports heartbeat, reconnect, upstream, MultiThread-safe write and close, etc.
- Built-in common unpacking modes (FixedLength, Delimiter, LengthField)
- RUDP support: WITH_KCP
- SSL/TLS support: (via WITH_OPENSSL or WITH_GNUTLS or WITH_MBEDTLS)
- HTTP client/server (support https http1/x http2 grpc)
- HTTP supports static service, indexof service, proxy service, sync/async API handler
- HTTP supports RESTful, URI router, keep-alive, chunked, etc.
- WebSocket client/server
- MQTT client
## ⌛️ Build
see [BUILD.md](BUILD.md)
Makefile:
```shell
./configure
make
sudo make install
```
or cmake:
```shell
mkdir build
cd build
cmake ..
cmake --build .
```
or vcpkg:
```shell
vcpkg install libhv
```
or xmake:
```shell
xrepo install libhv
```
## ⚡️ Getting Started
run `./getting_started.sh`:
```shell
git clone https://github.com/ithewei/libhv.git
cd libhv
./configure
make
bin/httpd -h
bin/httpd -d
#bin/httpd -c etc/httpd.conf -s restart -d
ps aux | grep httpd
# http file 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' -F 'pswd=123456'
bin/curl -v localhost:8080/upload -d "@LICENSE"
bin/curl -v localhost:8080/upload -F "file=@LICENSE"
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' -F 'int=123' -F 'float=3.14' -F 'string=hello'
# RESTful API: /group/:group_name/user/:user_id
bin/curl -v -X DELETE localhost:8080/group/test/user/123
# benchmark
bin/wrk -c 1000 -d 10 -t 4 http://127.0.0.1:8080/
```
### TCP
#### tcp server
**c version**: [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
**c++ version**: [evpp/TcpServer_test.cpp](evpp/TcpServer_test.cpp)
```c++
#include "TcpServer.h"
using namespace hv;
int main() {
int port = 1234;
TcpServer srv;
int listenfd = srv.createsocket(port);
if (listenfd < 0) {
return -1;
}
printf("server listen on port %d, listenfd=%d ...\n", port, listenfd);
srv.onConnection = [](const SocketChannelPtr& channel) {
std::string peeraddr = channel->peeraddr();
if (channel->isConnected()) {
printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
} else {
printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
}
};
srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
// echo
channel->write(buf);
};
srv.setThreadNum(4);
srv.start();
// press Enter to stop
while (getchar() != '\n');
return 0;
}
```
#### tcp client
**c version**: [examples/tcp_client_test.c](examples/tcp_client_test.c)
**c++ version**: [evpp/TcpClient_test.cpp](evpp/TcpClient_test.cpp)
```c++
#include <iostream>
#include "TcpClient.h"
using namespace hv;
int main() {
int port = 1234;
TcpClient cli;
int connfd = cli.createsocket(port);
if (connfd < 0) {
return -1;
}
cli.onConnection = [](const SocketChannelPtr& channel) {
std::string peeraddr = channel->peeraddr();
if (channel->isConnected()) {
printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
} else {
printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
}
};
cli.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
};
cli.start();
std::string str;
while (std::getline(std::cin, str)) {
if (str == "close") {
cli.closesocket();
} else if (str == "start") {
cli.start();
} else if (str == "stop") {
cli.stop();
break;
} else {
if (!cli.isConnected()) break;
cli.send(str);
}
}
return 0;
}
```
### HTTP
#### http server
see [examples/http_server_test.cpp](examples/http_server_test.cpp)
**golang gin style**
```c++
#include "HttpServer.h"
using namespace hv;
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.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
resp->json["origin"] = req->client_addr.ip;
resp->json["url"] = req->url;
resp->json["args"] = req->query_params;
resp->json["headers"] = req->headers;
return 200;
});
router.POST("/echo", [](const HttpContextPtr& ctx) {
return ctx->send(ctx->body(), ctx->type());
});
HttpServer server;
server.registerHttpService(&router);
server.setPort(8080);
server.setThreadNum(4);
server.run();
return 0;
}
```
#### http client
see [examples/http_client_test.cpp](examples/http_client_test.cpp)
**python requests style**
```c++
#include "requests.h"
int main() {
auto resp = requests::get("http://www.example.com");
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%s\n", resp->body.c_str());
}
resp = requests::post("127.0.0.1:8080/echo", "hello,world!");
if (resp == NULL) {
printf("request failed!\n");
} else {
printf("%s\n", resp->body.c_str());
}
return 0;
}
```
### WebSocket
#### W
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
在类unix系统中有五大I/O模型,依次为阻塞IO(BIO)、非阻塞IO(NIO)、IO多路复用(linux下有select、poll、epoll三种方案)、信号驱动IO、异步IO(前面四种都是同步IO),本文主要介绍常用的C的IO库,几乎都是基于IO多路复用,因为它性能很高,并且稳定,异步IO虽说性能更高但不稳定。 从linux源码看socket的阻塞和非阻塞 ps: 对于IO多路复用,epoll机制是linux独有的,其他类unix系统(macOS、FreeBSD、OpenBSD、NetBSD)使用的是kqueue,但是SUNOS系列使用的是event ports(即evports)。 IOCP是window基于线程池技术实现的异步IO,非常稳定。
资源推荐
资源详情
资源评论
收起资源包目录
特征:1、跨平台(Linux, Windows, MacOS, Solaris)2、高性能事件循环.rar (362个子文件)
cJSON.c 76KB
http_parser.c 70KB
appletls.c 47KB
ikcp.c 32KB
hloop.c 28KB
hevent.c 26KB
hmain.c 20KB
mqtt_client.c 17KB
nio.c 17KB
tinyproxyd.c 15KB
tinyhttpd.c 14KB
socks5_proxy_server.c 13KB
hlog.c 13KB
overlapio.c 13KB
hbase.c 12KB
rbtree.c 11KB
hsocket.c 10KB
webbench.c 10KB
dns.c 9KB
websocket_parser.c 8KB
sha1.c 8KB
multipart_parser.c 8KB
tcp_client_test.c 7KB
ftp.c 7KB
htime.c 7KB
md5.c 7KB
smtp.c 7KB
nc.c 6KB
unpack.c 6KB
gnutls.c 6KB
mbedtls.c 5KB
openssl.c 5KB
kqueue.c 5KB
hkcp.c 5KB
tcp_chat_server.c 5KB
select.c 4KB
rudp.c 4KB
jsonrpc_client.c 4KB
hbase_test.c 4KB
icmp.c 4KB
evport.c 4KB
epoll.c 4KB
base64_test.c 4KB
tcp_echo_server.c 4KB
hmutex_test.c 4KB
jsonrpc_server.c 4KB
base64.c 4KB
poll.c 3KB
mqtt_pub.c 3KB
hloop_test.c 3KB
mqtt_sub.c 3KB
rbtree_test.c 3KB
tcp_proxy_server.c 3KB
ftp_test.c 3KB
httpdef.c 2KB
one-acceptor-multi-workers.c 2KB
nlog.c 2KB
iocp.c 2KB
protorpc.c 2KB
libuv_echo.c 2KB
libev_echo.c 2KB
libevent_echo.c 2KB
htimer_test.c 2KB
multi-acceptor-processes.c 2KB
udp_echo_server.c 2KB
multi-acceptor-threads.c 2KB
sha1_test.c 2KB
md5_test.c 2KB
udp_proxy_server.c 1KB
wsdef.c 1KB
hversion.c 986B
connect_test.c 848B
nossl.c 844B
wintls.c 838B
hatomic_test.c 791B
nslookup_test.c 738B
socketpair_test.c 733B
mqtt_protocol.c 684B
sendmail_test.c 667B
libhv_echo.c 637B
hssl.c 478B
noevent.c 413B
herr.c 358B
date_test.c 353B
ping_test.c 344B
rmdir_test.c 201B
mkdir_test.c 201B
.clang-format 3KB
ios.toolchain.cmake 42KB
libhvConfig.cmake 3KB
vars.cmake 2KB
utils.cmake 1KB
libhv.cmake 198B
httpd.conf 776B
nginx.conf 694B
hmain_test.conf 247B
configure 6KB
HttpHandler.cpp 22KB
http_client.cpp 19KB
HttpMessage.cpp 16KB
共 362 条
- 1
- 2
- 3
- 4
资源评论
野生的狒狒
- 粉丝: 1495
- 资源: 1530
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功