简体   繁体   English

净值编码器未执行

[英]Netty encoder not executed

I made a server with Netty but I'm having a problem. 我用Netty制造了一台服务器,但是遇到了问题。 The encoder that i created is not being executed. 我创建的编码器未执行。

My pipeline on the server: 我在服务器上的管道:

            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("encoder", new PacketEncoder());
                ch.pipeline().addLast("decoder", new PacketDecoder());
                ch.pipeline().addLast(new LoginServerChannel());
            }
        });

My encoder: 我的编码器:

public class PacketEncoder extends MessageToByteEncoder<Packet> {

@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Packet packet, ByteBuf byteBuf) throws Exception {
    System.out.println("Encoding");
}

My decoder: 我的解码器:

public class PacketDecoder extends ReplayingDecoder<DecoderState> {

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    ...
}

My channel handler: 我的频道处理程序:

public class LoginServerChannel extends SimpleChannelInboundHandler<Packet> {

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Packet packet) throws Exception {
    channelHandlerContext.writeAndFlush(packet.encode());

    System.out.println("Sending");
}

The decoder is called and then the channel handler but not the encoder. 调用解码器,然后调用通道处理程序,但不调用编码器。 I have tried to change the order in the pipeline but same problem also try to used fireChannelRead(...) and same problem. 我试图更改管道中的顺序,但是同样的问题也尝试使用fireChannelRead(...)和同样的问题。

Thanks 谢谢

Your encoder extends MessageToByteEncoder<Packet> so it is called when a Packet is received in input to encode it. 您的编码器扩展了MessageToByteEncoder<Packet>因此在输入中接收到数据包进行编码时会调用它。

In your logic handler, you do 在您的逻辑处理程序中

 channelHandlerContext.writeAndFlush( packet.encode() );

I suppose that encode() returns a byte array, so your encoder ignore it and do nothing. 我猜想encode()返回一个字节数组,所以您的编码器会忽略它,什么也不做。

You probably should do something like that: 您可能应该执行以下操作:

channelHandlerContext.writeAndFlush( packet );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM