简单netty轮子
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
images
src random port Jul 3, 2018
.gitignore
.travis.yml
LICENSE Initial commit Aug 26, 2016
README.md codecov Jul 3, 2018
pom.xml

README.md

Build Status codecov

实现的功能

  • 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);
}