From 9443222b5a7a6e92b5dc49000226f6d67dccf6e7 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 17 Oct 2025 20:02:51 +0800 Subject: [PATCH 1/3] simplify --- include/rest_rpc/asio_util.hpp | 13 +++++++++++++ tests/test_rest_rpc.cpp | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/rest_rpc/asio_util.hpp b/include/rest_rpc/asio_util.hpp index 393e906..de5fc6b 100644 --- a/include/rest_rpc/asio_util.hpp +++ b/include/rest_rpc/asio_util.hpp @@ -1,4 +1,5 @@ #pragma once +#include "io_context_pool.hpp" #include "traits.h" #include "use_asio.hpp" @@ -29,6 +30,18 @@ template inline auto sync_wait(auto executor, Coro &&coro) { return async_future(executor, std::forward(coro)).get(); } +template inline auto async_start(Coro &&coro) { + async_start(get_global_executor(), std::forward(coro)); +} + +template inline auto async_future(Coro &&coro) { + return async_future(get_global_executor(), std::forward(coro)); +} + +template inline auto sync_wait(Coro &&coro) { + return sync_wait(get_global_executor(), std::forward(coro)); +} + inline auto async_start(auto executor, auto &&coro, auto callback) { using R = typename std::remove_cvref_t::value_type; if constexpr (std::is_void_v) { diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index 92f57f3..120ae01 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -94,7 +94,7 @@ asio::awaitable delay_response2(std::string_view str) { REST_LOG_INFO << ret.message(); CHECK(ret); }; - sync_wait(get_global_executor(), coro()); + sync_wait(coro()); rpc_context ctx; // right, created in io thread. co_await ctx.response(str); @@ -244,7 +244,7 @@ asio::awaitable test_router() { CHECK(result1.ec == rpc_errc::ok); } -TEST_CASE("test router") { sync_wait(get_global_executor(), test_router()); } +TEST_CASE("test router") { sync_wait(test_router()); } asio::awaitable get_last_rwtime_coro(std::shared_ptr conn) { From e581820afb1fe13a43211b32e91862b0ce5c7ab5 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 17 Oct 2025 20:05:54 +0800 Subject: [PATCH 2/3] update --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cb7e378..cfaca49 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ int main(){ } }; - sync_wait(get_global_executor(), rpc_call()); + sync_wait(rpc_call()); } ``` @@ -116,7 +116,7 @@ int main(){ } }; - sync_wait(get_global_executor(), rpc_call()); + sync_wait(rpc_call()); } ``` @@ -145,7 +145,7 @@ void publish() { } }; - sync_wait(get_global_executor(), pub()); + sync_wait(pub()); } client 端代码: @@ -167,7 +167,7 @@ void subscribe() { } }; - sync_wait(get_global_executor(), sub()); + sync_wait(sub()); } ``` From 6bd939b17770b78d1fb7c274abf64ef9a7a60645 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 17 Oct 2025 21:25:20 +0800 Subject: [PATCH 3/3] update --- examples/server.cpp | 48 +++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/server.cpp b/examples/server.cpp index 9f88639..b010b8a 100644 --- a/examples/server.cpp +++ b/examples/server.cpp @@ -1,11 +1,13 @@ #include using namespace rest_rpc; -std::string_view echo(std::string_view str) { - return str; +std::string_view echo(std::string_view str) { return str; } + +asio::awaitable echo_coro(std::string_view str) { + co_return str; } -struct dummy{ +struct dummy { int add(int a, int b) { return a + b; } }; int add(int a, int b) { return a + b; } @@ -24,46 +26,50 @@ person get_person(person p) { void basict_usage() { rpc_server server("127.0.0.1:9004", 4); server.register_handler(); - + server.register_handler(); + dummy d{}; server.register_handler<&dummy::add>(&d); - + server.register_handler(); - + auto ec = server.async_start(); - if(ec) { + if (ec) { REST_LOG_ERROR << ec.message(); return; } - + auto rpc_call = []() -> asio::awaitable { rpc_client client; auto ec0 = co_await client.connect("127.0.0.1:9004"); - if(ec0) { + if (ec0) { REST_LOG_ERROR << ec0.message(); co_return; } - + auto r = co_await client.call("test"); - if(r.ec==rpc_errc::ok) { + if (r.ec == rpc_errc::ok) { REST_LOG_INFO << "call result: " << r.value; assert(r.value == "test"); } - + + r = co_await client.call("test1"); + assert(r.value == "test1"); + auto r1 = co_await client.call<&dummy::add>(1, 2); - if(r1.ec==rpc_errc::ok) { + if (r1.ec == rpc_errc::ok) { REST_LOG_INFO << "call result: " << r1.value; assert(r1.value == 3); } - + person p{1, "tom", 20}; auto r2 = co_await client.call(p); - if(r2.ec==rpc_errc::ok) { + if (r2.ec == rpc_errc::ok) { REST_LOG_INFO << "call result: " << r2.value.name; assert(r2.value.name == "jack"); } }; - + sync_wait(get_global_executor(), rpc_call()); } @@ -72,26 +78,26 @@ void publish() { server.register_handler(); server.register_handler(); server.async_start(); - + REST_LOG_INFO << "will pubish, waiting for input"; auto pub = [&]() -> asio::awaitable { std::string str; while (true) { std::cin >> str; - if(str == "quit") { + if (str == "quit") { break; } - + co_await server.publish("topic1", str); } }; - + sync_wait(get_global_executor(), pub()); server.stop(); } int main() { basict_usage(); - + publish(); } \ No newline at end of file