diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-03-09 15:07:57 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-03-09 15:07:57 +1300 |
commit | 7bf00894ad47ef47ce4c9ccc84b560956f3b41f8 (patch) | |
tree | f5845e1677402c1b10817a00744868524215f2eb | |
parent | b7898abf1be64a2f9750d1020b20e9ed578d61e9 (diff) | |
download | trackermap-server-7bf00894ad47ef47ce4c9ccc84b560956f3b41f8.tar.gz trackermap-server-7bf00894ad47ef47ce4c9ccc84b560956f3b41f8.tar.bz2 trackermap-server-7bf00894ad47ef47ce4c9ccc84b560956f3b41f8.zip |
Extend Watch frame decoder
-rw-r--r-- | src/org/traccar/protocol/WatchFrameDecoder.java | 85 | ||||
-rw-r--r-- | test/org/traccar/protocol/WatchFrameDecoderTest.java | 4 |
2 files changed, 54 insertions, 35 deletions
diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index 826a8b4d0..9adea2843 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,47 +25,62 @@ import java.nio.charset.StandardCharsets; public class WatchFrameDecoder extends FrameDecoder { - public static final int MESSAGE_HEADER = 20; - @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() >= MESSAGE_HEADER) { - ChannelBuffer lengthBuffer = ChannelBuffers.dynamicBuffer(); - buf.getBytes(buf.readerIndex() + MESSAGE_HEADER - 4 - 1, lengthBuffer, 4); - int length = Integer.parseInt(lengthBuffer.toString(StandardCharsets.US_ASCII), 16) + MESSAGE_HEADER + 1; - if (buf.readableBytes() >= length) { - ChannelBuffer frame = ChannelBuffers.dynamicBuffer(); - int endIndex = buf.readerIndex() + length; - while (buf.readerIndex() < endIndex) { - byte b = buf.readByte(); - if (b == 0x7D) { - switch (buf.readByte()) { - case 0x01: - frame.writeByte(0x7D); - break; - case 0x02: - frame.writeByte(0x5B); - break; - case 0x03: - frame.writeByte(0x5D); - break; - case 0x04: - frame.writeByte(0x2C); - break; - case 0x05: - frame.writeByte(0x2A); - break; - default: - throw new IllegalArgumentException(); - } - } else { - frame.writeByte(b); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') + 1; + if (idIndex <= 0) { + return null; + } + + int lengthIndex = buf.indexOf(idIndex, buf.writerIndex(), (byte) '*') + 1; + if (lengthIndex <= 0) { + return null; + } else if (lengthIndex - idIndex > 10 + 1) { + lengthIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*') + 1; + if (lengthIndex <= 0) { + return null; + } + } + + int payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); + if (payloadIndex < 0) { + return null; + } + + int length = Integer.parseInt( + buf.toString(lengthIndex, payloadIndex - lengthIndex, StandardCharsets.US_ASCII), 16); + if (buf.readableBytes() >= payloadIndex + 1 + length + 1) { + ChannelBuffer frame = ChannelBuffers.dynamicBuffer(); + int endIndex = buf.readerIndex() + payloadIndex + 1 + length + 1; + while (buf.readerIndex() < endIndex) { + byte b = buf.readByte(); + if (b == 0x7D) { + switch (buf.readByte()) { + case 0x01: + frame.writeByte(0x7D); + break; + case 0x02: + frame.writeByte(0x5B); + break; + case 0x03: + frame.writeByte(0x5D); + break; + case 0x04: + frame.writeByte(0x2C); + break; + case 0x05: + frame.writeByte(0x2A); + break; + default: + throw new IllegalArgumentException(); } + } else { + frame.writeByte(b); } - return frame; } + return frame; } return null; diff --git a/test/org/traccar/protocol/WatchFrameDecoderTest.java b/test/org/traccar/protocol/WatchFrameDecoderTest.java index 664501a43..a1eca30bc 100644 --- a/test/org/traccar/protocol/WatchFrameDecoderTest.java +++ b/test/org/traccar/protocol/WatchFrameDecoderTest.java @@ -18,6 +18,10 @@ public class WatchFrameDecoderTest extends ProtocolTest { binary("5b33472a383330383430363237392a303030392a4c4b2c302c302c38345d"), decoder.decode(null, null, binary("5b33472a383330383430363237392a303030392a4c4b2c302c302c38345d"))); + verifyFrame( + binary("5b5a4a2a3031343131313030313335303330342a303033342a303030392a4c4b2c302c302c31395d"), + decoder.decode(null, null, binary("5b5a4a2a3031343131313030313335303330342a303033342a303030392a4c4b2c302c302c31395d"))); + } } |