39 lines
1.1 KiB
Dart
39 lines
1.1 KiB
Dart
|
|
class HeartbeatResponse {
|
|||
|
|
int statusCode; // 状态码,1字节无符号
|
|||
|
|
int nextPingTime; // 下次ping时间秒数,2字节无符号
|
|||
|
|
|
|||
|
|
HeartbeatResponse({required this.statusCode, required this.nextPingTime});
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
String toString() {
|
|||
|
|
return 'HeartbeatResponse{statusCode: $statusCode, nextPingTime: $nextPingTime}';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 反序列化方法
|
|||
|
|
static HeartbeatResponse deserialize(List<int> bytes) {
|
|||
|
|
if (bytes.length < 3) {
|
|||
|
|
throw FormatException("Invalid HeartbeatResponse length");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
final response = HeartbeatResponse(
|
|||
|
|
statusCode: bytes[0], // 状态码,1字节
|
|||
|
|
nextPingTime: (bytes[2] << 8) | bytes[1], // 下次ping时间,2字节小端序
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
return response;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 序列化方法
|
|||
|
|
static List<int> serialize(HeartbeatResponse response) {
|
|||
|
|
final List<int> bytes = [];
|
|||
|
|
|
|||
|
|
// 序列化状态码
|
|||
|
|
bytes.add(response.statusCode);
|
|||
|
|
|
|||
|
|
// 序列化下次ping时间(2字节小端序)
|
|||
|
|
bytes.add(response.nextPingTime & 0xFF);
|
|||
|
|
bytes.add((response.nextPingTime >> 8) & 0xFF);
|
|||
|
|
|
|||
|
|
return bytes;
|
|||
|
|
}
|
|||
|
|
}
|