From d711c899e42e8cacee3ed6408f4c57e91c962dc8 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期四, 17 四月 2025 17:03:10 +0800
Subject: [PATCH] 改正发布判断bug

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/channel/tcp/DataDecoder.java |  151 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 151 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/channel/tcp/DataDecoder.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/channel/tcp/DataDecoder.java
new file mode 100644
index 0000000..3508fcd
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/channel/tcp/DataDecoder.java
@@ -0,0 +1,151 @@
+package com.dy.common.mw.channel.tcp;
+
+import com.dy.common.util.ByteUtil;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+import org.apache.mina.core.buffer.* ;
+import org.apache.mina.core.session.IoSession;
+
+
+public class DataDecoder extends CumulativeProtocolDecoder {
+
+	private static final Logger log = LogManager.getLogger(DataDecoder.class) ;
+
+	protected PrefixedDataAvailableHandle pdaHandle ;
+	
+	public DataDecoder(PrefixedDataAvailableHandle pdaHandle) {
+		this.pdaHandle = pdaHandle ;
+	}
+
+	/**
+	 * 瑙g爜
+	 * (non-Javadoc)
+	 * @see org.apache.mina.filter.codec.CumulativeProtocolDecoder#doDecode(
+	 * 	org.apache.mina.core.session.IoSession, 
+	 * 	org.apache.mina.core.buffer.IoBuffer, 
+	 * 	org.apache.mina.filter.codec.ProtocolDecoderOutput)
+	 */
+	@Override
+	protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) {
+		String rtuAddr = (String)session.getAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrRtuAddr) ;
+		if(rtuAddr == null){
+			//浼氳瘽鏈绠$悊
+			//浼氳瘽绠$悊鍣ㄤ腑涓嶅瓨鍦ㄦ浼氳瘽锛岃鏄庡垰寤虹珛缃戠粶杩炴帴锛�
+			return this.doDecode_onLineData(session, in, out) ;
+		}else{
+			//浼氳瘽宸茶绠$悊
+			//浼氳瘽绠$悊鍣ㄤ腑瀛樺湪姝や細璇濓紝璇存槑宸茬粡涓婄嚎浜嗭紝
+			return this.doDecode_data(session, in, out, rtuAddr) ;
+		}
+	}
+	
+	/**
+	 * 閽堝缃戠粶杩炴帴鎴愬姛鍚庣涓�鍖呮暟鎹�
+	 * @param session IO浼氳瘽
+	 * @param in 杈撳叆Buffer
+	 * @param out 鍗忚杈撳嚭缂栫爜
+	 * @return 鏄惁姝eソ鎴栫矘鍖�
+	 */
+	@SuppressWarnings("unused")
+	private boolean doDecode_onLineData(IoSession session, IoBuffer in, ProtocolDecoderOutput out){
+		//鏈変竴浜涘崗璁紝璐熻矗鍙戜笂绾挎暟鎹紝浣嗕笂绾�(鎴栧績璺�)鏁版嵁涓棤 Rtu鍦板潃锛屾墍浠ヨ繖鏍风殑鏁版嵁鍙兘鏀捐锛岃�屼笖涓嶈兘浜х敓鏈夎韩浠�(Rtu鍦板潃)鐨勭綉缁滀細璇濓紝鍗充笉鑳芥妸session鏀惧叆浼氳瘽缂撳瓨涓�
+		//鏈変竴浜涘崗璁紝Rtu璐熻矗鍙戜笂绾挎暟鎹紝涓婄嚎鏁版嵁涓湁 Rtu鍦板潃锛屾墍浠ヨ繖鏍风殑鏁版嵁鏀捐鍚庯紝鑳戒骇鐢熸湁韬唤(Rtu鍦板潃)鐨勭綉缁滀細璇濓紝鍗宠兘鎶妔ession鏀惧叆浼氳瘽缂撳瓨涓�
+		//涓嶈浣曠鎯呭舰锛屼笂绾挎暟鎹殑鏁版嵁閲忎笉浼氬緢澶э紝涓�鑸笉浼氫骇鐢熸柇鍖咃紝鎵�浠ヨ繖閲屽彧杩涜绠�鍗曟柇鍖呮鏌ャ��
+		PrefixedDataAvailableStatus dataStatus = this.pdaHandle.forOnLine(session, in) ;
+
+		if(dataStatus.protocolName != null && dataStatus.protocolVersion != null){
+			session.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, dataStatus.protocolName) ;
+			session.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolVersion, dataStatus.protocolVersion) ;
+		}
+
+		if(dataStatus.isCompleted() || dataStatus.isAdjoined()){
+			//姝eソ鎴栫矘鍖�
+			this.nextDeal(in, dataStatus.getDataLen(), out) ;
+			return true;
+		}else if(dataStatus.isRubbish()){
+			//鍨冨溇鏁版嵁
+			this.dealRubbishData(in, dataStatus.getDataLen()) ;
+			return true ;
+		}else{
+			//鏂寘
+			return false ;
+		}
+	}
+	
+
+	/**
+	 * 閽堝缃戠粶杩炴帴鎴愬姛鍚庨潪绗竴鍖呮暟鎹�
+	 * @param session IO浼氳瘽
+	 * @param in 杈撳叆Buffer
+	 * @param out 鍗忚杈撳嚭缂栫爜
+	 * @param rtuAddr 鎺у埗鍣ㄥ湴鍧�
+	 * @return 鏄惁姝eソ鎴栫矘鍖�
+	 */
+	@SuppressWarnings("unused")
+	private boolean doDecode_data(IoSession session, IoBuffer in, ProtocolDecoderOutput out, String rtuAddr) {
+		//闈炰笂绾挎暟鎹紝鍙兘浼氬嚭鐜版柇鍖呮垨绮樺寘鐜拌薄
+		PrefixedDataAvailableStatus dataStatus = this.pdaHandle.forUpData(session, in) ;
+		if(dataStatus == null){
+			//涓嶅彲鑳藉彂鐢�
+			log.error("涓ラ噸閿欒锛孯tu (RTU" + rtuAddr + ")涓婅鏁版嵁瀹屾暣鎬ф鏌ユ椂锛岃繑鍥炵殑瀵硅薄涓虹┖銆�") ;
+			this.nextDeal(in, null, out) ;
+			return true;
+		}else{
+			if(dataStatus.isBreaked()){
+				//鏂寘浜�
+				return false ;
+			}else if(dataStatus.isCompleted() || dataStatus.isAdjoined()){
+				//鏈寘鏁版嵁宸茬粡鍏ㄩ儴鎺ユ敹锛屾垨鍙兘绮樻湁涓嬪寘鏁版嵁
+				this.nextDeal(in, dataStatus.getDataLen(), out) ;
+				if(dataStatus.isAdjoined()){
+					//璇存槑绮樺寘浜嗭紝杩樻湁鏁版嵁锛岄渶瑕佸杩欎簺鏁版嵁鍐嶆鎵цdoDecode_鏂规硶.
+					return this.doDecode_data(session, in, out, rtuAddr) ;//鍔犱笂閫掑綊
+				}else if(dataStatus.isCompleted()){
+					//鏁版嵁涓嶆柇涓嶇矘
+					return true;
+				}else{
+					//涓嶅瓨鍦ㄦ绉嶆儏鍐�
+					return true ;
+				}
+			}else if(dataStatus.isRubbish()){
+				//鍨冨溇鏁版嵁
+				this.dealRubbishData(in, dataStatus.getDataLen()) ;
+				return true ;
+			}else{
+				//涓嶅瓨鍦ㄦ绉嶆儏鍐�
+				return true ;
+			}
+		}
+	}	
+    
+	/**
+	 * 鍚庣画澶勭悊鏁版嵁
+	 * @param in IObuffer
+	 * @param length 闀垮害
+	 * @param out 鍗忚缂栫爜杈撳嚭
+	 */
+	private void nextDeal(IoBuffer in, Integer length, ProtocolDecoderOutput out){
+		if(length == null){
+			length = in.limit() ;
+		}
+		if(length > 0){
+			byte[] data = new byte[length];
+			in.get(data);//get涓�涓瓧鑺傦紝鐩稿簲position鍚戝悗绉诲姩涓�涓瓧鑺�
+			out.write(data);
+		}
+	}
+	
+	/**
+	 * 鍨冨溇鏁版嵁澶勭悊
+	 * @param in IObuffer
+	 * @param length 闀垮害
+	 */
+	@SuppressWarnings("unused")
+	private void dealRubbishData(IoBuffer in, Integer length){
+		byte[] data = new byte[in.limit()];
+		in.get(data);//get涓�涓瓧鑺傦紝鐩稿簲position鍚戝悗绉诲姩涓�涓瓧鑺�
+		log.error("鎶涘純鍨冨溇鏁版嵁锛�" + ByteUtil.bytes2Hex(data, true));
+	}
+}

--
Gitblit v1.8.0