[Update]自定义协议并解决TCP粘包/拆包问题
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user