From c58d005f72becc0438d7668766def3cf2d24904c Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Sat, 24 May 2025 16:02:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B9=BF=E6=92=AD=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- .../src/authentication_v2/dm_auth_state.cpp | 14 ++++-- .../relationship_sync_mgr.cpp | 47 ++++++++++++------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/services/implementation/src/authentication_v2/dm_auth_state.cpp b/services/implementation/src/authentication_v2/dm_auth_state.cpp index 782ed8db1..3476d8563 100644 --- a/services/implementation/src/authentication_v2/dm_auth_state.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_state.cpp @@ -46,6 +46,7 @@ const std::map TASK_TIME_OUT_MAP = { constexpr int32_t ONBINDRESULT_MAPPING_NUM = 2; constexpr int32_t MS_PER_SECOND = 1000; constexpr int32_t US_PER_MSECOND = 1000; +constexpr int32_t GET_SYSTEMTIME_MAX_NUM = 3; constexpr const static char* ONBINDRESULT_MAPPING_LIST[ONBINDRESULT_MAPPING_NUM] = { "CollaborationFwk", "cast_engine_service", @@ -382,11 +383,16 @@ uint64_t DmAuthState::GetSysTimeMs() struct timeval time; time.tv_sec = 0; time.tv_usec = 0; - if (gettimeofday(&time, nullptr) != 0) { - LOGE("GetSysTimeMs failed."); - return 0; + int32_t retryNum = 0; + while (retryNum < GET_SYSTEMTIME_MAX_NUM) { + if (gettimeofday(&time, nullptr) != 0) { + retryNum++; + LOGE("GetSysTimeMs failed. retryNum: %{public}d", retryNum); + continue; + } + return (uint64_t) time.tv_sec * MS_PER_SECOND + (uint64_t)time.tv_usec / US_PER_MSECOND; } - return (uint64_t) time.tv_sec * MS_PER_SECOND + (uint64_t)time.tv_usec / US_PER_MSECOND; + return 0; } void DmAuthState::DeleteAcl(std::shared_ptr context, diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index 428531518..dadc1ea59 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -69,6 +69,7 @@ namespace { const int32_t BROADCAST_PAYLOAD_LEN = 10; const int32_t BROADCAST_TIMEOUT_S = 5; const int32_t CURRENT_TIME_SEC_FLAG = 10; + const int32_t GET_CURRENT_TIME_MAX_NUM = 3; const char * const MSG_TYPE = "TYPE"; const char * const MSG_VALUE = "VALUE"; @@ -245,7 +246,7 @@ bool RelationShipChangeMsg::FromShareUnbindPayLoad(const cJSON *payloadJson) this->broadCastId = 0; for (uint32_t j = CREDID_PAYLOAD_LEN; j < SHARE_UNBIND_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { this->broadCastId |= (static_cast(payloadItem->valueint)) << ((j - CREDID_PAYLOAD_LEN) * BITS_PER_BYTE); @@ -466,7 +467,7 @@ bool RelationShipChangeMsg::FromAccountLogoutPayLoad(const cJSON *payloadJson) broadCastId = 0; for (uint32_t j = ACCOUNT_LOGOUT_BROADCAST_LEN; j < ACCOUNT_LOGOUT_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { broadCastId |= (static_cast(payloadItem->valueint)) << ((j - ACCOUNT_LOGOUT_BROADCAST_LEN) * BITS_PER_BYTE); @@ -497,7 +498,7 @@ bool RelationShipChangeMsg::FromDeviceUnbindPayLoad(const cJSON *payloadJson) broadCastId = 0; for (uint32_t j = USERID_PAYLOAD_LEN; j < DEVICE_UNBIND_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { broadCastId |= (static_cast(payloadItem->valueint)) << ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE); @@ -545,7 +546,7 @@ bool RelationShipChangeMsg::FromAppUnbindPayLoad(const cJSON *payloadJson) broadCastId = 0; for (uint32_t j = BROADCAST_PAYLOAD_LEN; j < APP_UNBIND_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { broadCastId |= (static_cast(payloadItem->valueint)) << ((j - BROADCAST_PAYLOAD_LEN) * BITS_PER_BYTE); @@ -563,7 +564,7 @@ bool RelationShipChangeMsg::GetBroadCastId(const cJSON *payloadJson, uint32_t us { broadCastId = 0; cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, userIdNum * USERID_BYTES + 1); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { broadCastId |= static_cast(payloadItem->valueint); } @@ -649,7 +650,7 @@ bool RelationShipChangeMsg::FromDelUserPayLoad(const cJSON *payloadJson) this->broadCastId = 0; for (uint32_t j = USERID_PAYLOAD_LEN; j < DEL_USER_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { this->broadCastId |= (static_cast(payloadItem->valueint)) << ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE); @@ -681,7 +682,7 @@ bool RelationShipChangeMsg::FromStopUserPayLoad(const cJSON *payloadJson) this->broadCastId = 0; for (uint32_t j = USERID_PAYLOAD_LEN; j < STOP_USER_PAYLOAD_LEN; j++) { cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j); - CHECK_NULL_RETURN(payloadItem, true); + CHECK_NULL_RETURN(payloadItem, false); if (cJSON_IsNumber(payloadItem)) { this->broadCastId |= (static_cast(payloadItem->valueint)) << ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE); @@ -846,27 +847,36 @@ void ReleationShipSyncMgr::HandleRecvBroadCastTimeout(const std::string &key) bool ReleationShipSyncMgr::GetCurrentTimeSec(int32_t &sec) { + int32_t retryNum = 0; time_t now; - time(&now); struct tm ltm; - struct tm *res = localtime_r(&now, <m); - if (res == nullptr) { - LOGE("get current time failed."); - return false; + while (retryNum < GET_CURRENT_TIME_MAX_NUM) { + time(&now); + struct tm *res = localtime_r(&now, <m); + if (res == nullptr) { + retryNum++; + LOGE("get current time failed. retryNum: %{public}d", retryNum); + continue; + } + sec = ltm.tm_sec % CURRENT_TIME_SEC_FLAG; + if (sec == 0) { + sec = CURRENT_TIME_SEC_FLAG; + } + return true; } - sec = ltm.tm_sec % CURRENT_TIME_SEC_FLAG; - return true; + return false; } std::string ReleationShipSyncMgr::SyncTrustRelationShip(RelationShipChangeMsg &msg) { int32_t currentTimeSec = 0; - msg.broadCastId = static_cast(CURRENT_TIME_SEC_FLAG); + msg.broadCastId = 0; if (!GetCurrentTimeSec(currentTimeSec)) { LOGE("get current time failed, use default value"); return msg.ToJson(); } msg.broadCastId = static_cast(currentTimeSec); + LOGI("send trust change msg: %{public}s", msg.ToString().c_str()); return msg.ToJson(); } @@ -881,11 +891,14 @@ RelationShipChangeMsg ReleationShipSyncMgr::ParseTrustRelationShipChange(const s bool ReleationShipSyncMgr::IsNewBroadCastId(const RelationShipChangeMsg &msg) { - if (timer_ == nullptr) { - timer_ = std::make_shared(); + if (msg.broadCastId == 0) { + return true; } std::string key = msg.ToMapKey(); std::lock_guard autoLock(lock_); + if (timer_ == nullptr) { + timer_ = std::make_shared(); + } auto iter = recvBroadCastIdMap_.find(key); if (iter == recvBroadCastIdMap_.end()) { recvBroadCastIdMap_[key] = msg.broadCastId; -- Gitee