V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wlgq2
V2EX  ›  C++

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

  •  
  •   wlgq2 ·
    wlgq2 · 2020-04-21 05:31:39 +08:00 · 5062 次点击
    这是一个创建于 1437 天前的主题,其中的信息可能已经有所发展或是发生改变。


    项目地址: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 做了性能对比

    asio1 asio2
    ping-pong 测试显示 uv-cpp 有不弱于 boost.asio 的并发性能,不过这主要是由于 libuv 本身很强大。



    1000 并发,100000 次请求和 nginx 对比

    nginx_http uv_http
    不出意料的,单位时间请求及字节传输都不如 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……

    23 条回复    2020-05-06 22:08:14 +08:00
    Hanggi
        1
    Hanggi  
       2020-04-21 07:47:37 +08:00
    编译要多久?
    fgwmlhdkkkw
        2
    fgwmlhdkkkw  
       2020-04-21 07:55:03 +08:00
    我有一个小建议。我之前也写过 libuv 的 wrapper,,但是摆脱不了回调地狱。看了 c++的协程我又没搞明白,所以就终止了。做好还是整合一些协程,这样用起来舒服点。
    sanjusss
        3
    sanjusss  
       2020-04-21 08:45:29 +08:00
    很强大,已 star 。但估计我想写 http 服务的时候就忘了 /狗头
    hankai17
        4
    hankai17  
       2020-04-21 09:00:34 +08:00
    paoqi2048
        5
    paoqi2048  
       2020-04-21 10:32:57 +08:00   ❤️ 1
    乍一看跟 muduo 有点像
    waruqi
        6
    waruqi  
       2020-04-21 10:38:48 +08:00
    赞,可以试试用 xmake 来构建维护,以及 c++包集成
    wlgq2
        7
    wlgq2  
    OP
       2020-04-21 12:31:39 +08:00
    @Hanggi cmake 一二十秒应该
    wlgq2
        8
    wlgq2  
    OP
       2020-04-21 12:32:28 +08:00
    @fgwmlhdkkkw 等 C 艹 20 以后考虑。
    cholerae
        9
    cholerae  
       2020-04-21 12:58:19 +08:00
    所以是用在什么生产环境的?
    wlgq2
        10
    wlgq2  
    OP
       2020-04-21 13:01:52 +08:00
    @cholerae 前公司,游戏服务相关。
    kylix
        11
    kylix  
       2020-04-21 13:14:34 +08:00
    很厉害,已收藏。。。
    Cloutain
        12
    Cloutain  
       2020-04-21 14:21:18 +08:00
    楼主 nice!!!
    我平时用的 libevent,哈哈哈,最喜欢简单易用的网络库
    BlackZhu
        13
    BlackZhu  
       2020-04-21 15:16:50 +08:00
    和瑞克学的?
    tienhua
        14
    tienhua  
       2020-04-21 18:22:25 +08:00
    性能对比的那两种结果图是怎么生成的
    wlgq2
        15
    wlgq2  
    OP
       2020-04-21 19:17:41 +08:00
    @tienhua 万能的 excel
    cabing
        16
    cabing  
       2020-04-21 21:03:32 +08:00
    不错哦。
    OneMan
        17
    OneMan  
       2020-04-21 22:28:37 +08:00
    evpp 还不错,服务器选择东西很多,其实一个简单的没依赖的客户端 TCP 库还挺少
    liuguang
        18
    liuguang  
       2020-04-21 22:55:18 +08:00
    牛逼是牛逼,不过 rust 的 hyper 更强!![狗头]
    yazoox
        19
    yazoox  
       2020-04-22 06:34:00 +08:00 via Android
    厉害啊
    OneMan
        20
    OneMan  
       2020-04-24 17:38:23 +08:00
    已经不太敢用个人开源的东西了,尤其是这些网络基础东西,其实库关键不是性能问题,性能差别不重要。
    重要的是生产环境的稳定,接口健壮性,稳定大于天。
    OneMan
        21
    OneMan  
       2020-04-24 17:39:05 +08:00
    什么 pingpong 性能之类的,没什么意义。
    wlgq2
        22
    wlgq2  
    OP
       2020-05-06 20:31:26 +08:00
    @OneMan 我自己用倒是挺稳定的。主要是它内核是基于 libuv,做了一层 modern cpp 的封装。而且代码就几千行,改起来也容易。如果项目本身就是基于 libuv,可以试试迁移。
    OneMan
        23
    OneMan  
       2020-05-06 22:08:14 +08:00
    @wlgq2 你最近赋闲呢,在哪,要工作吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3981 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:20 · PVG 18:20 · LAX 03:20 · JFK 06:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.