From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期一, 07 四月 2025 15:18:51 +0800 Subject: [PATCH] 整理版本 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/PrefixedDataAvailableV1.java | 135 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 135 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/PrefixedDataAvailableV1.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/PrefixedDataAvailableV1.java new file mode 100644 index 0000000..eece0d6 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/PrefixedDataAvailableV1.java @@ -0,0 +1,135 @@ +package com.dy.common.mw.protocol.p206V1; + +import com.dy.common.mw.channel.tcp.PrefixedDataAvailableStatus; +import com.dy.common.mw.protocol.AnnotationPrefixedDataAvailable; +import com.dy.common.mw.protocol.PrefixedDataAvailable; +import com.dy.common.util.ByteUtil; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.mina.core.buffer.BufferDataException; +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.session.IoSession; + + +@AnnotationPrefixedDataAvailable( + enable = true, + protocolName = ProtocolConstantV206V1.protocolName, + protocolVersion = ProtocolConstantV206V1.protocolVer, + priority = ProtocolConstantV206V1.priority, + onLineDataMinLength = ProtocolConstantV206V1.onLineDataMinLength, + headMinLength = ProtocolConstantV206V1.headMinLength) +@SuppressWarnings("unused") +public class PrefixedDataAvailableV1 implements PrefixedDataAvailable { + + private static final Logger log = LogManager.getLogger(PrefixedDataAvailableV1.class) ; + + private static final String protocolName = ProtocolConstantV206V1.protocolName ; + private static final short protocolVersion = ProtocolConstantV206V1.protocolVer ; + + /** + * 鍒嗘瀽涓婄嚎鏁版嵁(缃戠粶杩炴帴鍚庣涓�鍖呮暟鎹�)鏄惁鍙幏寰� + * @param in IoBuffer + * @param remain 涓�瀹氭椂澶т簬0鐨勬暟鎹� + * @param minDataLength 鏈�灏忔暟鎹暱搴� + * @param maxDataLength 鏈�澶ф暟鎹暱搴︼紝杈惧埌鎴栬秴杩囨闀垮害锛岃涓烘槸鍨冨溇鏁版嵁銆備緥濡傦細鏁版嵁澶撮儴鏄纭殑锛屼絾鍚堟硶鏁版嵁缁撳熬鎬讳笉鍑虹幇锛岃涓烘鏁版嵁鍨冨溇鏁版嵁 + * @return 涓嶆槸鏈崗璁暟鎹椂杩斿洖绌� + * @throws Exception 寮傚父 + */ + public PrefixedDataAvailableStatus forOnLine(IoSession ioSession, + IoBuffer in, + int remain, + int minDataLength, + int maxDataLength + ) throws Exception { + + int oldPosition = in.position() ; + byte[] preByte = new byte[remain]; + in.get(preByte) ; + //in.position(0) ;//閿欒鐢ㄦ硶锛屽鏋滃彂鐢熺矘鍖呮暟鎹紝灏嗕細姝诲惊鐜� + in.position(oldPosition) ; + + return this.doForData(ioSession, preByte, minDataLength, maxDataLength) ; + } + + /** + * 鍒嗘瀽涓婄嚎鍚�(缃戠粶杩炴帴鍚庨潪绗竴鍖呮暟鎹�)鏄惁鍙幏寰� + * @param in IoBuffer + * @param remain 鍓╀綑 + * @param minDataLength 鏈�灏忔暟鎹暱搴� + * @param maxDataLength 鏈�澶ф暟鎹暱搴� + * @return PrefixedDataAvailableStatus + * @throws Exception 寮傚父 + */ + @Override + public PrefixedDataAvailableStatus forUpData(IoSession ioSession, + IoBuffer in, + int remain, + int minDataLength, + int maxDataLength) throws Exception { + int oldPosition = in.position() ; + byte[] preByte = new byte[remain]; + in.get(preByte) ; + //in.position(0) ;//閿欒鐢ㄦ硶锛屽鏋滃彂鐢熺矘鍖呮暟鎹紝灏嗕細姝诲惊鐜� + in.position(oldPosition) ; + + return this.doForData(ioSession, preByte, minDataLength, maxDataLength) ; + + } + /** + * 杩涜鍒ゆ柇 + * @param ioSession ioSession + * @param preByte byte[] + * @param minDataLength 鏈�灏忔暟鎹暱搴� + * @param maxDataLength 鏈�澶ф暟鎹暱搴� + * @return PrefixedDataAvailableStatus + * @throws Exception 寮傚父 + */ + private PrefixedDataAvailableStatus doForData(IoSession ioSession, byte[] preByte, int minDataLength, int maxDataLength) throws Exception{ + int remain = preByte.length ; + + if (remain < minDataLength) { + log.warn("鍩轰簬鍗忚锛�" + protocolName + "锛夊垽鏂暟鎹ご閮ㄥ彂鐢熸柇鍖呯幇璞°��") ; + return new PrefixedDataAvailableStatus().breaked() ; + } + + if(remain >= maxDataLength){ + //瓒呭嚭浜嗘渶澶ч暱搴﹂檺鍒�,渚嬪锛氭暟鎹ご閮ㄦ槸姝g‘鐨勶紝浣嗗悎娉曟暟鎹粨灏炬�讳笉鍑虹幇锛岃涓烘鏁版嵁鍨冨溇鏁版嵁 + return new PrefixedDataAvailableStatus().rubbish(remain) ; + } + + CommonV1 parseCommon = new CommonV1(); + Boolean[] isThisProtocolData = parseCommon.isThisProtocolHead(preByte) ; + + if(isThisProtocolData == null || isThisProtocolData.length != 2 || !isThisProtocolData[0].booleanValue()){ + //涓嶆槸鏈琑TU鐨勫崗璁暟鎹� + return null ; + } + + Integer dataLen = parseCommon.parseFrameLen(preByte, isThisProtocolData[1]) ; + + if(dataLen == null){ + String headHex = ByteUtil.bytes2Hex(preByte, true) ; + throw new BufferDataException("鏀跺埌鏁版嵁涔嬪抚鍓嶉儴锛�" + headHex + "锛屼絾涓ラ噸閿欒锛屽湪杩涜鏂寘涓庣矘鍖呮鏌ユ椂锛屾湭鑳藉緱鍒版暟鎹抚鐨勯暱搴︺��"); + } + + if (dataLen <= 0 || dataLen > maxDataLength) { + String headHex = ByteUtil.bytes2Hex(preByte, true) ; + throw new BufferDataException("鏀跺埌鏁版嵁涔嬪抚鍓嶉儴锛�" + headHex + "锛屼絾涓ラ噸閿欒锛屽湪杩涜鏂寘涓庣矘鍖呮鏌ユ椂锛屾暟鎹抚鐨勯暱搴�(" + dataLen + ")瓒呭嚭鍚堟硶鑼冨洿銆�"); + } + + if(remain == dataLen){ + //涓嶆柇涓嶇矘 + return new PrefixedDataAvailableStatus().completed(dataLen, protocolName, protocolVersion) ; + }else if(remain > dataLen){ + String headHex = ByteUtil.bytes2Hex(preByte, true) ; + log.warn("鏀跺埌鏁版嵁涔嬪抚鍓嶉儴锛�" + headHex + "锛屼絾鍙戠敓绮樺寘鐜拌薄銆�") ; + return new PrefixedDataAvailableStatus().adjoined(dataLen, protocolName, protocolVersion) ; + }else{ + // remain < dataLen + String headHex = ByteUtil.bytes2Hex(preByte, true) ; + log.warn("鏀跺埌鏁版嵁涔嬪抚鍓嶉儴锛�" + headHex + "锛屼絾鍙戠敓鏂寘鐜拌薄銆�") ; + return new PrefixedDataAvailableStatus().breaked() ; + } + } + +} -- Gitblit v1.8.0