Browse Source

opt:对NDC S消息进行消息校验和拆包处理

1
liejiu946 3 days ago
parent
commit
bce9278c2b
  1. 38
      nlsso-server/src/main/java/org/nl/acs/auto/run/OneNDCSocketConnectionAutoRun.java

38
nlsso-server/src/main/java/org/nl/acs/auto/run/OneNDCSocketConnectionAutoRun.java

@ -27,6 +27,7 @@ import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -103,19 +104,41 @@ public class OneNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
break; break;
} }
int[] arr = new int[count]; int[] packetData = new int[count];
StringBuffer bs = new StringBuffer(); StringBuffer bs = new StringBuffer();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
int temp = b[i]; int temp = b[i];
if (temp < 0) if (temp < 0)
temp += 256; temp += 256;
arr[i] = temp; packetData[i] = temp;
StringBuffer bs1 = new StringBuffer("0"); StringBuffer bs1 = new StringBuffer("0");
bs.append(temp < 16 ? bs1.append(Integer.toHexString(temp)) : Integer.toHexString(temp)); bs.append(temp < 16 ? bs1.append(Integer.toHexString(temp)) : Integer.toHexString(temp));
} }
// 从字节流的开头开始读取
//System.out.println("收到请求参数:" + bs); int offset = 0;
// 循环处理字节流,逐个拆包 按照S消息解析
while (offset < packetData.length) {
if (offset + 6 > packetData.length) {
System.out.println("错误:没有足够的字节来读取长度字段。");
break;
}
int messageHeader = packetData[offset] * 256 + packetData[offset+1];
//检测消息头是否有效
if (messageHeader == 0x87CD){
//消息头长度
int messageHeaderLength = packetData[offset+2] * 256 + packetData[offset+3];
//消息数据长度
int messageDataLength = packetData[offset+4] * 256 + packetData[offset+5];
//消息总长度
int messageLength = messageHeaderLength + messageDataLength;
// 检查剩余字节是否足够处理完整的数据包
if (offset + messageLength > packetData.length) {
System.out.println("错误:检测到不完整的数据包。");
// 数据包不完整,停止处理
break;
}
int[] arr = Arrays.copyOfRange(packetData, offset, offset+messageLength);
boolean flag = false; boolean flag = false;
if (arr[8] * 256 + arr[9] == 0x73) { if (arr[8] * 256 + arr[9] == 0x73) {
byte[] data = null; byte[] data = null;
@ -218,6 +241,13 @@ public class OneNDCSocketConnectionAutoRun extends AbstractAutoRunnable {
} else { } else {
System.out.println("agv上报不是0073类型动作,不处理"); System.out.println("agv上报不是0073类型动作,不处理");
} }
// 更新偏移量,继续处理下一个数据包
offset+=messageLength;
}else {
System.out.println("消息头无效,不处理。");
break;
}
}
} }
} catch (Exception e) { } catch (Exception e) {

Loading…
Cancel
Save