Git Product home page Git Product logo

co-nio's Introduction

co-nio

A simple nio framework based on coroutines

co-nio features

  1. Nio and aio group based coroutine scheduler.
  2. Push and pull mode coroutine, coroutine channel.
  3. Coroutine future for supporting async programming style.
  4. Computation task execution in a worker thread pool.
  5. Using offbynull's high performance coroutines library.
  6. Coroutine and coroutine channel timer.
  7. Pull coroutine channel connection pool.

a sample

First we boot the server,

final CoGroup serverGroup = CoGroup.newBuilder()
    .setHost(HOST)
    .setName("serverCoGroup")
    .channelInitializer((channel, sside) -> {
        if(sside) {
            final PushCoChannel chan = (PushCoChannel)channel;
            chan.handler(new EchoServerHandler());
         }
     })
    .build();
serverGroup.start();

The server handler,

public class EchoServerHandler implements CoHandler {
    final static Logger log = LoggerFactory.getLogger(EchoServerHandler.class);

    final ByteBuffer buffer;

    public EchoServerHandler(){
        this(8192);
    }

    public EchoServerHandler(final int bufferSize){
        buffer = ByteBuffer.allocate(bufferSize);
    }

    @Override
    public void handle(Continuation co) {
        final PushCoChannel channel = (PushCoChannel)co.getContext();
        try{
            for(;!channel.group().isShutdown();){
                // co-blocking read but not thread-blocking
                final int n = channel.read(co, buffer);
                if(n == -1){
                    break;
                }
                //log.debug("{}: recv {} bytes", channel.name, n);
                buffer.flip();
                for(;buffer.hasRemaining();) {
                    // co-blocking write but not thread-blocking
                    final int i = channel.write(co, buffer);
                    //log.debug("{}: send {} bytes", channel.name, i);
                }
                buffer.clear();
            }
        }catch(final IOException e){
            log.warn("IO error", e);
        }finally {
            IoUtils.close(channel);
        }
    }

}

Then boot the client,

final CoGroup clientGroup = CoGroup.newBuilder()
    .setName("clientCoGroup")
    .build();
    clientGroup.start();

    final int n = 10;
    final EchoClientHandler handlers[] = new EchoClientHandler[n];
    for(int i = 0; i < n; ++i){
        final EchoClientHandler handler = new EchoClientHandler(1024);
        // async-connect
        clientGroup.connect(HOST, PORT, handler);
        handlers[i] = handler;
    }
    sleep(30 * 1000L);

    clientGroup.shutdown();
    clientGroup.await();

The client handler,

public class EchoClientHandler extends BaseTest implements CoHandler {
    final static Logger log = LoggerFactory.getLogger(EchoClientHandler.class);

    final ByteBuffer buffer;
    final byte[] data;

    public EchoClientHandler(){
        this(8192);
    }

    public EchoClientHandler(final int bufferSize){
        buffer = ByteBuffer.allocate(bufferSize);
        data   = new byte[bufferSize];
        for(int i = 0, size = data.length; i < size; ++i){
            data[i] = (byte)i;
        }
    }

    @Override
    public void handle(Continuation co) {
        final PushCoChannel channel = (PushCoChannel)co.getContext();
        try{
            final ByteBuffer dbuf = ByteBuffer.wrap(data);
            final CoGroup group = channel.group();
            for(;!group.isShutdown();){
                for(;dbuf.hasRemaining();){
                    // co-blocking write but not thread-blocking
                    final int n = channel.write(co, dbuf);
                    bytes += n;
                    //log.debug("{}: send {} bytes", channel.name, n);
                }
                dbuf.flip();
                for(;buffer.hasRemaining();){
                    // co-blocking read but not thread-blocking
                    final int n = channel.read(co, buffer);
                    if(n == -1){
                        throw new EOFException("Server closed");
                    }
                    bytes += n;
                }
                buffer.flip();
                if(!Arrays.equals(data, buffer.array())){
                    throw new IOException("Packet malformed");
                }
                buffer.clear();
                ++times;
            }
        }catch(final IOException e){
            log.warn("IO error", e);
        }finally {
            IoUtils.close(channel);
        }
    }

}

co-nio's People

Contributors

forchid avatar uncle-litpan avatar uncle-pan avatar

Stargazers

 avatar

Watchers

 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.