From 8f2c0a4fa07d8a4c3d3e7ea99f5f1449c145fcac Mon Sep 17 00:00:00 2001 From: qicosmos Date: Sun, 7 Sep 2025 07:59:44 +0800 Subject: [PATCH] safe async_call --- examples/client/main.cpp | 8 +++----- include/rest_rpc/router.h | 2 +- include/rest_rpc/rpc_client.hpp | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/client/main.cpp b/examples/client/main.cpp index e27e9e7..84fafea 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -229,11 +229,9 @@ void test_echo() { } { - client.async_call( - "echo", - [](const asio::error_code &ec, string_view data) { - auto str = as(data); - std::cout << "echo " << str << '\n'; + client.async_call_s<&dummy::add>( + [](const asio::error_code &ec, auto ret) { + std::cout << "echo " << ret << '\n'; }, "test"); } diff --git a/include/rest_rpc/router.h b/include/rest_rpc/router.h index a60be97..061358d 100644 --- a/include/rest_rpc/router.h +++ b/include/rest_rpc/router.h @@ -67,7 +67,7 @@ public: key2func_name_.erase(key); } - template void register_handler() { + template void remove_handler() { constexpr std::string_view name = get_func_name(); remove_handler(name); } diff --git a/include/rest_rpc/rpc_client.hpp b/include/rest_rpc/rpc_client.hpp index 03e6c83..6b5ac17 100644 --- a/include/rest_rpc/rpc_client.hpp +++ b/include/rest_rpc/rpc_client.hpp @@ -336,6 +336,23 @@ public: MD5::MD5Hash32(rpc_name.data(), rpc_name.length())); } + template + void async_call_s( + std::function::return_type)> + cb, + Args &&...args) { + constexpr auto rpc_name = get_func_name(); + async_call( + rpc_name, + [cb = std::move(cb)](asio::error_code ec, std::string_view data) { + using R = typename function_traits::return_type; + auto r = as(data); + cb(ec, r); + }, + std::forward(args)...); + } + void stop() { if (thd_ != nullptr) { work_.reset();