From 1d44c394b268c121674c2171664ad58524032888 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 20 Mar 2024 17:16:34 +0800 Subject: [PATCH] improve (#537) --- include/cinatra/ylt/coro_io/coro_io.hpp | 28 ++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/cinatra/ylt/coro_io/coro_io.hpp b/include/cinatra/ylt/coro_io/coro_io.hpp index ac2ca126..cd90df2a 100644 --- a/include/cinatra/ylt/coro_io/coro_io.hpp +++ b/include/cinatra/ylt/coro_io/coro_io.hpp @@ -260,13 +260,11 @@ inline async_simple::coro::Lazy async_handshake( #endif class period_timer : public asio::steady_timer { public: + using asio::steady_timer::steady_timer; template period_timer(coro_io::ExecutorWrapper *executor) : asio::steady_timer(executor->get_asio_executor()) {} - template - period_timer(const executor_t &executor, - const std::chrono::duration &timeout_duration) - : asio::steady_timer(executor, timeout_duration) {} + async_simple::coro::Lazy async_await() noexcept { callback_awaitor awaitor; @@ -297,10 +295,10 @@ inline async_simple::coro::Lazy sleep_for(const Duration &d) { } } -template +template struct post_helper { void operator()(auto handler) const { - asio::dispatch(e->get_asio_executor(), [this, handler]() { + asio::dispatch(e, [this, handler]() { try { if constexpr (std::is_same_v>) { func(); @@ -317,24 +315,30 @@ struct post_helper { } }); } - coro_io::ExecutorWrapper<> *e; + Executor e; Func func; }; -template +template inline async_simple::coro::Lazy< async_simple::Try::return_type>> -post(Func func, - coro_io::ExecutorWrapper<> *e = coro_io::get_global_block_executor()) { +post(Func func, Executor executor) { using R = async_simple::Try::return_type>; callback_awaitor awaitor; - - post_helper helper{e, std::move(func)}; + post_helper helper{executor, std::move(func)}; co_return co_await awaitor.await_resume(helper); } +template +inline async_simple::coro::Lazy< + async_simple::Try::return_type>> +post(Func func, + coro_io::ExecutorWrapper<> *e = coro_io::get_global_block_executor()) { + co_return co_await post(std::move(func), e->get_asio_executor()); +} + template struct coro_channel : public asio::experimental::channel {