mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
119 lines
2.8 KiB
Java
119 lines
2.8 KiB
Java
package component;
|
|
|
|
import encoding.*;
|
|
|
|
/*
|
|
* @Author: Wang Feng
|
|
* @Description: 表示网络包负载
|
|
// 1. 格式如下:
|
|
// Payload = PAYLOAD-TYPE TLV-LENGTH *OCTET
|
|
// 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
|
|
* @Version: 1.0.0
|
|
* @Date: 19:26 2021/3/9
|
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
*/
|
|
public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
|
|
// 成员变量
|
|
private byte[] value;
|
|
|
|
public Payload(){
|
|
}
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param block
|
|
*/
|
|
public Payload(Block block){
|
|
this.buildPayloadByBlock(block);
|
|
}
|
|
|
|
/**
|
|
* 根据 TLV Block 初始化一个 Payload
|
|
* @param block
|
|
* @return
|
|
*/
|
|
public boolean buildPayloadByBlock(Block block){
|
|
return this.wireDecode(block);
|
|
}
|
|
|
|
/**
|
|
* 得到Payload TLV中实际包含的数据
|
|
* @return
|
|
*/
|
|
public byte[] getValue() {
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* 设置 Payload TLV 中的Value
|
|
* @param value
|
|
*/
|
|
public void setValue(byte[] value) {
|
|
this.value = value;
|
|
this.doInitial();
|
|
}
|
|
|
|
// 接口实现-变量区
|
|
private boolean initial=false;
|
|
|
|
// 接口实现-方法区
|
|
@Override
|
|
public void doInitial() {
|
|
initial=true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isInitial() {
|
|
return initial;
|
|
}
|
|
|
|
/**
|
|
* 将 Payload 线速编码为一个 TLV
|
|
* @param encoder
|
|
* @return
|
|
*/
|
|
@Override
|
|
public int wireEncode(Encoder encoder) throws ComponentException {
|
|
try {
|
|
int totalLength = 0;
|
|
|
|
// 编码 TLV-VALUE
|
|
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
|
|
if (tmplen < 0) {
|
|
return -1;
|
|
}
|
|
totalLength += tmplen;
|
|
|
|
// 编码 TLV-LENGTH
|
|
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
|
|
if (tmplen < 0) {
|
|
return -1;
|
|
}
|
|
totalLength += tmplen;
|
|
|
|
// 编码 TLV-TYPE
|
|
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
|
|
if (tmplen < 0) {
|
|
return -1;
|
|
}
|
|
totalLength += tmplen;
|
|
|
|
return totalLength;
|
|
} catch (EncoderException e) {
|
|
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean wireDecode(Block block) {
|
|
// 检查 Type 是否正确
|
|
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvPayload))){
|
|
return false;
|
|
}
|
|
|
|
// 存储Value
|
|
this.setValue(block.getValue());
|
|
return true;
|
|
}
|
|
}
|