https://github.com/seaswalker/netty-wheel
简单netty轮子
https://github.com/seaswalker/netty-wheel
netty nio reinvent-the-wheel
Last synced: 6 months ago
JSON representation
简单netty轮子
- Host: GitHub
- URL: https://github.com/seaswalker/netty-wheel
- Owner: seaswalker
- License: apache-2.0
- Created: 2016-08-26T13:08:58.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T17:48:14.000Z (over 4 years ago)
- Last Synced: 2023-02-28T04:32:04.113Z (over 2 years ago)
- Topics: netty, nio, reinvent-the-wheel
- Language: Java
- Homepage:
- Size: 80.1 KB
- Stars: 19
- Watchers: 4
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/seaswalker/netty-wheel)
[](https://codecov.io/gh/seaswalker/netty-wheel)# 实现的功能
- channelActive/channelInActive/channelRead/channelWrite事件处理
- HandlerInitializer
- Handler链式处理
- StringEncoder/StringDecoder
- LengthFieldBasedDecoder
- DelimiterBasedDecoder
- LineBasedDecoder
# 线程模型

# 示例
## 服务器启动
以定长解码器为例:
```java
@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
简单地打印出事件触发以及收到的消息:
```java
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());
}
}
```## 客户端
数据发送代码:
```java
@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);
}
```# TODO
为了确保测试用例能够通过,使用了基于端口取余的worker线程分配方式,这样保证了向client发送数据的先后顺序。目前写功能很原始,只是将经过OutboundHandler处理的数据不做任何操作直接写入,这样的问题在于:
1. OS写缓冲区满,写入失败
2. OS缓冲区可用空间不足一次性写入,要分多次写入
3. 客户端如果已断开或网络出问题怎么办,可能需要心跳检测之类的手段