From 9f4893838600f3c4140bc36da511315494caf144 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 10 Apr 2024 14:19:03 +0000 Subject: [PATCH] Add blocker to stop; Remove Guard from TaskProcessor; Add stop blocker to Server --- include/cserver/components/work_guard.hpp | 12 ++++++++++++ include/cserver/engine/components.hpp | 21 +++++++++++---------- include/cserver/server/server/server.hpp | 18 ++++++++++-------- 3 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 include/cserver/components/work_guard.hpp diff --git a/include/cserver/components/work_guard.hpp b/include/cserver/components/work_guard.hpp new file mode 100644 index 0000000..5f7183a --- /dev/null +++ b/include/cserver/components/work_guard.hpp @@ -0,0 +1,12 @@ +#pragma once +#include + +namespace cserver { + +struct StopBlocker { + boost::asio::io_service::work work; + inline constexpr StopBlocker(auto, auto& context) : + work(context.template FindComponent().ioContext) {}; +}; + +} // namespace cserver diff --git a/include/cserver/engine/components.hpp b/include/cserver/engine/components.hpp index fa97e00..8bfd936 100644 --- a/include/cserver/engine/components.hpp +++ b/include/cserver/engine/components.hpp @@ -122,23 +122,24 @@ struct AsyncConditionVariable { template inline constexpr auto InitComponents(T& ccontext) -> void { static auto& context = ccontext; - static auto& ioContext = context.template FindComponent().ioContext; + static auto& taskProcessor = context.template FindComponent(); + static auto& ioContext = taskProcessor.ioContext; static utempl::Tuple inited = TransformDependencyGraphToInited(ioContext); + auto work = make_work_guard(ioContext); [](std::index_sequence){ - (boost::asio::co_spawn(ioContext, [](utempl::Wrapper) -> cserver::Task<> { - auto& dependencies = Get(inited); + (boost::asio::co_spawn(ioContext, []() -> cserver::Task<> { + auto& dependencies = Get(inited); for(auto* flag : dependencies) { co_await flag->AsyncWait(); }; - Get(context.storage).emplace(utempl::Wrapper(T::kNames)>{}, context); - auto& componentInitFlag = GetInitFlagFor(ioContext); + Get(context.storage).emplace(utempl::Wrapper(T::kNames)>{}, context); + auto& componentInitFlag = GetInitFlagFor(ioContext); componentInitFlag.NotifyAll(); - if constexpr(requires{Get(context.storage)->Run();}) { - Get(context.storage)->Run(); + if constexpr(requires{Get(context.storage)->Run();}) { + Get(context.storage)->Run(); }; - }(utempl::Wrapper{}), boost::asio::detached), ...); + }(), boost::asio::detached), ...); }(std::make_index_sequence>()); - }; } // namespace impl @@ -156,8 +157,8 @@ struct ServiceContext { ,storage{} { }; inline constexpr auto Run() { - this->taskProcessor.Run(); impl::InitComponents(*this); + this->taskProcessor.Run(); }; template inline constexpr auto FindComponent() -> auto& { diff --git a/include/cserver/server/server/server.hpp b/include/cserver/server/server/server.hpp index cb59df2..c5d2a20 100644 --- a/include/cserver/server/server/server.hpp +++ b/include/cserver/server/server/server.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -11,7 +12,7 @@ namespace cserver::server::server { template -struct Server { +struct Server : StopBlocker { TaskProcessor& taskProcessor; utempl::Tuple&...> handlers; static constexpr utempl::ConstexprString kName = "server"; @@ -34,13 +35,14 @@ struct Server { }); }; template < - utempl::ConstexprString name, + utempl::ConstexprString Name, typename T, std::size_t... Is> - inline constexpr Server(std::index_sequence, utempl::Wrapper, T& context) : + inline constexpr Server(std::index_sequence, utempl::Wrapper name, T& context) : + StopBlocker(name, context), taskProcessor(context.template FindComponent()), handlers{context.template FindComponent(kNames)>()...}, - port(T::kConfig.template Get().template Get<"port">()) { + port(T::kConfig.template Get().template Get<"port">()) { }; inline constexpr Server(auto name, auto& context) : @@ -69,11 +71,11 @@ struct Server { http::HTTPRequest request = http::HTTPRequestParser{buffer}; bool flag = false; co_await [&](std::index_sequence) -> cserver::Task { - (co_await [&](utempl::Wrapper) -> cserver::Task { - if(request.url.path().substr(0, Get(kPaths).size()) == Get(kPaths)) { - co_await this->ProcessHandler(std::move(socket), std::move(request)); + (co_await [&] -> cserver::Task { + if(request.url.path().substr(0, Get(kPaths).size()) == Get(kPaths)) { + co_await this->ProcessHandler(std::move(socket), std::move(request)); }; - }(utempl::Wrapper{}), ...); + }(), ...); }(std::index_sequence_for()); constexpr std::string_view error404 = "HTTP/1.1 404 Not Found\r\n" "Content-Length: 0\r\n"