feat:优化查找帧函数逻辑

This commit is contained in:
liyi 2025-03-11 15:48:08 +08:00
parent 9972b5ddeb
commit 1548934d34

View File

@ -189,40 +189,65 @@ class UdpTalkDataHandler extends ScpMessageBaseHandle
}
Future<List<Uint8List>> _processCompletePayload(Uint8List payload) async {
//
List<Uint8List> frames = [];
int startIdx = -1;
final length = payload.length - 1;
// payload
int i = 0;
while (i < payload.length - 1) {
// 0xFFD8
if (payload[i] == 0xFF && payload[i + 1] == 0xD8) {
int startIdx = i;
i += 2; //
for (int i = 0; i < length; i++) {
final currentByte = payload[i];
final nextByte = payload[i + 1];
// 0xFFD9
while (i < payload.length - 1) {
if (payload[i] == 0xFF && payload[i + 1] == 0xD9) {
// 0xFFD9
int endIdx = i + 2;
// 使 Uint8List.view
frames.add(
Uint8List.view(payload.buffer, startIdx, endIdx - startIdx));
i = endIdx; //
break;
} else {
i += 1; //
}
if (currentByte == 0xFF) {
if (nextByte == 0xD8) {
startIdx = i;
i++; // Skip the next byte
} else if (nextByte == 0xD9 && startIdx != -1) {
frames
.add(Uint8List.view(payload.buffer, startIdx, i + 2 - startIdx));
startIdx = -1;
i++; // Skip the next byte
}
} else {
i += 1; //
}
}
//
return frames;
}
// Future<List<Uint8List>> _processCompletePayload(Uint8List payload) async {
// //
// List<Uint8List> frames = [];
//
// // payload
// int i = 0;
// while (i < payload.length - 1) {
// // 0xFFD8
// if (payload[i] == 0xFF && payload[i + 1] == 0xD8) {
// int startIdx = i;
// i += 2; //
//
// // 0xFFD9
// while (i < payload.length - 1) {
// if (payload[i] == 0xFF && payload[i + 1] == 0xD9) {
// // 0xFFD9
// int endIdx = i + 2;
// // 使 Uint8List.view
// frames.add(
// Uint8List.view(payload.buffer, startIdx, endIdx - startIdx));
// i = endIdx; //
// break;
// } else {
// i += 1; //
// }
// }
// } else {
// i += 1; //
// }
// }
//
// //
// return frames;
// }
// Future<List<Uint8List>> _processCompletePayload(Uint8List payload) async {
// //
// List<Uint8List> frames = [];