From b7e91bd3b8b52f2b95a4d84a6122c5b2eeee0525 Mon Sep 17 00:00:00 2001 From: yanhui Date: Tue, 8 Apr 2025 16:19:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?SoftBusClient=E5=A2=9E=E5=8A=A0networkId?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanhui Change-Id: I494cc62ff95f1cb22a67337acfa7559235060aba --- .../communicator/src/softbus_adapter_standard.cpp | 13 ++++++++----- .../adapter/communicator/src/softbus_client.cpp | 9 +++++++-- .../adapter/communicator/src/softbus_client.h | 1 + .../adapter/include/communicator/commu_types.h | 1 + 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index 81b9f74e4..363699e6a 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -207,7 +207,8 @@ std::shared_ptr SoftBusAdapter::GetConnect(const PipeInfo &pipeIn uint32_t qosType) { std::shared_ptr conn; - connects_.Compute(deviceId.deviceId, [&pipeInfo, &deviceId, &conn, qosType](const auto &key, + auto networkId = DmAdapter::GetInstance().GetDeviceInfo(deviceId.deviceId).networkId; + connects_.Compute(deviceId.deviceId, [&pipeInfo, &deviceId, &conn, qosType, &networkId](const auto &key, std::vector> &connects) -> bool { for (auto &connect : connects) { if (connect == nullptr) { @@ -218,7 +219,8 @@ std::shared_ptr SoftBusAdapter::GetConnect(const PipeInfo &pipeIn return true; } } - auto connect = std::make_shared(pipeInfo, deviceId, qosType); + DeviceId device = { .deviceId = deviceId.deviceId, .networkId = std::move(networkId) }; + auto connect = std::make_shared(pipeInfo, device, qosType); connects.emplace_back(connect); conn = connect; return true; @@ -354,23 +356,24 @@ std::string SoftBusAdapter::DelConnect(int32_t socket, bool isForce) if (!isForce && DmAdapter::GetInstance().IsOHOSType(deviceId)) { return false; } + std::string networkId; for (auto iter = connects.begin(); iter != connects.end();) { if (*iter != nullptr && **iter == socket) { name += deviceId; name += " "; + networkId = (*iter)->GetNetworkId(); iter = connects.erase(iter); } else { iter++; } } if (connects.empty()) { - closedConnect.insert(deviceId); + closedConnect.insert(networkId); return true; } return false; }); - for (const auto &deviceId : closedConnect) { - auto networkId = DmAdapter::GetInstance().GetDeviceInfo(deviceId).networkId; + for (const auto &networkId : closedConnect) { ConnectManager::GetInstance()->OnSessionClose(networkId); } return name; diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp index f4451869f..84df847a0 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp @@ -125,7 +125,7 @@ int32_t SoftBusClient::CreateSocket() const SocketInfo socketInfo; std::string peerName = pipe_.pipeId; socketInfo.peerName = const_cast(peerName.c_str()); - std::string networkId = DmAdapter::GetInstance().ToNetworkID(device_.deviceId); + std::string networkId = device_.networkId; socketInfo.peerNetworkId = const_cast(networkId.c_str()); std::string clientName = pipe_.pipeId; socketInfo.name = const_cast(clientName.c_str()); @@ -177,7 +177,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(DmAdapter::GetInstance().GetDeviceInfo(device_.deviceId).networkId); + ConnectManager::GetInstance()->OnSessionOpen(device_.networkId); return status; } @@ -256,4 +256,9 @@ Status SoftBusClient::ReuseConnect(const ISocketListener *listener) int32_t status = Open(socket, QOS_REUSE, listener, false); return status == SOFTBUS_OK ? Status::SUCCESS : Status::NETWORK_ERROR; } + +std::string SoftBusClient::GetNetworkId() +{ + return device_.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 67709bf90..d4df243c2 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.h +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.h @@ -51,6 +51,7 @@ public: void UpdateExpireTime(bool async = true); int32_t GetSoftBusError(); Status ReuseConnect(const ISocketListener *listener); + std::string GetNetworkId(); private: int32_t Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async = true); diff --git a/services/distributeddataservice/adapter/include/communicator/commu_types.h b/services/distributeddataservice/adapter/include/communicator/commu_types.h index 6b1f1af69..6cca6af2f 100644 --- a/services/distributeddataservice/adapter/include/communicator/commu_types.h +++ b/services/distributeddataservice/adapter/include/communicator/commu_types.h @@ -71,6 +71,7 @@ struct API_EXPORT PipeInfo { struct API_EXPORT DeviceId { std::string deviceId; + std::string networkId; }; enum class API_EXPORT MessageType { -- Gitee From 1b44c19721f153c5cfa55eb8bf8ea2b625836786 Mon Sep 17 00:00:00 2001 From: yanhui Date: Wed, 9 Apr 2025 21:13:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0networkId=5F=E5=B1=9E?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanhui Change-Id: I570bbefc71e2243de319a616cf3b6cbb1e8e24da --- .../communicator/src/softbus_adapter_standard.cpp | 9 ++++----- .../adapter/communicator/src/softbus_client.cpp | 13 ++++++------- .../adapter/communicator/src/softbus_client.h | 6 ++++-- .../test/unittest/softbus_client_test.cpp | 2 +- .../adapter/include/communicator/commu_types.h | 1 - 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index 363699e6a..f4df6deec 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -207,7 +207,7 @@ std::shared_ptr SoftBusAdapter::GetConnect(const PipeInfo &pipeIn uint32_t qosType) { std::shared_ptr conn; - auto networkId = DmAdapter::GetInstance().GetDeviceInfo(deviceId.deviceId).networkId; + std::string networkId = DmAdapter::GetInstance().ToNetworkID(deviceId.deviceId); connects_.Compute(deviceId.deviceId, [&pipeInfo, &deviceId, &conn, qosType, &networkId](const auto &key, std::vector> &connects) -> bool { for (auto &connect : connects) { @@ -219,8 +219,7 @@ std::shared_ptr SoftBusAdapter::GetConnect(const PipeInfo &pipeIn return true; } } - DeviceId device = { .deviceId = deviceId.deviceId, .networkId = std::move(networkId) }; - auto connect = std::make_shared(pipeInfo, device, qosType); + auto connect = std::make_shared(pipeInfo, deviceId, networkId, qosType); connects.emplace_back(connect); conn = connect; return true; @@ -367,8 +366,8 @@ std::string SoftBusAdapter::DelConnect(int32_t socket, bool isForce) iter++; } } - if (connects.empty()) { - closedConnect.insert(networkId); + if (connects.empty() && !networkId.empty()) { + closedConnect.insert(std::move(networkId)); return true; } return false; diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp index 84df847a0..23c8c7916 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.cpp @@ -28,8 +28,8 @@ namespace OHOS::AppDistributedKv { using namespace OHOS::DistributedKv; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Context = DistributedData::CommunicatorContext; -SoftBusClient::SoftBusClient(const PipeInfo& pipeInfo, const DeviceId& deviceId, uint32_t type) - : type_(type), pipe_(pipeInfo), device_(deviceId) +SoftBusClient::SoftBusClient(const PipeInfo& pipeInfo, const DeviceId& deviceId, const std::string& networkId, + uint32_t type) : type_(type), pipe_(pipeInfo), device_(deviceId), networkId_(networkId) { mtu_ = DEFAULT_MTU_SIZE; } @@ -125,8 +125,7 @@ int32_t SoftBusClient::CreateSocket() const SocketInfo socketInfo; std::string peerName = pipe_.pipeId; socketInfo.peerName = const_cast(peerName.c_str()); - std::string networkId = device_.networkId; - socketInfo.peerNetworkId = const_cast(networkId.c_str()); + socketInfo.peerNetworkId = const_cast(networkId_.c_str()); std::string clientName = pipe_.pipeId; socketInfo.name = const_cast(clientName.c_str()); std::string pkgName = "ohos.distributeddata"; @@ -177,7 +176,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(device_.networkId); + ConnectManager::GetInstance()->OnSessionOpen(networkId_); return status; } @@ -257,8 +256,8 @@ Status SoftBusClient::ReuseConnect(const ISocketListener *listener) return status == SOFTBUS_OK ? Status::SUCCESS : Status::NETWORK_ERROR; } -std::string SoftBusClient::GetNetworkId() +const std::string& SoftBusClient::GetNetworkId() const { - return device_.networkId; + return 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 d4df243c2..a0196c94f 100644 --- a/services/distributeddataservice/adapter/communicator/src/softbus_client.h +++ b/services/distributeddataservice/adapter/communicator/src/softbus_client.h @@ -33,7 +33,8 @@ public: QOS_REUSE, QOS_BUTT }; - SoftBusClient(const PipeInfo &pipeInfo, const DeviceId &deviceId, uint32_t type = QOS_HML); + SoftBusClient(const PipeInfo &pipeInfo, const DeviceId &deviceId, const std::string& networkId, + uint32_t type = QOS_HML); ~SoftBusClient(); using Time = std::chrono::steady_clock::time_point; @@ -51,7 +52,7 @@ public: void UpdateExpireTime(bool async = true); int32_t GetSoftBusError(); Status ReuseConnect(const ISocketListener *listener); - std::string GetNetworkId(); + const std::string& GetNetworkId() const; private: int32_t Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async = true); @@ -96,6 +97,7 @@ private: int32_t socket_ = INVALID_SOCKET_ID; int32_t bindState_ = -1; int32_t softBusError_ = 0; + const std::string networkId_; }; } // namespace OHOS::AppDistributedKv 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 3ba198442..3f1d713de 100644 --- a/services/distributeddataservice/adapter/communicator/test/unittest/softbus_client_test.cpp +++ b/services/distributeddataservice/adapter/communicator/test/unittest/softbus_client_test.cpp @@ -46,7 +46,7 @@ void SoftbusClientTest::SetUpTestCase(void) pipeInfo.pipeId = "pipeId"; pipeInfo.userId = "userId"; DeviceId id = {"DeviceId"}; - client = std::make_shared(pipeInfo, id); + client = std::make_shared(pipeInfo, id, ""); } void SoftbusClientTest::TearDownTestCase(void) diff --git a/services/distributeddataservice/adapter/include/communicator/commu_types.h b/services/distributeddataservice/adapter/include/communicator/commu_types.h index 6cca6af2f..6b1f1af69 100644 --- a/services/distributeddataservice/adapter/include/communicator/commu_types.h +++ b/services/distributeddataservice/adapter/include/communicator/commu_types.h @@ -71,7 +71,6 @@ struct API_EXPORT PipeInfo { struct API_EXPORT DeviceId { std::string deviceId; - std::string networkId; }; enum class API_EXPORT MessageType { -- Gitee