diff --git a/frameworks/native/netconnclient/src/net_conn_client.cpp b/frameworks/native/netconnclient/src/net_conn_client.cpp index 61608a8bc948937d04cfc15d1c98b93b08f0007e..0bf2f430c0d4f9ebf27f1431c7906c139c3a1b15 100755 --- a/frameworks/native/netconnclient/src/net_conn_client.cpp +++ b/frameworks/native/netconnclient/src/net_conn_client.cpp @@ -1330,11 +1330,13 @@ int32_t NetConnClient::SetNetExtAttribute(const NetHandle &netHandle, const std: int32_t NetConnClient::NetConnCallbackManager::NetAvailable(sptr &netHandle) { + std::unique_lock handlerLock(netHandlerMutex_); if (netHandle == nullptr) { netHandle_ = nullptr; } else { netHandle_ = sptr::MakeSptr(netHandle->GetNetId()); } + handlerLock.unlock(); std::shared_lock lock(netConnCallbackListMutex_); std::list> tmpList(netConnCallbackList_); lock.unlock(); @@ -1347,9 +1349,11 @@ int32_t NetConnClient::NetConnCallbackManager::NetAvailable(sptr &net int32_t NetConnClient::NetConnCallbackManager::NetCapabilitiesChange(sptr &netHandle, const sptr &netAllCap) { + std::unique_lock handlerLock(netHandlerMutex_); if (netHandle_ != nullptr && netHandle->GetNetId() == netHandle_->GetNetId()) { netAllCap_ = netAllCap; } + handlerLock.unlock(); std::shared_lock lock(netConnCallbackListMutex_); std::list> tmpList(netConnCallbackList_); lock.unlock(); @@ -1362,9 +1366,11 @@ int32_t NetConnClient::NetConnCallbackManager::NetCapabilitiesChange(sptr &netHandle, const sptr &info) { + std::unique_lock handlerLock(netHandlerMutex_); if (netHandle_ != nullptr && netHandle->GetNetId() == netHandle_->GetNetId()) { netLinkInfo_ = info; } + handlerLock.unlock(); std::shared_lock lock(netConnCallbackListMutex_); std::list> tmpList(netConnCallbackList_); lock.unlock(); @@ -1376,11 +1382,13 @@ int32_t NetConnClient::NetConnCallbackManager::NetConnectionPropertiesChange(spt int32_t NetConnClient::NetConnCallbackManager::NetLost(sptr &netHandle) { + std::unique_lock handlerLock(netHandlerMutex_); if (netHandle_ != nullptr && netHandle->GetNetId() == netHandle_->GetNetId()) { netHandle_ = nullptr; netAllCap_ = nullptr; netLinkInfo_ = nullptr; } + handlerLock.unlock(); std::shared_lock lock(netConnCallbackListMutex_); std::list> tmpList(netConnCallbackList_); lock.unlock(); @@ -1392,7 +1400,9 @@ int32_t NetConnClient::NetConnCallbackManager::NetLost(sptr &netHandl int32_t NetConnClient::NetConnCallbackManager::NetUnavailable() { + std::unique_lock handlerLock(netHandlerMutex_); netHandle_ = nullptr; + handlerLock.unlock(); std::shared_lock lock(netConnCallbackListMutex_); std::list> tmpList(netConnCallbackList_); lock.unlock(); @@ -1412,6 +1422,22 @@ int32_t NetConnClient::NetConnCallbackManager::NetBlockStatusChange(sptr& callback, + const sptr &netHandle, const sptr &netAllCap, + const sptr &netLinkInfo) +{ + if (netHandle != nullptr) { + sptr tempNetHandler = sptr::MakeSptr(netHandle->GetNetId()); + callback->NetAvailable(tempNetHandler); + if (netAllCap != nullptr) { + callback->NetCapabilitiesChange(tempNetHandler, netAllCap); + } + if (netLinkInfo != nullptr) { + callback->NetConnectionPropertiesChange(tempNetHandler, netLinkInfo); + } + } +} int32_t NetConnClient::NetConnCallbackManager::AddNetConnCallback(const sptr& callback) { @@ -1423,18 +1449,18 @@ int32_t NetConnClient::NetConnCallbackManager::AddNetConnCallback(const sptr handlerLock(netHandlerMutex_); + sptr tempNetHandler(netHandle_); + sptr tempNetAllCap(netAllCap_); + sptr tempNetLinkInfo(netLinkInfo_); + handlerLock.unlock(); #ifndef NETMANAGER_TEST - ffrt::submit([this, callback] () { + ffrt::submit([this, callback, tempNetHandler, tempNetAllCap, tempNetLinkInfo] () { #endif - if (netHandle_ != nullptr) { - callback->NetAvailable(netHandle_); - if (netAllCap_ != nullptr) { - callback->NetCapabilitiesChange(netHandle_, netAllCap_); - } - if (netLinkInfo_ != nullptr) { - callback->NetConnectionPropertiesChange(netHandle_, netLinkInfo_); - } - } + this->PostTriggerNetChange(callback, tempNetHandler, tempNetAllCap, tempNetLinkInfo); #ifndef NETMANAGER_TEST }, {}, {}, ffrt::task_attr().name("AddNetConnCallback")); diff --git a/interfaces/innerkits/netconnclient/include/net_conn_client.h b/interfaces/innerkits/netconnclient/include/net_conn_client.h index 92df6798f32f68d4aa6a88ce4871d852847a7c62..f42b1b81cc19942e18b1d2d478134b51b9ce3e29 100644 --- a/interfaces/innerkits/netconnclient/include/net_conn_client.h +++ b/interfaces/innerkits/netconnclient/include/net_conn_client.h @@ -540,7 +540,11 @@ private: int32_t AddNetConnCallback(const sptr& callback); void RemoveNetConnCallback(const sptr& callback); bool HasExistCallback(const sptr& callback); + void PostTriggerNetChange(const sptr& callback, + const sptr &netHandle, const sptr &netAllCap, + const sptr &netLinkInfo); private: + std::mutex netHandlerMutex_; sptr netHandle_ = nullptr; sptr netAllCap_ = nullptr; sptr netLinkInfo_ = nullptr;