Skip to content

Commit

Permalink
Improve bi_web
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Sep 2, 2024
1 parent 6ca977e commit 26211f4
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 6 deletions.
11 changes: 11 additions & 0 deletions out/bi_web/include/fantasy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -611,6 +616,7 @@ class HelloWorldServer final {
}

~HelloWorldServer() {
stop();
#ifdef __cpp_impl_coroutine
if (m_pool_ptr)
m_pool_ptr->stop();
Expand Down Expand Up @@ -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<void(std::string)> error) {
Expand Down
6 changes: 6 additions & 0 deletions out/bi_web/include/impl/bi_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion out/bi_web/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ struct HelloWorldApi final {
});
}

void stop() {
m_client->stop();
}

std::unique_ptr<fantasy::HelloWorldClient> m_client;
};

Expand All @@ -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<void(const drogon::HttpResponsePtr&)>&& callback) {
nlohmann::json json;
using namespace fantasy;
Expand Down Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions out/bi_web/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
11 changes: 11 additions & 0 deletions out/include/fantasy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -611,6 +616,7 @@ class HelloWorldServer final {
}

~HelloWorldServer() {
stop();
#ifdef __cpp_impl_coroutine
if (m_pool_ptr)
m_pool_ptr->stop();
Expand Down Expand Up @@ -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<void(std::string)> error) {
Expand Down
6 changes: 6 additions & 0 deletions out/include/impl/bi_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions template/cpp/bi.inja
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public:
m_channel->start();
}

void stop() {
if (m_channel)
m_channel->stop();
}

decltype(auto) socket() {
return m_channel->socket();
}
Expand Down Expand Up @@ -332,6 +337,7 @@ public:
});
}
~{{value.callee}}() {
stop();
#ifdef __cpp_impl_coroutine
if (m_pool_ptr)
m_pool_ptr->stop();
Expand Down Expand Up @@ -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<void(std::string)> error) {
Expand Down
6 changes: 6 additions & 0 deletions template/cpp/impl/bi_channel.inja
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 22 additions & 1 deletion template/web/bi/src/main.cpp.inja
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ using Callback = std::function<void(const drogon::HttpRequestPtr&, std::function
}
{% endif %}
{% endfor %}

void stop() {
m_client->stop();
}

std::unique_ptr<{{node.property.namespace}}::{{value.caller}}> m_client;
};
{% endif %}
Expand Down Expand Up @@ -99,7 +104,7 @@ int main(int argc, char** argv) {

drogon::app().registerHandler(
"/interface",
[&] (const drogon::HttpRequestPtr& http_request_ptr, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
[&] (const drogon::HttpRequestPtr&, std::function<void(const drogon::HttpResponsePtr&)>&& callback) {
nlohmann::json json;
using namespace {{node.property.namespace}};
{% for value in node.value.interface %}
Expand Down Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions template/web/bi/xmake.lua.inja
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit 26211f4

Please sign in to comment.