我是靠谱客的博主 体贴小蝴蝶,最近开发中收集的这篇文章主要介绍当今乱世,谁能夺得C++标准网络库宝座?当今乱世,谁能夺得C++标准网络库宝座?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

当今乱世,谁能夺得C++标准网络库宝座?

文章目录

  • 当今乱世,谁能夺得C++标准网络库宝座?
    • 各编程语言HTTP请求对比
      • c: libcurl
      • c#: RestSharp
      • go: net/http
      • java: OKHttp
      • php: HttpRequest
      • python: requests
      • ruby: net/http
      • nodejs: http
      • 对比结论
    • c/c++网络库对比
      • libevent
      • libev
      • libuv
      • libhv
      • asio
      • 畅怀未来

C++标准库最大的痛是什么,毋庸置疑,那就是缺少网路库,犹如三军之中缺少主帅。

java、c#、go、ruby都有内置的net包,此外各语言都有各自流行的网络库HTTP请求库HTTP脚手架web框架

  • javanetty、OKHttp、tomcat、SpringBoot、SpringCloud
  • c#RestSharp、Nancy
  • gogin、iris、beego
  • pythonurllib、requests、flask、Django
  • rubyfaraday、httparty、rails
  • phpthinkphp、laravel
  • jsajax、axios、express、koa
  • ccurl

除了和C++有亲缘关系的c语言有一个curl耳熟能详外,c/c++基本上没有什么特别流行的网络库。

那C++为何不提供标准网络库呢?
答案很羞于出口,不是不想提供,不能提供,而是没有一个库能当选标准网络库。

各编程语言HTTP请求对比

c: libcurl

libcurl是一个支持多种协议的客户端库,熟悉命令行的同学一定不陌生,curl作为命令行工具确实简单好用,比wget更强大。当被作为库使用,并不也是那么简单,往往需要自己动手再封装一层。当然这也是c语言的通病,为了复用同一个接口,导致接口很难用。

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "http://127.0.0.1:8080/echo");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "content-type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{"key":"value"}");

CURLcode ret = curl_easy_perform(hnd);

上面的代码仅仅只是请求部分,想要获取响应内容,还得设置回调函数并在回调里解析数据:

curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_cb);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_userdata);

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, body_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_userdata);

curl_easy尚且如此繁琐,curl_multi更加让人望而却步。

c#: RestSharp

var client = new RestClient("http://127.0.0.1:8080/echo");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{"key":"value"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

go: net/http

package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {
	url := "http://127.0.0.1:8080/echo"

	payload := strings.NewReader("{"key":"value"}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("content-type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))
}

java: OKHttp

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{"key":"value"}");
Request request = new Request.Builder()
  .url("http://127.0.0.1:8080/echo")
  .post(body)
  .addHeader("content-type", "application/json")
  .build();

Response response = client.newCall(request).execute();

php: HttpRequest

<?php

$request = new HttpRequest();
$request->setUrl('http://127.0.0.1:8080/echo');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'content-type' => 'application/json'
));

$request->setBody('{"key":"value"}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

python: requests

import requests

url = "http://127.0.0.1:8080/echo"

payload = "{"key":"value"}"
headers = {
    'content-type': "application/json"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

ruby: net/http

require 'uri'
require 'net/http'

url = URI("http://127.0.0.1:8080/echo")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/json'
request.body = "{"key":"value"}"

response = http.request(request)
puts response.read_body

nodejs: http

var http = require("http");

var options = {
  "method": "POST",
  "hostname": "127.0.0.1",
  "port": "8080",
  "path": "/echo",
  "headers": {
    "content-type": "application/json"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ key: 'value' }));
req.end();

注:上面的HTTP请求代码均copy自postmanpostman列举了众多语言写法,独独没有C++(应该是默认C++使用Ccurl),可想而知C++的尴尬处境。

对比结论

  • 动态语言比静态语言更简单灵活;
  • 有反射机制的语言比无反射机制的语言更简单灵活;
  • c语言没有stringmap、反射、ORM,写复杂应用层协议简直就是找虐,更别说写数据库应用了。如不分场合,强行使用c/c++写web应用,开发效率注定感人,可能人家已经开始展示各种炫酷图表了,你还在那解析字符串。

c/c++网络库对比

我们以TCP Echo Server为例,展示各个网络库的写法。

注:以下代码皆可以在 https://github.com/ithewei/libhv/tree/master/echo-servers 目录下找到,并提供了编译脚本和压力测试脚本。

libevent

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "event2/event.h"
#include "event2/listener.h"
#include "event2/bufferevent.h"
#include "event2/buffer.h"

//#define RECV_BUFSIZE    8192

void error_cb(struct bufferevent* bev, short event, void* userdata) {
    bufferevent_free(bev);
}

void read_cb(struct bufferevent* bev, void* userdata) {
    //static char recvbuf[RECV_BUFSIZE];
    //int nread = bufferevent_read(bev, &recvbuf, RECV_BUFSIZE);
    //bufferevent_write(bev, recvbuf, nread);
    struct evbuffer* buf = evbuffer_new();
    int ret = bufferevent_read_buffer(bev, buf);
    if (ret == 0) {
        bufferevent_write_buffer(bev, buf);
    }
    evbuffer_free(buf);
}

void on_accept(struct evconnlistener* listener, evutil_socket_t connfd, struct sockaddr* peeraddr, int addrlen, void* userdata) {
    struct event_base* loop = evconnlistener_get_base(listener);
    struct bufferevent* bev = bufferevent_socket_new(loop, connfd, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_setcb(bev, read_cb, NULL, error_cb, NULL);
    bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);
}

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: cmd portn");
        return -10;
    }
    int port = atoi(argv[1]);

    struct event_base* loop = event_base_new();

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    struct evconnlistener* listener =  evconnlistener_new_bind(
            loop, on_accept, NULL,
            LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE,
            -1, (struct sockaddr*)&addr, sizeof(addr));
    if (listener == NULL) {
        return -20;
    }

    event_base_dispatch(loop);

    evconnlistener_free(listener);
    event_base_free(loop);
    return 0;
}

