144 lines
3.6 KiB
Dart
144 lines
3.6 KiB
Dart
import 'dart:convert';
|
||
import 'package:crc32_checksum/crc32_checksum.dart';
|
||
import 'package:crypto/crypto.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
|
||
class ScpMessage {
|
||
ScpMessage({
|
||
this.ProtocolFlag,
|
||
this.MessageType,
|
||
this.MessageId,
|
||
this.SpTotal,
|
||
this.SpIndex,
|
||
this.FromPeerId,
|
||
this.ToPeerId,
|
||
this.PayloadType,
|
||
this.PayloadCRC,
|
||
this.PayloadLength,
|
||
this.Payload,
|
||
});
|
||
|
||
String? ProtocolFlag;
|
||
int? MessageType;
|
||
int? MessageId;
|
||
int? SpTotal;
|
||
int? SpIndex;
|
||
String? FromPeerId;
|
||
String? ToPeerId;
|
||
int? PayloadType;
|
||
int? PayloadCRC;
|
||
int? PayloadLength;
|
||
String? Payload;
|
||
|
||
ScpMessage.fromJson(dynamic json) {
|
||
ProtocolFlag = json['ProtocolFlag'];
|
||
MessageType = json['MessageType'];
|
||
MessageId = json['MessageId'];
|
||
SpTotal = json['SpTotal'];
|
||
SpIndex = json['SpIndex'];
|
||
FromPeerId = json['FromPeerId'];
|
||
ToPeerId = json['ToPeerId'];
|
||
PayloadType = json['PayloadType'];
|
||
PayloadCRC = json['PayloadCRC'];
|
||
PayloadLength = json['PayloadLength'];
|
||
Payload = json['Payload'];
|
||
}
|
||
|
||
Map<String, dynamic> toJson() {
|
||
return {
|
||
'ProtocolFlag': ProtocolFlag,
|
||
'MessageType': MessageType,
|
||
'MessageId': MessageId,
|
||
'SpTotal': SpTotal,
|
||
'SpIndex': SpIndex,
|
||
'FromPeerId': FromPeerId,
|
||
'ToPeerId': ToPeerId,
|
||
'PayloadType': PayloadType,
|
||
'PayloadCRC': PayloadCRC,
|
||
'PayloadLength': PayloadLength,
|
||
'Payload': Payload,
|
||
};
|
||
}
|
||
|
||
String serialize() {
|
||
final bytes = <int>[];
|
||
|
||
// ProtocolFlag (4 bytes)
|
||
if (ProtocolFlag != null) {
|
||
bytes.addAll(utf8.encode(ProtocolFlag!));
|
||
}
|
||
|
||
// MessageType (1 byte)
|
||
if (MessageType != null) {
|
||
bytes.add(MessageType!);
|
||
}
|
||
|
||
// MessageId (2 bytes)
|
||
if (MessageId != null) {
|
||
final highByteMessageId = (MessageId! >> 8) & 0xFF;
|
||
final lowByteMessageId = MessageId! & 0xFF;
|
||
bytes.add(lowByteMessageId); // 交换位置
|
||
bytes.add(highByteMessageId); // 交换位置
|
||
}
|
||
|
||
// SpTotal (1 byte)
|
||
if (SpTotal != null) {
|
||
bytes.add(SpTotal!);
|
||
}
|
||
|
||
// SpIndex (1 byte)
|
||
if (SpIndex != null) {
|
||
bytes.add(SpIndex!);
|
||
}
|
||
|
||
// FromPeerId (字符串,记录长度)
|
||
if (FromPeerId != null) {
|
||
bytes.addAll(utf8.encode(FromPeerId!));
|
||
}
|
||
|
||
// ToPeerId (字符串,假设长度固定为32字节)
|
||
if (ToPeerId != null) {
|
||
bytes.addAll(utf8.encode(ToPeerId!));
|
||
}
|
||
|
||
// PayloadType (2 bytes)
|
||
if (PayloadType != null) {
|
||
final highBytePayloadType = (PayloadType! >> 8) & 0xFF;
|
||
final lowBytePayloadType = PayloadType! & 0xFF;
|
||
bytes.add(lowBytePayloadType); // 交换位置
|
||
bytes.add(highBytePayloadType); // 交换位置
|
||
}
|
||
|
||
// 计算 PayloadCRC (2 bytes)
|
||
if (PayloadCRC != null) {
|
||
final highBytePayloadCRC = (PayloadCRC! >> 8) & 0xFF;
|
||
final lowBytePayloadCRC = PayloadCRC! & 0xFF;
|
||
bytes.add(lowBytePayloadCRC); // 交换位置
|
||
bytes.add(highBytePayloadCRC); // 交换位置
|
||
}
|
||
|
||
// PayloadLength (4 bytes)
|
||
if (PayloadLength != null) {
|
||
bytes.add(PayloadLength! & 0xFF);
|
||
bytes.add((PayloadLength! >> 8) & 0xFF);
|
||
bytes.add((PayloadLength! >> 16) & 0xFF);
|
||
bytes.add((PayloadLength! >> 24) & 0xFF);
|
||
}
|
||
|
||
// Payload (字符串,转换为字节)
|
||
if (Payload != null) {
|
||
bytes.addAll(utf8.encode(Payload!));
|
||
}
|
||
|
||
// 转16进制字符串
|
||
final bytesToHexString = bytesToHex(bytes);
|
||
|
||
|
||
return bytesToHexString;
|
||
}
|
||
|
||
static String bytesToHex(List<int> bytes) {
|
||
return bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join('');
|
||
}
|
||
}
|