吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 648|回复: 1
收起左侧

[求助] nettty ByteBuf 被方法调用后直接返回出错

[复制链接]
ppgjx 发表于 2023-1-23 12:27
本帖最后由 ppgjx 于 2023-1-23 12:28 编辑

我建立了一个netty 服务器 然后添加了pip两个编解码 编解码原样返回ByteBuf 然后就会出现一个问题 走不通 按理说是原样返回的 但是我把UtilMy 编解码改成 buf.copy() 再返回就可以了 有人知道是怎么回事吗
[Asm] 纯文本查看 复制代码
                     pipeline.addLast(new ChannelInboundHandlerAdapter(){

                            //浏览器和本地服务器建立链接
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("浏览器与服务器建立成功");
                                server = ctx.channel();
                                EventLoopGroup boss = new NioEventLoopGroup(2);
                                Bootstrap bootstrap = new Bootstrap();
                                bootstrap.group(boss)
                                        .channel(NioSocketChannel.class)
                                        .remoteAddress("localhost", 9999)
                                        .option(ChannelOption.TCP_NODELAY, true)//禁用 Nagle 算法
                                        .handler(new ChannelInitializer<SocketChannel>() {
                                            @Override
                                            protected void initChannel(SocketChannel ch) throws Exception {
                                                ChannelPipeline pipeline = ch.pipeline();

                                                ch.pipeline().addLast(new MessageToMessageEncoder<ByteBuf>() {
                                                    @Override
                                                    protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
                                                        ByteBuf byteBuf = UtilMy.enCode(msg);
                                                        out.add(byteBuf);
                                                    }
                                                });

                                                ch.pipeline().addLast(new MessageToMessageDecoder<ByteBuf>() {
                                                    @Override
                                                    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
                                                        System.out.println("123");
                                                        ByteBuf byteBuf = UtilMy.deCode(msg);
                                                        out.add(byteBuf);
                                                    }
                                                });

                                                pipeline.addLast(new ChannelOutboundHandlerAdapter(){
                                                    @Override
                                                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                                        System.out.println("写出异常");
                                                        super.exceptionCaught(ctx, cause);
                                                    }

                                                    @Override
                                                    public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
                                                        System.out.println("链接被关闭");
                                                        super.close(ctx, promise);
                                                    }

                                                    @Override
                                                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

                                                        super.write(ctx, msg, promise);
                                                    }
                                                });
                                                pipeline.addLast(new ChannelInboundHandlerAdapter(){
                                                    @Override
                                                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                                        System.out.println("与远程服务器建立成功");
                                                        super.channelActive(ctx);
                                                    }

                                                    @Override
                                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//                                                        if(msg instanceof  ByteBuf){
//                                                            ByteBuf buf = (ByteBuf) msg;
//                                                            ByteBuf copy = buf.copy();
//                                                            System.out.println("client收到流量" + Arrays.toString(ByteBufUtil.getBytes(copy)));
//                                                        }
                                                        server.writeAndFlush(msg).sync();
                                                        super.channelRead(ctx, msg);
                                                    }
                                                });
                                            }
                                        });

                                ChannelFuture sync = bootstrap.connect().sync();

                                client = sync.channel();

                                super.channelActive(ctx);
                            }

[Asm] 纯文本查看 复制代码
public class UtilMy {

    public static ByteBuf enCode(ByteBuf buf){
//        byte[] bytes = ByteBufUtil.getBytes(buf.copy());
//        ByteBuf b = ByteBufAllocator.DEFAULT.buffer(bytes.length);

        return buf;
    }

    public static ByteBuf deCode(ByteBuf buf){
//        byte[] bytes = ByteBufUtil.getBytes(buf.copy());
//        ByteBuf b = ByteBufAllocator.DEFAULT.buffer(bytes.length);

        return buf;
    }

}


改成这样就能走的通

[Asm] 纯文本查看 复制代码
public class UtilMy {

    public static ByteBuf enCode(ByteBuf buf){
//        byte[] bytes = ByteBufUtil.getBytes(buf.copy());
//        ByteBuf b = ByteBufAllocator.DEFAULT.buffer(bytes.length);

        return buf.copy();
    }

    public static ByteBuf deCode(ByteBuf buf){
//        byte[] bytes = ByteBufUtil.getBytes(buf.copy());
//        ByteBuf b = ByteBufAllocator.DEFAULT.buffer(bytes.length);

        return buf.copy();
    }

}



发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hxr1023 发表于 2023-1-23 17:47
有可能这个netty服务器不支持UtilMy 编解码,试试其他编解码?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 01:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表