Giter VIP home page Giter VIP logo

libaco-sched's Introduction

Build Status

A coroutine scheduler and lightweight network library for libaco.

Introduction

libaco-sched is a N:1 cooperative multi-task library, which enables you to build concurrent network applications easily.

Install

git clone https://github.com/guangqianpeng/libaco-sched
cd libaco-sched
mkdir build && cd build
cmake .. && make && make install

Example

Here is a simple echo server which handles at most 1024 concurrent TCP connections:

#include "app_template.h"
#include "aco_assert_override.h"

void session() {
    // get connection socket
    int fd = (int)(int64_t)aco_get_arg();
    
    // create echo buffer
    char buf[65536];

    while(1) {
        int64_t err;
		
        // read data
        size_t read_n = aco_read_timeout(fd, buf, sizeof(buf), 10000, &err);
        if (err != ACO_OK) {
            break;
        }
		
        // write data
        size_t write_n = aco_write(fd, buf, read_n, &err);
        if (err != ACO_OK) {
            break;
        }

        assert(read_n == write_n);
    }
    aco_socket_close(fd);
    aco_exit();
}

int main() {
    app_config_t config;
    aco_config_init(&config);

    config.type = APP_NETWORK_SERVER;
    config.name = "echo";
    
    // set maximum concurrent tasks in the process
    config.task_pool_size = 1024;
    
    // listen 0.0.0.0:2007
    config.ip = "0.0.0.0";
    config.port = 2007;
    
    // use session() to handle connection sockets
    config.connection_func = session;
    
    // log level: debug, info, error
    config.log_level = ACO_LOG_LEVEL_DEBUG;

    // start running
    app_template_run(&config);
}

A more complex example is here: socks4 proxy.

libaco-sched's People

Contributors

guangqianpeng 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.