项目Github主页 Coke。
在这个时间点开发本项目,有以下几点考虑
- 常用的编译器对C++ 20的支持已经逐步完善,本项目依赖于
GCC >= 11
或Clang >= 15
- 常用的操作系统发行版支持了新编译器,例如
CentOS Stream 8
、Ubuntu 22.04
、Fedora 38
等 - C++ Workflow使用回调函数的方式组织异步任务,一部分习惯写同步代码的用户可能会对此感到困扰,尤其是随着回调函数个数的增加,其理解难度也会越来越高
借助C++ 20提供的协程组件,Coke使C++ Workflow的任务以顺序的方式编写,大大简化了理解的复杂度。Coke项目的一项重要目标就是简洁、便捷,让代码写起来方便、读起来方便、跑起来也方便。
下面通过发起Http请求的示例来展示Coke的便捷性
#include <iostream>
#include <string>
#include <string_view>
#include "coke/coke.h"
#include "coke/http_client.h"
#include "coke/http_utils.h"
void show_response(const coke::HttpResponse &resp) {
std::cout << resp.get_http_version() << ' '
<< resp.get_status_code() << ' '
<< resp.get_reason_phrase() << std::endl;
// 使用 coke::HttpHeaderCursor 方便地遍历所有header
for (const coke::HttpHeaderView &header : coke::HttpHeaderCursor(resp))
std::cout << header.name << ": " << header.value << std::endl;
std::cout << "\n";
// 为了简洁起见,省略了Http body内容的输出
if (resp.is_chunked()) {
// 对于Chunk编码的Http body,可使用coke::HttpChunkCursor来遍历
for (std