Reputation: 33
I am trying to connect to an tcp/ip interface which sends many different packet types. Each packet is different in length and content. I simply want to process each packet type and generate POJOs which will be processed again by another state-handler. So far I am not sure if there is any structure in Netty which supports this type of processing packets/frames. One solution I could think of, was to create one decoder inbound handler which manipulates the pipeline depending on the first byte (which is the type field). Which structure or algorithm in Netty could help me to realize such simple Switch-Case problem?
thx, Tom
Upvotes: 2
Views: 1950
Reputation: 12351
If your connection is supposed to handle the stream of same packet types as the first packet type (i.e. the first packet determines the state of the connection), you could take a look into the port unification example.
If your connection is supposed to handle the stream of arbitrary packet types, you'd better write a decoder that understands all packet types and converts them into a POJO. Unless the number of packet types to handle are too many, it shouldn't be very difficult. Once a decoder decodes a packet, your last handler in the pipeline will look like the following:
public class MyPacketHandler extends SimpleChannelInboundHandler {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof MsgA) {
handleA(ctx, (MsgA) msg);
} else if (msg instanceof MsgB) {
handleB(ctx, (MsgB) msg);
} ...
}
private void handleA(ChannelHandlerContext ctx, MsgA msg) {
...
}
...
}
If you do not like the tedious if-else blocks, you could make use of a java.util.Map
and Class.isAssignableFrom()
.
Upvotes: 4
Reputation: 23567
Check the portunification example which does something similar:
Upvotes: 2