mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-30 02:23:15 +00:00
feat[protocol]: 上层需要知道是那个消息解析异常
This commit is contained in:
@@ -14,6 +14,7 @@ package com.zfoo.protocol;
|
||||
|
||||
import com.zfoo.protocol.buffer.ByteBufUtils;
|
||||
import com.zfoo.protocol.collection.HashMapIntShort;
|
||||
import com.zfoo.protocol.exception.DecodeException;
|
||||
import com.zfoo.protocol.generate.GenerateOperation;
|
||||
import com.zfoo.protocol.registration.IProtocolRegistration;
|
||||
import com.zfoo.protocol.registration.ProtocolAnalysis;
|
||||
@@ -70,7 +71,13 @@ public class ProtocolManager {
|
||||
* ByteBuf convert to byte[] using ByteBufUtils.readAllBytes(ByteBuf) in zfoo
|
||||
*/
|
||||
public static Object read(ByteBuf buffer) {
|
||||
return protocols[ByteBufUtils.readShort(buffer)].read(buffer);
|
||||
short protocolId = -1;
|
||||
try {
|
||||
protocolId = ByteBufUtils.readShort(buffer);
|
||||
return protocols[protocolId].read(buffer);
|
||||
} catch (Throwable e) {
|
||||
throw new DecodeException(e, protocolId);
|
||||
}
|
||||
}
|
||||
|
||||
public static IProtocolRegistration getProtocol(short protocolId) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.zfoo.protocol.exception;
|
||||
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
|
||||
public class DecodeException extends RuntimeException {
|
||||
/**
|
||||
* msg id
|
||||
*/
|
||||
private final short protocolId;
|
||||
|
||||
public DecodeException(Throwable e, short protocolId) {
|
||||
super(e);
|
||||
this.protocolId = protocolId;
|
||||
}
|
||||
|
||||
public short getProtocolId() {
|
||||
return protocolId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String sss = super.toString();
|
||||
return StringUtils.format("{}, protocolId={} ", sss, protocolId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user