优点:

  • 历史最为悠久,有不少著名项目背书(包括memcachedlibwebsockets360的evpp),稳定性有保障;

缺点:

  • libevent最为古老、有历史包袱,bufferevent虽为精妙,却也难以上手;
  • 我认为libevent里的结构体命名不太好,没什么规律可言,event_base使用event_loop会更加形象,evconnlistener又太长,buffereventevbuffer命名容易混淆,不清晰;
  • 宏定义flags有点让人生畏,如BEV_OPT_CLOSE_ON_FREEEV_PERSIST,增加了认知负担;

libev

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "ev.h"

#define RECV_BUFSIZE    8192
static char recvbuf[RECV_BUFSIZE];

void do_recv(struct ev_loop *loop, struct ev_io *io, int revents) {
    int nread, nsend;
    nread = recv(io->fd, recvbuf, RECV_BUFSIZE, 0);
    if (nread <= 0) {
        goto error;
    }
    nsend = send(io->fd, recvbuf, nread, 0);
    if (nsend != nread) {
        goto error;
    }
    return;

error:
    ev_io_stop(loop, io);
    close(io->fd);
    free(io);
}

void do_accept(struct ev_loop *loop, struct ev_io *listenio, int revents) {
    struct sockaddr_in peeraddr;
    socklen_t addrlen = sizeof(peeraddr);
    int connfd = accept(listenio->fd, (struct sockaddr*)&peeraddr, &addrlen);
    if (connfd <= 0) {
        return;
    }

    struct ev_io* io = (struct ev_io*)malloc(sizeof(struct ev_io));
    ev_io_init(io, do_recv, connfd, EV_READ);
    ev_io_start(loop, io);
}

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: cmd portn");
        return -10;
    }
    int port = atoi(argv[1]);

    struct sockaddr_in addr;
    int addrlen = sizeof(addr);
    memset(&addr, 0, addrlen);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    int listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listenfd < 0) {
        return -20;
    }
    if (bind(listenfd, (struct sockaddr*)&addr, addrlen) < 0) {
        return -30;
    }
    if (listen(listenfd, SOMAXCONN) < 0) {
        return -40;
    }

    struct ev_loop* loop = ev_loop_new(0);

    struct ev_io listenio;
    ev_io_init(&listenio, do_accept, listenfd, EV_READ);
    ev_io_start(loop, &listenio);

    ev_run(loop, 0);
    ev_loop_destroy(loop);
    return 0;
}

优点:

  • libev可以说是libevent的精简版,库源码极为短小精悍,不到八千行;

缺点:

  • 库源码中定义了大量的宏,想读懂需要扎实的c功底;
  • 封装层次较低,如监听端口,需要老老实实手写socket->bind->listen这个流程;
  • IO事件只是通知你可读可写,需要自行调用recv/send
  • windows平台实现不佳;

libuv

#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "uv.h"

typedef struct {
    uv_write_t  req;
    uv_buf_t    buf;
} uv_write_req_t;

void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
    buf->base = (char*)malloc(suggested_size);
    buf->len = suggested_size;
}

void close_cb(uv_handle_t* handle) {
    free(handle);
}

void write_cb(uv_write_t* req, int status) {
    uv_write_req_t* wr = (uv_write_req_t*)req;
    free(wr->buf.base);
    free(wr);
}

