diff --git a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java index 93618a1..279fab1 100644 --- a/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java +++ b/src/main/java/com/zjyr/beidouservice/adapter/impl/netty/MessageHandler.java @@ -6,6 +6,7 @@ import com.zjyr.beidouservice.pojo.vo.YuanRongBinProtocol; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -22,10 +23,11 @@ public class MessageHandler extends SimpleChannelInboundHandler regEx(String patten, String textArea) { + Pattern compile = Pattern.compile(patten); + Matcher matcher = compile.matcher(textArea); + List targetList = new ArrayList<>(); + while (matcher.find()) { + String substring = textArea.substring(matcher.start(), matcher.end()); + targetList.add(substring); + } + return targetList; + } + + private List protocolPackageSplit() { + List startIndex = new ArrayList<>(); + byte[] startFlag = YuanRongBinProtocol.START.getBytes(); + byte[] val = new byte[receiveBuffer.readableBytes()]; + receiveBuffer.readBytes(val, 0, receiveBuffer.readableBytes()); + + for (int i = 0; i < val.length; i++) { + YuanRongBinProtocol yrPro = new YuanRongBinProtocol(); + + byte[] sclicedArr = Arrays.copyOfRange(val, i, i + startFlag.length); + if (Arrays.equals(sclicedArr, startFlag)) { + //startIndex.add(i); + for (int j = i; j < val.length; j++) { + // Find package head end flag + if (val[j] == '$') { + // It must be a package + byte[] head = new byte[j - i]; + receiveBuffer.readBytes(head, i, j-i); + yrPro.setProHeader(head); + } + } + } + } + + return startIndex; + } + @Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List out) throws Exception { - if (buffer.readableBytes() > 0) { - byte[] val = new byte[buffer.readableBytes()]; - buffer.readBytes(val, 0, buffer.readableBytes()); - YuanRongBinProtocol v = new YuanRongBinProtocol(); - v.setStart(val); - out.add(v); - } + // cache receive data + receiveBuffer.writeBytes(buffer.array()); + + ctx.channel().eventLoop().execute(new Runnable() { + @Override + public void run() { + if (receiveBuffer.readableBytes() > YuanRongBinProtocol.MIN_LEN) { + byte[] val = new byte[buffer.readableBytes()]; + buffer.readBytes(val, 0, buffer.readableBytes()); + } + } + }); } } diff --git a/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java b/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java index a40e700..4af4c0e 100644 --- a/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java +++ b/src/main/java/com/zjyr/beidouservice/pojo/vo/YuanRongBinProtocol.java @@ -4,5 +4,16 @@ import lombok.Data; @Data public class YuanRongBinProtocol { - private byte[] start; + + public static int MIN_LEN = 25; + public static String START = "[length="; + public static String HEAD_PARTERN = "\\[length=\\d{1,4}\\]@\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:\\d{1,5}\\|\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}@\\$"; + + private byte[] proHeader; + private byte[] proData; + + private int dataLength; + private int sendPort; + private String sendIp; + private String recvIp; } diff --git a/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java b/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java index 92969c7..55de68a 100644 --- a/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java +++ b/src/test/java/com/zjyr/beidouservice/BaidouServiceApplicationTests.java @@ -3,12 +3,24 @@ package com.zjyr.beidouservice; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + @SpringBootTest class BaidouServiceApplicationTests { @Test void contextLoads() { - + String str = "[length=108]@127.0.0.1:51141|192.168.1.80@$"; + Pattern compile = Pattern.compile("\\[length=\\d{1,4}\\]@\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}:\\d{1,5}\\|\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}@\\$"); + Matcher matcher = compile.matcher(str); + List targetList = new ArrayList<>(); + while (matcher.find()) { + String substring = str.substring(matcher.start(), matcher.end()); + targetList.add(substring); + } } }