Sfoglia il codice sorgente

经纬解析器调整

jianghouwei 6 anni fa
parent
commit
4d60322510

+ 21 - 8
src/main/java/com/tidecloud/dataacceptance/service/DelimiterJingWeiFrameDecoder.java

@@ -1,9 +1,6 @@
 package com.tidecloud.dataacceptance.service;
 
-import com.tidecloud.dataacceptance.service.impl.WatchJWServerHandler;
 import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufAllocator;
-import io.netty.buffer.ByteBufProcessor;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.ByteToMessageDecoder;
@@ -46,6 +43,7 @@ public class DelimiterJingWeiFrameDecoder extends ByteToMessageDecoder {
 
     @Override
     protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
+        in.markReaderIndex();
         int index = indexOf(in, delimiters);
         if (index <= 0) {
             return;
@@ -60,14 +58,16 @@ public class DelimiterJingWeiFrameDecoder extends ByteToMessageDecoder {
             for (int i = 0; i < 5; i++) {
                 int commaLength = in.forEachByte(in.readerIndex(), 30, FIND_COMMA);
                 if (commaLength <= 0) {
-                    in.readerIndex(beginIndex);
+//                    in.readerIndex(beginIndex);
+                    in.resetReaderIndex();
                     return;
                 } else {
                     cutLength = commaLength;
                     in.readerIndex(commaLength + 1);
                 }
             }
-            in.readerIndex(beginIndex);
+            in.resetReaderIndex();
+           // in.readerIndex(beginIndex);
             // 标记数据
             byte[] req = new byte[cutLength + 1];
             in.readBytes(req);
@@ -75,13 +75,18 @@ public class DelimiterJingWeiFrameDecoder extends ByteToMessageDecoder {
             String[] msgArr = msg.split(",");
             Integer length = Integer.valueOf(msgArr[4]);
             if (in.readableBytes() < length) {
-                in.readerIndex(beginIndex);
+//                in.readerIndex(beginIndex);
+                in.resetReaderIndex();
                 return;
             }
             // 语音数据
             in.readerIndex(beginIndex);
-            ByteBuf otherByteBufRef = in.readBytes(cutLength + 2 + length);
-            out.add(otherByteBufRef);
+            int readLength = cutLength + 1 + length + 1;
+//            ByteBuf otherByteBufRef = in.readBytes(readLength);
+//            out.add(otherByteBufRef);
+            Object decoded = decode(readLength, ctx, in);
+            out.add(decoded);
+//            in.retain();
         } else {// 是其他包
             Object decoded = decode(ctx, in);
             if (decoded != null) {
@@ -91,6 +96,14 @@ public class DelimiterJingWeiFrameDecoder extends ByteToMessageDecoder {
 
     }
 
+    protected Object decode(int readLength, ChannelHandlerContext ctx, ByteBuf in) throws Exception {
+        if (in.readableBytes() < readLength) {
+            return null;
+        } else {
+            return in.readRetainedSlice(readLength);
+        }
+    }
+
     /**
      * Creates a new instance.
      *