本帖最后由 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();
}
}
|