瀏覽代碼

猛改了一波核心, 解决了netty内存泄漏的问题,之前没大影响用就没管

xiuwei 4 年之前
父節點
當前提交
1b8c320766

+ 6 - 0
README.md

@@ -10,6 +10,12 @@
 
 使用方式请参见项目内的介绍,也可参考本人的另一个项目  protocolconverter
 
+使用者可以install发布到本地 ,如果有条件的可以deploy到自己家的仓库
+
+有问题可以发邮件到 weiyigulu524710549@gmail.com 与我联系,探讨交流
+
+欢迎大家批评指正。
+
 104依赖引入
 
 ```java

+ 4 - 4
pom.xml

@@ -6,7 +6,7 @@
 
     <groupId>wei.yigulu</groupId>
     <artifactId>protocol</artifactId>
-    <version>1.1</version>
+    <version>1.1.1</version>
     <packaging>pom</packaging>
     <modules>
         <module>protocol-core</module>
@@ -32,9 +32,9 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
-        <protocol.version>1.0</protocol.version>
-        <iec104.version>1.4.7</iec104.version>
-        <modbus.version>1.2.2</modbus.version>
+        <protocol.version>1.1.1</protocol.version>
+        <iec104.version>1.4.8</iec104.version>
+        <modbus.version>1.2.3</modbus.version>
         <cdt.version>1.0.0</cdt.version>
     </properties>
     <distributionManagement>

+ 2 - 1
protocol-all/pom.xml

@@ -6,10 +6,11 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>1.0</version>
+        <version>1.1.1</version>
     </parent>
 
 
+
     <packaging>jar</packaging>
     <artifactId>protocol-all</artifactId>
 

+ 1 - 1
protocol-cdt/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>1.0</version>
+        <version>1.1.1</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 1
protocol-core/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>1.0</version>
+        <version>1.1.1</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 1 - 0
protocol-core/src/main/java/wei/yigulu/netty/AbstractDelimiterHandler.java

@@ -128,6 +128,7 @@ public abstract class AbstractDelimiterHandler extends ChannelInboundHandlerAdap
 	 */
 	protected void mergeOrFlushByTimeSpan(ByteBuf byteBuf){
 		if (timeMark.plusMillis(maxTimeSpace).isBeforeNow()) {
+			System.out.println(cumulation);
 			log.warn("上一帧数据长度不足,但两帧时间间隔较长上一帧被舍弃 舍弃的数据帧为:" + DataConvertor.ByteBuf2String(cumulation));
 			while (!cumulation.release()) {
 			}

+ 5 - 0
protocol-core/src/main/java/wei/yigulu/netty/BaseProtocolBuilder.java

@@ -1,5 +1,6 @@
 package wei.yigulu.netty;
 
+import io.netty.util.ResourceLeakDetector;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.experimental.Accessors;
@@ -18,6 +19,10 @@ import java.util.UUID;
  */
 public class BaseProtocolBuilder {
 
+	public  BaseProtocolBuilder(){
+		//ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
+	}
+
 	@Setter
 	@Getter
 	@Accessors(chain = true)

+ 16 - 1
protocol-core/src/main/java/wei/yigulu/utils/DataConvertor.java

@@ -1,6 +1,7 @@
 package wei.yigulu.utils;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.util.ReferenceCountUtil;
 
 import java.util.Formatter;
 
@@ -57,8 +58,22 @@ public class DataConvertor {
 		ByteBuf b1 = buf.copy();
 		byte[] bs = new byte[b1.readableBytes()];
 		b1.readBytes(bs);
-		b1.release();
+		ReferenceCountUtil.release( b1);
 		return Byte2String(bs);
 	}
 
+
+	/**
+	 * 字节数缓冲区字转16进制字符串 并解除指向
+	 *
+	 * @param buf buf
+	 * @return string
+	 */
+	public static String ByteBuf2StringAndRelease(ByteBuf buf) {
+		String s= ByteBuf2String(buf);
+		ReferenceCountUtil.release( buf);
+		return s;
+
+	}
+
 }

+ 1 - 1
protocol-iec104/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>1.0</version>
+        <version>1.1.1</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

+ 2 - 2
protocol-iec104/src/main/java/wei/yigulu/iec104/nettyconfig/AllCustomDelimiterHandler.java

@@ -42,12 +42,12 @@ public class AllCustomDelimiterHandler extends AbstractDelimiterHandler {
 		while (cumulation.readableBytes() >= 6 && headIndex != -1) {
 			//如果头字节不在第一个字节 那么读取标志向后推到头字节位置
 			if (headIndex > cumulation.readerIndex()) {
-				log.warn("舍弃了一无用段报文:" + DataConvertor.ByteBuf2String(cumulation.readBytes(headIndex - cumulation.readerIndex())));
+				log.warn("舍弃了一无用段报文:" + DataConvertor.ByteBuf2StringAndRelease(cumulation.readBytes(headIndex - cumulation.readerIndex())));
 			}
 			//标记读取位置
 			cumulation.markReaderIndex();
 			//向后读取一位 即0x68的占位
-			cumulation.readBytes(1);
+			cumulation.readBytes(1).release();
 			//获取到该帧的长度 帧内标定的长度
 			len = cumulation.readUnsignedByte();
 			//如果帧的真实长度少于 帧内标定长度则代表数据帧不完整,退出循环等待下一数据帧进入进行粘帧

+ 6 - 4
protocol-iec104/src/main/java/wei/yigulu/iec104/nettyconfig/Master104Handle.java

@@ -5,7 +5,9 @@ import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.SimpleChannelInboundHandler;
 import io.netty.handler.timeout.IdleStateEvent;
+import io.netty.util.ReferenceCountUtil;
 import lombok.NoArgsConstructor;
 import org.slf4j.Logger;
 import wei.yigulu.iec104.apdumodel.Apdu;
@@ -24,7 +26,7 @@ import java.net.InetSocketAddress;
  */
 
 @NoArgsConstructor
-public class Master104Handle extends ChannelInboundHandlerAdapter {
+public class Master104Handle extends SimpleChannelInboundHandler<ByteBuf> {
 
 	private Logger log;
 
@@ -58,11 +60,11 @@ public class Master104Handle extends ChannelInboundHandlerAdapter {
 
 
 	@Override
-	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+	public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
 		//收数据
 		log.debug("----------------------------------------------------------------------------------");
-		log.debug("re <= "+DataConvertor.ByteBuf2String((ByteBuf) msg));
-		Apdu apdu = apduClass.newInstance().setChannel(ctx.channel()).setIec104Builder(masterBuilder).setLog(log).loadByteBuf((ByteBuf) msg);
+		log.debug("re <= "+DataConvertor.ByteBuf2String(msg));
+		Apdu apdu = apduClass.newInstance().setChannel(ctx.channel()).setIec104Builder(masterBuilder).setLog(log).loadByteBuf(msg);
 		if (apdu.getApciType() == Apdu.ApciType.I_FORMAT) {
 			this.testNum = 0;
 		}

+ 5 - 4
protocol-iec104/src/main/java/wei/yigulu/iec104/nettyconfig/Slave104Handle.java

@@ -5,6 +5,7 @@ import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.SimpleChannelInboundHandler;
 import lombok.NoArgsConstructor;
 import org.slf4j.Logger;
 import wei.yigulu.iec104.apdumodel.Apdu;
@@ -22,7 +23,7 @@ import java.net.InetSocketAddress;
  * @version 3.0
  */
 @NoArgsConstructor
-public class Slave104Handle extends ChannelInboundHandlerAdapter {
+public class Slave104Handle extends SimpleChannelInboundHandler<ByteBuf> {
 
 	private Logger log;
 
@@ -53,11 +54,11 @@ public class Slave104Handle extends ChannelInboundHandlerAdapter {
 	private Class<? extends Apdu> apduClass = Apdu.class;
 
 	@Override
-	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+	public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
 		//收数据
 		log.debug("----------------------------------------------------------------------------------");
-		log.debug(DataConvertor.ByteBuf2String((ByteBuf) msg));
-		Apdu apdu = apduClass.newInstance().setChannel(ctx.channel()).setIec104Builder(slaverBuilder).setLog(slaverBuilder.getLog()).loadByteBuf((ByteBuf) msg);
+		log.debug(DataConvertor.ByteBuf2String( msg));
+		Apdu apdu = apduClass.newInstance().setChannel(ctx.channel()).setIec104Builder(slaverBuilder).setLog(slaverBuilder.getLog()).loadByteBuf(msg);
 		apdu.answer();
 	}
 

+ 3 - 2
protocol-iec104/src/test/java/MasterTest.java

@@ -1,3 +1,4 @@
+import io.netty.util.ResourceLeakDetector;
 import wei.yigulu.iec104.apdumodel.Apdu;
 import wei.yigulu.iec104.apdumodel.Asdu;
 import wei.yigulu.iec104.asdudataframe.TotalSummonType;
@@ -14,8 +15,8 @@ import wei.yigulu.iec104.nettyconfig.Iec104MasterBuilder;
 public class MasterTest {
 
 	public static void main(String[] args) throws Exception {
-
-		Iec104MasterBuilder masterBuilder = new Iec104MasterBuilder("127.0.0.1", 24004);
+		ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
+		Iec104MasterBuilder masterBuilder = new Iec104MasterBuilder("127.0.0.1", 2404);
 		masterBuilder.createByUnBlock();
 
 	/*	//创建总召唤类型I帧

+ 1 - 1
protocol-modbus/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>protocol</artifactId>
         <groupId>wei.yigulu</groupId>
-        <version>1.0</version>
+        <version>1.1.1</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <version>${modbus.version}</version>

+ 1 - 1
protocol-modbus/src/main/java/wei/yigulu/modbus/netty/ModbusRtuMasterDelimiterHandler.java

@@ -45,7 +45,7 @@ public class ModbusRtuMasterDelimiterHandler extends AbstractDelimiterHandler {
 		int functionCode;
 		while (cumulation.readableBytes() >= 5) {
 			cumulation.markReaderIndex();
-			cumulation.readBytes(1);
+			cumulation.readBytes(1).release();
 			functionCode = cumulation.readUnsignedByte();
 			if (functionCode > 0x80) {
 				//异常功能码 异常帧

+ 4 - 2
protocol-modbus/src/main/java/wei/yigulu/modbus/netty/ModbusTcpDelimiterHandler.java

@@ -25,13 +25,15 @@ public class ModbusTcpDelimiterHandler extends AbstractDelimiterHandler {
 	@Override
 	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
 		//log.warn("接收到原始的报文 :"+ DataConvertor.ByteBuf2String((ByteBuf) msg));
-		isOverMaxLength((ByteBuf) msg);
+		if (isOverMaxLength((ByteBuf) msg)) {
+			return;
+		}
 		//数据帧长度不足 记录时间 等待下一帧进入
 		int length;
 		while(cumulation.readableBytes()>=MINLENGTH){
 			cumulation.markReaderIndex();
 			//去掉四位事务帧
-			cumulation.readBytes(4);
+			cumulation.readBytes(4).release();
 			length = cumulation.readUnsignedShort();
 			if (length > 255 || length < 3) {
 				log.warn("不是正常的长度,该帧疑似异常帧,舍弃");

+ 3 - 3
protocol-modbus/src/main/java/wei/yigulu/modbus/netty/ModbusTcpMasterHandler.java

@@ -4,6 +4,7 @@ package wei.yigulu.modbus.netty;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.SimpleChannelInboundHandler;
 import org.slf4j.Logger;
 import wei.yigulu.netty.AbstractTcpMasterBuilder;
 import wei.yigulu.utils.DataConvertor;
@@ -16,7 +17,7 @@ import java.net.InetSocketAddress;
  * @author: xiuwei
  * @version:
  */
-public class ModbusTcpMasterHandler extends ChannelInboundHandlerAdapter {
+public class ModbusTcpMasterHandler extends SimpleChannelInboundHandler<ByteBuf> {
 
 	protected Logger log;
 	protected AbstractTcpMasterBuilder masterBuilder;
@@ -37,9 +38,8 @@ public class ModbusTcpMasterHandler extends ChannelInboundHandlerAdapter {
 	}
 
 	@Override
-	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+	public void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf ) throws Exception {
 		//收数据
-		ByteBuf byteBuf = (ByteBuf) msg;
 		log.debug("re <== " + DataConvertor.ByteBuf2String(byteBuf));
 		((ModbusMasterBuilderInterface) this.masterBuilder).getOrCreateSynchronousWaitingRoom().setData(byteBuf.nioBuffer());
 	}

+ 3 - 3
protocol-modbus/src/main/java/wei/yigulu/modbus/netty/ModbusTcpSlaverHandle.java

@@ -5,6 +5,7 @@ import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.SimpleChannelInboundHandler;
 import lombok.NoArgsConstructor;
 import org.slf4j.Logger;
 import wei.yigulu.modbus.domain.request.TcpModbusRequest;
@@ -22,7 +23,7 @@ import java.net.InetSocketAddress;
  * @version 3.0
  */
 @NoArgsConstructor
-public class ModbusTcpSlaverHandle extends ChannelInboundHandlerAdapter {
+public class ModbusTcpSlaverHandle extends SimpleChannelInboundHandler<ByteBuf> {
 
 	protected Logger log;
 
@@ -40,9 +41,8 @@ public class ModbusTcpSlaverHandle extends ChannelInboundHandlerAdapter {
 
 
 	@Override
-	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+	public void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
 		//收数据
-		ByteBuf byteBuf = (ByteBuf) msg;
 		log.debug("re <=" + DataConvertor.ByteBuf2String(byteBuf));
 		TcpModbusRequest request = new TcpModbusRequest().decode(byteBuf.nioBuffer());
 		TcpModbusResponse response = new TcpModbusResponse();

+ 1 - 1
protocol-modbus/src/test/java/TestMaster.java

@@ -46,7 +46,7 @@ public class TestMaster {
 			} catch (Exception e) {
 				e.printStackTrace();
 			}
-			Thread.sleep(3000L);
+			Thread.sleep(30L);
 		}
 
 

+ 1 - 1
protocol-modbus/src/test/java/TestRtuMaster.java

@@ -45,7 +45,7 @@ public class TestRtuMaster {
 			} catch (ModbusException e) {
 				System.out.println(e.getMsg());
 			}
-			Thread.sleep(3000L);
+			Thread.sleep(10L);
 		}