DelimiterBasedFrameDecoder是一个分隔符解码器。
FixedLengthFrameDecoder作用和它差不多,只不过是定长。
可以同时接受多个分隔符,第一个参数是缓冲区大小,如果长度超过1024(可以指定),并且没有找到分隔符,则会抛异常。
如果长度小于1024,并且没有找到分隔符,会缓存收到的消息,直到接收到分隔符,或者超出1024抛异常。
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
ByteBuf delimiter0 = Unpooled.copiedBuffer("A".getBytes());
ByteBuf delimiter1 = Unpooled.copiedBuffer("B".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter,delimiter0,delimiter1));
同时存在多个分隔符时,优先匹配长度最短的分隔符,如果一样长,则哪个先出现,匹配哪个。
应用示例
服务端
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,100)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
ByteBuf delimiter0 = Unpooled.copiedBuffer("A".getBytes());
ByteBuf delimiter1 = Unpooled.copiedBuffer("B".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter,delimiter0,delimiter1));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new EchoServerHandler());
}
});
ChannelFuture channelFuture =serverBootstrap.bind(7799).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public class EchoServerHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String o) throws Exception {
System.out.println(o);
o +="$_";
ByteBuf echo = Unpooled.copiedBuffer(o.getBytes());
channelHandlerContext.writeAndFlush(echo);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端
public class EchoClient {
public void connect(int port,String host){
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY,true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
// ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
// socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture channelFuture=b.connect(host,port).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) {
EchoClient echoClient = new EchoClient();
echoClient.connect(7799,"127.0.0.1");
}
}
public class EchoClientHandler extends SimpleChannelInboundHandler<String> {
public static final String STRINGTAG="HELLO,这是什";
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// for (int i=0;i<10;i++){
ctx.writeAndFlush(Unpooled.copiedBuffer(STRINGTAG.getBytes()));
Thread.sleep(3000);
ctx.writeAndFlush(Unpooled.copiedBuffer("么你A好吗$_不是B".getBytes()));
// ctx.writeAndFlush(Unpooled.copiedBuffer(new byte[1024]));
// }
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String o) throws Exception {
System.out.println(o);
}
}