消息解析接口

This commit is contained in:
walker 2023-06-06 09:43:17 +08:00
parent ea4cfdd042
commit 2615bbf3d6
2 changed files with 35 additions and 3 deletions

View File

@ -1,9 +1,11 @@
package club.joylink.xiannccda.ats.message;
public class MessageData {
import io.netty.buffer.ByteBuf;
public abstract class MessageData {
/**
* 消息的长度: 时间戳Time长度+ 版本号Version长度+消息IDmessage _id长度2字节+ 消息内容content长度
* 2字节,消息的长度: 时间戳Time长度+ 版本号Version长度+消息IDmessage _id长度2字节+ 消息内容content长度
*/
int length;
/**
@ -11,8 +13,33 @@ public class MessageData {
*/
long time;
/**
* 用来区分协议的版本本版指定为01H
* 2字节,用来区分协议的版本本版指定为01H
*/
int version;
/**
* 2字节,消息id
*/
MessageId msgId;
public void decode(ByteBuf buf) throws Exception {
final int headerBytes = 10;
int readableBytes = buf.readableBytes();
if (readableBytes >= headerBytes) {
this.length = buf.readUnsignedShort();
this.time = buf.readUnsignedInt();
this.version = buf.readUnsignedShort();
this.msgId = MessageId.of(buf.readShort());
} else {
throw new Exception(
String.format("OCC消息可读字节数小于%sreadableBytes=%s", headerBytes, readableBytes));
}
}
/**
* 解析公共消息的解析已经处理过只需继续往后读取并解析
*
* @param buf
* @throws Exception
*/
public abstract void decode2(ByteBuf buf) throws Exception;
}

View File

@ -1,7 +1,12 @@
package club.joylink.xiannccda.ats.message.line3;
import club.joylink.xiannccda.ats.message.MessageData;
import io.netty.buffer.ByteBuf;
public class HeartBeatMsg extends MessageData {
@Override
public void decode2(ByteBuf buf) throws Exception {
}
}