From f64840b9e85d4869c92e9f20a2f5043522ad4008 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 20 Oct 2016 16:32:24 +1300 Subject: Use heuristics for H02 message length --- src/org/traccar/protocol/H02FrameDecoder.java | 16 +++++++++++++--- src/org/traccar/protocol/H02Protocol.java | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/H02FrameDecoder.java b/src/org/traccar/protocol/H02FrameDecoder.java index feba8033d..2a005e760 100644 --- a/src/org/traccar/protocol/H02FrameDecoder.java +++ b/src/org/traccar/protocol/H02FrameDecoder.java @@ -22,6 +22,9 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder; public class H02FrameDecoder extends FrameDecoder { + private static final int MESSAGE_SHORT = 32; + private static final int MESSAGE_LONG = 45; + private int messageLength; public H02FrameDecoder(int messageLength) { @@ -49,10 +52,17 @@ public class H02FrameDecoder extends FrameDecoder { return buf.readBytes(index + 1 - buf.readerIndex()); } - } else if (marker == '$' && buf.readableBytes() >= messageLength) { + } else if (marker == '$') { - // Return binary message - return buf.readBytes(messageLength); + if (messageLength > 0 && buf.readableBytes() >= messageLength) { + return buf.readBytes(messageLength); + } else if (buf.readableBytes() >= MESSAGE_SHORT) { + if (buf.getUnsignedByte(buf.readerIndex() + MESSAGE_SHORT - 1) == 0) { + return buf.readBytes(MESSAGE_SHORT); + } else if (buf.readableBytes() >= MESSAGE_LONG) { + return buf.readBytes(MESSAGE_LONG); + } + } } diff --git a/src/org/traccar/protocol/H02Protocol.java b/src/org/traccar/protocol/H02Protocol.java index 06ac2a6fa..089721aed 100644 --- a/src/org/traccar/protocol/H02Protocol.java +++ b/src/org/traccar/protocol/H02Protocol.java @@ -43,7 +43,7 @@ public class H02Protocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - int messageLength = Context.getConfig().getInteger(getName() + ".messageLength", 32); + int messageLength = Context.getConfig().getInteger(getName() + ".messageLength"); pipeline.addLast("frameDecoder", new H02FrameDecoder(messageLength)); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectEncoder", new H02ProtocolEncoder()); -- cgit v1.2.3