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