From e336615dbed3f261dd2b42de475b5d1f2a63c6f9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 17 Apr 2019 00:39:46 -0700 Subject: Improve frame decoder --- .../java/org/traccar/protocol/ItsFrameDecoder.java | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java index 79b341760..eb2c59e91 100644 --- a/src/main/java/org/traccar/protocol/ItsFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/ItsFrameDecoder.java @@ -19,30 +19,40 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import org.traccar.BaseFrameDecoder; +import org.traccar.helper.BufferUtil; public class ItsFrameDecoder extends BaseFrameDecoder { + private static final int MINIMUM_LENGTH = 20; + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); - - if (buf.writerIndex() > delimiterIndex + 1 && buf.getByte(delimiterIndex + 1) == '*') { - delimiterIndex += 1; - } - ByteBuf frame; - if (buf.getByte(delimiterIndex - 2) == ',') { - frame = buf.readRetainedSlice(delimiterIndex - 1 - buf.readerIndex()); - buf.skipBytes(1); // binary checksum - } else { + int delimiterIndex = BufferUtil.indexOf("\r\n", buf);; + if (delimiterIndex > MINIMUM_LENGTH) { frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + buf.skipBytes(2); + return frame; + } else { + delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + if (delimiterIndex > MINIMUM_LENGTH) { + if (buf.writerIndex() > delimiterIndex + 1 && buf.getByte(delimiterIndex + 1) == '*') { + delimiterIndex += 1; + } + if (buf.getByte(delimiterIndex - 2) == ',') { + frame = buf.readRetainedSlice(delimiterIndex - 1 - buf.readerIndex()); + buf.skipBytes(1); // binary checksum + } else { + frame = buf.readRetainedSlice(delimiterIndex - buf.readerIndex()); + } + buf.skipBytes(1); // delimiter + return frame; + } } - buf.skipBytes(1); // delimiter - - return frame; + return null; } } -- cgit v1.2.3