From 6c64a7efec4aa48155bd487a68d7516325b04985 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Tue, 9 Jul 2024 19:26:27 +0800 Subject: [PATCH] update --- include/ylt/coro_io/coro_io.hpp | 38 +++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/include/ylt/coro_io/coro_io.hpp b/include/ylt/coro_io/coro_io.hpp index 224f4f7e5..f22b91445 100644 --- a/include/ylt/coro_io/coro_io.hpp +++ b/include/ylt/coro_io/coro_io.hpp @@ -21,14 +21,11 @@ #include #include -#include "asio/error.hpp" - #if defined(YLT_ENABLE_SSL) || defined(CINATRA_ENABLE_SSL) #include #endif #include -#include #include #include #include @@ -40,13 +37,16 @@ #include #include "io_context_pool.hpp" +#if __has_include("ylt/util/type_traits.h") #include "ylt/util/type_traits.h" +#else +#include "../util/type_traits.h" +#endif #ifdef __linux__ #include #endif namespace coro_io { - template constexpr inline bool is_lazy_v = util::is_specialization_v, async_simple::coro::Lazy>; @@ -369,19 +369,37 @@ struct coro_channel using return_type = R; using ValueType = std::pair; using asio::experimental::channel::channel; + coro_channel(coro_io::ExecutorWrapper<> *executor, size_t capacity) + : executor_(executor), + asio::experimental::channel( + executor->get_asio_executor(), capacity) {} + auto get_executor() { return executor_; } + + private: + coro_io::ExecutorWrapper<> *executor_; }; template inline coro_channel create_channel( size_t capacity, - asio::io_context::executor_type executor = - coro_io::get_global_block_executor()->get_asio_executor()) { + coro_io::ExecutorWrapper<> *executor = coro_io::get_global_executor()) { return coro_channel(executor, capacity); } +template +inline auto create_shared_channel( + size_t capacity, + coro_io::ExecutorWrapper<> *executor = coro_io::get_global_executor()) { + return std::make_shared>(executor, capacity); +} + template inline async_simple::coro::Lazy async_send( asio::experimental::channel &channel, T val) { + bool r = channel.try_send(std::error_code{}, val); + if (r) { + co_return std::error_code{}; + } callback_awaitor awaitor; co_return co_await awaitor.await_resume( [&, val = std::move(val)](auto handler) { @@ -395,6 +413,14 @@ template async_simple::coro::Lazy> inline async_receive(Channel &channel) { + using value_type = typename Channel::return_type; + value_type val; + bool r = channel.try_receive([&val](std::error_code, value_type result) { + val = result; + }); + if (r) { + co_return std::make_pair(std::error_code{}, val); + } callback_awaitor> awaitor; co_return co_await awaitor.await_resume([&](auto handler) {