From 26211f43d9956a045b5811662b42a461cc3b1e1e Mon Sep 17 00:00:00 2001 From: fantasy-peak <1356346239@qq.com> Date: Mon, 2 Sep 2024 15:54:05 +0800 Subject: [PATCH] Improve bi_web --- out/bi_web/include/fantasy.hpp | 11 +++++++++++ out/bi_web/include/impl/bi_channel.h | 6 ++++++ out/bi_web/src/main.cpp | 10 +++++++++- out/bi_web/xmake.lua | 4 ++-- out/include/fantasy.hpp | 11 +++++++++++ out/include/impl/bi_channel.h | 6 ++++++ template/cpp/bi.inja | 11 +++++++++++ template/cpp/impl/bi_channel.inja | 6 ++++++ template/web/bi/src/main.cpp.inja | 23 ++++++++++++++++++++++- template/web/bi/xmake.lua.inja | 4 ++-- 10 files changed, 86 insertions(+), 6 deletions(-) diff --git a/out/bi_web/include/fantasy.hpp b/out/bi_web/include/fantasy.hpp index 8bc46e2..4b7b470 100644 --- a/out/bi_web/include/fantasy.hpp +++ b/out/bi_web/include/fantasy.hpp @@ -98,6 +98,11 @@ class HelloWorldClient final { m_channel->start(); } + void stop() { + if (m_channel) + m_channel->stop(); + } + decltype(auto) socket() { return m_channel->socket(); } @@ -611,6 +616,7 @@ class HelloWorldServer final { } ~HelloWorldServer() { + stop(); #ifdef __cpp_impl_coroutine if (m_pool_ptr) m_pool_ptr->stop(); @@ -639,6 +645,11 @@ class HelloWorldServer final { m_channel->start(); } + void stop() { + if (m_channel) + m_channel->stop(); + } + static auto create(frpc::ChannelConfig& config, VariantHandler handler, std::function error) { diff --git a/out/bi_web/include/impl/bi_channel.h b/out/bi_web/include/impl/bi_channel.h index 4e70512..b725c71 100644 --- a/out/bi_web/include/impl/bi_channel.h +++ b/out/bi_web/include/impl/bi_channel.h @@ -59,6 +59,12 @@ class BiChannel final { } ~BiChannel() { + stop(); + } + + void stop() { + if (!m_running.load(std::memory_order_acquire)) + return; m_running.store(false, std::memory_order_release); if (m_thread.joinable()) m_thread.join(); diff --git a/out/bi_web/src/main.cpp b/out/bi_web/src/main.cpp index 6ed581e..2aca1f8 100644 --- a/out/bi_web/src/main.cpp +++ b/out/bi_web/src/main.cpp @@ -65,6 +65,10 @@ struct HelloWorldApi final { }); } + void stop() { + m_client->stop(); + } + std::unique_ptr m_client; }; @@ -85,7 +89,7 @@ int main(int argc, char** argv) { drogon::app().registerHandler( "/interface", - [&](const drogon::HttpRequestPtr& http_request_ptr, + [&](const drogon::HttpRequestPtr&, std::function&& callback) { nlohmann::json json; using namespace fantasy; @@ -139,5 +143,9 @@ int main(int argc, char** argv) { spdlog::info("{}", json); return settings; }()) + .setIntSignalHandler([&] { + drogon::app().quit(); + hello_world_api_client.stop(); + }) .run(); } diff --git a/out/bi_web/xmake.lua b/out/bi_web/xmake.lua index 1e612eb..9997cd6 100644 --- a/out/bi_web/xmake.lua +++ b/out/bi_web/xmake.lua @@ -6,7 +6,7 @@ set_xmakever("2.8.5") add_repositories("my_private_repo https://github.com/fantasy-peak/xmake-repo.git") -add_requires("nlohmann_json", "spdlog", "drogon", "asio", "cppzmq") +add_requires("nlohmann_json", "spdlog", "drogon", "asio", "cppzmq", "msgpack-cxx") set_languages("c++23") set_policy("check.auto_ignore_flags", false) @@ -16,5 +16,5 @@ add_includedirs("include") target("bi-web") set_kind("binary") add_files("src/main.cpp") - add_packages("drogon", "nlohmann_json", "spdlog", "asio", "cppzmq") + add_packages("drogon", "nlohmann_json", "spdlog", "asio", "cppzmq", "msgpack-cxx") target_end() diff --git a/out/include/fantasy.hpp b/out/include/fantasy.hpp index 8bc46e2..4b7b470 100644 --- a/out/include/fantasy.hpp +++ b/out/include/fantasy.hpp @@ -98,6 +98,11 @@ class HelloWorldClient final { m_channel->start(); } + void stop() { + if (m_channel) + m_channel->stop(); + } + decltype(auto) socket() { return m_channel->socket(); } @@ -611,6 +616,7 @@ class HelloWorldServer final { } ~HelloWorldServer() { + stop(); #ifdef __cpp_impl_coroutine if (m_pool_ptr) m_pool_ptr->stop(); @@ -639,6 +645,11 @@ class HelloWorldServer final { m_channel->start(); } + void stop() { + if (m_channel) + m_channel->stop(); + } + static auto create(frpc::ChannelConfig& config, VariantHandler handler, std::function error) { diff --git a/out/include/impl/bi_channel.h b/out/include/impl/bi_channel.h index 4e70512..b725c71 100644 --- a/out/include/impl/bi_channel.h +++ b/out/include/impl/bi_channel.h @@ -59,6 +59,12 @@ class BiChannel final { } ~BiChannel() { + stop(); + } + + void stop() { + if (!m_running.load(std::memory_order_acquire)) + return; m_running.store(false, std::memory_order_release); if (m_thread.joinable()) m_thread.join(); diff --git a/template/cpp/bi.inja b/template/cpp/bi.inja index d2126d2..201bff1 100644 --- a/template/cpp/bi.inja +++ b/template/cpp/bi.inja @@ -44,6 +44,11 @@ public: m_channel->start(); } + void stop() { + if (m_channel) + m_channel->stop(); + } + decltype(auto) socket() { return m_channel->socket(); } @@ -332,6 +337,7 @@ public: }); } ~{{value.callee}}() { + stop(); #ifdef __cpp_impl_coroutine if (m_pool_ptr) m_pool_ptr->stop(); @@ -359,6 +365,11 @@ public: m_channel->start(); } + void stop() { + if (m_channel) + m_channel->stop(); + } + static auto create(frpc::ChannelConfig& config, VariantHandler handler, std::function error) { diff --git a/template/cpp/impl/bi_channel.inja b/template/cpp/impl/bi_channel.inja index af7cbbe..ecb4ec6 100644 --- a/template/cpp/impl/bi_channel.inja +++ b/template/cpp/impl/bi_channel.inja @@ -54,6 +54,12 @@ public: init_socket(config); } ~BiChannel() { + stop(); + } + + void stop() { + if (!m_running.load(std::memory_order_acquire)) + return; m_running.store(false, std::memory_order_release); if (m_thread.joinable()) m_thread.join(); diff --git a/template/web/bi/src/main.cpp.inja b/template/web/bi/src/main.cpp.inja index 183da0c..97a9b18 100644 --- a/template/web/bi/src/main.cpp.inja +++ b/template/web/bi/src/main.cpp.inja @@ -60,6 +60,11 @@ using Callback = std::functionstop(); + } + std::unique_ptr<{{node.property.namespace}}::{{value.caller}}> m_client; }; {% endif %} @@ -99,7 +104,7 @@ int main(int argc, char** argv) { drogon::app().registerHandler( "/interface", - [&] (const drogon::HttpRequestPtr& http_request_ptr, std::function&& callback) { + [&] (const drogon::HttpRequestPtr&, std::function&& callback) { nlohmann::json json; using namespace {{node.property.namespace}}; {% for value in node.value.interface %} @@ -150,5 +155,21 @@ int main(int argc, char** argv) { spdlog::info("{}", json); return settings; }()) + .setIntSignalHandler([&] { + drogon::app().quit(); + {% for value in node.value.interface %} + {% if value.pattern == "bi" %} + {% set has_web="false" %} + {% for func in value.definitions %} + {% if existsIn(func, "web") == true %} + {% set has_web="true" %} + {% endif %} + {% endfor %} + {% if has_web == "true" %} + {{_snake(value.node_name)}}_client.stop(); + {% endif %} + {% endif %} + {% endfor %} + }) .run(); } diff --git a/template/web/bi/xmake.lua.inja b/template/web/bi/xmake.lua.inja index d86c428..3dbc3f9 100644 --- a/template/web/bi/xmake.lua.inja +++ b/template/web/bi/xmake.lua.inja @@ -6,7 +6,7 @@ set_xmakever("2.8.5") add_repositories("my_private_repo https://github.com/fantasy-peak/xmake-repo.git") -add_requires("nlohmann_json", "spdlog", "drogon", "asio", "cppzmq") +add_requires("nlohmann_json", "spdlog", "drogon", "asio", "cppzmq", "msgpack-cxx") set_languages("c++23") set_policy("check.auto_ignore_flags", false) @@ -16,5 +16,5 @@ add_includedirs("include") target("bi-web") set_kind("binary") add_files("src/main.cpp") - add_packages("drogon", "nlohmann_json", "spdlog", "asio", "cppzmq") + add_packages("drogon", "nlohmann_json", "spdlog", "asio", "cppzmq", "msgpack-cxx") target_end()