app-starlock/lib/talk/startChart/command/message_command.dart

90 lines
2.5 KiB
Dart
Raw Normal View History

import 'package:crc32_checksum/crc32_checksum.dart';
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-11-28 14:57:49 +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,
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,
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(
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,
PayloadType: PayloadTypeConstant.echoTest,
2024-11-28 14:57:49 +08:00
);
String serializedBytesString = message.serialize();
return _hexToBytes(serializedBytesString);
}
// 心跳消息
static List<int> heartbeatMessage({
required String FromPeerId,
required String ToPeerId,
}) {
2024-11-28 14:57:49 +08:00
ScpMessage message = ScpMessage(
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,
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;
}
static int calculationCrc(payload){
var checkSumResult = Crc32.calculate(payload);
return checkSumResult;
}
2024-11-28 14:57:49 +08:00
}