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