1. 备份代码

This commit is contained in:
黄昕 2023-08-09 18:03:15 +08:00
parent ba18d9d06a
commit 91c3493f43
4 changed files with 98 additions and 13 deletions

View File

@ -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<YuanRongBinProto
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
log.info("{}:: Trigger Heart Signle", ctx.channel().id());
if (evt instanceof IdleStateEvent) {
if (evt instanceof IdleStateEvent idleStateEvent) {
if (idleStateEvent.state() == IdleState.ALL_IDLE) {
log.info("{}:: Trigger Heart Signe", ctx.channel().id());
}
} else {
super.userEventTriggered(ctx, evt);
}
@ -33,7 +35,14 @@ public class MessageHandler extends SimpleChannelInboundHandler<YuanRongBinProto
@Override
public void channelRead0(ChannelHandlerContext ctx, YuanRongBinProtocol message) throws Exception {
log.info("{}:: Receive Message: {}", ctx.channel().id(), HelperUtils.bytesToHexString(message.getStart()));
//获取到线程池eventLoop添加线程执行
ctx.channel().eventLoop().execute(new Runnable() {
@Override
public void run() {
//长时间操作不至于长时间的业务操作导致Handler阻塞
//log.info("{}:: Receive Message: {}", ctx.channel().id(), HelperUtils.bytesToHexString(message.getStart()));
}
});
}
@Override

View File

@ -1,25 +1,78 @@
package com.zjyr.beidouservice.adapter.impl.netty.decode;
import com.zjyr.beidouservice.pojo.vo.ControlAdapterSocketCtx;
import com.zjyr.beidouservice.pojo.vo.YuanRongBinProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Component
@RequiredArgsConstructor
public class YuanRongProtocolDecode extends ByteToMessageDecoder {
public static final ByteBuf receiveBuffer = Unpooled.buffer(1024 * 1024);
public static List<String> regEx(String patten, String textArea) {
Pattern compile = Pattern.compile(patten);
Matcher matcher = compile.matcher(textArea);
List<String> targetList = new ArrayList<>();
while (matcher.find()) {
String substring = textArea.substring(matcher.start(), matcher.end());
targetList.add(substring);
}
return targetList;
}
private List<Integer> protocolPackageSplit() {
List<Integer> 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<Object> out) throws Exception {
if (buffer.readableBytes() > 0) {
// 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());
YuanRongBinProtocol v = new YuanRongBinProtocol();
v.setStart(val);
out.add(v);
}
}
});
}
}

View File

@ -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;
}

View File

@ -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<String> targetList = new ArrayList<>();
while (matcher.find()) {
String substring = str.substring(matcher.start(), matcher.end());
targetList.add(substring);
}
}
}