2024-12-02 15:43:59 +08:00
|
|
|
|
import 'package:crc32_checksum/crc32_checksum.dart';
|
2024-11-30 15:39:06 +08:00
|
|
|
|
import 'package:star_lock/talk/startChart/constant/message_type_constant.dart';
|
2024-11-28 14:57:49 +08:00
|
|
|
|
import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/startChart/constant/protocol_flag_constant.dart';
|
|
|
|
|
|
import 'package:star_lock/talk/startChart/entity/scp_message.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class MessageCommand {
|
2024-12-02 15:43:59 +08:00
|
|
|
|
|
2024-11-28 14:57:49 +08:00
|
|
|
|
/// 客户端去中继上线命令
|
2024-12-02 15:43:59 +08:00
|
|
|
|
static List<int> goOnlineRelay({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
}) {
|
2024-11-28 14:57:49 +08:00
|
|
|
|
String serializedBytesString = ScpMessage(
|
|
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
MessageId: 1,
|
|
|
|
|
|
SpTotal: 0,
|
|
|
|
|
|
SpIndex: 0,
|
2024-12-02 15:43:59 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
Payload: 'hello',
|
|
|
|
|
|
PayloadCRC: 55230,
|
|
|
|
|
|
PayloadLength: 5,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.goOnline,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
).serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 回声测试消息
|
|
|
|
|
|
static List<int> echoMessage({
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
}) {
|
|
|
|
|
|
ScpMessage message = ScpMessage(
|
2024-11-30 15:39:06 +08:00
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
MessageId: 1,
|
|
|
|
|
|
SpTotal: 0,
|
|
|
|
|
|
SpIndex: 0,
|
|
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
|
|
|
|
|
Payload: 'hello',
|
|
|
|
|
|
PayloadCRC: 55230,
|
|
|
|
|
|
PayloadLength: 5,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
PayloadType: PayloadTypeConstant.echoTest,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 心跳消息
|
2024-12-02 15:43:59 +08:00
|
|
|
|
static List<int> heartbeatMessage({
|
|
|
|
|
|
required String FromPeerId,
|
|
|
|
|
|
required String ToPeerId,
|
|
|
|
|
|
}) {
|
2024-11-28 14:57:49 +08:00
|
|
|
|
ScpMessage message = ScpMessage(
|
2024-11-30 15:39:06 +08:00
|
|
|
|
ProtocolFlag: ProtocolFlagConstant.scp01,
|
|
|
|
|
|
MessageType: MessageTypeConstant.Req,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
MessageId: 1,
|
|
|
|
|
|
SpTotal: 0,
|
|
|
|
|
|
SpIndex: 0,
|
2024-12-02 15:43:59 +08:00
|
|
|
|
FromPeerId: FromPeerId,
|
|
|
|
|
|
ToPeerId: ToPeerId,
|
2024-11-30 15:39:06 +08:00
|
|
|
|
Payload: 'hello',
|
|
|
|
|
|
PayloadCRC: 55230,
|
|
|
|
|
|
PayloadLength: 5,
|
|
|
|
|
|
PayloadType: PayloadTypeConstant.heartbeat,
|
2024-11-28 14:57:49 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
String serializedBytesString = message.serialize();
|
|
|
|
|
|
return _hexToBytes(serializedBytesString);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 辅助方法:将16进制字符串转换为字节列表
|
|
|
|
|
|
static List<int> _hexToBytes(String hex) {
|
|
|
|
|
|
final bytes = <int>[];
|
|
|
|
|
|
for (int i = 0; i < hex.length; i += 2) {
|
|
|
|
|
|
bytes.add(int.parse(hex.substring(i, i + 2), radix: 16));
|
|
|
|
|
|
}
|
|
|
|
|
|
return bytes;
|
|
|
|
|
|
}
|
2024-12-02 15:43:59 +08:00
|
|
|
|
|
|
|
|
|
|
static int calculationCrc(payload){
|
|
|
|
|
|
var checkSumResult = Crc32.calculate(payload);
|
|
|
|
|
|
return checkSumResult;
|
|
|
|
|
|
}
|
2024-11-28 14:57:49 +08:00
|
|
|
|
}
|