diff --git a/src/account.h b/src/account.h index 5baf2bb48e4e748c22ab540f16bbe7431ac7c95e..3200da86d1e48fd37c199643b8be8ec1347c7852 100644 --- a/src/account.h +++ b/src/account.h @@ -166,7 +166,7 @@ public: * Unregister the account. * This should update the getRegistrationState() return value. */ - virtual void doUnregister(std::function<void(bool)> cb = std::function<void(bool)>()) = 0; + virtual void doUnregister(bool forceShutdownConnections = false) = 0; RegistrationState getRegistrationState() const { return registrationState_; } diff --git a/src/client/configurationmanager.cpp b/src/client/configurationmanager.cpp index 245faab6fc5115fc472b694073e5ac62c5cdfc4f..7110b07af3590915a6f0f619d79474c56f0b217d 100644 --- a/src/client/configurationmanager.cpp +++ b/src/client/configurationmanager.cpp @@ -968,13 +968,12 @@ setCredentials(const std::string& accountId, const std::vector<std::map<std::string, std::string>>& details) { if (auto sipaccount = jami::Manager::instance().getAccount<SIPAccount>(accountId)) { - sipaccount->doUnregister([&](bool /* transport_free */) { - sipaccount->editConfig( - [&](jami::SipAccountConfig& config) { config.setCredentials(details); }); - sipaccount->loadConfig(); - if (sipaccount->isEnabled()) - sipaccount->doRegister(); - }); + sipaccount->doUnregister(); + sipaccount->editConfig( + [&](jami::SipAccountConfig& config) { config.setCredentials(details); }); + sipaccount->loadConfig(); + if (sipaccount->isEnabled()) + sipaccount->doRegister(); jami::Manager::instance().saveConfig(sipaccount); } } diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index a36ad6494bc3b14a2bfa24538aecaa0fbe26e8c0..3c30fa5613d640b46119f5ffcc8cbecfecf516a3 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -2346,13 +2346,10 @@ JamiAccount::loadConversation(const std::string& convId) } void -JamiAccount::doUnregister(std::function<void(bool)> released_cb) +JamiAccount::doUnregister(bool forceShutdownConnections) { std::unique_lock<std::recursive_mutex> lock(configurationMutex_); if (registrationState_ >= RegistrationState::ERROR_GENERIC) { - lock.unlock(); - if (released_cb) - released_cb(false); return; } @@ -2381,9 +2378,9 @@ JamiAccount::doUnregister(std::function<void(bool)> released_cb) } // Stop all current P2P connections if account is disabled - // Else, we let the system managing if the co is down or not - // NOTE: this is used for changing account's config. - if (not isEnabled()) + // or if explicitly requested by the caller. + // NOTE: Leaving the connections open is useful when changing an account's config. + if (not isEnabled() || forceShutdownConnections) shutdownConnections(); // Release current UPnP mapping if any. @@ -2400,8 +2397,6 @@ JamiAccount::doUnregister(std::function<void(bool)> released_cb) lock.unlock(); - if (released_cb) - released_cb(false); #ifdef ENABLE_PLUGIN jami::Manager::instance().getJamiPluginManager().getChatServicesManager().cleanChatSubjects( getAccountID()); diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h index ceda7125c75a13adf1b08b8eccc4bb1030cbafae..c6c00ef61bad125d7c0e881c96c5ea9ee05c86ed 100644 --- a/src/jamidht/jamiaccount.h +++ b/src/jamidht/jamiaccount.h @@ -177,7 +177,7 @@ public: /** * Disconnect from the DHT. */ - void doUnregister(std::function<void(bool)> cb = {}) override; + void doUnregister(bool forceShutdownConnections = false) override; /** * Set the registration state of the specified link diff --git a/src/manager.cpp b/src/manager.cpp index 48b3ed9ea6b97268853bc19e559ac59ee0f00235..2f2e4bdc71f9f94b2610fdaeffa45ae01eab73d7 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -1070,12 +1070,7 @@ Manager::unregisterAccounts() { for (const auto& account : getAllAccounts()) { if (account->isEnabled()) { - if (auto acc = std::dynamic_pointer_cast<JamiAccount>(account)) { - // Note: shutdown the connections as doUnregister will not do it (because the - // account is enabled) - acc->shutdownConnections(); - } - account->doUnregister(); + account->doUnregister(true); } } } @@ -2751,17 +2746,17 @@ Manager::setAccountDetails(const std::string& accountID, return; // Unregister before modifying any account information - account->doUnregister([&](bool /* transport_free */) { - account->setAccountDetails(details); + account->doUnregister(); - if (account->isUsable()) - account->doRegister(); - else - account->doUnregister(); + account->setAccountDetails(details); - // Update account details to the client side - emitSignal<libjami::ConfigurationSignal::AccountDetailsChanged>(accountID, details); - }); + if (account->isUsable()) + account->doRegister(); + else + account->doUnregister(); + + // Update account details to the client side + emitSignal<libjami::ConfigurationSignal::AccountDetailsChanged>(accountID, details); } std::mt19937_64 @@ -2821,13 +2816,10 @@ Manager::removeAccount(const std::string& accountID, bool flush) { // Get it down and dying if (const auto& remAccount = getAccount(accountID)) { - // Force stopping connection before doUnregister as it will - // wait for dht threads to finish if (auto acc = std::dynamic_pointer_cast<JamiAccount>(remAccount)) { acc->hangupCalls(); - acc->shutdownConnections(); } - remAccount->doUnregister(); + remAccount->doUnregister(true); if (flush) remAccount->flush(); accountFactory.removeAccount(*remAccount); @@ -3025,12 +3017,7 @@ Manager::setAccountActive(const std::string& accountID, bool active, bool shutdo if (active) { acc->doRegister(); } else { - acc->doUnregister(); - if (shutdownConnections) { - if (auto jamiAcc = std::dynamic_pointer_cast<JamiAccount>(acc)) { - jamiAcc->shutdownConnections(); - } - } + acc->doUnregister(shutdownConnections); } } emitSignal<libjami::ConfigurationSignal::VolatileDetailsChanged>( diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp index e0a8b0a57d9ceb5d1ac7b1e1df4d7df20fa15274..13cf63204e0a464d55e76eaa8676a6c7893d39d5 100644 --- a/src/sip/sipaccount.cpp +++ b/src/sip/sipaccount.cpp @@ -583,8 +583,10 @@ SIPAccount::setPushNotificationToken(const std::string& pushDeviceToken) { JAMI_WARNING("[SIP Account {}] setPushNotificationToken: {}", getAccountID(), pushDeviceToken); if (SIPAccountBase::setPushNotificationToken(pushDeviceToken)) { - if (config().enabled) - doUnregister([&](bool /* transport_free */) { doRegister(); }); + if (config().enabled) { + doUnregister(); + doRegister(); + } return true; } return false; @@ -594,8 +596,10 @@ bool SIPAccount::setPushNotificationConfig(const std::map<std::string, std::string>& data) { if (SIPAccountBase::setPushNotificationConfig(data)) { - if (config().enabled) - doUnregister([&](bool /* transport_free */) { doRegister(); }); + if (config().enabled) { + doUnregister(); + doRegister(); + } return true; } return false; @@ -607,8 +611,10 @@ SIPAccount::pushNotificationReceived(const std::string& from, { JAMI_WARNING("[SIP Account {:s}] pushNotificationReceived: {:s}", getAccountID(), from); - if (config().enabled) - doUnregister([&](bool /* transport_free */) { doRegister(); }); + if (config().enabled) { + doUnregister(); + doRegister(); + } } void @@ -744,7 +750,7 @@ SIPAccount::doRegister2_() } void -SIPAccount::doUnregister(std::function<void(bool)> released_cb) +SIPAccount::doUnregister(bool /* forceShutdownConnections */) { std::unique_lock<std::recursive_mutex> lock(configurationMutex_); @@ -761,10 +767,6 @@ SIPAccount::doUnregister(std::function<void(bool)> released_cb) if (transport_) setTransport(); resetAutoRegistration(); - - lock.unlock(); - if (released_cb) - released_cb(not isIP2IP()); } void @@ -775,10 +777,9 @@ SIPAccount::connectivityChanged() return; } - doUnregister([acc = shared()](bool /* transport_free */) { - if (acc->isUsable()) - acc->doRegister(); - }); + doUnregister(); + if (isUsable()) + doRegister(); } void diff --git a/src/sip/sipaccount.h b/src/sip/sipaccount.h index 237e65c19a8a905248503ecefa3c745b825c0d74..5caabcc818c0462b0c9f8b082fcd68f167d3f199 100644 --- a/src/sip/sipaccount.h +++ b/src/sip/sipaccount.h @@ -140,7 +140,7 @@ public: /** * Send unregistration. */ - void doUnregister(std::function<void(bool)> cb = std::function<void(bool)>()) override; + void doUnregister(bool forceShutdownConnections = false) override; /** * Build and send SIP registration request