javanettyminecraft

Decoding and Encoding multiple packet types in Netty


So I am working on a netty server that handles a specific type packet. This netty server was not made by me and I am trying to add another type of packet handler, decoder, and encoder to it. Because of poor implementation of the existing handlers, I have to add mine before like so.

channel.pipeline()
    .addLast(new custom.PacketDecoder().server())  // My Packet Decoder
    .addLast(new custom.PacketEncoder().server())  // My Packet Encoder
    .addLast(new custom.NetworkHandler().server()) // My Packet Handler
    .addLast("timeout", new ReadTimeoutHandler(30)) // Not made by me
    .addLast("legacy_query", new LegacyPingHandler(ServerConnection.this)) // Not made by me
    .addLast("splitter", new PacketSplitter()) // Not made by me
    .addLast("decoder", new PacketDecoder(EnumProtocolDirection.SERVERBOUND)) // Not made by me
    .addLast("prepender", new PacketPrepender()) // Not made by me
    .addLast("encoder", new PacketEncoder(EnumProtocolDirection.CLIENTBOUND)) // Not made by me
    .addLast("packet_handler", networkmanager); // Not made by me

As of now this sort of works but the issue is that if it receives the packets that aren't mine that it should be able to handle, my handlers stop those packets from going through and being handled correctly. How do I pass packets that are not mine through to other handlers without touching them.


Solution

  • You handler should check if an inbound packet is of specific type, that the handler can process. If a packet is "not yours", handler should pass it upstream - to the next handler in pipeline (for netty 4.x - ChannelHandlerContext.fireChannelRead(Object)). Maybe this documentation can help.