Coder Social home page Coder Social logo

mininetty's Introduction

实现的功能

  • channelActive/channelInActive/channelRead/channelWrite事件处理

  • HandlerInitializer

  • Handler链式处理

  • StringEncoder/StringDecoder

  • LengthFieldBasedDecoder

  • DelimiterBasedDecoder

  • LineBasedDecoder

线程模型

ThreadMode

示例

服务器启动

以定长解码器为例:

@Test
public void lengthFieldBasedDecoder() {
    Server server = new Server();
    server.bind(8080).setHandlers(new HandlerInitializer() {
        @Override
        public Handler[] init() {
            return new Handler[] {new LengthFieldBasedDecoder(0, 4), 
                new StringDecoder(), new SimpleInBoundHandler()};
        }
    }).start();
}

SimpleInBoundHandler

简单地打印出事件触发以及收到的消息:

public class SimpleInBoundHandler extends InBoundHandlerAdapter {
    @Override
    public void channelActive(HandlerContext context) {
        System.out.println("channel active");
    }
    @Override
    public void channelInActive(HandlerContext context) {
        System.out.println("channel inActive");
    }
    @Override
    public void channelRead(Object message, HandlerContext context) {
        System.out.println(message.toString());
    }
}

客户端

数据发送代码:

@Test
public void lengthFieldBasedDecoder() throws IOException, InterruptedException {
    byte[] result = new byte[35];
    System.arraycopy(DataUtils.int2Bytes(31), 0, result, 0, 4);
    System.arraycopy("org.apache.commons.lang.builder".getBytes(), 0, result, 4, 31);
    for (int i = 0; i < 6; i++) {
        bos.write(result);
    }
    TimeUnit.SECONDS.sleep(6);
}

mininetty's People

Contributors

seaswalker avatar

Watchers

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