[Update]自定义协议并解决TCP粘包/拆包问题

This commit is contained in:
chenfufeng
2022-02-14 15:58:48 +08:00
parent 4e2c6ffd7a
commit fc89d1a34b
10 changed files with 227 additions and 84 deletions

View File

@@ -0,0 +1,20 @@
package com.mogo.telematic;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
public class MogoMessageEncoder extends MessageToByteEncoder<MogoProtocolMsg> {
@Override
protected void encode(ChannelHandlerContext ctx, MogoProtocolMsg msg, ByteBuf out) throws Exception {
int protocolType = msg.getProtocolType();
int length = msg.getBodyLength();
byte[] body = msg.getBody();
out.writeInt(protocolType);
out.writeInt(length);
if (length > 0) {
out.writeBytes(body);
}
}
}