mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
finished component: NackHeader
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:52 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 变量
|
||||
private long nackReason;
|
||||
|
||||
// 函数
|
||||
public long getNackReason() {
|
||||
return nackReason;
|
||||
}
|
||||
|
||||
public void setNackReason(long nackReason) {
|
||||
this.nackReason = nackReason;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 NackHeader 编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.nackReason);
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNackHeader));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Block TLV 中解码出一个 NackHeader
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNackHeader))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setNackReason(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user