aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/ItsFrameDecoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2019-04-18 22:53:27 -0700
committerAnton Tananaev <anton.tananaev@gmail.com>2019-04-18 22:53:27 -0700
commit2c6dc2ea108e8f35ad8324e6a6149010b8e258ef (patch)
treeca844def27ed503022fa88bbabddacb6f4877162 /src/main/java/org/traccar/protocol/ItsFrameDecoder.java
parentbfe6c5736899e82ba5f9754d4acc5493c34faff4 (diff)
downloadtrackermap-server-2c6dc2ea108e8f35ad8324e6a6149010b8e258ef.tar.gz
trackermap-server-2c6dc2ea108e8f35ad8324e6a6149010b8e258ef.tar.bz2
trackermap-server-2c6dc2ea108e8f35ad8324e6a6149010b8e258ef.zip
Handle messages without delimiter
Diffstat (limited to 'src/main/java/org/traccar/protocol/ItsFrameDecoder.java')
-rw-r--r--src/main/java/org/traccar/protocol/ItsFrameDecoder.java27
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;
}
}