Explorar el Código

对于已标记关闭的通道,在收到心跳包时进行强制关闭,并重置通道标记

chenwen hace 2 semanas
padre
commit
a396e1f23d
Se han modificado 1 ficheros con 29 adiciones y 0 borrados
  1. 29 0
      src/main/java/com/hb/proj/gather/protocol/ZLOpdProtHandler.java

+ 29 - 0
src/main/java/com/hb/proj/gather/protocol/ZLOpdProtHandler.java

@@ -1,8 +1,12 @@
 package com.hb.proj.gather.protocol;
 package com.hb.proj.gather.protocol;
 
 
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
+import com.hb.proj.gather.scheduler.TaskFutureCode;
 import com.hb.proj.gather.utils.ByteUtils;
 import com.hb.proj.gather.utils.ByteUtils;
 import com.hb.proj.gather.utils.Crc16Utils;
 import com.hb.proj.gather.utils.Crc16Utils;
 
 
@@ -28,6 +32,24 @@ public class ZLOpdProtHandler extends ChannelInboundHandlerAdapter {
 	public void channelActive(ChannelHandlerContext ctx) throws Exception {
 	public void channelActive(ChannelHandlerContext ctx) throws Exception {
 		logger.info("有设备连接上:{}",ctx.channel().remoteAddress());
 		logger.info("有设备连接上:{}",ctx.channel().remoteAddress());
 	}
 	}
+	
+	
+	//判断通道的状态标记是否为关闭状态(多次发指令未响应,主动关闭)
+	private boolean  isChannelCloseTag(String serial) {
+		Integer futureRst=TaskFutureCode.SUCCESS;
+		try {
+			Future<Integer> chnTskFuture=ChannelGroupMgr.getFuture(serial);
+			if(chnTskFuture!=null && !chnTskFuture.isCancelled()) {
+				futureRst=chnTskFuture.get(1, TimeUnit.SECONDS); 
+			}
+		}
+		catch(Exception e) {
+			logger.error("获取通道{}状态标志出错:{}",serial,e.getMessage());
+		}
+		
+		return futureRst!=null && futureRst==TaskFutureCode.CLOSED;
+		
+	}
 
 
 	
 	
 	/**
 	/**
@@ -68,6 +90,13 @@ public class ZLOpdProtHandler extends ChannelInboundHandlerAdapter {
 			
 			
 			logger.info("收到心跳:{}",hexmsg);
 			logger.info("收到心跳:{}",hexmsg);
 			
 			
+			if(isChannelCloseTag(serial)) {
+				logger.info("通道{}被标记为关闭,但还收到心跳,准备强制关闭,并移除标记",serial);
+				ChannelGroupMgr.resetChannel(serial);
+				ctx.channel().unsafe().closeForcibly();
+			}
+			
+			
 		}
 		}
 		else if(byteCount>2 && isWriteBackMsg){ //(动液面设备设置指令返回消息不规范(按原指令返回),不做crc校验)
 		else if(byteCount>2 && isWriteBackMsg){ //(动液面设备设置指令返回消息不规范(按原指令返回),不做crc校验)
 			if(!ByteUtils.isValidWriteBackMsg(cmd)) { //针对部分设备在电流图,电功率图采集时非正常返回处理(两次返回,第1次,返回原指令(去掉了地址号),第2次才正常返回)
 			if(!ByteUtils.isValidWriteBackMsg(cmd)) { //针对部分设备在电流图,电功率图采集时非正常返回处理(两次返回,第1次,返回原指令(去掉了地址号),第2次才正常返回)