Multiple messages contained in one net buffer problem

I created a login server for the game client using the network network library.
This game client likes to send several packets to one buffer, which creates a problem; this problem is that in the netty decoding class it can only return one message.

Then it is impossible for me to read several packets in several messages and return them to one call to the decoding method.

My question is: what is the best way to use multiple packages in one call to the DecoderClass.decode () method? Since I can only return one object, I am puzzled.

My preliminary decoding class is below:

protected Object decode(ChannelHandlerContext ctx, Channel c, ChannelBuffer buf, VoidEnum state) throws Exception { short length = -1; short opcode = -1; short security = -1; while(buf.readableBytes() != 0 ){ length = buf.readShort(); opcode = buf.readShort(); security = buf.readShort(); } System.out.println("---------------------------------------"); System.out.println("receivedLength: " + length); System.out.println("receivedOPCode: " + opcode); System.out.println("receivedSecurity: " + security); System.out.println("---------------------------------------"); MessageCodec<?> codec = CodecLookupService.find(opcode); if (codec == null) { throw new IOException("Unknown op code: " + opcode + " (previous opcode: " + previousOpcode + ")."); } previousOpcode = opcode; return codec.decode(buf); 

My full github repository is here: https://github.com/desmin88/LoginServer

I hope I have provided enough information so that someone can adequately understand my problem.

Thanks,

Billy

+4
source share
1 answer

You want to use FrameDecoder to split the received data into several β€œframes” to go to your decoder. The API link for FrameDecoder has sample code .

Instead of commenting more, you would do something like this:

  • Implement your own FrameDecoder or use one of the existing ones. Let's say you implement your own MyGameFrameDecoder . If you are writing your own, I recommend checking out ReplayingDecoder (this is a bad ass).
  • Add MyGameFrameDecoder to the ChannelPipeline along with an existing decoder ( DecoderClass ).

Which would look something like this:

 /* ... stuff ... */ pipeline.addLast("framer", new MyGameFrameDecoder()); pipeline.addLast("decoder", new DecoderClass()); /* ... more stuff ... */ 

Then the incoming data will go through FrameDecoder and break the stream into "frames", which will then be sent to your decoder, which can simply handle the conversion of data into an object that you can work on.

+4
source

Source: https://habr.com/ru/post/1440639/


All Articles