add error handle: ComponentException & PacketException

This commit is contained in:
free will committed 2021-03-24 11:14:54 +08:00
1 parent d7f063cbb9
commit 7bf0906291
35 files changed
+955 -362

No files matched your search

+23 -6
View File
@@ -43,25 +43,38 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.hopLimit);
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.hopLimit);
} catch (EncoderException e) {
throw new ComponentException("HopLimit.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("HopLimit.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
} catch (EncoderException e) {
throw new ComponentException("HopLimit.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -71,7 +84,7 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
}
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){
return false;
@@ -79,7 +92,11 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
// 读取 HopLimit
long value= 0;
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
} catch (TLVException | VlIntException e) {
throw new ComponentException("HopLimit.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}