Giter VIP home page Giter VIP logo

coroutine-cpp's Introduction

CoroutineCPP

CoroutineCPP is a wrapper for epoll and socket which is implemented with C++20. It supports the syntax of coroutine and can be easily used out of box.

Feature

  • coroutine supported

Usage

If you use CMake for your project. All you need is adding some code to your CMakeLists.txt and add coroutinecpp as a dependency.

    add_subdirectory(coroutinecpp)

    target_link_library(<your_project> PUBLIC coroutinecpp)

Example

You can write a echo server like this.

co_cpp::Task<void> echo(co_cpp::Socket sock)
{
    auto &ctx = co_await this_core::excutor;
    while (true)
    {
        auto [content, ok_read] = co_await sock.read();
        if (!ok_read)
        {
            co_cpp::log("return code:", ok_read);
            co_return;
        }
        auto [len, ok_write] = co_await sock.write(content);
        if (!ok_write)
        {
            co_cpp::log("return code:", ok_write);
            co_return;
        }
    }
}

co_cpp::Task<void> listen(co_cpp::Socket sock)
{
    auto &ctx = co_await this_core::excutor;
    while (true)
    {
        auto [sock_fd, ok] = co_await sock.accept();
        if (!ok)
        {
            co_return;
        }
        co_cpp::co_spawn(ctx, echo(co_cpp::Socket{ctx,sock_fd}));
    }
}

int main()
{
    co_cpp::IOContext ctx;
    co_cpp::Socket socket(ctx);
    socket.bind(8080);
    co_cpp::co_spawn(ctx, listen(std::move(socket)));
    ctx.run();

    return 0;
}

Getting the context in your coroutine frame is easy, just use co_await this_core::excutor, the reference of context is returned immediately.

co_cpp::Task<T> holds the promise_type which is needed in coroutine. Forget the promise_type, the lifetime is handled well by Task and IOContext.

coroutine-cpp's People

Contributors

kidsunbo avatar

Watchers

James Cloos avatar  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.