Giter Club home page Giter Club logo

co_async's Introduction

co_async

基于 C++20 协程的高并发异步 I/O 库(小彭老师教学用)

教学视频

steps 目录 是本库代码逐渐成形的过程,可以配合教学视频自己动手尝试。

使用案例

import co_async;
import std;

using namespace co_async;

Task<> amain() {
    auto serv = co_await server_bind({"127.0.0.1", 8080});
    HTTPServer http;
    http.route("/", [] (HTTPRequest const &request) -> Task<HTTPResponse> {
        if (request.method != "GET")
            co_return HTTPServer::make_error_response(405);
        co_return {
            .status = 200,
            .headers = {
                {"content-type", "text/html;charset=utf-8"},
            },
            .body = "<h1>It works!</h1>",
        };
    });

    co_await stdio().putline("正在监听: " + serv.address().toString());
    while (1) {
        auto conn = co_await server_accept(serv);
        co_await stdio().putline("收到请求: " + serv.address().toString());
        co_spawn(http.process_connection(FileStream(std::move(conn))));
    }
}

int main() {
    co_spawn_and_wait(amain());
    return 0;
}

examples 目录 中有更多案例。

平台要求

  • Linux 内核 >= 5.1
  • GCC >= 10
  • Clang >= 16

小彭老师推荐使用 Arch Linux 系统作为开发平台

或者:

  • Windows >= 10(施工中,稍后支持)
  • Visual Studio >= 2022(施工中,稍后支持)

安装与导入

作为单个头文件导入

点击此处 下载 co_async.hpp,然后在你的项目中引入即可:

#include "co_async.hpp"

作为 C++20 模块导入

将本项目下载到你的项目中作为子文件夹,引入并链接:

add_subdirectory(co_async)
target_link_libraries(你的名字 PRIVATE co_async)

在你的代码中 import 即可:

import co_async;

需要 GCC >= 11、Clang >= 17、MSVC >= 19 以支持 C++20 模块

co_async's People

Contributors

archibate avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.