From 090ea0803ebec20c44684c92d28c4af00ecbc10f Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sun, 10 Nov 2024 18:02:28 +0800 Subject: [PATCH] rewrite IoContext. --- .vscode/c_cpp_properties.json | 7 +++++++ Nng/Buffer.cpp | 5 +++++ Nng/Buffer.h | 3 +++ Universal/IoContext.cpp | 34 +++++++++++++++++++++++++++------- Universal/IoContext.h | 26 ++++++++------------------ 5 files changed, 50 insertions(+), 25 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index df653dd..5687ab4 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -12,6 +12,13 @@ "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "linux-gcc-x64" + }, + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**", + "E:/Projects/Libraries/boost_1_86_0_msvc2022_64bit/include/boost-1_86" + ] } ], "version": 4 diff --git a/Nng/Buffer.cpp b/Nng/Buffer.cpp index 034bdbc..0ef4d54 100644 --- a/Nng/Buffer.cpp +++ b/Nng/Buffer.cpp @@ -47,4 +47,9 @@ void *Buffer::release() { m_size = 0; return m_buffer; } + +std::string_view Buffer::data() const noexcept { + return std::string_view{reinterpret_cast(m_buffer), m_size}; +} + } // namespace Nng diff --git a/Nng/Buffer.h b/Nng/Buffer.h index c7b46b1..9b957d4 100644 --- a/Nng/Buffer.h +++ b/Nng/Buffer.h @@ -2,6 +2,7 @@ #define __BUFFER_H__ #include +#include namespace Nng { @@ -37,6 +38,8 @@ public: return reinterpret_cast(m_buffer); } + std::string_view data() const noexcept; + std::size_t size() const noexcept { return m_size; } diff --git a/Universal/IoContext.cpp b/Universal/IoContext.cpp index f37663f..0633d36 100644 --- a/Universal/IoContext.cpp +++ b/Universal/IoContext.cpp @@ -3,23 +3,43 @@ #include #include +IoContext::IoContext(int concurrency_hint) + : m_concurrency(concurrency_hint), m_ioContext(std::make_shared(concurrency_hint)) { +} + IoContext::~IoContext() { stop(); } void IoContext::stop() { + m_guarder.reset(); m_ioContext->stop(); - if (m_thread.joinable()) m_thread.join(); + if (!m_asynchronous) { + for (auto &thread : m_threads) { + if (thread.joinable()) thread.join(); + } + m_threads.clear(); + } +} + +void IoContext::run(bool asynchronous) { + using namespace boost::asio; + if (m_concurrency < 1) m_concurrency = 1; + m_asynchronous = asynchronous; + for (int i = 0; i < m_concurrency; i++) { + m_threads.push_back(std::thread(&IoContext::runIoContext, this)); + } + m_guarder = std::make_unique>(m_ioContext->get_executor()); + if (asynchronous) { + for (auto &thread : m_threads) { + if (thread.joinable()) thread.join(); + } + m_threads.clear(); + } } void IoContext::runIoContext() { - LOG(info) << "asio context started ..."; - BOOST_SCOPE_EXIT(void) { - LOG(info) << "asio context exited ..."; - } - BOOST_SCOPE_EXIT_END try { - boost::asio::executor_work_guard work(m_ioContext->get_executor()); m_ioContext->run(); } catch (const boost::log::system_error &error) { LOG(error) << error.what(); diff --git a/Universal/IoContext.h b/Universal/IoContext.h index bc53a2f..aaa579d 100644 --- a/Universal/IoContext.h +++ b/Universal/IoContext.h @@ -9,28 +9,17 @@ class IoContext { public: - enum class Mode { - Synchronous, - Asynchronous, - }; friend class Amass::Singleton; std::shared_ptr ioContext() const { return m_ioContext; } - template - void run() { - if constexpr (mode == Mode::Asynchronous) { - m_thread = std::thread(&IoContext::runIoContext, this); - } else { - runIoContext(); - } - } + void run(bool asynchronous = true) ; void stop(); ~IoContext(); /** - * @brief - * + * @brief + * * @tparam Executor boost::asio::io_context、boost::asio::strand(通过 boost::asio::make_strand(io_context); 得到) */ template @@ -47,14 +36,15 @@ public: } protected: - template - IoContext(Args &&...args) : m_ioContext(std::make_shared(std::forward(args)...)) { - } + IoContext(int concurrency); void runIoContext(); private: - std::thread m_thread; + int m_concurrency = 1; std::shared_ptr m_ioContext; + std::unique_ptr> m_guarder; + std::vector m_threads; + bool m_asynchronous=false; }; #endif // IOCONTEXT_H