void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
    if (nread <= 0) {
        uv_close((uv_handle_t*)stream, close_cb);
        return;
    }
    uv_write_req_t* wr = (uv_write_req_t*)malloc(sizeof(uv_write_req_t));
    wr->buf.base = buf->base;
    wr->buf.len = nread;
    uv_write(&wr->req, stream, &wr->buf, 1, write_cb);
}

void do_accept(uv_stream_t* server, int status) {
    uv_tcp_t* tcp_stream = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
    uv_tcp_init(server->loop, tcp_stream);
    uv_accept(server, (uv_stream_t*)tcp_stream);
    uv_read_start((uv_stream_t*)tcp_stream, alloc_cb, read_cb);
}

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: cmd portn");
        return -10;
    }
    int port = atoi(argv[1]);

    uv_loop_t loop;
    uv_loop_init(&loop);

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    //addr.sin_family = AF_INET;
    //addr.sin_port = htons(port);
    uv_ip4_addr("0.0.0.0", port, &addr);

    uv_tcp_t tcp_server;
    uv_tcp_init(&loop, &tcp_server);
    int ret = uv_tcp_bind(&tcp_server, (struct sockaddr*)&addr, 0);
    if (ret) {
        return -20;
    }
    ret = uv_listen((uv_stream_t*)&tcp_server, SOMAXCONN, do_accept);
    if (ret) {
        return -30;
    }

    uv_run(&loop, UV_RUN_DEFAULT);
    return 0;
}

优点:

  • nodejs的底层库,有nodejs这个大佬背书,稳定性、性能都毋庸置疑;
  • 命名很清晰,统一以uv_前缀开头;
  • 功能很强大,同时支持管道、文件的异步读写;

缺点:

  • 封装的结构体比较多,有一定的上手学习成本;
  • 监听端口还是需要uv_ip4_addr->uv_tcp_bind->uv_listen这一套流程;
  • 没有提供读写bufferuv_read_start/uv_write需要自己申请和释放内存,往往需要结合自己的业务实现一个内存池,不然每次读写都直接malloc/free,在某些操作系统上可能有性能损耗和内存碎片问题;

libhv

c版本:

#include "hv/hloop.h"

void on_close(hio_t* io) {
}

void on_recv(hio_t* io, void* buf, int readbytes) {
    hio_write(io, buf, readbytes);
}

void on_accept(hio_t* io) {
    hio_setcb_close(io, on_close);
    hio_setcb_read(io, on_recv);
    hio_read(io);
}

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: cmd portn");
        return -10;
    }
    int port = atoi(argv[1]);

    hloop_t* loop = hloop_new(0);
    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
    if (listenio == NULL) {
        return -20;
    }
    hloop_run(loop);
    hloop_free(&loop);
    return 0;
}

c++版本:

#include "hv/TcpServer.h"

using namespace hv;

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("Usage: %s portn", argv[0]);
        return -10;
    }
    int port = atoi(argv[1]);

    TcpServer srv;
    int listenfd = srv.createsocket(port);
    if (listenfd < 0) {
        return -20;
    }
    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=%dn", peeraddr.c_str(), channel->fd());
        } else {
            printf("%s disconnected! connfd=%dn", peeraddr.c_str(), channel->fd());
        }
    };
    srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
        // echo
        printf("< %.*sn", (int)buf->size(), (char*)buf->data());
        channel->write(buf);
    };
    srv.onWriteComplete = [](const SocketChannelPtr& channel, Buffer* buf) {
        printf("> %.*sn", (int)buf->size(), (char*)buf->data());
    };
    srv.setThreadNum(4);
    srv.start();

    while (1) sleep(1);
    return 0;
}

优点:

  • libhv本身是参考了libevent、libev、libuv的实现思路,它们的核心都是事件循环(即在一个事件循环中处理IO、定时器等事件),没有历史包袱,去其糟粕,取其精华;
  • 提供的接口最为精简,API接近原生系统调用,最容易上手;
  • 提供了c++的封装,参考了muduoevpp;
  • 原生支持SSL/TLS;
  • 提供了HTTP脚手架;
  • 支持WebSocket协议;
  • 未来将支持更多的常见协议,如MQTTredis;
  • 国产开源库,有中文教程,有QQ技术交流群(739352073)可供寻求技术支持;

缺点:

  • 问世时间较短,不到三年,缺少知名项目背书;

asio

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

class session {
public:
    session(boost::asio::io_service& io_service) :
        socket_(io_service) {
    }

    tcp::socket& socket() {
        return socket_;
    }

