diff options
author | Abyss777 <abyss@fox5.ru> | 2016-07-07 09:26:07 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-07-07 09:26:07 +0500 |
commit | ca2d3882664111969059f7cad78f5d082a0e9f6a (patch) | |
tree | c7f6359b153071915f1b9a9404fadedbb060d0c3 /src | |
parent | 10af44fdf94ad407bc521d11f547a93855620cc2 (diff) | |
download | traccar-server-ca2d3882664111969059f7cad78f5d082a0e9f6a.tar.gz traccar-server-ca2d3882664111969059f7cad78f5d082a0e9f6a.tar.bz2 traccar-server-ca2d3882664111969059f7cad78f5d082a0e9f6a.zip |
- Cleaned up GranitFrameDecoder
- Added test for GranitFrameDecoder
- Removed \r\n from GranitProtocolDecoderTest
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/protocol/GranitFrameDecoder.java | 36 |
1 files changed, 9 insertions, 27 deletions
diff --git a/src/org/traccar/protocol/GranitFrameDecoder.java b/src/org/traccar/protocol/GranitFrameDecoder.java index b0581c96c..5f1297c6d 100644 --- a/src/org/traccar/protocol/GranitFrameDecoder.java +++ b/src/org/traccar/protocol/GranitFrameDecoder.java @@ -15,10 +15,7 @@ */ package org.traccar.protocol; -import java.nio.charset.StandardCharsets; - 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; @@ -26,39 +23,24 @@ import org.traccar.helper.StringFinder; public class GranitFrameDecoder extends FrameDecoder { - private static final int HEADER_LENGTH = 6; - private static final int CHECKSUM_LENGTH = 3; - private static final int SHORT_LENGTH = 2; - @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() > HEADER_LENGTH + SHORT_LENGTH) { - ChannelBuffer frame = ChannelBuffers.dynamicBuffer(); - buf.getBytes(buf.readerIndex(), frame, HEADER_LENGTH); - String headerString = frame.toString(StandardCharsets.US_ASCII); - if (headerString.equals("+RRCB~") || headerString.equals("+DDAT~")) { - int length = buf.getUnsignedShort(buf.readerIndex() + HEADER_LENGTH); - if (buf.readableBytes() >= HEADER_LENGTH + SHORT_LENGTH + length + CHECKSUM_LENGTH) { - frame = buf.readBytes(HEADER_LENGTH + SHORT_LENGTH + length + CHECKSUM_LENGTH); - if (buf.readableBytes() > 2) { - buf.skipBytes(2); //skip \r\n - } - return frame; - } else { - return null; //damaged packet + int indexEnd = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n")); + if (indexEnd != -1) { + int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("~")); + if (indexTilde != -1 && indexTilde < indexEnd) { + int length = buf.getUnsignedShort(indexTilde + 1); + indexEnd = buf.indexOf(indexTilde + 2 + length, buf.writerIndex(), new StringFinder("\r\n")); + if (indexEnd == -1) { + return null; } } - } - - int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n")); - if (index != -1) { - ChannelBuffer frame = buf.readBytes(index - buf.readerIndex()); + ChannelBuffer frame = buf.readBytes(indexEnd - buf.readerIndex()); buf.skipBytes(2); return frame; } - return null; } |