|  |  |  | 
|---|
|  |  |  | 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( | 
|---|
|  |  |  | protocolName = ProtocolConstantV206V202404.protocolName, | 
|---|
|  |  |  | protocolVersion = ProtocolConstantV206V202404.protocolVer, | 
|---|
|  |  |  | priority = ProtocolConstantV206V202404.priority, | 
|---|
|  |  |  | onLineDataMinLength = ProtocolConstantV206V202404.onLineDataMinLength, | 
|---|
|  |  |  | headMinLength = ProtocolConstantV206V202404.headMinLength) | 
|---|
|  |  |  | 
|---|
|  |  |  | private static final Logger log = LogManager.getLogger(PrefixedDataAvailableV202404.class) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final String protocolName = ProtocolConstantV206V202404.protocolName ; | 
|---|
|  |  |  | private static final short protocolVersion = ProtocolConstantV206V202404.protocolVer ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return 不是本协议数据时返回空 | 
|---|
|  |  |  | * @throws Exception 异常 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public PrefixedDataAvailableStatus forOnLine(IoBuffer in, | 
|---|
|  |  |  | int remain, | 
|---|
|  |  |  | int minDataLength, //对应ProtocolConstantV2_0.onLineDataMinLength | 
|---|
|  |  |  | int maxDataLength //对应ProtocolConstant.errorMaxLength | 
|---|
|  |  |  | public PrefixedDataAvailableStatus forOnLine(IoSession ioSession, | 
|---|
|  |  |  | IoBuffer in, | 
|---|
|  |  |  | int remain, | 
|---|
|  |  |  | int minDataLength, //对应ProtocolConstantV2_0.onLineDataMinLength | 
|---|
|  |  |  | int maxDataLength //对应ProtocolConstant.errorMaxLength | 
|---|
|  |  |  | ) throws Exception { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int oldPosition = in.position() ; | 
|---|
|  |  |  | 
|---|
|  |  |  | //in.position(0) ;//错误用法,如果发生粘包数据,将会死循环 | 
|---|
|  |  |  | in.position(oldPosition) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return this.doForData(preByte, minDataLength, maxDataLength) ; | 
|---|
|  |  |  | return this.doForData(ioSession, preByte, minDataLength, maxDataLength) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @throws Exception 异常 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PrefixedDataAvailableStatus forUpData(IoBuffer in, | 
|---|
|  |  |  | int remain, | 
|---|
|  |  |  | int minDataLength, | 
|---|
|  |  |  | int maxDataLength) throws Exception { | 
|---|
|  |  |  | 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(preByte, minDataLength, maxDataLength) ; | 
|---|
|  |  |  | return this.doForData(ioSession, preByte, minDataLength, maxDataLength) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 进行判断 | 
|---|
|  |  |  | * @param ioSession ioSession | 
|---|
|  |  |  | * @param preByte byte[] | 
|---|
|  |  |  | * @param minDataLength 最小数据长度 | 
|---|
|  |  |  | * @param maxDataLength 最大数据长度 | 
|---|
|  |  |  | * @return PrefixedDataAvailableStatus | 
|---|
|  |  |  | * @throws Exception 异常 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private PrefixedDataAvailableStatus doForData(byte[] preByte, int minDataLength, int maxDataLength) throws Exception{ | 
|---|
|  |  |  | private PrefixedDataAvailableStatus doForData(IoSession ioSession, byte[] preByte, int minDataLength, int maxDataLength) throws Exception{ | 
|---|
|  |  |  | int remain = preByte.length ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (remain < minDataLength) { | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(remain == frameLen){ | 
|---|
|  |  |  | //不断不粘 | 
|---|
|  |  |  | return new PrefixedDataAvailableStatus().completed(frameLen) ; | 
|---|
|  |  |  | return new PrefixedDataAvailableStatus().completed(frameLen, protocolName, protocolVersion) ; | 
|---|
|  |  |  | }else if(remain > frameLen){ | 
|---|
|  |  |  | String headHex = ByteUtil.bytes2Hex(preByte, true) ; | 
|---|
|  |  |  | log.warn("收到数据之帧前部:" + headHex + ",但发生粘包现象。") ; | 
|---|
|  |  |  | return new PrefixedDataAvailableStatus().adjoined(frameLen) ; | 
|---|
|  |  |  | return new PrefixedDataAvailableStatus().adjoined(frameLen, protocolName, protocolVersion) ; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | // remain < dataLen | 
|---|
|  |  |  | String headHex = ByteUtil.bytes2Hex(preByte, true) ; | 
|---|