diff options
author | Valerii Vyshniak <valeravi@vi-soft.com.ua> | 2017-11-23 00:15:35 +0100 |
---|---|---|
committer | Valerii Vyshniak <valeravi@vi-soft.com.ua> | 2017-11-23 00:15:35 +0100 |
commit | d0a55d5a5c52e2f668e7c74952fca59019b0c405 (patch) | |
tree | d4c9b8cd2d19b3c106d85243e171e9b2d52e9ee7 /src/org/traccar/protocol | |
parent | e5a8be1987aecf3735801165ee19c1bcdd4fcc05 (diff) | |
download | traccar-server-d0a55d5a5c52e2f668e7c74952fca59019b0c405.tar.gz traccar-server-d0a55d5a5c52e2f668e7c74952fca59019b0c405.tar.bz2 traccar-server-d0a55d5a5c52e2f668e7c74952fca59019b0c405.zip |
T580W: tk103 frame decoder improvements
- skip garbarge before frame start to speed-up parsing;
- speed-up free text block parsing;
- some recovery procedure for stuck frames (becauce of wrong amount
of start/end/free text frame symbols, etc.).
Diffstat (limited to 'src/org/traccar/protocol')
-rw-r--r-- | src/org/traccar/protocol/Tk103FrameDecoder.java | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/src/org/traccar/protocol/Tk103FrameDecoder.java b/src/org/traccar/protocol/Tk103FrameDecoder.java index 17e791553..a27eb0714 100644 --- a/src/org/traccar/protocol/Tk103FrameDecoder.java +++ b/src/org/traccar/protocol/Tk103FrameDecoder.java @@ -17,35 +17,46 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; -import java.nio.ByteOrder; - public class Tk103FrameDecoder extends FrameDecoder { + private static final int FRAME_MIN_SIZE = 2; + private static final int FRAME_MAX_SIZE = 1024; + private static final byte FRAME_START_SYMBOL = (byte) '('; + private static final byte FRAME_END_SYMBOL = (byte) ')'; + private static final byte FRAME_FREE_TEXT_BLOCK_SYMBOL = (byte) '$'; + @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() < 2) { + if (buf.readableBytes() < FRAME_MIN_SIZE) { + return null; + } + + int indexStart = buf.indexOf(buf.readerIndex(), buf.writerIndex(), FRAME_START_SYMBOL); + if (indexStart == -1) { + buf.clear(); return null; } + indexStart++; - int index; - for (index = buf.readerIndex(); true; index++) { - index = buf.indexOf(index, buf.writerIndex(), (byte) ')'); + int index, cnt; + for (index = indexStart, cnt = 0;; index++) { + int i = index; + index = buf.indexOf(index, buf.writerIndex(), FRAME_END_SYMBOL); if (index == -1) { break; } - int cnt = 0; - for (int i = buf.readerIndex(); i < index; i++) { - if (buf.getByte(i) == (byte) '$') { - cnt++; + for (;; i++, cnt++) { + i = buf.indexOf(i, index, FRAME_FREE_TEXT_BLOCK_SYMBOL); + if (i == -1 || i >= index) { + break; } } if (cnt % 2 == 0) { @@ -53,14 +64,24 @@ public class Tk103FrameDecoder extends FrameDecoder { } } - if (index != -1) { - ChannelBuffer result = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, index + 1 - buf.readerIndex()); - buf.readBytes(result, index + 1 - buf.readerIndex()); - result.writerIndex(result.writerIndex() - 1); - return result; + if (index == -1) { + while (buf.readableBytes() > FRAME_MAX_SIZE) { + int i = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), FRAME_START_SYMBOL); + if (i == -1) { + buf.clear(); + } else { + buf.readerIndex(i); + } + } + return null; } - return null; + buf.readerIndex(indexStart); + ChannelBuffer result = buf.readBytes(index - indexStart); + buf.readerIndex(buf.readerIndex() + 1); + + return result; + } } |