use of sugar.free.sightparser.error.UnknownServiceError in project SightRemote by TebbeUbben.
the class AppLayerMessage method deserialize.
public static AppLayerMessage deserialize(ByteBuf byteBuf) throws Exception {
byte version = byteBuf.readByte();
byte service = byteBuf.readByte();
short command = byteBuf.readShort();
short error = byteBuf.readShort();
byte[] data = byteBuf.readBytes();
if (version != VERSION)
throw new InvalidAppVersionError(version, VERSION);
if (!MESSAGES.containsKey(service))
throw new UnknownServiceError(service);
Class<? extends AppLayerMessage> clazz = MESSAGES.get(service).get(command);
if (clazz == null)
throw new UnknownAppMessageError(service, command);
if (error != 0x0000 && error != 0xF0CC) {
Class<? extends AppErrorCodeError> errorClass = Errors.ERRORS.get(error);
if (errorClass != null)
throw errorClass.getConstructor(Class.class, short.class).newInstance(clazz, error);
else
throw new UnknownAppErrorCodeError(clazz, error);
}
AppLayerMessage message = clazz.newInstance();
ByteBuf dataBuf = new ByteBuf(data.length);
dataBuf.putBytes(data);
if (message.inCRC()) {
int crc = dataBuf.getUInt16LE(data.length - 2);
byte[] bytes = dataBuf.getBytes(data.length - 2);
int calculatedCRC = Cryptograph.calculateCRC(bytes);
if (crc != calculatedCRC)
throw new InvalidAppCRCError(crc, calculatedCRC);
dataBuf = new ByteBuf(bytes.length);
dataBuf.putBytes(bytes);
}
message.parse(dataBuf);
return message;
}
Aggregations