添加encode编码接口

This commit is contained in:
walker 2023-06-06 11:13:24 +08:00
parent 1be24d9298
commit 282276e80e

View File

@ -1,6 +1,7 @@
package club.joylink.xiannccda.ats.message;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public abstract class MessageData {
@ -42,4 +43,30 @@ public abstract class MessageData {
* @throws Exception
*/
public abstract void decode2(ByteBuf buf) throws Exception;
/**
* 消息总长度: 时间戳Time长度+ 版本号Version长度+消息IDmessage _id长度2字节+ 消息内容content长度
*
* @return
*/
public abstract int messageLength();
public ByteBuf encode() {
final int messageLength = this.messageLength();
ByteBuf buf = Unpooled.buffer(messageLength + 2);
buf.writeShort(messageLength);
buf.writeInt((int) this.time);
buf.writeShort(this.version);
buf.writeShort(this.msgId.val);
this.encode2(buf);
return buf;
}
/**
* 编码通用部分已经处理只需接着往后写即可
*
* @param buf
*/
protected abstract void encode2(ByteBuf buf);
}