diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/traccar/protocol/ItsFrameDecoder.java | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java index b28d6e96f..cebfdca5f 100644 --- a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java @@ -25,16 +25,28 @@ public class ItsFrameDecoder extends BaseFrameDecoder { private static final int MINIMUM_LENGTH = 20; + private ByteBuf readFrame(ByteBuf buf, int delimiterIndex, int skip) { + int headerIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) '$'); + if (headerIndex > 0 && headerIndex < delimiterIndex) { + return buf.readRetainedSlice(headerIndex - buf.readerIndex()); + } else { + ByteBuf frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + buf.skipBytes(skip); + return frame; + } + } + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - ByteBuf frame; + while (buf.isReadable() && buf.getByte(buf.readerIndex()) != '$') { + buf.skipBytes(1); + } + int delimiterIndex = BufferUtil.indexOf("\r\n", buf); if (delimiterIndex > MINIMUM_LENGTH) { - frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); - buf.skipBytes(2); - return frame; + return readFrame(buf, delimiterIndex, 2); } else { delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); if (delimiterIndex > MINIMUM_LENGTH) { @@ -42,13 +54,10 @@ public class ItsFrameDecoder extends BaseFrameDecoder { delimiterIndex += 1; } if (buf.getByte(delimiterIndex - 2) == ',') { - frame = buf.readRetainedSlice(delimiterIndex - 1 - buf.readerIndex()); - buf.skipBytes(1); // binary checksum + return readFrame(buf, delimiterIndex - 1, 2); // skip binary checksum } else { - frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + return readFrame(buf, delimiterIndex, 1); } - buf.skipBytes(1); // delimiter - return frame; } } |