From ff74aa33969a3e693ecc06f19e0eed336b571eb1 Mon Sep 17 00:00:00 2001 From: fantasy-peak <1356346239@qq.com> Date: Thu, 22 Aug 2024 09:19:39 +0800 Subject: [PATCH] Improve internal implementation --- out/bi_web/include/fantasy.hpp | 64 +++++++++++++++++++-- out/bi_web/include/impl/asio_context_pool.h | 11 +++- out/bi_web/include/impl/coroutine.h | 5 +- out/bi_web/include/impl/utils.h | 2 + out/include/fantasy.hpp | 64 +++++++++++++++++++-- out/include/impl/asio_context_pool.h | 11 +++- out/include/impl/coroutine.h | 5 +- out/include/impl/utils.h | 2 + template/cpp/bi.inja | 29 ++++++++-- template/cpp/bi_stream.inja | 16 ++++++ template/cpp/impl/asio_context_pool.inja | 34 +++++++---- template/cpp/impl/coroutine.h.inja | 5 +- template/cpp/impl/utils.inja | 2 + template/cpp/uni.inja | 19 ++++++ 14 files changed, 227 insertions(+), 42 deletions(-) diff --git a/out/bi_web/include/fantasy.hpp b/out/bi_web/include/fantasy.hpp index 1136036..8bc46e2 100644 --- a/out/bi_web/include/fantasy.hpp +++ b/out/bi_web/include/fantasy.hpp @@ -57,7 +57,7 @@ namespace fantasy { class HelloWorldClient final { public: HelloWorldClient(const frpc::ChannelConfig& config, - std::function error) + const std::function& error) : m_channel(std::make_unique( config, error, @@ -68,7 +68,7 @@ class HelloWorldClient final { HelloWorldClient(const frpc::ChannelConfig& config, const std::shared_ptr& context_ptr, const std::shared_ptr& socket_ptr, - std::function error) + const std::function& error) : m_channel(std::make_unique( config, context_ptr, @@ -80,7 +80,7 @@ class HelloWorldClient final { HelloWorldClient(const frpc::ChannelConfig& config, const std::shared_ptr& context_ptr, - std::function error) + const std::function& error) : m_channel(std::make_unique( config, context_ptr, @@ -89,6 +89,11 @@ class HelloWorldClient final { m_error(error) { } + HelloWorldClient(const HelloWorldClient&) = delete; + HelloWorldClient& operator=(const HelloWorldClient&) = delete; + HelloWorldClient(HelloWorldClient&&) = delete; + HelloWorldClient& operator=(HelloWorldClient&&) = delete; + void start() { m_channel->start(); } @@ -178,7 +183,7 @@ class HelloWorldClient final { std::optional date, frpc::DateTime date_time) mutable { auto handler_ptr = - std::make_shared(std::move(handler)); + std::make_shared(std::forward(handler)); this->hello_world( std::move(bank_info), std::move(bank_name), @@ -239,7 +244,7 @@ class HelloWorldClient final { frpc::DateTime date_time, const auto& timeout) mutable { auto handler_ptr = - std::make_shared(std::move(handler)); + std::make_shared(std::forward(handler)); this->hello_world( std::move(bank_info), std::move(bank_name), @@ -473,6 +478,9 @@ class HelloWorldClient final { }; struct HelloWorldServerHandler { + HelloWorldServerHandler() = default; + virtual ~HelloWorldServerHandler() = default; + virtual void hello_world( BankInfo bank_info, std::string bank_name, @@ -485,6 +493,9 @@ struct HelloWorldServerHandler { }; struct AsioCoroHelloWorldServerHandler { + AsioCoroHelloWorldServerHandler() = default; + virtual ~AsioCoroHelloWorldServerHandler() = default; + #ifdef __cpp_impl_coroutine virtual asio::awaitable hello_world( BankInfo bank_info, @@ -509,6 +520,9 @@ struct AsioCoroHelloWorldServerHandler { }; struct FrpcCoroHelloWorldServerHandler { + FrpcCoroHelloWorldServerHandler() = default; + virtual ~FrpcCoroHelloWorldServerHandler() = default; + #ifdef __cpp_impl_coroutine virtual frpc::Task hello_world( BankInfo bank_info, @@ -603,6 +617,11 @@ class HelloWorldServer final { #endif } + HelloWorldServer(const HelloWorldServer&) = delete; + HelloWorldServer& operator=(const HelloWorldServer&) = delete; + HelloWorldServer(HelloWorldServer&&) = delete; + HelloWorldServer& operator=(HelloWorldServer&&) = delete; + auto& socket() { return m_channel->socket(); } @@ -843,11 +862,17 @@ MSGPACK_ADD_ENUM(fantasy::HelloWorldSenderHelloWorldReceiver) namespace fantasy { struct HelloWorldReceiverHandler { + HelloWorldReceiverHandler() = default; + virtual ~HelloWorldReceiverHandler() = default; + virtual void hello_world(std::string in) noexcept = 0; virtual void notice(int32_t in, std::string info) noexcept = 0; }; struct AsioCoroHelloWorldReceiverHandler { + AsioCoroHelloWorldReceiverHandler() = default; + virtual ~AsioCoroHelloWorldReceiverHandler() = default; + #ifdef __cpp_impl_coroutine virtual asio::awaitable hello_world(std::string in) noexcept = 0; virtual asio::awaitable notice(int32_t in, @@ -859,6 +884,9 @@ struct AsioCoroHelloWorldReceiverHandler { }; struct FrpcCoroHelloWorldReceiverHandler { + FrpcCoroHelloWorldReceiverHandler() = default; + virtual ~FrpcCoroHelloWorldReceiverHandler() = default; + #ifdef __cpp_impl_coroutine virtual frpc::Task hello_world(std::string in) noexcept = 0; virtual frpc::Task notice(int32_t in, std::string info) noexcept = 0; @@ -931,6 +959,11 @@ class HelloWorldReceiver final { #endif } + HelloWorldReceiver(const HelloWorldReceiver&) = delete; + HelloWorldReceiver& operator=(const HelloWorldReceiver&) = delete; + HelloWorldReceiver(HelloWorldReceiver&&) = delete; + HelloWorldReceiver& operator=(HelloWorldReceiver&&) = delete; + void start() { m_channel->start(); } @@ -1126,6 +1159,11 @@ class HelloWorldSender final { ~HelloWorldSender() { } + HelloWorldSender(const HelloWorldSender&) = delete; + HelloWorldSender& operator=(const HelloWorldSender&) = delete; + HelloWorldSender(HelloWorldSender&&) = delete; + HelloWorldSender& operator=(HelloWorldSender&&) = delete; + static auto create(frpc::ChannelConfig& config) { if ((config.socktype != zmq::socket_type::pub) && config.socktype != zmq::socket_type::push) @@ -1291,6 +1329,11 @@ class StreamClient final { m_pool_ptr->stop(); } + StreamClient(const StreamClient&) = delete; + StreamClient& operator=(const StreamClient&) = delete; + StreamClient(StreamClient&&) = delete; + StreamClient& operator=(StreamClient&&) = delete; + void start() { m_channel->start(); } @@ -1430,6 +1473,9 @@ class StreamClient final { }; struct StreamServerHandler { + StreamServerHandler() = default; + virtual ~StreamServerHandler() = default; + virtual void hello_world( std::shared_ptr>, @@ -1437,6 +1483,9 @@ struct StreamServerHandler { }; struct CoroStreamServerHandler { + CoroStreamServerHandler() = default; + virtual ~CoroStreamServerHandler() = default; + #ifdef __cpp_impl_coroutine virtual asio::awaitable hello_world( std::shared_ptrstop(); } + StreamServer(const StreamServer&) = delete; + StreamServer& operator=(const StreamServer&) = delete; + StreamServer(StreamServer&&) = delete; + StreamServer& operator=(StreamServer&&) = delete; + decltype(auto) socket() { return m_channel->socket(); } diff --git a/out/bi_web/include/impl/asio_context_pool.h b/out/bi_web/include/impl/asio_context_pool.h index 04548c3..2991481 100644 --- a/out/bi_web/include/impl/asio_context_pool.h +++ b/out/bi_web/include/impl/asio_context_pool.h @@ -3,6 +3,9 @@ #ifndef _FRPC_CONTEXT_POOL_H_ #define _FRPC_CONTEXT_POOL_H_ +#include +#include + #ifdef FRPC_USE_BOOST_ASIO #include #include @@ -38,12 +41,16 @@ class ContextPool final { void start() { for (auto& context : m_io_contexts) - m_threads.emplace_back(std::jthread([&] { context->run(); })); + m_threads.emplace_back(std::thread([&] { context->run(); })); } void stop() { for (auto& context_ptr : m_io_contexts) context_ptr->stop(); + for (auto& thread : m_threads) { + if (thread.joinable()) + thread.join(); + } } asio::io_context& getIoContext() { @@ -62,7 +69,7 @@ class ContextPool final { std::vector> m_io_contexts; std::list m_work; std::atomic_uint64_t m_next_io_context; - std::vector m_threads; + std::vector m_threads; }; } // namespace frpc diff --git a/out/bi_web/include/impl/coroutine.h b/out/bi_web/include/impl/coroutine.h index c2451f5..c8b20a3 100644 --- a/out/bi_web/include/impl/coroutine.h +++ b/out/bi_web/include/impl/coroutine.h @@ -4,15 +4,12 @@ #define _FRPC_COROUTINE_H_ #include -#include #include -#include #include #include -#include -#include #include #include +#include namespace frpc { diff --git a/out/bi_web/include/impl/utils.h b/out/bi_web/include/impl/utils.h index adb4a25..3e56ee6 100644 --- a/out/bi_web/include/impl/utils.h +++ b/out/bi_web/include/impl/utils.h @@ -7,7 +7,9 @@ #include #include +#include #include +#include namespace nlohmann { diff --git a/out/include/fantasy.hpp b/out/include/fantasy.hpp index 1136036..8bc46e2 100644 --- a/out/include/fantasy.hpp +++ b/out/include/fantasy.hpp @@ -57,7 +57,7 @@ namespace fantasy { class HelloWorldClient final { public: HelloWorldClient(const frpc::ChannelConfig& config, - std::function error) + const std::function& error) : m_channel(std::make_unique( config, error, @@ -68,7 +68,7 @@ class HelloWorldClient final { HelloWorldClient(const frpc::ChannelConfig& config, const std::shared_ptr& context_ptr, const std::shared_ptr& socket_ptr, - std::function error) + const std::function& error) : m_channel(std::make_unique( config, context_ptr, @@ -80,7 +80,7 @@ class HelloWorldClient final { HelloWorldClient(const frpc::ChannelConfig& config, const std::shared_ptr& context_ptr, - std::function error) + const std::function& error) : m_channel(std::make_unique( config, context_ptr, @@ -89,6 +89,11 @@ class HelloWorldClient final { m_error(error) { } + HelloWorldClient(const HelloWorldClient&) = delete; + HelloWorldClient& operator=(const HelloWorldClient&) = delete; + HelloWorldClient(HelloWorldClient&&) = delete; + HelloWorldClient& operator=(HelloWorldClient&&) = delete; + void start() { m_channel->start(); } @@ -178,7 +183,7 @@ class HelloWorldClient final { std::optional date, frpc::DateTime date_time) mutable { auto handler_ptr = - std::make_shared(std::move(handler)); + std::make_shared(std::forward(handler)); this->hello_world( std::move(bank_info), std::move(bank_name), @@ -239,7 +244,7 @@ class HelloWorldClient final { frpc::DateTime date_time, const auto& timeout) mutable { auto handler_ptr = - std::make_shared(std::move(handler)); + std::make_shared(std::forward(handler)); this->hello_world( std::move(bank_info), std::move(bank_name), @@ -473,6 +478,9 @@ class HelloWorldClient final { }; struct HelloWorldServerHandler { + HelloWorldServerHandler() = default; + virtual ~HelloWorldServerHandler() = default; + virtual void hello_world( BankInfo bank_info, std::string bank_name, @@ -485,6 +493,9 @@ struct HelloWorldServerHandler { }; struct AsioCoroHelloWorldServerHandler { + AsioCoroHelloWorldServerHandler() = default; + virtual ~AsioCoroHelloWorldServerHandler() = default; + #ifdef __cpp_impl_coroutine virtual asio::awaitable hello_world( BankInfo bank_info, @@ -509,6 +520,9 @@ struct AsioCoroHelloWorldServerHandler { }; struct FrpcCoroHelloWorldServerHandler { + FrpcCoroHelloWorldServerHandler() = default; + virtual ~FrpcCoroHelloWorldServerHandler() = default; + #ifdef __cpp_impl_coroutine virtual frpc::Task hello_world( BankInfo bank_info, @@ -603,6 +617,11 @@ class HelloWorldServer final { #endif } + HelloWorldServer(const HelloWorldServer&) = delete; + HelloWorldServer& operator=(const HelloWorldServer&) = delete; + HelloWorldServer(HelloWorldServer&&) = delete; + HelloWorldServer& operator=(HelloWorldServer&&) = delete; + auto& socket() { return m_channel->socket(); } @@ -843,11 +862,17 @@ MSGPACK_ADD_ENUM(fantasy::HelloWorldSenderHelloWorldReceiver) namespace fantasy { struct HelloWorldReceiverHandler { + HelloWorldReceiverHandler() = default; + virtual ~HelloWorldReceiverHandler() = default; + virtual void hello_world(std::string in) noexcept = 0; virtual void notice(int32_t in, std::string info) noexcept = 0; }; struct AsioCoroHelloWorldReceiverHandler { + AsioCoroHelloWorldReceiverHandler() = default; + virtual ~AsioCoroHelloWorldReceiverHandler() = default; + #ifdef __cpp_impl_coroutine virtual asio::awaitable hello_world(std::string in) noexcept = 0; virtual asio::awaitable notice(int32_t in, @@ -859,6 +884,9 @@ struct AsioCoroHelloWorldReceiverHandler { }; struct FrpcCoroHelloWorldReceiverHandler { + FrpcCoroHelloWorldReceiverHandler() = default; + virtual ~FrpcCoroHelloWorldReceiverHandler() = default; + #ifdef __cpp_impl_coroutine virtual frpc::Task hello_world(std::string in) noexcept = 0; virtual frpc::Task notice(int32_t in, std::string info) noexcept = 0; @@ -931,6 +959,11 @@ class HelloWorldReceiver final { #endif } + HelloWorldReceiver(const HelloWorldReceiver&) = delete; + HelloWorldReceiver& operator=(const HelloWorldReceiver&) = delete; + HelloWorldReceiver(HelloWorldReceiver&&) = delete; + HelloWorldReceiver& operator=(HelloWorldReceiver&&) = delete; + void start() { m_channel->start(); } @@ -1126,6 +1159,11 @@ class HelloWorldSender final { ~HelloWorldSender() { } + HelloWorldSender(const HelloWorldSender&) = delete; + HelloWorldSender& operator=(const HelloWorldSender&) = delete; + HelloWorldSender(HelloWorldSender&&) = delete; + HelloWorldSender& operator=(HelloWorldSender&&) = delete; + static auto create(frpc::ChannelConfig& config) { if ((config.socktype != zmq::socket_type::pub) && config.socktype != zmq::socket_type::push) @@ -1291,6 +1329,11 @@ class StreamClient final { m_pool_ptr->stop(); } + StreamClient(const StreamClient&) = delete; + StreamClient& operator=(const StreamClient&) = delete; + StreamClient(StreamClient&&) = delete; + StreamClient& operator=(StreamClient&&) = delete; + void start() { m_channel->start(); } @@ -1430,6 +1473,9 @@ class StreamClient final { }; struct StreamServerHandler { + StreamServerHandler() = default; + virtual ~StreamServerHandler() = default; + virtual void hello_world( std::shared_ptr>, @@ -1437,6 +1483,9 @@ struct StreamServerHandler { }; struct CoroStreamServerHandler { + CoroStreamServerHandler() = default; + virtual ~CoroStreamServerHandler() = default; + #ifdef __cpp_impl_coroutine virtual asio::awaitable hello_world( std::shared_ptrstop(); } + StreamServer(const StreamServer&) = delete; + StreamServer& operator=(const StreamServer&) = delete; + StreamServer(StreamServer&&) = delete; + StreamServer& operator=(StreamServer&&) = delete; + decltype(auto) socket() { return m_channel->socket(); } diff --git a/out/include/impl/asio_context_pool.h b/out/include/impl/asio_context_pool.h index 04548c3..2991481 100644 --- a/out/include/impl/asio_context_pool.h +++ b/out/include/impl/asio_context_pool.h @@ -3,6 +3,9 @@ #ifndef _FRPC_CONTEXT_POOL_H_ #define _FRPC_CONTEXT_POOL_H_ +#include +#include + #ifdef FRPC_USE_BOOST_ASIO #include #include @@ -38,12 +41,16 @@ class ContextPool final { void start() { for (auto& context : m_io_contexts) - m_threads.emplace_back(std::jthread([&] { context->run(); })); + m_threads.emplace_back(std::thread([&] { context->run(); })); } void stop() { for (auto& context_ptr : m_io_contexts) context_ptr->stop(); + for (auto& thread : m_threads) { + if (thread.joinable()) + thread.join(); + } } asio::io_context& getIoContext() { @@ -62,7 +69,7 @@ class ContextPool final { std::vector> m_io_contexts; std::list m_work; std::atomic_uint64_t m_next_io_context; - std::vector m_threads; + std::vector m_threads; }; } // namespace frpc diff --git a/out/include/impl/coroutine.h b/out/include/impl/coroutine.h index c2451f5..c8b20a3 100644 --- a/out/include/impl/coroutine.h +++ b/out/include/impl/coroutine.h @@ -4,15 +4,12 @@ #define _FRPC_COROUTINE_H_ #include -#include #include -#include #include #include -#include -#include #include #include +#include namespace frpc { diff --git a/out/include/impl/utils.h b/out/include/impl/utils.h index adb4a25..3e56ee6 100644 --- a/out/include/impl/utils.h +++ b/out/include/impl/utils.h @@ -7,7 +7,9 @@ #include #include +#include #include +#include namespace nlohmann { diff --git a/template/cpp/bi.inja b/template/cpp/bi.inja index e3aba4c..d2126d2 100644 --- a/template/cpp/bi.inja +++ b/template/cpp/bi.inja @@ -17,24 +17,29 @@ namespace {{node.property.namespace}} { class {{value.caller}} final { public: {{value.caller}}(const frpc::ChannelConfig& config, - std::function error) + const std::function& error) : m_channel(std::make_unique(config, error, [this](auto& recv_msgs) mutable { dispatch(recv_msgs); })) , m_error(error) { } {{value.caller}}(const frpc::ChannelConfig& config, const std::shared_ptr& context_ptr, const std::shared_ptr& socket_ptr, - std::function error) + const std::function& error) : m_channel(std::make_unique(config, context_ptr, socket_ptr, error, [this](auto& recv_msgs) mutable { dispatch(recv_msgs); })) , m_error(error) { } {{value.caller}}(const frpc::ChannelConfig& config, const std::shared_ptr& context_ptr, - std::function error) + const std::function& error) : m_channel(std::make_unique(config, context_ptr, error, [this](auto& recv_msgs) mutable { dispatch(recv_msgs); })) , m_error(error) { } + {{value.caller}}(const {{value.caller}}&) = delete; + {{value.caller}}& operator=(const {{value.caller}}&) = delete; + {{value.caller}}({{value.caller}}&&) = delete; + {{value.caller}}& operator=({{value.caller}}&&) = delete; + void start() { m_channel->start(); } @@ -85,7 +90,7 @@ public: auto {{func.func_name}}_coro({{_format_args(func.inputs)}}, CompletionToken&& token) { return asio::async_initiate( [this](Handler&& handler, {{_format_args(func.inputs)}}) mutable { - auto handler_ptr = std::make_shared(std::move(handler)); + auto handler_ptr = std::make_shared(std::forward(handler)); this->{{func.func_name}}( {{_format_args_name_and_move(func.inputs)}}, [handler_ptr = std::move(handler_ptr)]({{_format_args(func.outputs)}}) mutable { @@ -103,7 +108,7 @@ public: auto {{func.func_name}}_coro({{_format_args(func.inputs)}}, const std::chrono::milliseconds& timeout, CompletionToken&& token) { return asio::async_initiate>)>( [this](Handler&& handler, {{_format_args(func.inputs)}}, const auto& timeout) mutable { - auto handler_ptr = std::make_shared(std::move(handler)); + auto handler_ptr = std::make_shared(std::forward(handler)); this->{{func.func_name}}( {{_format_args_name_and_move(func.inputs)}}, [handler_ptr]({{_format_args(func.outputs)}}) mutable { @@ -242,12 +247,18 @@ private: }; struct {{value.callee}}Handler { + {{value.callee}}Handler() = default; + virtual ~{{value.callee}}Handler() = default; + {% for func in value.definitions %} virtual void {{func.func_name}}({{_format_args(func.inputs)}}, std::function cb) noexcept = 0; {% endfor %} }; struct AsioCoro{{value.callee}}Handler { + AsioCoro{{value.callee}}Handler() = default; + virtual ~AsioCoro{{value.callee}}Handler() = default; + #ifdef __cpp_impl_coroutine {% for func in value.definitions %} virtual asio::awaitable {{func.func_name}}({{_format_args(func.inputs)}}, std::function cb) noexcept = 0; @@ -260,6 +271,9 @@ struct AsioCoro{{value.callee}}Handler { }; struct FrpcCoro{{value.callee}}Handler { + FrpcCoro{{value.callee}}Handler() = default; + virtual ~FrpcCoro{{value.callee}}Handler() = default; + #ifdef __cpp_impl_coroutine {% for func in value.definitions %} virtual frpc::Task {{func.func_name}}({{_format_args(func.inputs)}}, std::function cb) noexcept = 0; @@ -324,6 +338,11 @@ public: #endif } + {{value.callee}}(const {{value.callee}}&) = delete; + {{value.callee}}& operator=(const {{value.callee}}&) = delete; + {{value.callee}}({{value.callee}}&&) = delete; + {{value.callee}}& operator=({{value.callee}}&&) = delete; + auto& socket() { return m_channel->socket(); } diff --git a/template/cpp/bi_stream.inja b/template/cpp/bi_stream.inja index 5dfd58c..eebb2b6 100644 --- a/template/cpp/bi_stream.inja +++ b/template/cpp/bi_stream.inja @@ -56,6 +56,11 @@ public: m_pool_ptr->stop(); } + {{value.caller}}(const {{value.caller}}&) = delete; + {{value.caller}}& operator=(const {{value.caller}}&) = delete; + {{value.caller}}({{value.caller}}&&) = delete; + {{value.caller}}& operator=({{value.caller}}&&) = delete; + void start() { m_channel->start(); } @@ -177,6 +182,9 @@ private: }; struct {{value.callee}}Handler { + {{value.callee}}Handler() = default; + virtual ~{{value.callee}}Handler() = default; + {% for func in value.definitions %} virtual void {{func.func_name}}(std::shared_ptr>, std::shared_ptr>) noexcept = 0; @@ -184,6 +192,9 @@ struct {{value.callee}}Handler { }; struct Coro{{value.callee}}Handler { + Coro{{value.callee}}Handler() = default; + virtual ~Coro{{value.callee}}Handler() = default; + {% for func in value.definitions %} #ifdef __cpp_impl_coroutine virtual asio::awaitable {{func.func_name}}(std::shared_ptr>, @@ -235,6 +246,11 @@ public: m_pool_ptr->stop(); } + {{value.callee}}(const {{value.callee}}&) = delete; + {{value.callee}}& operator=(const {{value.callee}}&) = delete; + {{value.callee}}({{value.callee}}&&) = delete; + {{value.callee}}& operator=({{value.callee}}&&) = delete; + decltype(auto) socket() { return m_channel->socket(); } diff --git a/template/cpp/impl/asio_context_pool.inja b/template/cpp/impl/asio_context_pool.inja index 5123f51..12ed8db 100644 --- a/template/cpp/impl/asio_context_pool.inja +++ b/template/cpp/impl/asio_context_pool.inja @@ -3,16 +3,21 @@ #ifndef _FRPC_CONTEXT_POOL_H_ #define _FRPC_CONTEXT_POOL_H_ +#include +#include + #ifdef FRPC_USE_BOOST_ASIO #include #include namespace asio = boost::asio; + namespace frpc { using error_code = boost::system::error_code; } #else #include #include + namespace frpc { using error_code = asio::error_code; } @@ -21,47 +26,54 @@ using error_code = asio::error_code; namespace frpc { class ContextPool final { -public: - ContextPool(std::size_t pool_size) - : m_next_io_context(0) { + public: + ContextPool(std::size_t pool_size) : m_next_io_context(0) { if (pool_size == 0) throw std::runtime_error("ContextPool size is 0"); for (std::size_t i = 0; i < pool_size; ++i) { auto io_context_ptr = std::make_shared(); m_io_contexts.emplace_back(io_context_ptr); m_work.emplace_back( - asio::require(io_context_ptr->get_executor(), asio::execution::outstanding_work.tracked)); + asio::require(io_context_ptr->get_executor(), + asio::execution::outstanding_work.tracked)); } } void start() { for (auto& context : m_io_contexts) - m_threads.emplace_back(std::jthread([&] { context->run(); })); + m_threads.emplace_back(std::thread([&] { context->run(); })); } void stop() { for (auto& context_ptr : m_io_contexts) context_ptr->stop(); + for (auto& thread : m_threads) { + if (thread.joinable()) + thread.join(); + } } asio::io_context& getIoContext() { - size_t index = m_next_io_context.fetch_add(1, std::memory_order_relaxed); + size_t index = + m_next_io_context.fetch_add(1, std::memory_order_relaxed); return *m_io_contexts[index % m_io_contexts.size()]; } auto& getIoContextPtr() { - size_t index = m_next_io_context.fetch_add(1, std::memory_order_relaxed); + size_t index = + m_next_io_context.fetch_add(1, std::memory_order_relaxed); return m_io_contexts[index % m_io_contexts.size()]; } -private: + private: std::vector> m_io_contexts; std::list m_work; std::atomic_uint64_t m_next_io_context; - std::vector m_threads; + std::vector m_threads; }; -} // frpc +} // namespace frpc + +#endif // _FRPC_CONTEXT_POOL_H_ -#endif // _FRPC_CONTEXT_POOL_H_ diff --git a/template/cpp/impl/coroutine.h.inja b/template/cpp/impl/coroutine.h.inja index 8d0f487..1f2917b 100644 --- a/template/cpp/impl/coroutine.h.inja +++ b/template/cpp/impl/coroutine.h.inja @@ -4,15 +4,12 @@ #define _FRPC_COROUTINE_H_ #include -#include #include -#include #include #include -#include -#include #include #include +#include namespace frpc { diff --git a/template/cpp/impl/utils.inja b/template/cpp/impl/utils.inja index 5aa673a..2113234 100644 --- a/template/cpp/impl/utils.inja +++ b/template/cpp/impl/utils.inja @@ -7,7 +7,9 @@ #include #include +#include #include +#include namespace nlohmann { diff --git a/template/cpp/uni.inja b/template/cpp/uni.inja index 4d9be71..b136e43 100644 --- a/template/cpp/uni.inja +++ b/template/cpp/uni.inja @@ -35,12 +35,18 @@ MSGPACK_ADD_ENUM({{node.property.namespace}}::{{value.caller}}{{value.callee}}) namespace {{node.property.namespace}} { struct {{value.callee}}Handler { + {{value.callee}}Handler() = default; + virtual ~{{value.callee}}Handler() = default; + {% for func in value.definitions %} virtual void {{func.func_name}}({{_format_args(func.inputs)}}) noexcept = 0; {% endfor %} }; struct AsioCoro{{value.callee}}Handler { + AsioCoro{{value.callee}}Handler() = default; + virtual ~AsioCoro{{value.callee}}Handler() = default; + #ifdef __cpp_impl_coroutine {% for func in value.definitions %} virtual asio::awaitable {{func.func_name}}({{_format_args(func.inputs)}}) noexcept = 0; @@ -54,6 +60,9 @@ struct AsioCoro{{value.callee}}Handler { }; struct FrpcCoro{{value.callee}}Handler { + FrpcCoro{{value.callee}}Handler() = default; + virtual ~FrpcCoro{{value.callee}}Handler() = default; + #ifdef __cpp_impl_coroutine {% for func in value.definitions %} virtual frpc::Task {{func.func_name}}({{_format_args(func.inputs)}}) noexcept = 0; @@ -111,6 +120,11 @@ public: #endif } + {{value.callee}}(const {{value.callee}}&) = delete; + {{value.callee}}& operator=(const {{value.callee}}&) = delete; + {{value.callee}}({{value.callee}}&&) = delete; + {{value.callee}}& operator=({{value.callee}}&&) = delete; + void start() { m_channel->start(); } @@ -241,6 +255,11 @@ public: ~{{value.caller}}() {} + {{value.caller}}(const {{value.caller}}&) = delete; + {{value.caller}}& operator=(const {{value.caller}}&) = delete; + {{value.caller}}({{value.caller}}&&) = delete; + {{value.caller}}& operator=({{value.caller}}&&) = delete; + static auto create(frpc::ChannelConfig& config) { if ((config.socktype != zmq::socket_type::pub) && config.socktype != zmq::socket_type::push) config.socktype = zmq::socket_type::pub;