Netty LengthFieldBasedFrameDecoder未调用下一个处理程序

Netty LengthFieldBasedFrameDecoder未调用下一个处理程序,netty,Netty,我使用LengthFieldBasedFrameDecoder作为从客户端发送的入站流量的第一个处理程序 长度:4字节 有效负载:你好-5字节 [0,0,0,9,72,101,108,108,111] ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelIn

我使用
LengthFieldBasedFrameDecoder
作为从客户端发送的入站流量的第一个处理程序

长度:4字节 有效负载:你好-5字节

[0,0,0,9,72,101,108,108,111]

ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
    .channel(NioServerSocketChannel.class)
    .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("frameDecoder", new PacketFrameDecoder());
            ch.pipeline().addLast(new GameServerHandler());
        }
    })
    .option(ChannelOption.SO_BACKLOG, 128)
    .childOption(ChannelOption.SO_KEEPALIVE, true);

// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync();
之后,我想在扩展了ChannelInboundHandlerAdapter的
GameServerHandler
中处理我的身体/负载

public class GameServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * This method is called with the received message, whenever new data is received from a client.
     * @param context
     * @param message
     */
    @Override
    public void channelRead(ChannelHandlerContext context, Object message) {
        Log.i("GameServerHandler"); //Breakpoint here
        ByteBuf in = (ByteBuf) message;
        try {
            String test = in.toString(io.netty.util.CharsetUtil.US_ASCII);
            Log.i(test);
        } finally {
            ReferenceCountUtil.release(message);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        context.close();
    }

}

但是my
GameServerHandler
中的
channelRead
未被调用或激发。我不知道我做错了什么。

我刚刚发现问题似乎与长度调整有关。 我的标题(4字节)包含在总长度中,因此有2个选项:

  • 从长度字段调整中减去标题长度:
    length\u Field\u Adjustment=-4
  • 发送消息时不要封装报头长度,只包含有效负载
  • public class GameServerHandler extends ChannelInboundHandlerAdapter {
    
        /**
         * This method is called with the received message, whenever new data is received from a client.
         * @param context
         * @param message
         */
        @Override
        public void channelRead(ChannelHandlerContext context, Object message) {
            Log.i("GameServerHandler"); //Breakpoint here
            ByteBuf in = (ByteBuf) message;
            try {
                String test = in.toString(io.netty.util.CharsetUtil.US_ASCII);
                Log.i(test);
            } finally {
                ReferenceCountUtil.release(message);
            }
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
            // Close the connection when an exception is raised.
            cause.printStackTrace();
            context.close();
        }
    
    }