0
votes

I made a server with Netty but I'm having a problem. 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.

Thanks

1
What does your packet.encode() return?Ferrybig
@Ferrybig at this moment nothing. The print is no executed.AtuaPrimade4
I don't understand you. It should return a Packet object, this packet object will then be passed further down the chain to the encoderFerrybig
@Ferrybig nevermind xD It returns a Packet object like you said. I have just omitted because i do allot of stuff there.AtuaPrimade4
Can you add the full code of the project, maybe the error is somewhere else?Ferrybig

1 Answers

2
votes

Your encoder extends MessageToByteEncoder<Packet> so it is called when a Packet is received in input to encode it.

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.

You probably should do something like that:

channelHandlerContext.writeAndFlush( packet );