From 1264c4cec35c7e26aaaa32362bbf5ab3d97635aa Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 9 Oct 2018 11:11:31 +1300 Subject: Simplify Watch frame decoder --- src/org/traccar/protocol/WatchFrameDecoder.java | 45 +++++-------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index 1e2f0cea3..b2ac5b4f7 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -19,9 +19,6 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; - -import java.nio.charset.StandardCharsets; - import org.traccar.BaseFrameDecoder; public class WatchFrameDecoder extends BaseFrameDecoder { @@ -30,53 +27,27 @@ public class WatchFrameDecoder extends BaseFrameDecoder { protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') + 1; - if (idIndex <= 0) { - return null; - } - - int lengthIndex = buf.indexOf(idIndex, buf.writerIndex(), (byte) '*') + 1; - if (lengthIndex <= 0) { - return null; - } - - int payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); - if (payloadIndex < 0) { - return null; - } - - if (payloadIndex + 5 < buf.writerIndex() && buf.getByte(payloadIndex + 5) == '*' - && buf.toString(payloadIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { - lengthIndex = payloadIndex + 1; - payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); - if (payloadIndex < 0) { - return null; - } - } - - int length = Integer.parseInt( - buf.toString(lengthIndex, payloadIndex - lengthIndex, StandardCharsets.US_ASCII), 16); - if (buf.readableBytes() >= payloadIndex + 1 + length + 1) { + int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ']') + 1; + if (endIndex > 0) { ByteBuf frame = Unpooled.buffer(); - int endIndex = buf.readerIndex() + payloadIndex + 1 + length + 1; while (buf.readerIndex() < endIndex) { byte b = buf.readByte(); - if (b == 0x7D) { + if (b == '}') { switch (buf.readByte()) { case 0x01: - frame.writeByte(0x7D); + frame.writeByte('}'); break; case 0x02: - frame.writeByte(0x5B); + frame.writeByte('['); break; case 0x03: - frame.writeByte(0x5D); + frame.writeByte(']'); break; case 0x04: - frame.writeByte(0x2C); + frame.writeByte(','); break; case 0x05: - frame.writeByte(0x2A); + frame.writeByte('*'); break; default: throw new IllegalArgumentException(); -- cgit v1.2.3