From 952bcf83ff9764995866ee872c5c41fb968e2be2 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Tue, 1 Aug 2023 18:02:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=98=B2=E6=AD=A2=E4=BA=8C=E6=AC=A1create?= =?UTF-8?q?=20session=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- .../distributed_input_transport_base.h | 3 +++ .../src/distributed_input_transport_base.cpp | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/services/transportbase/include/distributed_input_transport_base.h b/services/transportbase/include/distributed_input_transport_base.h index 75bd78f..ce2b1e6 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,6 +67,8 @@ private: void Release(); private: + std::atomic sessionServer_ {false}; + std::mutex sessServerMutex_; std::mutex operationMutex_; std::string remoteDeviceId_; std::map remoteDevSessionMap_; diff --git a/services/transportbase/src/distributed_input_transport_base.cpp b/services/transportbase/src/distributed_input_transport_base.cpp index fb058e1..6477c72 100644 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -129,11 +129,18 @@ int32_t DistributedInputTransportBase::Init() mySessionName_ = SESSION_NAME + networkId.substr(0, INTERCEPT_STRING_LENGTH); + std::unique_lock sessionServerLock(sessServerMutex_); + if (sessionServer_.load()) { + DHLOGI("SessionServer already create success."); + return DH_SUCCESS; + } int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); if (ret != DH_SUCCESS) { DHLOGE("Init CreateSessionServer failed, error code %d.", ret); return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_INIT_FAIL; } + sessionServer_.store(true); + return DH_SUCCESS; } @@ -144,7 +151,17 @@ 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(sessServerMutex_); + if (!sessionServer_.load()) { + DHLOGI("SessionServer already remove success."); + } else { + (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); + sessionServer_.store(false); + } + } + remoteDevSessionMap_.clear(); channelStatusMap_.clear(); } -- Gitee From eca489b5655dd5cf4dc2010a3058980b551eed03 Mon Sep 17 00:00:00 2001 From: liqiao49 Date: Tue, 1 Aug 2023 20:08:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liqiao49 --- .../distributed_input_transport_base.h | 6 ++--- .../src/distributed_input_transport_base.cpp | 25 ++++++++----------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/services/transportbase/include/distributed_input_transport_base.h b/services/transportbase/include/distributed_input_transport_base.h index ce2b1e6..e6eebfc 100644 --- a/services/transportbase/include/distributed_input_transport_base.h +++ b/services/transportbase/include/distributed_input_transport_base.h @@ -67,14 +67,14 @@ private: void Release(); private: - std::atomic sessionServer_ {false}; - std::mutex sessServerMutex_; + 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 6477c72..bd29f77 100644 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -127,20 +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); - - std::unique_lock sessionServerLock(sessServerMutex_); - if (sessionServer_.load()) { + std::unique_lock sessionServerLock(sessSerOperMutex_); + if (isSessSerCreateFlag_.load()) { DHLOGI("SessionServer already create success."); return DH_SUCCESS; } - int32_t ret = CreateSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str(), &iSessionListener); + 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; } - sessionServer_.store(true); - + isSessSerCreateFlag_.store(true); return DH_SUCCESS; } @@ -153,15 +151,14 @@ void DistributedInputTransportBase::Release() } { - std::unique_lock sessionServerLock(sessServerMutex_); - if (!sessionServer_.load()) { + std::unique_lock sessionServerLock(sessSerOperMutex_); + if (!isSessSerCreateFlag_.load()) { DHLOGI("SessionServer already remove success."); } else { - (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), mySessionName_.c_str()); - sessionServer_.store(false); + (void)RemoveSessionServer(DINPUT_PKG_NAME.c_str(), localSessionName_.c_str()); + isSessSerCreateFlag_.store(false); } } - remoteDevSessionMap_.clear(); channelStatusMap_.clear(); } @@ -199,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); @@ -207,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."); -- Gitee