55 lines
1.4 KiB
Dart
55 lines
1.4 KiB
Dart
class HeartbeatResponse {
|
|
int? statusCode;
|
|
int? nextPingTime;
|
|
String? clientAddr;
|
|
|
|
HeartbeatResponse({
|
|
this.statusCode,
|
|
this.nextPingTime,
|
|
this.clientAddr,
|
|
});
|
|
|
|
factory HeartbeatResponse.fromBytes(List<int> bytes) {
|
|
final message = HeartbeatResponse();
|
|
int offset = 0;
|
|
|
|
// 检查是否有足够的数据来解析状态码
|
|
if (bytes.length < 1) {
|
|
throw FormatException("Insufficient data for HeartbeatResponse");
|
|
}
|
|
|
|
// 解析状态码 (1 byte)
|
|
message.statusCode = bytes[offset];
|
|
offset += 1;
|
|
|
|
// 检查是否有足够的数据来解析 nextPingTime
|
|
if (bytes.length < offset + 2) {
|
|
throw FormatException("Insufficient data for nextPingTime");
|
|
}
|
|
|
|
// 解析 nextPingTime (2 bytes, little-endian)
|
|
message.nextPingTime = (bytes[offset + 1] << 8) | bytes[offset];
|
|
offset += 2;
|
|
|
|
// 解析 clientAddr (null-terminated string)
|
|
final clientAddrBytes = <int>[];
|
|
while (offset < bytes.length && bytes[offset] != 0) {
|
|
clientAddrBytes.add(bytes[offset]);
|
|
offset++;
|
|
}
|
|
|
|
// 跳过 null terminator
|
|
if (offset < bytes.length && bytes[offset] == 0) {
|
|
offset++;
|
|
}
|
|
|
|
message.clientAddr = String.fromCharCodes(clientAddrBytes);
|
|
|
|
return message;
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'HeartbeatResponse{statusCode: $statusCode, nextPingTime: $nextPingTime, clientAddr: $clientAddr}';
|
|
}
|
|
} |