diff --git a/common/include/dm_error_type.h b/common/include/dm_error_type.h index a22dffc64002c12ebd065ea2f7d17df7f0bbf29b..cd5225aedad804d56a1c05ad077019671ef1d2e2 100644 --- a/common/include/dm_error_type.h +++ b/common/include/dm_error_type.h @@ -141,6 +141,7 @@ enum { ERR_DM_GET_PARAM_FAILED = 969298352, ERR_DM_VERIFY_SAME_ACCOUNT_FAILED = 969298353, ERR_DM_DEVICE_FREEZED = 969298355, + ERR_DM_SOCKET_IN_USED = 969298356, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/include/relationshipsyncmgr/dm_transport.h b/services/service/include/relationshipsyncmgr/dm_transport.h index 5bc97dfd0f152cc388257e71106ee66fc7623989..68eea2a4c8b6412a704b06695ee76c59c2dd54c2 100644 --- a/services/service/include/relationshipsyncmgr/dm_transport.h +++ b/services/service/include/relationshipsyncmgr/dm_transport.h @@ -46,6 +46,7 @@ private: std::string GetRemoteNetworkIdBySocketId(int32_t socketId); void ClearDeviceSocketOpened(const std::string &remoteDevId, int32_t socketId); void HandleReceiveMessage(const int32_t socketId, const std::string &payload); + int32_t StartSocketInner(const std::string &rmtNetworkId, int32_t &socketId); private: std::mutex rmtSocketIdMtx_; diff --git a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp index 56111b6916952f20cf2f4e40458319d0ad65f333..40d8dbf995defbdd36144e695b5831f424aa6212 100644 --- a/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp +++ b/services/service/src/relationshipsyncmgr/dm_comm_tool.cpp @@ -844,6 +844,8 @@ int32_t DMCommTool::SendLogoutAccountInfo(const std::string &rmtNetworkId, void DMCommTool::ProcessReceiveLogoutEvent(const std::shared_ptr commMsg) { CHECK_NULL_VOID(commMsg); + CHECK_NULL_VOID(dmTransportPtr_); + this->dmTransportPtr_->StopSocket(commMsg->remoteNetworkId); LOGI("Receive remote logout, networkId: %{public}s", GetAnonyString(commMsg->remoteNetworkId).c_str()); std::string rmtUdid = ""; SoftbusCache::GetInstance().GetUdidFromCache(commMsg->remoteNetworkId.c_str(), rmtUdid); diff --git a/services/service/src/relationshipsyncmgr/dm_transport.cpp b/services/service/src/relationshipsyncmgr/dm_transport.cpp index ba18320284caf8f268d2ffe4b0908248571f7fe9..1bacbcc7878c257c576b19020d6aca9be24229c4 100644 --- a/services/service/src/relationshipsyncmgr/dm_transport.cpp +++ b/services/service/src/relationshipsyncmgr/dm_transport.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include + #include "dm_transport.h" #include "dm_anonymous.h" #include "dm_comm_tool.h" @@ -20,6 +22,7 @@ #include "dm_log.h" #include "dm_softbus_cache.h" #include "dm_transport_msg.h" +#include "softbus_error_code.h" namespace OHOS { namespace DistributedHardware { @@ -28,6 +31,7 @@ namespace { constexpr uint32_t MAX_SEND_MSG_LENGTH = 4 * 1024 * 1024; constexpr uint32_t INTERCEPT_STRING_LENGTH = 20; constexpr uint32_t MAX_ROUND_SIZE = 1000; +const int32_t USLEEP_TIME_US_200000 = 200000; // 200ms static QosTV g_qosInfo[] = { { .qos = QOS_TYPE_MIN_BW, .value = 256 * 1024}, { .qos = QOS_TYPE_MAX_LATENCY, .value = 8000 }, @@ -373,13 +377,31 @@ void DMTransport::ClearDeviceSocketOpened(const std::string &remoteDevId, int32_ } int32_t DMTransport::StartSocket(const std::string &rmtNetworkId, int32_t &socketId) +{ + int32_t errCode = ERR_DM_FAILED; + int32_t count = 0; + const int32_t maxCount = 10; + + do { + errCode = StartSocketInner(rmtNetworkId, socketId); + if (errCode != ERR_DM_SOCKET_IN_USED) { + break; + } + count++; + usleep(USLEEP_TIME_US_200000); + } while (count < maxCount); + + return errCode; +} + +int32_t DMTransport::StartSocketInner(const std::string &rmtNetworkId, int32_t &socketId) { if (!IsIdLengthValid(rmtNetworkId)) { return ERR_DM_INPUT_PARA_INVALID; } if (IsDeviceSessionOpened(rmtNetworkId, socketId)) { LOGE("Softbus session has already opened, deviceId: %{public}s", GetAnonyString(rmtNetworkId).c_str()); - return DM_OK; + return ERR_DM_SOCKET_IN_USED; } int32_t socket = CreateClientSocket(rmtNetworkId); @@ -390,6 +412,10 @@ int32_t DMTransport::StartSocket(const std::string &rmtNetworkId, int32_t &socke int32_t ret = Bind(socket, g_qosInfo, g_qosTvParamIndex, &iSocketListener); if (ret < DM_OK) { + if (ret == SOFTBUS_TRANS_SOCKET_IN_USE) { + LOGI("Softbus trans socket in use."); + return ERR_DM_SOCKET_IN_USED; + } LOGE("OpenSession fail, rmtNetworkId: %{public}s, socket: %{public}d, ret: %{public}d", GetAnonyString(rmtNetworkId).c_str(), socket, ret); Shutdown(socket);