DelimiterBasedFrameDecoder使用

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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章