From 0e90309cb1885c582c6e00a23c9f0e6f8e5bb16e Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Wed, 9 Oct 2024 18:54:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=8C=E6=AC=A1=E5=9B=9E?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- .../service/include/pinholder/pin_holder.h | 9 +++++ services/service/src/pinholder/pin_holder.cpp | 36 +++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/services/service/include/pinholder/pin_holder.h b/services/service/include/pinholder/pin_holder.h index b1aa7c149..75d999cd9 100644 --- a/services/service/include/pinholder/pin_holder.h +++ b/services/service/include/pinholder/pin_holder.h @@ -21,6 +21,7 @@ #include "pin_holder_session.h" #include "pinholder_session_callback.h" +#include #include #include @@ -35,6 +36,12 @@ typedef enum PinHolderState { SINK_DESTROY, } PinHolderState; +typedef enum DestroyState { + STATE_UNKNOW = 0x0, + STATE_REMOTE_WRONG = 0x1, + STATE_TIME_OUT = 0x2, +} DestroyState; + class PinHolder final : public IPinholderSessionCallback, public std::enable_shared_from_this { public: @@ -76,6 +83,8 @@ private: PinHolderState sourceState_; int32_t sessionId_ = -1; bool isRemoteSupported_ = false; + std::atomic isDestroy_ {false}; + DestroyState destroyState_ = STATE_UNKNOW; }; } } diff --git a/services/service/src/pinholder/pin_holder.cpp b/services/service/src/pinholder/pin_holder.cpp index 3674c0929..132bec70a 100644 --- a/services/service/src/pinholder/pin_holder.cpp +++ b/services/service/src/pinholder/pin_holder.cpp @@ -45,6 +45,7 @@ constexpr const char* TAG_REMOTE_DEVICE_ID = "REMOTE_DEVICE_ID"; constexpr int32_t DM_OK = 0; constexpr int32_t ERR_DM_FAILED = 96929744; +constexpr int32_t ERR_DM_TIME_OUT = 96929745; constexpr const char* TAG_MSG_TYPE = "MSG_TYPE"; constexpr const char* TAG_DM_VERSION = "DM_VERSION"; constexpr const char* DM_CONNECTION_DISCONNECTED = "DM_CONNECTION_DISCONNECTED"; @@ -307,6 +308,7 @@ void PinHolder::ProcessCreateRespMsg(const std::string &message) listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::CREATE_RESULT, ERR_DM_FAILED, ""); session_->CloseSessionServer(sessionId_); sessionId_ = SESSION_ID_INVALID; + destroyState_ = STATE_REMOTE_WRONG; } } @@ -337,12 +339,15 @@ void PinHolder::ProcessDestroyMsg(const std::string &message) jsonObj[TAG_REPLY] = REPLY_SUCCESS; sinkState_ = SINK_INIT; sourceState_ = SOURCE_INIT; - listener_->OnPinHolderDestroy(registerPkgName_, pinType, payload); - nlohmann::json jsonContent; - jsonContent[TAG_PIN_TYPE] = pinType; - jsonContent[TAG_PAYLOAD] = payload; - std::string content = jsonContent.dump(); - listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, DM_OK, content); + if (!isDestroy_.load()) { + listener_->OnPinHolderDestroy(registerPkgName_, pinType, payload); + nlohmann::json jsonContent; + jsonContent[TAG_PIN_TYPE] = pinType; + jsonContent[TAG_PAYLOAD] = payload; + std::string content = jsonContent.dump(); + listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, DM_OK, content); + isDestroy_.store(true); + } } std::string msg = jsonObj.dump(); @@ -364,18 +369,20 @@ void PinHolder::CloseSession(const std::string &name) nlohmann::json jsonObj; jsonObj[DM_CONNECTION_DISCONNECTED] = true; std::string payload = jsonObj.dump(); - if (listener_ != nullptr) { + if (listener_ != nullptr && !isDestroy_.load()) { listener_->OnPinHolderDestroy(registerPkgName_, pinType_, payload); nlohmann::json jsonContent; jsonContent[TAG_PIN_TYPE] = pinType_; jsonContent[TAG_PAYLOAD] = payload; std::string content = jsonContent.dump(); - listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, DM_OK, content); + listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, ERR_DM_TIME_OUT, content); + isDestroy_.store(true); } session_->CloseSessionServer(sessionId_); if (timer_ != nullptr) { timer_->DeleteAll(); } + destroyState_ = STATE_TIME_OUT; sessionId_ = SESSION_ID_INVALID; sinkState_ = SINK_INIT; sourceState_ = SOURCE_INIT; @@ -468,6 +475,8 @@ void PinHolder::GetPeerDeviceId(int32_t sessionId, std::string &udidHash) void PinHolder::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int32_t result) { + isDestroy_.store(false); + destroyState_ = STATE_UNKNOW; char peerDeviceId[DEVICE_UUID_LENGTH] = {0}; int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH); if (ret != DM_OK) { @@ -505,13 +514,20 @@ void PinHolder::OnSessionClosed(int32_t sessionId) nlohmann::json jsonObj; jsonObj[DM_CONNECTION_DISCONNECTED] = true; std::string payload = jsonObj.dump(); - if (listener_ != nullptr) { + if (listener_ != nullptr && !isDestroy_.load()) { listener_->OnPinHolderDestroy(registerPkgName_, pinType_, payload); nlohmann::json jsonContent; jsonContent[TAG_PIN_TYPE] = pinType_; jsonContent[TAG_PAYLOAD] = payload; std::string content = jsonContent.dump(); - listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, DM_OK, content); + if (destroyState_ == STATE_UNKNOW) { + listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, sessionId, content); + } else if (destroyState_ == STATE_REMOTE_WRONG) { + listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, ERR_DM_FAILED, content); + } else if (destroyState_ == STATE_TIME_OUT) { + listener_->OnPinHolderEvent(registerPkgName_, DmPinHolderEvent::DESTROY, ERR_DM_TIME_OUT, content); + } + isDestroy_.store(true); } if (timer_ != nullptr) { timer_->DeleteAll(); -- Gitee