From b2d2c75b54a6739f69dfe8d6dfabb4dda580fa5b Mon Sep 17 00:00:00 2001 From: liyi Date: Thu, 8 May 2025 13:43:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=A2=9E=E5=8A=A0peerId=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E8=A1=A5=E9=BD=90=E9=80=BB=E8=BE=91=E4=BD=86=E4=B8=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/talk/starChart/entity/scp_message.dart | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/talk/starChart/entity/scp_message.dart b/lib/talk/starChart/entity/scp_message.dart index fc8a3141..ca5fcef3 100644 --- a/lib/talk/starChart/entity/scp_message.dart +++ b/lib/talk/starChart/entity/scp_message.dart @@ -68,6 +68,18 @@ class ScpMessage { return 'ScpMessage{ProtocolFlag: $ProtocolFlag, MessageType: $MessageType, MessageId: $MessageId, SpTotal: $SpTotal, SpIndex: $SpIndex, FromPeerId: $FromPeerId, ToPeerId: $ToPeerId, PayloadType: $PayloadType, PayloadCRC: $PayloadCRC, PayloadLength: $PayloadLength, Payload: $Payload}'; } + // 辅助函数:定长字符串编码 + List encodeFixedLengthString(String? str, int length) { + final bytes = utf8.encode(str ?? ''); + if (bytes.length > length) { + return bytes.sublist(0, length); + } else if (bytes.length < length) { + return bytes + List.filled(length - bytes.length, 0); + } else { + return bytes; + } + } + String serialize() { final bytes = []; @@ -98,16 +110,19 @@ class ScpMessage { if (SpIndex != null) { bytes.add(SpIndex!); } - - // FromPeerId (字符串,记录长度) + // FromPeerId (字符串,长度固定为44字节) if (FromPeerId != null) { bytes.addAll(utf8.encode(FromPeerId!)); } + // FromPeerId (44字节定长) + // bytes.addAll(encodeFixedLengthString(FromPeerId, 44)); - // ToPeerId (字符串,假设长度固定为32字节) + // ToPeerId (字符串,长度固定为44字节) if (ToPeerId != null) { bytes.addAll(utf8.encode(ToPeerId!)); } + // ToPeerId (44字节定长) + // bytes.addAll(encodeFixedLengthString(ToPeerId, 44)); // PayloadType (2 bytes) if (PayloadType != null) {