Giter VIP home page Giter VIP logo

coco's Introduction

coco

A lightweight and easy to use async IO library implemented with io_uring and C++20 coroutine.

Build

This project depends on liburing. Please install liburing-devel(SUSE/RHEL based distributions) or liburing-dev(Debian based distributions) before building this project.

CMake Options

Option Description Default Value
COCO_BUILD_STATIC_LIBRARY Build coco shared library. The shared target is coco::shared. ON
COCO_BUILD_SHARED_LIBRARY Build coco static library. The static target is coco::static. ON
COCO_ENABLE_LTO Enable LTO if possible. ON
COCO_WARNINGS_AS_ERRORS Treat warnings as errors. OFF
COCO_BUILD_EXAMPLES Build coco examples. OFF

If both of the static target and shared target are build, the default target coco::coco and coco will be set to the static library.

Examples

Echo Server

See examples/echo for details.

auto echo(tcp_connection connection) noexcept -> task<void> {
    char            buffer[4096];
    size_t          bytes;
    std::error_code error;

    for (;;) {
        error = co_await connection.receive(buffer, sizeof(buffer), bytes);
        // Handle errors.
        if (bytes == 0) 
            // Connection closed.

        error = co_await connection.send(buffer, bytes, bytes);
        // Handle errors.
    }
    co_return;
}

auto acceptor(io_context &io_ctx, tcp_server server) noexcept -> task<void> {
    std::error_code error;
    tcp_connection  connection;
    for (;;) {
        error = co_await server.accept(connection);
        // Handle errors.
        io_ctx.execute(echo(std::move(connection)));
    }
}

coco's People

Contributors

longhao-li avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

coco's Issues

关于 io_context_worker::execute(task<T> coro) 方法实现的疑问

在 task.hpp 817行中, io_context_workerexecute(task<T> coro) 方法中,调用了 schedule(std::coroutine_handle<> coro) 方法

execute 方法不是将协程插入队列并立即唤醒 worker 吗,不应该调用 execute(std::coroutine_handle<> coro) 方法吗

请问是有其他的考虑吗,望指点

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.