Merge pull request #172 from qicosmos/simplify_wait

This commit is contained in:
qicosmos
2025-10-18 11:16:45 +08:00
committed by GitHub
4 changed files with 46 additions and 27 deletions
+4 -4
View File
@@ -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());
}
```
+15 -9
View File
@@ -1,11 +1,13 @@
#include <rest_rpc.hpp>
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<std::string_view> 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,6 +26,7 @@ person get_person(person p) {
void basict_usage() {
rpc_server server("127.0.0.1:9004", 4);
server.register_handler<echo>();
server.register_handler<echo_coro>();
dummy d{};
server.register_handler<&dummy::add>(&d);
@@ -31,7 +34,7 @@ void basict_usage() {
server.register_handler<get_person>();
auto ec = server.async_start();
if(ec) {
if (ec) {
REST_LOG_ERROR << ec.message();
return;
}
@@ -39,26 +42,29 @@ void basict_usage() {
auto rpc_call = []() -> asio::awaitable<void> {
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<echo>("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<echo_coro>("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<get_person>(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");
}
@@ -78,7 +84,7 @@ void publish() {
std::string str;
while (true) {
std::cin >> str;
if(str == "quit") {
if (str == "quit") {
break;
}
+13
View File
@@ -1,4 +1,5 @@
#pragma once
#include "io_context_pool.hpp"
#include "traits.h"
#include "use_asio.hpp"
@@ -29,6 +30,18 @@ template <typename Coro> inline auto sync_wait(auto executor, Coro &&coro) {
return async_future(executor, std::forward<Coro>(coro)).get();
}
template <typename Coro> inline auto async_start(Coro &&coro) {
async_start(get_global_executor(), std::forward<Coro>(coro));
}
template <typename Coro> inline auto async_future(Coro &&coro) {
return async_future(get_global_executor(), std::forward<Coro>(coro));
}
template <typename Coro> inline auto sync_wait(Coro &&coro) {
return sync_wait(get_global_executor(), std::forward<Coro>(coro));
}
inline auto async_start(auto executor, auto &&coro, auto callback) {
using R = typename std::remove_cvref_t<decltype(coro)>::value_type;
if constexpr (std::is_void_v<R>) {
+2 -2
View File
@@ -94,7 +94,7 @@ asio::awaitable<std::string> 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<void> 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<void>
get_last_rwtime_coro(std::shared_ptr<rpc_connection> conn) {