项目地址:uv-cpp
之前发过,最近失业空闲,给这个网络库增加了 http 支持,实现了一个基于 RadixTree 的路由,可以支持通配符*,或者设置参数之类。大概可以实现这样的效果
int main(int argc, char** args) { uv::EventLoop loop; uv::http::HttpServer::SetBufferMode(uv::GlobalConfig::BufferMode::CycleBuffer); uv::http::HttpServer server(&loop); //example: 127.0.0.1:10010/test server.Get("/test",std::bind(&func1,std::placeholders::_1,std::placeholders::_2)); //example: 127.0.0.1:10010/some123abc server.Get("/some*",std::bind(&func2, std::placeholders::_1, std::placeholders::_2)); //example: 127.0.0.1:10010/value:1234 server.Get("/value:",std::bind(&func3, std::placeholders::_1, std::placeholders::_2)); //example: 127.0.0.1:10010/sum?param1=100¶m2=23 server.Get("/sum",std::bind(&func4, std::placeholders::_1, std::placeholders::_2)); uv::SocketAddr addr("127.0.0.1", 10010); server.bindAndListen(addr); loop.run(); }
顺带和 boost.asio 以及 nginx 做了性能对比
ping-pong 测试显示 uv-cpp 有不弱于 boost.asio 的并发性能,不过这主要是由于 libuv 本身很强大。
1000 并发,100000 次请求和 nginx 对比
不出意料的,单位时间请求及字节传输都不如 nginx,不过 nginx 不知道是不是我配置有问题,有 500+次失败请求,uv-cpp 没有。
接口也比较简单,10 行代码实现一个 echo 服务器
#include <iostream> #include <uv/include/uv11.h> int main(int argc, char** args) { uv::EventLoop* loop = uv::EventLoop::DefaultLoop(); uv::TcpServer server(loop); server.setMessageCallback([](uv::TcpConnectionPtr ptr,const char* data, ssize_t size) { ptr->write(data, size, nullptr); }); uv::SocketAddr addr("0.0.0.0", 10005, uv::SocketAddr::Ipv4); server.bindAndListen(addr); loop->run(); }
欢迎 star 、issue 、pr……
![]() | 1 Hanggi 2020-04-21 07:47:37 +08:00 编译要多久? |
![]() | 2 fgwmlhdkkkw 2020-04-21 07:55:03 +08:00 我有一个小建议。我之前也写过 libuv 的 wrapper,,但是摆脱不了回调地狱。看了 c++的协程我又没搞明白,所以就终止了。做好还是整合一些协程,这样用起来舒服点。 |
![]() | 3 sanjusss 2020-04-21 08:45:29 +08:00 很强大,已 star 。但估计我想写 http 服务的时候就忘了 /狗头 |
4 hankai17 2020-04-21 09:00:34 +08:00 |
![]() | /td> | 5 paoqi2048 2020-04-21 10:32:57 +08:00 ![]() 乍一看跟 muduo 有点像 |
![]() | 6 waruqi 2020-04-21 10:38:48 +08:00 赞,可以试试用 xmake 来构建维护,以及 c++包集成 |
![]() | 8 wlgq2 OP @fgwmlhdkkkw 等 C 艹 20 以后考虑。 |
9 6ufq0VLZn0DDkL80 2020-04-21 12:58:19 +08:00 所以是用在什么生产环境的? |
11 kylix 2020-04-21 13:14:34 +08:00 很厉害,已收藏。。。 |
![]() | 12 Cloutain 2020-04-21 14:21:18 +08:00 楼主 nice!!! 我平时用的 libevent,哈哈哈,最喜欢简单易用的网络库 |
13 BlackZhu 2020-04-21 15:16:50 +08:00 和瑞克学的? |
14 tienhua 2020-04-21 18:22:25 +08:00 性能对比的那两种结果图是怎么生成的 |
![]() | 16 cabing 2020-04-21 21:03:32 +08:00 不错哦。 |
17 OneMan 2020-04-21 22:28:37 +08:00 evpp 还不错,服务器选择东西很多,其实一个简单的没依赖的客户端 TCP 库还挺少 |
18 liuguang 2020-04-21 22:55:18 +08:00 牛逼是牛逼,不过 rust 的 hyper 更强!![狗头] |
![]() | 19 yazoox 2020-04-22 06:34:00 +08:00 via Android 厉害啊 |
20 OneMan 2020-04-24 17:38:23 +08:00 已经不太敢用个人开源的东西了,尤其是这些网络基础东西,其实库关键不是性能问题,性能差别不重要。 重要的是生产环境的稳定,接口健壮性,稳定大于天。 |
21 OneMan 2020-04-24 17:39:05 +08:00 什么 pingpong 性能之类的,没什么意义。 |
![]() | 22 wlgq2 OP @OneMan 我自己用倒是挺稳定的。主要是它内核是基于 libuv,做了一层 modern cpp 的封装。而且代码就几千行,改起来也容易。如果项目本身就是基于 libuv,可以试试迁移。 |