fix:增加分包发送、接收分包数据后的组包逻辑、增加部分测试代码
This commit is contained in:
parent
ba47b2731b
commit
5574dc8043
@ -31,8 +31,12 @@ class StarChartLogic extends BaseGetXController {
|
|||||||
state.oneMinuteTime.value = 0;
|
state.oneMinuteTime.value = 0;
|
||||||
|
|
||||||
if (state.talkStatus.value == TalkStatus.rejected.index ||
|
if (state.talkStatus.value == TalkStatus.rejected.index ||
|
||||||
|
state.talkStatus.value == TalkStatus.notTalkData.index ||
|
||||||
|
state.talkStatus.value == TalkStatus.notTalkPing.index ||
|
||||||
state.talkStatus.value == TalkStatus.end.index) {
|
state.talkStatus.value == TalkStatus.end.index) {
|
||||||
_cancelTimers();
|
_cancelTimers();
|
||||||
|
// 状态错误,返回页面
|
||||||
|
Get.back();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -220,14 +220,17 @@ class _StarLockMainPageState extends State<StarLockMainPage>
|
|||||||
SizedBox(
|
SizedBox(
|
||||||
height: 160.h,
|
height: 160.h,
|
||||||
),
|
),
|
||||||
SubmitBtn(
|
// _buildImage(),
|
||||||
btnName: '发送回声测试'.tr,
|
// SubmitBtn(
|
||||||
onClick: () async {
|
// btnName: '发送回声测试'.tr,
|
||||||
String assetPath = 'assets/test.jpg'; // 替换为你的图片路径
|
// onClick: () async {
|
||||||
List<int> imageBytes = await loadAssetImageAsBytes(assetPath);
|
// String assetPath = 'assets/test.jpg'; // 替换为你的图片路径
|
||||||
StartChartManage().sendEchoMessage(payload: imageBytes);
|
// List<int> imageBytes = await loadAssetImageAsBytes(assetPath);
|
||||||
},
|
// // state.talkData.value = imageBytes;
|
||||||
),
|
// // setState(() {});
|
||||||
|
// StartChartManage().sendEchoMessage(payload: imageBytes);
|
||||||
|
// },
|
||||||
|
// ),
|
||||||
if (F.isLite)
|
if (F.isLite)
|
||||||
Container()
|
Container()
|
||||||
else
|
else
|
||||||
|
|||||||
@ -51,9 +51,9 @@ class MessageCommand {
|
|||||||
SpIndex: 1,
|
SpIndex: 1,
|
||||||
FromPeerId: FromPeerId,
|
FromPeerId: FromPeerId,
|
||||||
ToPeerId: ToPeerId,
|
ToPeerId: ToPeerId,
|
||||||
Payload: 'hello',
|
Payload: [0],
|
||||||
PayloadCRC: 55230,
|
PayloadCRC: 0,
|
||||||
PayloadLength: 5,
|
PayloadLength: 0,
|
||||||
PayloadType: PayloadTypeConstant.goOnline,
|
PayloadType: PayloadTypeConstant.goOnline,
|
||||||
).serialize();
|
).serialize();
|
||||||
return _hexToBytes(serializedBytesString);
|
return _hexToBytes(serializedBytesString);
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'package:get/get.dart';
|
||||||
import 'package:protobuf/protobuf.dart';
|
import 'package:protobuf/protobuf.dart';
|
||||||
import 'package:star_lock/app_settings/app_settings.dart';
|
import 'package:star_lock/app_settings/app_settings.dart';
|
||||||
import 'package:star_lock/talk/startChart/constant/message_type_constant.dart';
|
import 'package:star_lock/talk/startChart/constant/message_type_constant.dart';
|
||||||
import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart';
|
import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart';
|
||||||
import 'package:star_lock/talk/startChart/entity/heartbeat_response.dart';
|
import 'package:star_lock/talk/startChart/entity/heartbeat_response.dart';
|
||||||
import 'package:star_lock/talk/startChart/entity/login_response.dart';
|
import 'package:star_lock/talk/startChart/entity/login_response.dart';
|
||||||
|
import 'package:star_lock/talk/startChart/handle/other/talk_data_repository.dart';
|
||||||
import 'package:star_lock/talk/startChart/proto/ble_message.pb.dart';
|
import 'package:star_lock/talk/startChart/proto/ble_message.pb.dart';
|
||||||
import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart';
|
import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart';
|
||||||
import 'package:star_lock/talk/startChart/proto/gateway_transfer.pb.dart';
|
import 'package:star_lock/talk/startChart/proto/gateway_transfer.pb.dart';
|
||||||
@ -168,9 +170,9 @@ class ScpMessage {
|
|||||||
static ScpMessage deserialize(List<int> bytes) {
|
static ScpMessage deserialize(List<int> bytes) {
|
||||||
final message = ScpMessage();
|
final message = ScpMessage();
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
_log(
|
// _log(
|
||||||
text:
|
// text:
|
||||||
'原始字节数组: ${bytes.sublist(0, 20).map((b) => b.toRadixString(16)).join(" ")}');
|
// '原始字节数组: ${bytes.sublist(0, 20).map((b) => b.toRadixString(16)).join(" ")}');
|
||||||
// ProtocolFlag (4 bytes)
|
// ProtocolFlag (4 bytes)
|
||||||
if (bytes.length - offset >= 4) {
|
if (bytes.length - offset >= 4) {
|
||||||
message.ProtocolFlag = utf8.decode(bytes.sublist(offset, offset + 4));
|
message.ProtocolFlag = utf8.decode(bytes.sublist(offset, offset + 4));
|
||||||
@ -271,6 +273,9 @@ class ScpMessage {
|
|||||||
spTotal: message.SpTotal,
|
spTotal: message.SpTotal,
|
||||||
messageId: message.MessageId,
|
messageId: message.MessageId,
|
||||||
);
|
);
|
||||||
|
// if (message.Payload != null && message.Payload is List<int>) {
|
||||||
|
// message.PayloadLength = message.Payload.length;
|
||||||
|
// }
|
||||||
} else {
|
} else {
|
||||||
throw FormatException("Invalid Payload or PayloadLength");
|
throw FormatException("Invalid Payload or PayloadLength");
|
||||||
}
|
}
|
||||||
@ -466,11 +471,9 @@ class ScpMessage {
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_log(text: '❌反序列化udp数据时遇到错误----》$e');
|
e.printError();
|
||||||
|
_log(text: '❌反序列化udp数据时遇到错误----》$e,message:$messageType byte:${byte.take(20)}');
|
||||||
// 尝试打印原始字节数组以供调试
|
// 尝试打印原始字节数组以供调试
|
||||||
_log(
|
|
||||||
text:
|
|
||||||
'原始字节数组: ${byte.sublist(0, 20).map((b) => b.toRadixString(16)).join(" ")}');
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -495,12 +498,14 @@ class ScpMessage {
|
|||||||
|
|
||||||
/// 处理分包逻辑
|
/// 处理分包逻辑
|
||||||
/// 如果没有收到所有包则返回null
|
/// 如果没有收到所有包则返回null
|
||||||
static String? _handleFragmentedPayload({
|
static dynamic _handleFragmentedPayload({
|
||||||
required int messageId,
|
required int messageId,
|
||||||
required int spTotal,
|
required int spTotal,
|
||||||
required int spIndex,
|
required int spIndex,
|
||||||
required List<int> byte,
|
required List<int> byte,
|
||||||
}) {
|
}) {
|
||||||
|
print(
|
||||||
|
'_handleFragmentedPayload spTotal:$spTotal spindex:$spIndex} byte:${byte.take(20)}');
|
||||||
// 初始化分包列表
|
// 初始化分包列表
|
||||||
if (!_packetBuffer.containsKey(messageId)) {
|
if (!_packetBuffer.containsKey(messageId)) {
|
||||||
_packetBuffer[messageId] = List.filled(spTotal, []);
|
_packetBuffer[messageId] = List.filled(spTotal, []);
|
||||||
@ -518,9 +523,11 @@ class ScpMessage {
|
|||||||
// 清除已重组的分包数据
|
// 清除已重组的分包数据
|
||||||
_packetBuffer.remove(messageId);
|
_packetBuffer.remove(messageId);
|
||||||
|
|
||||||
|
return completePayload;
|
||||||
|
|
||||||
// 解析完整的 payload
|
// 解析完整的 payload
|
||||||
String payload = utf8.decode(completePayload);
|
// final TalkData talkData = TalkData.fromBuffer(completePayload);
|
||||||
return payload;
|
// String payload = utf8.decode(completePayload);
|
||||||
} else {
|
} else {
|
||||||
// 如果分包尚未接收完全,返回 null 或其他指示符
|
// 如果分包尚未接收完全,返回 null 或其他指示符
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import 'package:get/get.dart';
|
|||||||
import 'package:star_lock/talk/startChart/entity/scp_message.dart';
|
import 'package:star_lock/talk/startChart/entity/scp_message.dart';
|
||||||
import 'package:star_lock/talk/startChart/handle/scp_message_base_handle.dart';
|
import 'package:star_lock/talk/startChart/handle/scp_message_base_handle.dart';
|
||||||
import 'package:star_lock/talk/startChart/handle/scp_message_handle.dart';
|
import 'package:star_lock/talk/startChart/handle/scp_message_handle.dart';
|
||||||
|
import 'package:star_lock/talk/startChart/proto/talk_data.pb.dart';
|
||||||
|
|
||||||
import '../../start_chart_manage.dart';
|
import '../../start_chart_manage.dart';
|
||||||
|
|
||||||
@ -16,7 +17,13 @@ class UdpEchoTestHandler extends ScpMessageBaseHandle
|
|||||||
@override
|
@override
|
||||||
void handleResp(ScpMessage scpMessage) {
|
void handleResp(ScpMessage scpMessage) {
|
||||||
// TODO: 收到回声测试回复
|
// TODO: 收到回声测试回复
|
||||||
EasyLoading.showToast(scpMessage.Payload, duration: 2000.milliseconds);
|
final List<int> payload = scpMessage.Payload;
|
||||||
|
|
||||||
|
talkDataRepository.addTalkData(
|
||||||
|
TalkData(content: payload, contentType: TalkData_ContentTypeE.Image));
|
||||||
|
if (payload is String) {
|
||||||
|
EasyLoading.showToast(scpMessage.Payload, duration: 2000.milliseconds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@ -26,9 +26,8 @@ class UdpTalkAcceptHandler extends ScpMessageBaseHandle
|
|||||||
// 收到同意接听回复
|
// 收到同意接听回复
|
||||||
final GenericResp genericResp = scpMessage.Payload;
|
final GenericResp genericResp = scpMessage.Payload;
|
||||||
if (checkGenericRespSuccess(genericResp)) {
|
if (checkGenericRespSuccess(genericResp)) {
|
||||||
|
|
||||||
// 延迟2秒后启动监听
|
// 延迟2秒后启动监听
|
||||||
Future.delayed(Duration(seconds: 4), () {
|
Future.delayed(Duration(seconds: 3), () {
|
||||||
// 启动通话保持定时器
|
// 启动通话保持定时器
|
||||||
_handleStartTalkPing();
|
_handleStartTalkPing();
|
||||||
// 启动发送预期数据请求
|
// 启动发送预期数据请求
|
||||||
|
|||||||
@ -75,7 +75,7 @@ class StartChartManage {
|
|||||||
int talkDataIntervalTime = 10; // 通话数据的消息间隔(ms)
|
int talkDataIntervalTime = 10; // 通话数据的消息间隔(ms)
|
||||||
Timer? talkDataTimer; // 发送通话数据消息定时器
|
Timer? talkDataTimer; // 发送通话数据消息定时器
|
||||||
|
|
||||||
final int _maxPayloadSize = 8 * 1024; // 分包大小
|
final int _maxPayloadSize = 1 * 1024; // 分包大小
|
||||||
|
|
||||||
// 默认通话的期望数据格式
|
// 默认通话的期望数据格式
|
||||||
TalkExpect defaultTalkExpect = TalkExpect(
|
TalkExpect defaultTalkExpect = TalkExpect(
|
||||||
@ -281,7 +281,7 @@ class StartChartManage {
|
|||||||
|
|
||||||
// 发送回声测试消息
|
// 发送回声测试消息
|
||||||
void sendEchoMessage({required List<int> payload}) async {
|
void sendEchoMessage({required List<int> payload}) async {
|
||||||
String toPeerId = 'G7fzJkbS5MigMqnbTCQVk7VspcDsnGeikJpQwS8fbhim';
|
String toPeerId = '2vzXdjdzipJBpWpJxhiRzCFXrDKk54t3YJ7EjYPSRuij';
|
||||||
|
|
||||||
// 计算需要分多少个包发送
|
// 计算需要分多少个包发送
|
||||||
final int totalPackets = (payload.length / _maxPayloadSize).ceil();
|
final int totalPackets = (payload.length / _maxPayloadSize).ceil();
|
||||||
@ -295,6 +295,7 @@ class StartChartManage {
|
|||||||
// 取到分包数据
|
// 取到分包数据
|
||||||
List<int> packet = payload.sublist(start, end);
|
List<int> packet = payload.sublist(start, end);
|
||||||
|
|
||||||
|
final messageId = MessageCommand.getNextMessageId(toPeerId, increment: false);
|
||||||
// 组装分包数据
|
// 组装分包数据
|
||||||
final message = MessageCommand.echoMessage(
|
final message = MessageCommand.echoMessage(
|
||||||
ToPeerId: toPeerId,
|
ToPeerId: toPeerId,
|
||||||
@ -302,13 +303,13 @@ class StartChartManage {
|
|||||||
payload: packet,
|
payload: packet,
|
||||||
SpTotal: totalPackets,
|
SpTotal: totalPackets,
|
||||||
SpIndex: i + 1,
|
SpIndex: i + 1,
|
||||||
MessageId: MessageCommand.getNextMessageId(toPeerId, increment: false),
|
MessageId: messageId,
|
||||||
);
|
);
|
||||||
// 发送消息
|
// 发送消息
|
||||||
await _sendMessage(message: message);
|
await _sendMessage(message: message);
|
||||||
_log(
|
// _log(
|
||||||
text:
|
// text:
|
||||||
'发送回声测试分包消息=====SpTotal:$totalPackets,SpIndex:${i + 1},packet:${packet.length}');
|
// '发送回声测试分包消息=====messageId:$messageId,SpTotal:$totalPackets,SpIndex:${i + 1},packet:${packet.length}');
|
||||||
}
|
}
|
||||||
// 分包发送完了递增一下id
|
// 分包发送完了递增一下id
|
||||||
MessageCommand.getNextMessageId(toPeerId);
|
MessageCommand.getNextMessageId(toPeerId);
|
||||||
@ -760,7 +761,7 @@ class StartChartManage {
|
|||||||
_handleUdpResultData(deserialize);
|
_handleUdpResultData(deserialize);
|
||||||
}
|
}
|
||||||
if (deserialize.PayloadType != PayloadTypeConstant.heartbeat) {
|
if (deserialize.PayloadType != PayloadTypeConstant.heartbeat) {
|
||||||
_log(text: 'Udp收到结构体数据---》$deserialize');
|
// _log(text: 'Udp收到结构体数据---》$deserialize');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user