From 831e99bc28fba43f8f7d75634f3f0e764e6512a3 Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Sun, 20 Oct 2024 07:30:35 +0800 Subject: [PATCH] boost-ssl not depends on tbb --- .../bcos-boostssl/websocket/WsInitializer.cpp | 4 ++++ bcos-boostssl/bcos-boostssl/websocket/WsService.cpp | 6 ++---- bcos-boostssl/bcos-boostssl/websocket/WsService.h | 6 ++++-- bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp | 12 ++++++------ bcos-boostssl/bcos-boostssl/websocket/WsSession.h | 8 +++----- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp index 6ff478960b..5c882438f0 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsInitializer.cpp @@ -68,6 +68,10 @@ void WsInitializer::initWsService(WsService::Ptr _wsService) auto builder = std::make_shared(); + // set the threadPool + auto threadPool = std::make_shared("ws-pool", _config->threadPoolSize()); + _wsService->setThreadPool(threadPool); + std::shared_ptr srvCtx = nullptr; std::shared_ptr clientCtx = nullptr; if (!_config->disableSsl()) diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp index 3af0bb1bb8..4fb3b3c586 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp @@ -53,7 +53,6 @@ WsService::WsService() WsService::~WsService() { stop(); - m_taskGroup.wait(); WEBSOCKET_SERVICE(INFO) << LOG_KV("[DELOBJ][WsService]", this); } @@ -141,8 +140,7 @@ void WsService::stop() { m_ioservicePool->stop(); } - m_taskGroup.cancel(); - m_taskGroup.wait(); + m_threadPool->stop(); if (m_statTimer) { @@ -390,7 +388,7 @@ std::shared_ptr WsService::newSession( _wsStreamDelegate->setMaxReadMsgSize(m_config->maxMsgSize()); std::string endPoint = _wsStreamDelegate->remoteEndpoint(); - auto session = m_sessionFactory->createSession(m_taskGroup); + auto session = m_sessionFactory->createSession(m_threadPool); session->setWsStreamDelegate(std::move(_wsStreamDelegate)); session->setIoc(m_ioservicePool->getIOService()); diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsService.h b/bcos-boostssl/bcos-boostssl/websocket/WsService.h index fd6e8617cc..a025d32e93 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsService.h +++ b/bcos-boostssl/bcos-boostssl/websocket/WsService.h @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -189,9 +188,12 @@ class WsService : public std::enable_shared_from_this return m_reconnectedPeers; } + void setThreadPool(std::shared_ptr threadPool) + { + m_threadPool = std::move(threadPool); + } private: bool m_running{false}; - tbb::task_group m_taskGroup; int32_t m_waitConnectFinishTimeout = 30000; diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp index f29dafa75a..c4f74497d1 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp @@ -41,8 +41,8 @@ using namespace bcos::boostssl; using namespace bcos::boostssl::ws; using namespace bcos::boostssl::http; -WsSession::WsSession(tbb::task_group& taskGroup) - : m_taskGroup(taskGroup) +WsSession::WsSession(std::shared_ptr threadPool) + : m_threadPool(std::move(threadPool)) { WEBSOCKET_SESSION(INFO) << LOG_KV("[NEWOBJ][WSSESSION]", this); @@ -86,7 +86,7 @@ void WsSession::drop(uint32_t _reason) WEBSOCKET_SESSION(TRACE) << LOG_DESC("the session has been disconnected") << LOG_KV("seq", cbEntry.first); - m_taskGroup.run([callback = std::move(callback), error]() { + m_threadPool->enqueue([callback = std::move(callback), error]() { callback->respCallBack(error, nullptr, nullptr); }); } @@ -103,7 +103,7 @@ void WsSession::drop(uint32_t _reason) m_wsStreamDelegate->close(); } - m_taskGroup.run([self]() { + m_threadPool->enqueue([self]() { auto session = self.lock(); if (session) { @@ -190,7 +190,7 @@ void WsSession::onReadPacket() void WsSession::onMessage(bcos::boostssl::MessageFace::Ptr _message) { // task enqueue - m_taskGroup.run([self = weak_from_this(), _message = std::move(_message)]() { + m_threadPool->enqueue([self = weak_from_this(), _message = std::move(_message)]() { auto session = self.lock(); if (!session) { @@ -478,7 +478,7 @@ void WsSession::onRespTimeout(const boost::system::error_code& _error, const std WEBSOCKET_SESSION(WARNING) << LOG_BADGE("onRespTimeout") << LOG_KV("seq", _seq); auto error = BCOS_ERROR_PTR(WsError::TimeOut, "waiting for message response timed out"); - m_taskGroup.run([callback = std::move(callback), error = std::move(error)]() { + m_threadPool->enqueue([callback = std::move(callback), error = std::move(error)]() { callback->respCallBack(error, nullptr, nullptr); }); } diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsSession.h b/bcos-boostssl/bcos-boostssl/websocket/WsSession.h index 992951178f..dcaa7f0626 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsSession.h +++ b/bcos-boostssl/bcos-boostssl/websocket/WsSession.h @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -53,7 +52,7 @@ class WsSession : public std::enable_shared_from_this, using Ptrs = std::vector>; public: - explicit WsSession(tbb::task_group& taskGroup); + explicit WsSession(std::shared_ptr threadPool); virtual ~WsSession() noexcept { @@ -180,7 +179,6 @@ class WsSession : public std::enable_shared_from_this, }; protected: - tbb::task_group& m_taskGroup; // flag for message that need to check respond packet like p2p message bool m_needCheckRspPacket = false; // @@ -231,9 +229,9 @@ class WsSessionFactory virtual ~WsSessionFactory() = default; public: - virtual WsSession::Ptr createSession(tbb::task_group& taskGroup) + virtual WsSession::Ptr createSession(std::shared_ptr threadPool) { - auto session = std::make_shared(taskGroup); + auto session = std::make_shared(threadPool); return session; } };