    void start() {
        socket_.async_read_some(boost::asio::buffer(data_, max_length),
                boost::bind(&session::handle_read, this,
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
    }

    void handle_read(const boost::system::error_code& error,
            size_t bytes_transferred) {
        if (!error) {
            boost::asio::async_write(socket_, boost::asio::buffer(data_,
                    bytes_transferred), boost::bind(&session::handle_write,
                    this, boost::asio::placeholders::error));
        } else {
            delete this;
        }
    }

    void handle_write(const boost::system::error_code& error) {
        if (!error) {
            socket_.async_read_some(boost::asio::buffer(data_, max_length),
                    boost::bind(&session::handle_read, this,
                            boost::asio::placeholders::error,
                            boost::asio::placeholders::bytes_transferred));
        } else {
            delete this;
        }
    }

private:
    tcp::socket socket_;
    enum {
        max_length = 1024
    };
    char data_[max_length];
};

class server {
public:
    server(boost::asio::io_service& io_service, short port) :
        io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(),
                port)) {
        session* new_session = new session(io_service_);
        acceptor_.async_accept(new_session->socket(), boost::bind(
                &server::handle_accept, this, new_session,
                boost::asio::placeholders::error));
    }

    void handle_accept(session* new_session,
            const boost::system::error_code& error) {
        if (!error) {
            new_session->start();
            new_session = new session(io_service_);
            acceptor_.async_accept(new_session->socket(), boost::bind(
                    &server::handle_accept, this, new_session,
                    boost::asio::placeholders::error));
        } else {
            delete new_session;
        }
    }

private:
    boost::asio::io_service& io_service_;
    tcp::acceptor acceptor_;
};

int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: cmd portn");
        return -10;
    }
    int port = atoi(argv[1]);

    boost::asio::io_service io_service;
    server s(io_service, port);
    io_service.run();

    return 0;
}

优点:

  • 曾被提名标准网络库,可见其背景和实力之强大,可惜boost不能整体入标准库,asio自然也不得成神;
  • 极具争议,赞者骂者参半,匪夷所思,一个库的影响恐怖如斯;
  • beast用来支持HTTP协议;
  • websocketpp用来支持WebSocket协议;

缺点:

  • 个人认为proactor模式并不好理解,不如reactor来的直观,不知道是否有同感的,proactor需要提前投入资源,在完成时又需要释放;而reactor是事件来了才处理,在资源利用率上更合理;
  • boost太庞大厚重了,所以在没有引入boost的项目中引入asio,显然有点得不偿失了,编译速度感人、包体积感人;
  • 源码几乎不可读,只能当黑盒使用,缺少中文资料;

当然还有ACEPOCOHP_SocketuSocketmuduohandyZLToolKitsylarworkflow等茫茫多的C++网络库,这里就不一一列举了,不得不感慨C++ coder的造轮子精神。

然而,当前确实没有一款网络库能够有资格当选标准网络库,C++委员会也是秉承宁缺毋滥的精神,让标准网络库这个宝座一直空着。

畅怀未来

我认为将来有资格坐上这个宝座的网络库有两种:
1、基于c++11标准高仿java nettyreactor模型网络库;
2、基于c++20标准的协程网络库;

理由如下:

  • c++ modern network library必定要使用智能指针管理连接上下文,使用lambda设置连接断链回调、数据读写回调;
  • proactor模型难用,除了windows IOCP外,其它如select、poll、epoll、kqueue等IO多路复用机制都更契合reactor模型;
  • java同为面向对象的静态语言,java能实现的,c++必定也能实现,模仿实现netty是可行的。
    实际上facebookwangle就是在模仿netty,只是依赖了自家的follyfolly又依赖了boost,而且实现不完整;
  • 鉴于c++20标准太新,c++20协程还有待推广挖掘,很多老项目无法立马迁移过来,所以我觉得当前最好的解决方案还是基于c++11出一个高仿nettyreactor模型网络库;

当然,如果libhv项目发展的好,我也会基于libhv另起一个CppNetty的项目,接口命名官方标准化、实现ByteBufpipeline、提供各种常见的codec、发起对标准网络库宝座的觊觎。希望各位大佬点赞支持。

也许未来C++标准网络库的宝座还是会一直空下去,但我真诚祈祷能有一个网络库能脱颖而出,结束这纷争的乱世。这样大家就不会那么的选择困难,能够集中精力,基于标准网络库做出更多如nginxredis一般优秀的高性能服务,完善C++的网络生态,延续C++的万世繁荣。

最后

以上就是体贴小蝴蝶为你收集整理的当今乱世,谁能夺得C++标准网络库宝座?当今乱世,谁能夺得C++标准网络库宝座?的全部内容,希望文章能够帮你解决当今乱世,谁能夺得C++标准网络库宝座?当今乱世,谁能夺得C++标准网络库宝座?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部