wlgq2
V2EX  ›  C++

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

  •  
  •   wlgq2 ·
    wlgq2 · Apr 21, 2020 · 6098 views
    This topic created in 2263 days ago, the information mentioned may be changed or developed.


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