diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index 81b9f74e4cf492bf50fd8eb15cdeade7abb8343c..f4df6deecc68bd90d9e58361b9bff9fd9c29ab2b 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, + 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) { if (connect == nullptr) { @@ -218,7 +219,7 @@ std::shared_ptr SoftBusAdapter::GetConnect(const PipeInfo &pipeIn return true; } } - auto connect = std::make_shared(pipeInfo, deviceId, qosType); + auto connect = std::make_shared(pipeInfo, deviceId, networkId, qosType); connects.emplace_back(connect); conn = connect; return true; @@ -354,23 +355,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); + if (connects.empty() && !networkId.empty()) { + closedConnect.insert(std::move(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 f4451869fc0dd6eb479961ce84b27b9aaed1e225..23c8c79167f5735c46c620a3d592cda106bede2b 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 = DmAdapter::GetInstance().ToNetworkID(device_.deviceId); - 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(DmAdapter::GetInstance().GetDeviceInfo(device_.deviceId).networkId); + ConnectManager::GetInstance()->OnSessionOpen(networkId_); return status; } @@ -256,4 +255,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; } + +const std::string& SoftBusClient::GetNetworkId() const +{ + 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 67709bf9007d755123e27b8bfc25033dae199c50..a0196c94f3203f1475bb84c3c5c111e4dbc6ee31 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,6 +52,7 @@ public: void UpdateExpireTime(bool async = true); int32_t GetSoftBusError(); Status ReuseConnect(const ISocketListener *listener); + const std::string& GetNetworkId() const; private: int32_t Open(int32_t socket, uint32_t type, const ISocketListener *listener, bool async = true); @@ -95,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 3ba198442597f4c79adeaf845e4b802a0910dff3..3f1d713deb0fc7b2b05b72ae2cde2ac687dd6375 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)