aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2019-04-17 00:39:46 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2019-04-17 00:39:46 -0700
commite336615dbed3f261dd2b42de475b5d1f2a63c6f9 (patch)
treea0ce89cdcf2f7428c67b00e66d786f563871f9de /src/main
parent8852f1176ac32a065db734079c295783811c192f (diff)
downloadtraccar-server-e336615dbed3f261dd2b42de475b5d1f2a63c6f9.tar.gz
traccar-server-e336615dbed3f261dd2b42de475b5d1f2a63c6f9.tar.bz2
traccar-server-e336615dbed3f261dd2b42de475b5d1f2a63c6f9.zip
Improve frame decoder
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/traccar/protocol/ItsFrameDecoder.java36
1 files changed, 23 insertions, 13 deletions
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;
}
}