fix:调整udp返回数据为工厂模式处理
This commit is contained in:
parent
3e25831132
commit
28e346a163
@ -1,3 +1,6 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crc32_checksum/crc32_checksum.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/message_type_constant.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart';
|
||||
@ -32,6 +35,7 @@ class MessageCommand {
|
||||
required String ToPeerId,
|
||||
required String FromPeerId,
|
||||
}) {
|
||||
String payload = 'hello';
|
||||
ScpMessage message = ScpMessage(
|
||||
ProtocolFlag: ProtocolFlagConstant.scp01,
|
||||
MessageType: MessageTypeConstant.Req,
|
||||
@ -40,12 +44,11 @@ class MessageCommand {
|
||||
SpIndex: 0,
|
||||
FromPeerId: FromPeerId,
|
||||
ToPeerId: ToPeerId,
|
||||
Payload: 'hello',
|
||||
PayloadCRC: 55230,
|
||||
PayloadLength: 5,
|
||||
Payload: payload,
|
||||
PayloadCRC: calculationCrc(_stringToUint8List(payload)),
|
||||
PayloadLength: payload.length,
|
||||
PayloadType: PayloadTypeConstant.echoTest,
|
||||
);
|
||||
|
||||
String serializedBytesString = message.serialize();
|
||||
return _hexToBytes(serializedBytesString);
|
||||
}
|
||||
@ -55,6 +58,7 @@ class MessageCommand {
|
||||
required String FromPeerId,
|
||||
required String ToPeerId,
|
||||
}) {
|
||||
String payload = 'hello';
|
||||
ScpMessage message = ScpMessage(
|
||||
ProtocolFlag: ProtocolFlagConstant.scp01,
|
||||
MessageType: MessageTypeConstant.Req,
|
||||
@ -63,9 +67,9 @@ class MessageCommand {
|
||||
SpIndex: 0,
|
||||
FromPeerId: FromPeerId,
|
||||
ToPeerId: ToPeerId,
|
||||
Payload: 'hello',
|
||||
PayloadCRC: 55230,
|
||||
PayloadLength: 5,
|
||||
Payload: payload,
|
||||
PayloadCRC: calculationCrc(_stringToUint8List(payload)),
|
||||
PayloadLength: payload.length,
|
||||
PayloadType: PayloadTypeConstant.heartbeat,
|
||||
);
|
||||
String serializedBytesString = message.serialize();
|
||||
@ -95,7 +99,7 @@ class MessageCommand {
|
||||
FromPeerId: FromPeerId,
|
||||
ToPeerId: ToPeerId,
|
||||
Payload: payload,
|
||||
PayloadCRC: 55230,
|
||||
PayloadCRC: calculationCrc(payload),
|
||||
PayloadLength: payload.length,
|
||||
PayloadType: PayloadTypeConstant.gatewayReset,
|
||||
);
|
||||
@ -108,6 +112,7 @@ class MessageCommand {
|
||||
required String FromPeerId,
|
||||
required String ToPeerId,
|
||||
}) {
|
||||
String payload = 'hello';
|
||||
ScpMessage message = ScpMessage(
|
||||
ProtocolFlag: ProtocolFlagConstant.scp01,
|
||||
MessageType: MessageTypeConstant.Req,
|
||||
@ -116,9 +121,9 @@ class MessageCommand {
|
||||
SpIndex: 0,
|
||||
FromPeerId: FromPeerId,
|
||||
ToPeerId: ToPeerId,
|
||||
Payload: 'hello',
|
||||
PayloadCRC: 55230,
|
||||
PayloadLength: 5,
|
||||
Payload: payload,
|
||||
PayloadCRC: calculationCrc(_stringToUint8List(payload)),
|
||||
PayloadLength: payload.length,
|
||||
PayloadType: PayloadTypeConstant.talkPing,
|
||||
);
|
||||
String serializedBytesString = message.serialize();
|
||||
@ -134,8 +139,43 @@ class MessageCommand {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static int calculationCrc(payload) {
|
||||
var checkSumResult = Crc32.calculate(payload);
|
||||
return checkSumResult;
|
||||
static int calculationCrc(Uint8List uint8Payload) {
|
||||
// 使用自定义 CRC32 算法
|
||||
final int crc32Value = _crc32Uint16(uint8Payload);
|
||||
return crc32Value;
|
||||
}
|
||||
|
||||
// 将字符串转换为 Uint8List
|
||||
static Uint8List _stringToUint8List(String input) {
|
||||
return Uint8List.fromList(utf8.encode(input));
|
||||
}
|
||||
|
||||
// 自定义 CRC32 实现
|
||||
static int _crc32Uint16(Uint8List data) {
|
||||
const int polynomial = 0xD5828281;
|
||||
|
||||
// 创建 CRC32 表
|
||||
final List<int> table = List<int>.generate(256, (i) {
|
||||
int crc = i;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if ((crc & 1) != 0) {
|
||||
crc = (crc >> 1) ^ polynomial;
|
||||
} else {
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
});
|
||||
|
||||
// 计算 CRC32 校验值
|
||||
int crc = 0xFFFFFFFF;
|
||||
for (final int byte in data) {
|
||||
crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
|
||||
}
|
||||
|
||||
crc ^= 0xFFFFFFFF;
|
||||
|
||||
// 返回 CRC32 的低 16 位
|
||||
return crc & 0xFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
@ -594,7 +594,7 @@ class StartChartManage {
|
||||
ScpMessageHandlerFactory.createHandler(payloadType);
|
||||
handler.handle(scpMessage);
|
||||
} catch (e) {
|
||||
_log(text: '❌ _handleUdpResultData 处理udp返回数据时遇到错误---> $e');
|
||||
_log(text: '❌ 处理udp返回数据时遇到错误---> $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user