From ef52b738c452023472e00ef2aea75822777c5a9d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 13 Jul 2020 20:36:13 -0700 Subject: Improve eSky frame decoder --- .../java/org/traccar/protocol/EskyFrameDecoder.java | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/protocol/EskyFrameDecoder.java b/src/main/java/org/traccar/protocol/EskyFrameDecoder.java index da24c1273..a374f29b1 100644 --- a/src/main/java/org/traccar/protocol/EskyFrameDecoder.java +++ b/src/main/java/org/traccar/protocol/EskyFrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2020 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. @@ -26,14 +26,18 @@ public class EskyFrameDecoder extends BaseFrameDecoder { protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { - buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 'E')); - - int endIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 'E'); - if (endIndex > 0) { - return buf.readRetainedSlice(endIndex - buf.readerIndex()); - } else { - return buf.readRetainedSlice(buf.readableBytes()); // assume full frame + int startIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 'E'); + if (startIndex >= 0) { + buf.readerIndex(startIndex); + int endIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 'E'); + if (endIndex > 0) { + return buf.readRetainedSlice(endIndex - buf.readerIndex()); + } else { + return buf.readRetainedSlice(buf.readableBytes()); // assume full frame + } } + + return null; } } -- cgit v1.2.3