Skip to content

Commit 8c766a3

Browse files
committed
2 parents 586d337 + d78fa12 commit 8c766a3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

CORE FUNCTIONS/Decoding delimited and length-based protocols.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,25 @@ Figure 8.5 Handling delimited frames
2727
下面展示了如何用 LineBasedFrameDecoder 处理
2828

2929
Listing 8.8 Handling line-delimited frames
30-
30+
31+
public class LineBasedHandlerInitializer extends ChannelInitializer<Channel> {
32+
33+
@Override
34+
protected void initChannel(Channel ch) throws Exception {
35+
ChannelPipeline pipeline = ch.pipeline();
36+
pipeline.addLast(new LineBasedFrameDecoder(65 * 1024)); //1
37+
pipeline.addLast(new FrameHandler()); //2
38+
}
39+
40+
public static final class FrameHandler extends SimpleChannelInboundHandler<ByteBuf> {
41+
@Override
42+
public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { //3
43+
// Do something with the frame
44+
}
45+
}
46+
}
47+
48+
1. 添加一个 LineBasedFrameDecoder 用于提取帧并把数据包转发到下一个管道中的处理程序,在这种情况下就是 FrameHandler
49+
2. 添加 FrameHandler 用于接收帧
50+
3. 每次调用都需要传递一个单帧的内容
3151

0 commit comments

Comments
 (0)