safe async_call

This commit is contained in:
qicosmos
2025-09-07 07:59:44 +08:00
parent 26a8e227b4
commit 8f2c0a4fa0
3 changed files with 21 additions and 6 deletions
+3 -5
View File
@@ -229,11 +229,9 @@ void test_echo() {
}
{
client.async_call(
"echo",
[](const asio::error_code &ec, string_view data) {
auto str = as<std::string>(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");
}
+1 -1
View File
@@ -67,7 +67,7 @@ public:
key2func_name_.erase(key);
}
template <auto func> void register_handler() {
template <auto func> void remove_handler() {
constexpr std::string_view name = get_func_name<func>();
remove_handler(name);
}
+17
View File
@@ -336,6 +336,23 @@ public:
MD5::MD5Hash32(rpc_name.data(), rpc_name.length()));
}
template <auto func, typename... Args>
void async_call_s(
std::function<void(asio::error_code,
typename function_traits<decltype(func)>::return_type)>
cb,
Args &&...args) {
constexpr auto rpc_name = get_func_name<func>();
async_call(
rpc_name,
[cb = std::move(cb)](asio::error_code ec, std::string_view data) {
using R = typename function_traits<decltype(func)>::return_type;
auto r = as<R>(data);
cb(ec, r);
},
std::forward<Args>(args)...);
}
void stop() {
if (thd_ != nullptr) {
work_.reset();