发一个自己用于生产环境 C++网络库

2020-04-21 05:31:39 +08:00
 wlgq2


项目地址: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&param2=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……

5091 次点击
所在节点    C++
23 条回复
Hanggi
2020-04-21 07:47:37 +08:00
编译要多久?
fgwmlhdkkkw
2020-04-21 07:55:03 +08:00
我有一个小建议。我之前也写过 libuv 的 wrapper,,但是摆脱不了回调地狱。看了 c++的协程我又没搞明白,所以就终止了。做好还是整合一些协程,这样用起来舒服点。
sanjusss
2020-04-21 08:45:29 +08:00
很强大,已 star 。但估计我想写 http 服务的时候就忘了 /狗头
hankai17
2020-04-21 09:00:34 +08:00
paoqi2048
2020-04-21 10:32:57 +08:00
乍一看跟 muduo 有点像
waruqi
2020-04-21 10:38:48 +08:00
赞,可以试试用 xmake 来构建维护,以及 c++包集成
wlgq2
2020-04-21 12:31:39 +08:00
@Hanggi cmake 一二十秒应该
wlgq2
2020-04-21 12:32:28 +08:00
@fgwmlhdkkkw 等 C 艹 20 以后考虑。
cholerae
2020-04-21 12:58:19 +08:00
所以是用在什么生产环境的?
wlgq2
2020-04-21 13:01:52 +08:00
@cholerae 前公司,游戏服务相关。
kylix
2020-04-21 13:14:34 +08:00
很厉害,已收藏。。。
Cloutain
2020-04-21 14:21:18 +08:00
楼主 nice!!!
我平时用的 libevent,哈哈哈,最喜欢简单易用的网络库
BlackZhu
2020-04-21 15:16:50 +08:00
和瑞克学的?
tienhua
2020-04-21 18:22:25 +08:00
性能对比的那两种结果图是怎么生成的
wlgq2
2020-04-21 19:17:41 +08:00
@tienhua 万能的 excel
cabing
2020-04-21 21:03:32 +08:00
不错哦。
OneMan
2020-04-21 22:28:37 +08:00
evpp 还不错,服务器选择东西很多,其实一个简单的没依赖的客户端 TCP 库还挺少
liuguang
2020-04-21 22:55:18 +08:00
牛逼是牛逼,不过 rust 的 hyper 更强!![狗头]
yazoox
2020-04-22 06:34:00 +08:00
厉害啊
OneMan
2020-04-24 17:38:23 +08:00
已经不太敢用个人开源的东西了,尤其是这些网络基础东西,其实库关键不是性能问题,性能差别不重要。
重要的是生产环境的稳定,接口健壮性,稳定大于天。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/664486

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX