当前位置:   article > 正文

C++ 网络库:libevent、libev、libuv、poco、libhv、asi、ace、QUIC 协议_c++网络库

c++网络库

From:https://zhuanlan.zhihu.com/p/357075167

1、各编程语言HTTP请求对比

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

  • java 有 netty、OKHttp、tomcat、SpringBoot、SpringCloud
  • c# 有 RestSharp、Nancy
  • go 有 gin、iris、beego
  • python 有 urllib、requests、flask、Django
  • ruby 有 faraday、httparty、rails
  • php 有 thinkphp、laravel
  • js 有 ajax、axios、express、koa
  • c 有 curl

下面的 HTTP 请求代码均 copy 自 postman,postman 列举了众多语言写法,独独没有C++。

c: libcurl

libcurl:https://curl.se/libcurl/

curl作为命令行工具确实简单好用,比wget更强大。当被作为库使用,并不也是那么简单,往往需要自己动手再封装一层。当然这也是c语言的通病,为了复用同一个接口,导致接口很难用。

  1. CURL *hnd = curl_easy_init();
  2. curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
  3. curl_easy_setopt(hnd, CURLOPT_URL, "http://127.0.0.1:8080/echo");
  4. struct curl_slist *headers = NULL;
  5. headers = curl_slist_append(headers, "content-type: application/json");
  6. curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
  7. curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"key\":\"value\"}");
  8. CURLcode ret = curl_easy_perform(hnd);

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

  1. curl_easy_setopt(curl, CURLOPT_HEADER, 0);
  2. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_cb);
  3. curl_easy_setopt(curl, CURLOPT_HEADERDATA, header_userdata);
  4. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, body_cb);
  5. curl_easy_setopt(curl, CURLOPT_WRITEDATA, body_userdata);

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

c#: RestSharp

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

go: net/http

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "net/http"
  6. "io/ioutil"
  7. )
  8. func main() {
  9. url := "http://127.0.0.1:8080/echo"
  10. payload := strings.NewReader("{\"key\":\"value\"}")
  11. req, _ := http.NewRequest("POST", url, payload)
  12. req.Header.Add("content-type", "application/json")
  13. res, _ := http.DefaultClient.Do(req)
  14. defer res.Body.Close()
  15. body, _ := ioutil.ReadAll(res.Body)
  16. fmt.Println(res)
  17. fmt.Println(string(body))
  18. }

java: OKHttp

  1. OkHttpClient client = new OkHttpClient();
  2. MediaType mediaType = MediaType.parse("application/json");
  3. RequestBody body = RequestBody.create(mediaType, "{\"key\":\"value\"}");
  4. Request request = new Request.Builder()
  5. .url("http://127.0.0.1:8080/echo")
  6. .post(body)
  7. .addHeader("content-type", "application/json")
  8. .build();
  9. Response response = client.newCall(request).execute();

php: HttpRequest

  1. <?php
  2. $request = new HttpRequest();
  3. $request->setUrl('http://127.0.0.1:8080/echo');
  4. $request->setMethod(HTTP_METH_POST);
  5. $request->setHeaders(array(
  6. 'content-type' => 'application/json'
  7. ));
  8. $request->setBody('{"key":"value"}');
  9. try {
  10. $response = $request->send();
  11. echo $response->getBody();
  12. } catch (HttpException $ex) {
  13. echo $ex;
  14. }

python: requests

  1. import requests
  2. url = "http://127.0.0.1:8080/echo"
  3. payload = "{\"key\":\"value\"}"
  4. headers = {
  5. 'content-type': "application/json"
  6. }
  7. response = requests.request("POST", url, data=payload, headers=headers)
  8. print(response.text)

ruby: net/http

  1. require 'uri'
  2. require 'net/http'
  3. url = URI("http://127.0.0.1:8080/echo")
  4. http = Net::HTTP.new(url.host, url.port)
  5. request = Net::HTTP::Post.new(url)
  6. request["content-type"] = 'application/json'
  7. request.body = "{\"key\":\"value\"}"
  8. response = http.request(request)
  9. puts response.read_body

nodejs: http

  1. var http = require("http");
  2. var options = {
  3. "method": "POST",
  4. "hostname": "127.0.0.1",
  5. "port": "8080",
  6. "path": "/echo",
  7. "headers": {
  8. "content-type": "application/json"
  9. }
  10. };
  11. var req = http.request(options, function (res) {
  12. var chunks = [];
  13. res.on("data", function (chunk) {
  14. chunks.push(chunk);
  15. });
  16. res.on("end", function () {
  17. var body = Buffer.concat(chunks);
  18. console.log(body.toString());
  19. });
  20. });
  21. req.write(JSON.stringify({ key: 'value' }));
  22. req.end();

对比结论

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

2、C/C++ 网络库对比

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

代码:https://github.com/ithewei/libhv/tree/master/echo-servers

libevent

libevent:http://libevent.org/

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "event2/event.h"
  5. #include "event2/listener.h"
  6. #include "event2/bufferevent.h"
  7. #include "event2/buffer.h"
  8. //#define RECV_BUFSIZE 8192
  9. void error_cb(struct bufferevent* bev, short event, void* userdata) {
  10. bufferevent_free(bev);
  11. }
  12. void read_cb(struct bufferevent* bev, void* userdata) {
  13. //static char recvbuf[RECV_BUFSIZE];
  14. //int nread = bufferevent_read(bev, &recvbuf, RECV_BUFSIZE);
  15. //bufferevent_write(bev, recvbuf, nread);
  16. struct evbuffer* buf = evbuffer_new();
  17. int ret = bufferevent_read_buffer(bev, buf);
  18. if (ret == 0) {
  19. bufferevent_write_buffer(bev, buf);
  20. }
  21. evbuffer_free(buf);
  22. }
  23. void on_accept(struct evconnlistener* listener, evutil_socket_t connfd, struct sockaddr* peeraddr, int addrlen, void* userdata) {
  24. struct event_base* loop = evconnlistener_get_base(listener);
  25. struct bufferevent* bev = bufferevent_socket_new(loop, connfd, BEV_OPT_CLOSE_ON_FREE);
  26. bufferevent_setcb(bev, read_cb, NULL, error_cb, NULL);
  27. bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);
  28. }
  29. int main(int argc, char** argv) {
  30. if (argc < 2) {
  31. printf("Usage: cmd port\n");
  32. return -10;
  33. }
  34. int port = atoi(argv[1]);
  35. struct event_base* loop = event_base_new();
  36. struct sockaddr_in addr;
  37. memset(&addr, 0, sizeof(addr));
  38. addr.sin_family = AF_INET;
  39. addr.sin_port = htons(port);
  40. struct evconnlistener* listener = evconnlistener_new_bind(
  41. loop, on_accept, NULL,
  42. LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE,
  43. -1, (struct sockaddr*)&addr, sizeof(addr));
  44. if (listener == NULL) {
  45. return -20;
  46. }
  47. event_base_dispatch(loop);
  48. evconnlistener_free(listener);
  49. event_base_free(loop);
  50. return 0;
  51. }
  • 优点:历史最为悠久,有不少著名项目背书(包括memcachedlibwebsockets360的evpp),稳定性有保障;
  • 缺点:libevent 最为古老、有历史包袱,bufferevent 虽为精妙,却也难以上手;

libev

libev:http://software.schmorp.de/pkg/libev.html

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include "ev.h"
  9. #define RECV_BUFSIZE 8192
  10. static char recvbuf[RECV_BUFSIZE];
  11. void do_recv(struct ev_loop *loop, struct ev_io *io, int revents) {
  12. int nread, nsend;
  13. nread = recv(io->fd, recvbuf, RECV_BUFSIZE, 0);
  14. if (nread <= 0) {
  15. goto error;
  16. }
  17. nsend = send(io->fd, recvbuf, nread, 0);
  18. if (nsend != nread) {
  19. goto error;
  20. }
  21. return;
  22. error:
  23. ev_io_stop(loop, io);
  24. close(io->fd);
  25. free(io);
  26. }
  27. void do_accept(struct ev_loop *loop, struct ev_io *listenio, int revents) {
  28. struct sockaddr_in peeraddr;
  29. socklen_t addrlen = sizeof(peeraddr);
  30. int connfd = accept(listenio->fd, (struct sockaddr*)&peeraddr, &addrlen);
  31. if (connfd <= 0) {
  32. return;
  33. }
  34. struct ev_io* io = (struct ev_io*)malloc(sizeof(struct ev_io));
  35. ev_io_init(io, do_recv, connfd, EV_READ);
  36. ev_io_start(loop, io);
  37. }
  38. int main(int argc, char** argv) {
  39. if (argc < 2) {
  40. printf("Usage: cmd port\n");
  41. return -10;
  42. }
  43. int port = atoi(argv[1]);
  44. struct sockaddr_in addr;
  45. int addrlen = sizeof(addr);
  46. memset(&addr, 0, addrlen);
  47. addr.sin_family = AF_INET;
  48. addr.sin_port = htons(port);
  49. int listenfd = socket(AF_INET, SOCK_STREAM, 0);
  50. if (listenfd < 0) {
  51. return -20;
  52. }
  53. if (bind(listenfd, (struct sockaddr*)&addr, addrlen) < 0) {
  54. return -30;
  55. }
  56. if (listen(listenfd, SOMAXCONN) < 0) {
  57. return -40;
  58. }
  59. struct ev_loop* loop = ev_loop_new(0);
  60. struct ev_io listenio;
  61. ev_io_init(&listenio, do_accept, listenfd, EV_READ);
  62. ev_io_start(loop, &listenio);
  63. ev_run(loop, 0);
  64. ev_loop_destroy(loop);
  65. return 0;
  66. }

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

缺点:

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

libuv

  1. #define _GNU_SOURCE 1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "uv.h"
  6. typedef struct {
  7. uv_write_t req;
  8. uv_buf_t buf;
  9. } uv_write_req_t;
  10. void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
  11. buf->base = (char*)malloc(suggested_size);
  12. buf->len = suggested_size;
  13. }
  14. void close_cb(uv_handle_t* handle) {
  15. free(handle);
  16. }
  17. void write_cb(uv_write_t* req, int status) {
  18. uv_write_req_t* wr = (uv_write_req_t*)req;
  19. free(wr->buf.base);
  20. free(wr);
  21. }
  22. void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
  23. if (nread <= 0) {
  24. uv_close((uv_handle_t*)stream, close_cb);
  25. return;
  26. }
  27. uv_write_req_t* wr = (uv_write_req_t*)malloc(sizeof(uv_write_req_t));
  28. wr->buf.base = buf->base;
  29. wr->buf.len = nread;
  30. uv_write(&wr->req, stream, &wr->buf, 1, write_cb);
  31. }
  32. void do_accept(uv_stream_t* server, int status) {
  33. uv_tcp_t* tcp_stream = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
  34. uv_tcp_init(server->loop, tcp_stream);
  35. uv_accept(server, (uv_stream_t*)tcp_stream);
  36. uv_read_start((uv_stream_t*)tcp_stream, alloc_cb, read_cb);
  37. }
  38. int main(int argc, char** argv) {
  39. if (argc < 2) {
  40. printf("Usage: cmd port\n");
  41. return -10;
  42. }
  43. int port = atoi(argv[1]);
  44. uv_loop_t loop;
  45. uv_loop_init(&loop);
  46. struct sockaddr_in addr;
  47. memset(&addr, 0, sizeof(addr));
  48. //addr.sin_family = AF_INET;
  49. //addr.sin_port = htons(port);
  50. uv_ip4_addr("0.0.0.0", port, &addr);
  51. uv_tcp_t tcp_server;
  52. uv_tcp_init(&loop, &tcp_server);
  53. int ret = uv_tcp_bind(&tcp_server, (struct sockaddr*)&addr, 0);
  54. if (ret) {
  55. return -20;
  56. }
  57. ret = uv_listen((uv_stream_t*)&tcp_server, SOMAXCONN, do_accept);
  58. if (ret) {
  59. return -30;
  60. }
  61. uv_run(&loop, UV_RUN_DEFAULT);
  62. return 0;
  63. }

优点:

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

缺点:

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

POCO

POCO:http://pocoproject.org/

POCO 用户手册、示例:https://docs.pocoproject.org/current/

POCO C++库入门指南:https://blog.csdn.net/danlan_shiguang/article/details/124419814

libhv ( 国产网络库 )

比 libevent、libuv 更易用的国产网络库:github:https://github.com/ithewei/libhv

c 版本:

  1. #include "hv/hloop.h"
  2. void on_close(hio_t* io) {
  3. }
  4. void on_recv(hio_t* io, void* buf, int readbytes) {
  5. hio_write(io, buf, readbytes);
  6. }
  7. void on_accept(hio_t* io) {
  8. hio_setcb_close(io, on_close);
  9. hio_setcb_read(io, on_recv);
  10. hio_read(io);
  11. }
  12. int main(int argc, char** argv) {
  13. if (argc < 2) {
  14. printf("Usage: cmd port\n");
  15. return -10;
  16. }
  17. int port = atoi(argv[1]);
  18. hloop_t* loop = hloop_new(0);
  19. hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
  20. if (listenio == NULL) {
  21. return -20;
  22. }
  23. hloop_run(loop);
  24. hloop_free(&loop);
  25. return 0;
  26. }

c++版本:

  1. #include "hv/TcpServer.h"
  2. using namespace hv;
  3. int main(int argc, char* argv[]) {
  4. if (argc < 2) {
  5. printf("Usage: %s port\n", argv[0]);
  6. return -10;
  7. }
  8. int port = atoi(argv[1]);
  9. TcpServer srv;
  10. int listenfd = srv.createsocket(port);
  11. if (listenfd < 0) {
  12. return -20;
  13. }
  14. printf("server listen on port %d, listenfd=%d ...\n", port, listenfd);
  15. srv.onConnection = [](const SocketChannelPtr& channel) {
  16. std::string peeraddr = channel->peeraddr();
  17. if (channel->isConnected()) {
  18. printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
  19. } else {
  20. printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
  21. }
  22. };
  23. srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
  24. // echo
  25. printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
  26. channel->write(buf);
  27. };
  28. srv.onWriteComplete = [](const SocketChannelPtr& channel, Buffer* buf) {
  29. printf("> %.*s\n", (int)buf->size(), (char*)buf->data());
  30. };
  31. srv.setThreadNum(4);
  32. srv.start();
  33. while (1) sleep(1);
  34. return 0;
  35. }

优点:

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

asio

asio有C++11版本,不依赖boost。Asio:http://think-async.com/

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <boost/bind.hpp>
  4. #include <boost/asio.hpp>
  5. using boost::asio::ip::tcp;
  6. class session {
  7. public:
  8. session(boost::asio::io_service& io_service) :
  9. socket_(io_service) {
  10. }
  11. tcp::socket& socket() {
  12. return socket_;
  13. }
  14. void start() {
  15. socket_.async_read_some(boost::asio::buffer(data_, max_length),
  16. boost::bind(&session::handle_read, this,
  17. boost::asio::placeholders::error,
  18. boost::asio::placeholders::bytes_transferred));
  19. }
  20. void handle_read(const boost::system::error_code& error,
  21. size_t bytes_transferred) {
  22. if (!error) {
  23. boost::asio::async_write(socket_, boost::asio::buffer(data_,
  24. bytes_transferred), boost::bind(&session::handle_write,
  25. this, boost::asio::placeholders::error));
  26. } else {
  27. delete this;
  28. }
  29. }
  30. void handle_write(const boost::system::error_code& error) {
  31. if (!error) {
  32. socket_.async_read_some(boost::asio::buffer(data_, max_length),
  33. boost::bind(&session::handle_read, this,
  34. boost::asio::placeholders::error,
  35. boost::asio::placeholders::bytes_transferred));
  36. } else {
  37. delete this;
  38. }
  39. }
  40. private:
  41. tcp::socket socket_;
  42. enum {
  43. max_length = 1024
  44. };
  45. char data_[max_length];
  46. };
  47. class server {
  48. public:
  49. server(boost::asio::io_service& io_service, short port) :
  50. io_service_(io_service), acceptor_(io_service, tcp::endpoint(tcp::v4(),
  51. port)) {
  52. session* new_session = new session(io_service_);
  53. acceptor_.async_accept(new_session->socket(), boost::bind(
  54. &server::handle_accept, this, new_session,
  55. boost::asio::placeholders::error));
  56. }
  57. void handle_accept(session* new_session,
  58. const boost::system::error_code& error) {
  59. if (!error) {
  60. new_session->start();
  61. new_session = new session(io_service_);
  62. acceptor_.async_accept(new_session->socket(), boost::bind(
  63. &server::handle_accept, this, new_session,
  64. boost::asio::placeholders::error));
  65. } else {
  66. delete new_session;
  67. }
  68. }
  69. private:
  70. boost::asio::io_service& io_service_;
  71. tcp::acceptor acceptor_;
  72. };
  73. int main(int argc, char** argv) {
  74. if (argc < 2) {
  75. printf("Usage: cmd port\n");
  76. return -10;
  77. }
  78. int port = atoi(argv[1]);
  79. boost::asio::io_service io_service;
  80. server s(io_service, port);
  81. io_service.run();
  82. return 0;
  83. }

