feat:调整组包逻辑

This commit is contained in:
liyi 2025-03-13 13:39:19 +08:00
parent fc23d8f851
commit 5e781a5f00

View File

@ -43,7 +43,7 @@ class ScpMessageBaseHandle {
// messageId // messageId
static Map<String, List<List<int>>> _packetBuffer = {}; static Map<String, List<List<int>>> _packetBuffer = {};
final Map<String, Timer> _packetTimers = {}; final Map<String, Timer> _packetTimers = {};
final Duration _timeoutDuration = Duration(seconds: 10); // final Duration _timeoutDuration = Duration(seconds: 3); //
// //
final TalkDataRepository talkDataRepository = TalkDataRepository.instance; final TalkDataRepository talkDataRepository = TalkDataRepository.instance;
@ -106,40 +106,104 @@ class ScpMessageBaseHandle {
required int payloadType, required int payloadType,
}) { }) {
// //
String key = '$messageId-$payloadType'; // 使key生成方式
if (!_packetBuffer.containsKey(key)) { final key = '${messageId}_$payloadType';
_packetBuffer[key] = List.filled(spTotal, []);
_startTimer(key); //
} // AppLog.log(
// '📦 收到分包 - MessageId: $messageId, 总包数: $spTotal, 当前包序号: $spIndex');
// //
if (spIndex < 1 || spIndex > spTotal) { if (spIndex < 1 || spIndex > spTotal) {
// print( AppLog.log(
// 'Invalid spTotal: $spTotal spIndex: $spIndex for messageId: $messageId'); '❌ 分包序号异常 - MessageId: $messageId, 总包数: $spTotal, 无效包序号: $spIndex');
return null; return null;
} }
// ()
if (spIndex < 1 || spIndex > spTotal) return null;
// (使)
var packets = _packetBuffer[key];
if (packets == null) {
// ,
packets = List<List<int>>.filled(spTotal, const [], growable: false);
_packetBuffer[key] = packets;
_startTimer(key);
// AppLog.log('📝 新建分包缓存 - MessageId: $messageId, 预期总包数: $spTotal');
}
// //
_packetBuffer[key]![spIndex - 1] = byte; packets[spIndex - 1] = byte;
// // ,使every
if (_packetBuffer[key]!.every((packet) => packet.isNotEmpty)) { var isComplete = true;
// var totalLength = 0;
Uint8List completePayload = Uint8List.fromList( for (var i = 0; i < packets.length; i++) {
_packetBuffer[key]!.expand((packet) => packet).toList()); if (packets[i].isEmpty) {
// isComplete = false;
} else {
totalLength += packets[i].length;
}
}
if (isComplete) {
// buffer,
final buffer = Uint8List(totalLength);
var offset = 0;
// ,使expand
for (var packet in packets) {
buffer.setRange(offset, offset + packet.length, packet);
offset += packet.length;
}
//
_clearPacketData(key); _clearPacketData(key);
// 使TalkData // TalkData
if (payloadType == PayloadTypeConstant.talkData) { if (payloadType == PayloadTypeConstant.talkData) {
final talkData = TalkData(); final talkData = TalkData();
talkData.mergeFromBuffer(completePayload); talkData.mergeFromBuffer(buffer);
return talkData; return talkData;
} }
} else {
// null
return null;
} }
return null;
// if (!_packetBuffer.containsKey(key)) {
// _packetBuffer[key] = List.filled(spTotal, []);
// _startTimer(key);
// }
//
// //
// if (spIndex < 1 || spIndex > spTotal) {
// // print(
// // 'Invalid spTotal: $spTotal spIndex: $spIndex for messageId: $messageId');
// return null;
// }
//
// //
// _packetBuffer[key]![spIndex - 1] = byte;
//
// //
// if (_packetBuffer[key]!.every((packet) => packet.isNotEmpty)) {
// //
// Uint8List completePayload = Uint8List.fromList(
// _packetBuffer[key]!.expand((packet) => packet).toList());
// //
// _clearPacketData(key);
//
// // 使TalkData
// if (payloadType == PayloadTypeConstant.talkData) {
// final talkData = TalkData();
// talkData.mergeFromBuffer(completePayload);
// return talkData;
// }
// } else {
// // null
// return null;
// }
} }
// //