From f117e77a0f4360eb15a4d09eaf4905aa41c07562 Mon Sep 17 00:00:00 2001 From: yanhui Date: Thu, 22 May 2025 15:59:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0softbusclient=E7=9A=84net?= =?UTF-8?q?workId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanhui Change-Id: Iab708e588658157924c7c855bead26d7e6a1fbf4 --- .../src/softbus_adapter_standard.cpp | 10 +++++++++- .../communicator/src/softbus_client.cpp | 12 ++++++++++-- .../adapter/communicator/src/softbus_client.h | 3 ++- .../softbus_adapter_standard_test.cpp | 19 +++++++++++++++++++ .../test/unittest/softbus_client_test.cpp | 13 +++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index 5d8e2cb4d..0289f25d9 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 23c8c7916..e3ee9ff27 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp @@ -155,8 +155,9 @@ 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); + 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", @@ -258,6 +259,13 @@ Status SoftBusClient::ReuseConnect(const ISocketListener *listener) const std::string& SoftBusClient::GetNetworkId() const { + std::lock_guard lock(mutex_); return networkId_; } + +void SoftBusClient::UpdateNetworkId(const std::string& networkId) +{ + std::lock_guard lock(mutex_); + 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 a0196c94f..d69bc7c2f 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.h +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.h @@ -53,6 +53,7 @@ public: int32_t GetSoftBusError(); Status ReuseConnect(const ISocketListener *listener); 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); @@ -97,7 +98,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 842b47777..e5c1588c5 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 @@ -585,6 +585,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 3f1d713de..b8ffd0994 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 -- Gitee From 5b3fb817892404fa6c94a49f8486ff3a5aa295a3 Mon Sep 17 00:00:00 2001 From: yanhui Date: Thu, 5 Jun 2025 20:30:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E4=B8=BA=E6=8B=B7=E8=B4=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanhui Change-Id: Icf5b79a1fa77e5b46121dd9460b463fe8849345b --- .../adapter/communicator/src/softbus_client.cpp | 14 ++++++++------ .../adapter/communicator/src/softbus_client.h | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp index e3ee9ff27..f5cc66391 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,9 +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); + 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()); + KvStoreUtils::ToBeAnonymous(networkId).c_str()); if (status != 0) { ZLOGE("[Bind] device:%{public}s socket failed, session:%{public}s,result:%{public}d", @@ -177,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; } @@ -257,15 +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(mutex_); + std::lock_guard lock(networkIdMutex_); return networkId_; } void SoftBusClient::UpdateNetworkId(const std::string& networkId) { - std::lock_guard lock(mutex_); + 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 d69bc7c2f..d224e6b47 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.h +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.h @@ -52,7 +52,7 @@ 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: @@ -89,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_; -- Gitee