diff --git a/services/transportbase/include/distributed_input_transport_base.h b/services/transportbase/include/distributed_input_transport_base.h index 75bd78f30c56497cd6dc0185e2125ce7d5098aa6..e6eebfcbe9a27fec1d3c7c04188e41c554cb0027 100644 --- a/services/transportbase/include/distributed_input_transport_base.h +++ b/services/transportbase/include/distributed_input_transport_base.h @@ -16,6 +16,7 @@ #ifndef DISTRIBUTED_INPUT_TRANSPORT_BASE_H #define DISTRIBUTED_INPUT_TRANSPORT_BASE_H +#include #include #include #include @@ -66,12 +67,14 @@ private: void Release(); private: + std::atomic isSessSerCreateFlag_ = false; + std::mutex sessSerOperMutex_; std::mutex operationMutex_; std::string remoteDeviceId_; std::map remoteDevSessionMap_; std::map channelStatusMap_; std::condition_variable openSessionWaitCond_; - std::string mySessionName_ = ""; + std::string localSessionName_ = ""; int32_t sessionId_ = 0; std::shared_ptr srcCallback_; diff --git a/services/transportbase/src/distributed_input_transport_base.cpp b/services/transportbase/src/distributed_input_transport_base.cpp index fb058e183b917f214fca05d776ff140b2e26e138..bd29f77f601a4161a51564ccf2c92d955babcba9 100644 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -127,13 +127,18 @@ int32_t DistributedInputTransportBase::Init() std::string networkId = localNode->networkId; DHLOGI("Init device local networkId is %s", GetAnonyString(networkId).c_str()); - mySessionName_ = SESSION_NAME + networkId.substr(0, INTERCEPT_STRING_LENGTH); - - int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); + std::unique_lock sessionServerLock(sessSerOperMutex_); + if (isSessSerCreateFlag_.load()) { + DHLOGI("SessionServer already create success."); + return DH_SUCCESS; + } + localSessionName_ = SESSION_NAME + networkId.substr(0, INTERCEPT_STRING_LENGTH); + int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), localSessionName_.c_str(), &iSessionListener); if (ret != DH_SUCCESS) { DHLOGE("Init CreateSessionServer failed, error code %d.", ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; } + isSessSerCreateFlag_.store(true); return DH_SUCCESS; } @@ -144,7 +149,16 @@ void DistributedInputTransportBase::Release() for (; iter != remoteDevSessionMap_.end(); ++iter) { CloseSession(iter->second); } - (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); + + { + std::unique_lock sessionServerLock(sessSerOperMutex_); + if (!isSessSerCreateFlag_.load()) { + DHLOGI("SessionServer already remove success."); + } else { + (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), localSessionName_.c_str()); + isSessSerCreateFlag_.store(false); + } + } remoteDevSessionMap_.clear(); channelStatusMap_.clear(); } @@ -182,7 +196,7 @@ int32_t DistributedInputTransportBase::StartSession(const std::string &remoteDev DHLOGI("OpenInputSoftbus peerSessionName:%s", peerSessionName.c_str()); StartAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_OPEN_SESSION_START, DINPUT_OPEN_SESSION_TASK); - int32_t sessionId = OpenSession(mySessionName_.c_str(), peerSessionName.c_str(), remoteDevId.c_str(), + int32_t sessionId = OpenSession(localSessionName_.c_str(), peerSessionName.c_str(), remoteDevId.c_str(), GROUP_ID.c_str(), &g_sessionAttr); if (sessionId < 0) { DHLOGE("OpenSession fail, remoteDevId: %s, sessionId: %d", GetAnonyString(remoteDevId).c_str(), sessionId); @@ -190,7 +204,7 @@ int32_t DistributedInputTransportBase::StartSession(const std::string &remoteDev return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_OPEN_SESSION_FAIL; } - HiDumper::GetInstance().CreateSessionInfo(remoteDevId, sessionId, mySessionName_, peerSessionName, + HiDumper::GetInstance().CreateSessionInfo(remoteDevId, sessionId, localSessionName_, peerSessionName, SessionStatus::OPENING); DHLOGI("Wait for channel session opened.");