Update ios_controller.mm

This commit is contained in:
AnhTVc 2024-12-07 13:06:33 +07:00 committed by GitHub
parent 0822bf8a45
commit b899b2a68f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -885,9 +885,9 @@ void IosController::requestInetAccess() {
void IosController::stopForHandshake() { void IosController::stopForHandshake() {
if (m_handshakeTimer) { if (m_handshakeTimer) {
// Stop the timer if it's active if (m_handshakeTimer->isActive()) {
m_handshakeTimer->stop(); m_handshakeTimer->stop();
delete m_handshakeTimer; }
m_handshakeTimer = nullptr; m_handshakeTimer = nullptr;
qDebug() << "Handshake monitoring stopped."; qDebug() << "Handshake monitoring stopped.";
@ -899,39 +899,57 @@ void IosController::stopForHandshake() {
void IosController::waitForHandshake() { void IosController::waitForHandshake() {
qDebug() << "Waiting for last_handshake_time_sec to be greater than 0..."; qDebug() << "Waiting for last_handshake_time_sec to be greater than 0...";
// Lambda to handle the response // Initialize the timer if it's null
auto checkHandshake = [this](NSDictionary *response) { if (!m_handshakeTimer) {
uint64_t last_handshake_time_sec = 0; m_handshakeTimer = new QTimer(this);
if (response && response[@"last_handshake_time_sec"] && ![response[@"last_handshake_time_sec"] isKindOfClass:[NSNull class]]) {
last_handshake_time_sec = [response[@"last_handshake_time_sec"] unsignedLongLongValue];
}
qDebug() << "last_handshake_time_sec:" << last_handshake_time_sec; // Connect the timer's timeout signal to perform handshake checking
connect(m_handshakeTimer, &QTimer::timeout, this, [this]() {
// Prepare the message to check status
NSString *actionKey = [NSString stringWithUTF8String:MessageKey::action];
NSString *actionValue = [NSString stringWithUTF8String:Action::getStatus];
NSString *tunnelIdKey = [NSString stringWithUTF8String:MessageKey::tunnelId];
NSString *tunnelIdValue = !m_tunnelId.isEmpty() ? m_tunnelId.toNSString() : @"";
if (last_handshake_time_sec > 0) { NSDictionary *message = @{actionKey: actionValue, tunnelIdKey: tunnelIdValue};
// Handshake successful, update state
qDebug() << "Handshake detected, updating state to CONNECTED.";
emit connectionStateChanged(Vpn::ConnectionState::Connected);
} else {
if (last_handshake_time_sec == 0) {
// Retry after a delay
emit connectionStateChanged(Vpn::ConnectionState::Connecting);
QTimer::singleShot(1000, this, [this]() { waitForHandshake(); });
}else{
emit connectionStateChanged(Vpn::ConnectionState::Disconnected);
stopForHandshake(); // Stop monitoring on error
}
}
};
// Prepare the message to check status // Lambda to handle the response
NSString *actionKey = [NSString stringWithUTF8String:MessageKey::action]; auto checkHandshake = [this](NSDictionary *response) {
NSString *actionValue = [NSString stringWithUTF8String:Action::getStatus]; uint64_t last_handshake_time_sec = 0;
NSString *tunnelIdKey = [NSString stringWithUTF8String:MessageKey::tunnelId]; if (response && response[@"last_handshake_time_sec"] &&
NSString *tunnelIdValue = !m_tunnelId.isEmpty() ? m_tunnelId.toNSString() : @""; ![response[@"last_handshake_time_sec"] isKindOfClass:[NSNull class]]) {
last_handshake_time_sec = [response[@"last_handshake_time_sec"] unsignedLongLongValue];
}
NSDictionary *message = @{actionKey: actionValue, tunnelIdKey: tunnelIdValue}; qDebug() << "last_handshake_time_sec:" << last_handshake_time_sec;
// Send the message to the VPN extension if (last_handshake_time_sec > 0) {
sendVpnExtensionMessage(message, checkHandshake); // Handshake successful, update state
qDebug() << "Handshake detected, updating state to CONNECTED.";
emit connectionStateChanged(Vpn::ConnectionState::Connected);
} else {
if (last_handshake_time_sec == 0) {
// Keep retrying
emit connectionStateChanged(Vpn::ConnectionState::Connecting);
} else {
// Handle handshake failure and stop monitoring
emit connectionStateChanged(Vpn::ConnectionState::Disconnected);
stopForHandshake();
return;
}
}
};
// Send the message to the VPN extension
sendVpnExtensionMessage(message, checkHandshake);
});
qDebug() << "Handshake timer initialized.";
}
// Start the timer only if it's not already active
if (m_handshakeTimer && !m_handshakeTimer->isActive()) {
m_handshakeTimer->start(1000); // Retry every 1 second
qDebug() << "Handshake timer Retry every 1 second";
}
} }