mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
safe async_call
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user