添加encode编码接口
This commit is contained in:
parent
1be24d9298
commit
282276e80e
@ -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)长度+消息ID(message _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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user