优点:

  • 曾被提名标准网络库,可惜boost不能整体入标准库,asio自然也不得成神;
  • 极具争议,赞者骂者参半
  • beast支持HTTPWebSocket;
  • websocketpp支持WebSocket协议;

ACE

ACE:https://github.com/cflowe/ACE

ACE也是很经典的网络库,出自《C++网络编程》作者之手,设计精妙程度堪称一流,支持协议范围也很广,但是使用复杂度和学习复杂度较高,一直有“学我者生,用我者死”的评价。

总结

  • aio 是linux2.6以后内核实现的异步IO,或者说他才是真正意义上的异步IO。
  • epoll作为select的linux的替代品,解决了selectfd_set的限制。性能优于select。而在mac os x平台上替代方案是kqueue。
  • libevent是一个跨平台异步解决方案,他根据不同的平台提供了不同的异步方案,采用Reactor模型实现。
  • Boost::asio是一个跨平台的网络及底层IO的C++编程库,实现了对TCP、UDP、ICMP、串口的支持。对于读写方式,ASIO支持同步和异步两种方式。采用了epoll来实现,插入了大量的信号处理。Asio库不需要单独便于,但是测试过程中对boost::system的依赖可能会需要编译部分boost中的库。
  • muduo采用Reactor模型实现的网络库,只支持Linux 2.6.x下的并发非阻塞TCP网络编程,不跨平台,不支持udp和ipv6。吞吐量方面muduo比libevent2快18%,在事件处理效率方面,muduo与libevent2总体比较接近,muduo吞吐量比boost.asio高15%以上。性能方面作为解决大数据吞吐量很有优势,但是对平台和网络协议支持方面是一个问题。
  • aio 和 epoll 主要是对异步提供解决方案,不是网络库不提供网络支持。而 libevent 也是主要解决 IO 的问题但是只提供简单的http支持。asio 和 muduo 还有 ACE 一样是高性能网络库。

3、QUIC 协议

QUIC 全称:Quick UDP Internet Connections,是一种基于 UDP 的传输层协议。由 Google 自研,2012 年部署上线,2013 年提交 IETF,2021 年 5 月,IETF 推出标准版 RFC9000。

QUIC协议是传输层 "更快、更稳、更高效" 的网络通信协议。

QUIC 协议是在 UDP 的基础之上,集成了 TCP 的可靠传输特性,集成了 TLS1.3 协议,保证了用户数据传输的安全。

QUIC 协议详解

https://zhuanlan.zhihu.com/p/405387352

一文读懂 QUIC 协议

https://zhuanlan.zhihu.com/p/655070575

传输层协议:QUIC

https://cloud.tencent.com/developer/article/2336551

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/554116
推荐阅读
相关标签
  

闽ICP备14008679号