app-starlock/lib/talk/starChart/entity/login_response.dart

60 lines
1.4 KiB
Dart

import 'dart:convert';
class LoginResponse {
int? responseType;
int? rejectType;
int? nextPingTime;
String? clientAddr;
LoginResponse({
this.responseType,
this.rejectType,
this.nextPingTime,
this.clientAddr,
});
factory LoginResponse.fromBytes(List<int> bytes) {
final message = LoginResponse();
int offset = 0;
// responseType (1 byte)
if (bytes.length - offset >= 1) {
message.responseType = bytes[offset];
offset += 1;
} else {
throw FormatException("Invalid responseType length");
}
// rejectType (1 byte)
if (bytes.length - offset >= 1) {
message.rejectType = bytes[offset];
offset += 1;
} else {
throw FormatException("Invalid rejectType length");
}
// nextPingTime (2 bytes, little-endian)
if (bytes.length - offset >= 2) {
message.nextPingTime = (bytes[offset + 1] << 8) | bytes[offset];
offset += 2;
} else {
throw FormatException("Invalid nextPingTime length");
}
// ClientAddr (remaining bytes)
if (bytes.length > offset) {
message.clientAddr =
utf8.decode(bytes.sublist(offset).takeWhile((byte) => byte != 0).toList());
} else {
throw FormatException("Invalid ClientAddr length");
}
return message;
}
@override
String toString() {
return 'LoginResponse{responseType: $responseType, rejectType: $rejectType, nextPingTime: $nextPingTime, clientAddr: $clientAddr}';
}
}