diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-04-18 22:53:27 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-04-18 22:53:27 -0700 |
commit | 2c6dc2ea108e8f35ad8324e6a6149010b8e258ef (patch) | |
tree | ca844def27ed503022fa88bbabddacb6f4877162 /src | |
parent | bfe6c5736899e82ba5f9754d4acc5493c34faff4 (diff) | |
download | traccar-server-2c6dc2ea108e8f35ad8324e6a6149010b8e258ef.tar.gz traccar-server-2c6dc2ea108e8f35ad8324e6a6149010b8e258ef.tar.bz2 traccar-server-2c6dc2ea108e8f35ad8324e6a6149010b8e258ef.zip |
Handle messages without delimiter
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/traccar/protocol/ItsFrameDecoder.java | 27 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java | 4 |
2 files changed, 22 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; } } diff --git a/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java b/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java index 6112f773b..f5c98b1aa 100644 --- a/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java +++ b/src/test/java/org/traccar/protocol/ItsFrameDecoderTest.java @@ -11,6 +11,10 @@ public class ItsFrameDecoderTest extends ProtocolTest { ItsFrameDecoder decoder = new ItsFrameDecoder(); verifyFrame( + binary("242c2c3836383732383033373731373434312c312e3444335f4149533134305f312e302c56455253494f4e312e302c32382e3633333731372c4e2c37372e3232323730322c45"), + decoder.decode(null, null, binary("242c2c3836383732383033373731373434312c312e3444335f4149533134305f312e302c56455253494f4e312e302c32382e3633333731372c4e2c37372e3232323730322c45242c43502c41544c2c312e3444335f4149533134305f312e302c49462c30382c4c2c3836383732383033373731373434312c2c312c31373034323031392c3133313830392c32382e3633333731352c4e2c37372e3232323730322c452c302e302c342e30302c302c3231312e302c312e312c362e362c496465612c302c312c31322e362c332e392c302c4f2c31322c3430342c31312c3430612c3564332c326464642c3430612c30392c3438622c3430612c30362c3564342c3430612c30352c6165392c3430612c30352c303131312c30302c3236303831342c662a"))); + + verifyFrame( binary("244865616465722c69547269616e676c65312c4b41303147313233342c3836343439353033343433343631302c315f333654303242303136344d4149535f362c4149533134302c302e3030303030302c4e2c302e3030303030302c452a3545"), decoder.decode(null, null, binary("244865616465722c69547269616e676c65312c4b41303147313233342c3836343439353033343433343631302c315f333654303242303136344d4149535f362c4149533134302c302e3030303030302c4e2c302e3030303030302c452a35450d0a"))); |