diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index f4df6deecc68bd90d9e58361b9bff9fd9c29ab2b..3652765b91de3ea454cd85063cfa7b8c8dd7f8ac 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -230,13 +230,21 @@ std::shared_ptr SoftBusAdapter::GetConnect(const PipeInfo &pipeIn std::pair SoftBusAdapter::OpenConnect(const std::shared_ptr &conn, const DeviceId &deviceId) { + auto networkId = DmAdapter::GetInstance().ToNetworkID(deviceId.deviceId); + if (conn != nullptr) { + auto oldNetworkId = conn->GetNetworkId(); + if (networkId != oldNetworkId) { + ZLOGI("NetworkId changed, %{public}s->%{public}s", KvStoreUtils::ToBeAnonymous(oldNetworkId).c_str(), + KvStoreUtils::ToBeAnonymous(networkId).c_str()); + conn->UpdateNetworkId(networkId); + } + } auto task = [this, connect = std::weak_ptr(conn)]() { auto conn = connect.lock(); if (conn != nullptr) { conn->OpenConnect(&clientListener_); } }; - auto networkId = DmAdapter::GetInstance().GetDeviceInfo(deviceId.deviceId).networkId; ConnectManager::GetInstance()->ApplyConnect(networkId, task); return std::make_pair(Status::RATE_LIMIT, 0); } diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp index 23c8c79167f5735c46c620a3d592cda106bede2b..f5cc66391ec6dab22d1faf86f36277870670f758 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp @@ -125,7 +125,8 @@ int32_t SoftBusClient::CreateSocket() const SocketInfo socketInfo; std::string peerName = pipe_.pipeId; socketInfo.peerName = const_cast(peerName.c_str()); - socketInfo.peerNetworkId = const_cast(networkId_.c_str()); + auto networkId = GetNetworkId(); + socketInfo.peerNetworkId = const_cast(networkId.c_str()); std::string clientName = pipe_.pipeId; socketInfo.name = const_cast(clientName.c_str()); std::string pkgName = "ohos.distributeddata"; @@ -155,8 +156,10 @@ Status SoftBusClient::CheckStatus() int32_t SoftBusClient::Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async) { int32_t status = ::Bind(socket, QOS_INFOS[type % QOS_BUTT], QOS_COUNTS[type % QOS_BUTT], listener); - ZLOGI("Bind %{public}s,session:%{public}s,socketId:%{public}d", - KvStoreUtils::ToBeAnonymous(device_.deviceId).c_str(), pipe_.pipeId.c_str(), socket); + auto networkId = GetNetworkId(); + ZLOGI("Bind device:%{public}s,session:%{public}s,socketId:%{public}d,networkId:%{public}s", + KvStoreUtils::ToBeAnonymous(device_.deviceId).c_str(), pipe_.pipeId.c_str(), socket, + KvStoreUtils::ToBeAnonymous(networkId).c_str()); if (status != 0) { ZLOGE("[Bind] device:%{public}s socket failed, session:%{public}s,result:%{public}d", @@ -176,7 +179,7 @@ int32_t SoftBusClient::Open(int32_t socket, uint32_t type, const ISocketListener UpdateBindInfo(socket, mtu, status, async); ZLOGI("open %{public}s, session:%{public}s success, socket:%{public}d", KvStoreUtils::ToBeAnonymous(device_.deviceId).c_str(), pipe_.pipeId.c_str(), socket_); - ConnectManager::GetInstance()->OnSessionOpen(networkId_); + ConnectManager::GetInstance()->OnSessionOpen(networkId); return status; } @@ -256,8 +259,15 @@ Status SoftBusClient::ReuseConnect(const ISocketListener *listener) return status == SOFTBUS_OK ? Status::SUCCESS : Status::NETWORK_ERROR; } -const std::string& SoftBusClient::GetNetworkId() const +std::string SoftBusClient::GetNetworkId() const { + std::lock_guard lock(networkIdMutex_); return networkId_; } + +void SoftBusClient::UpdateNetworkId(const std::string& networkId) +{ + std::lock_guard lock(networkIdMutex_); + networkId_ = networkId; +} } // namespace OHOS::AppDistributedKv \ No newline at end of file diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_client.h b/services/distributeddataservice/adapter/communicator/src/softbus_client.h index a0196c94f3203f1475bb84c3c5c111e4dbc6ee31..d224e6b47f1c10c698365a22ce0595d48ffa26b5 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.h +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.h @@ -52,7 +52,8 @@ public: void UpdateExpireTime(bool async = true); int32_t GetSoftBusError(); Status ReuseConnect(const ISocketListener *listener); - const std::string& GetNetworkId() const; + std::string GetNetworkId() const; + void UpdateNetworkId(const std::string &networkId); private: int32_t Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async = true); @@ -88,6 +89,7 @@ private: static constexpr uint32_t QOS_COUNTS[QOS_BUTT] = { BR_QOS_COUNT, HML_QOS_COUNT, REUSE_QOS_COUNT }; std::atomic_bool isOpening_ = false; mutable std::mutex mutex_; + mutable std::mutex networkIdMutex_; uint32_t type_ = QOS_HML; PipeInfo pipe_; DeviceId device_; @@ -97,7 +99,7 @@ private: int32_t socket_ = INVALID_SOCKET_ID; int32_t bindState_ = -1; int32_t softBusError_ = 0; - const std::string networkId_; + std::string networkId_; }; } // namespace OHOS::AppDistributedKv diff --git a/services/distributeddataservice/adapter/communicator/test/unittest/softbus_adapter_standard_test.cpp b/services/distributeddataservice/adapter/communicator/test/unittest/softbus_adapter_standard_test.cpp index b4b4e92b347d0fce0a8f3b042970b066882d2896..b6a43a8c8ffe94c7f46ece687d59d1958fa1a0ef 100644 --- a/services/distributeddataservice/adapter/communicator/test/unittest/softbus_adapter_standard_test.cpp +++ b/services/distributeddataservice/adapter/communicator/test/unittest/softbus_adapter_standard_test.cpp @@ -353,6 +353,25 @@ HWTEST_F(SoftbusAdapterStandardTest, OpenConnect, TestSize.Level1) EXPECT_EQ(status.second, 0); } +/** +* @tc.name: OpenConnect002 +* @tc.desc: open connect with networkId changed. +* @tc.type: FUNC +*/ +HWTEST_F(SoftbusAdapterStandardTest, OpenConnect002, TestSize.Level1) +{ + PipeInfo pipeInfo; + pipeInfo.pipeId = "appId"; + pipeInfo.userId = "groupId"; + DeviceId device = {"DeviceId"}; + std::shared_ptr conn = std::make_shared( + pipeInfo, device, "old", SoftBusClient::QOS_HML); + SoftBusAdapter::GetInstance()->OpenConnect(conn, device); + EXPECT_NE(conn->GetNetworkId(), "old"); + SoftBusAdapter::GetInstance()->OpenConnect(conn, device); + EXPECT_EQ(conn->GetNetworkId(), ""); +} + /** * @tc.name: CloseSession * @tc.desc: close session diff --git a/services/distributeddataservice/adapter/communicator/test/unittest/softbus_client_test.cpp b/services/distributeddataservice/adapter/communicator/test/unittest/softbus_client_test.cpp index 3f1d713deb0fc7b2b05b72ae2cde2ac687dd6375..b8ffd09945a6f938cf205bd3f8a4ea103f4429be 100644 --- a/services/distributeddataservice/adapter/communicator/test/unittest/softbus_client_test.cpp +++ b/services/distributeddataservice/adapter/communicator/test/unittest/softbus_client_test.cpp @@ -186,4 +186,17 @@ HWTEST_F(SoftbusClientTest, ReuseConnect, TestSize.Level0) status = client->ReuseConnect(listener); EXPECT_EQ(status, Status::NETWORK_ERROR); } + +/** +* @tc.name: UpdateNetworkId +* @tc.desc: UpdateNetworkId test +* @tc.type: FUNC + */ +HWTEST_F(SoftbusClientTest, UpdateNetworkId, TestSize.Level1) +{ + ASSERT_NE(client, nullptr); + const std::string newNetworkId = "newId"; + client->UpdateNetworkId(newNetworkId); + EXPECT_EQ(client->GetNetworkId(), newNetworkId); +} } // namespace OHOS::Test \ No newline at end of file