Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve post method #537

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions include/cinatra/ylt/coro_io/coro_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,11 @@ inline async_simple::coro::Lazy<std::error_code> async_handshake(
#endif
class period_timer : public asio::steady_timer {
public:
using asio::steady_timer::steady_timer;
template <typename T>
period_timer(coro_io::ExecutorWrapper<T> *executor)
: asio::steady_timer(executor->get_asio_executor()) {}
template <typename executor_t, typename Rep, typename Period>
period_timer(const executor_t &executor,
const std::chrono::duration<Rep, Period> &timeout_duration)
: asio::steady_timer(executor, timeout_duration) {}

async_simple::coro::Lazy<bool> async_await() noexcept {
callback_awaitor<bool> awaitor;

Expand Down Expand Up @@ -297,10 +295,10 @@ inline async_simple::coro::Lazy<void> sleep_for(const Duration &d) {
}
}

template <typename R, typename Func>
template <typename R, typename Func, typename Executor>
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<R, async_simple::Try<void>>) {
func();
Expand All @@ -317,24 +315,30 @@ struct post_helper {
}
});
}
coro_io::ExecutorWrapper<> *e;
Executor e;
Func func;
};

template <typename Func>
template <typename Func, typename Executor>
inline async_simple::coro::Lazy<
async_simple::Try<typename util::function_traits<Func>::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<typename util::function_traits<Func>::return_type>;

callback_awaitor<R> awaitor;

post_helper<R, Func> helper{e, std::move(func)};
post_helper<R, Func, Executor> helper{executor, std::move(func)};
co_return co_await awaitor.await_resume(helper);
}

template <typename Func>
inline async_simple::coro::Lazy<
async_simple::Try<typename util::function_traits<Func>::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 <typename R>
struct coro_channel
: public asio::experimental::channel<void(std::error_code, R)> {
Expand Down
Loading