From 97cd8263a61e862b835faa1086679a8f53d65809 Mon Sep 17 00:00:00 2001 From: saipubw Date: Fri, 3 Feb 2023 15:26:15 +0800 Subject: [PATCH] [coro_rpc] Add more example, fix typo (#169) --- include/coro_rpc/coro_rpc/rpc_execute.hpp | 4 +-- .../examples/helloworld/client/main.cpp | 22 ++++++++++--- .../helloworld/rpc_service/rpc_service.h | 7 +++- .../examples/helloworld/server/main.cpp | 5 +-- .../helloworld/server/rpc_service.cpp | 32 +++++++++++++++---- 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/include/coro_rpc/coro_rpc/rpc_execute.hpp b/include/coro_rpc/coro_rpc/rpc_execute.hpp index fed05dc55..0dc283d1f 100644 --- a/include/coro_rpc/coro_rpc/rpc_execute.hpp +++ b/include/coro_rpc/coro_rpc/rpc_execute.hpp @@ -144,7 +144,7 @@ inline auto execute(std::string_view data, rpc_conn &conn, func, std::tuple_cat( std::forward_as_tuple( - o, connnect( + o, connection( std::get>(conn))), args)); } @@ -272,7 +272,7 @@ execute_coro(std::string_view data, rpc_conn &conn, Self *self = nullptr) { func, std::tuple_cat( std::forward_as_tuple( - o, connnect( + o, connection( std::get>(conn))), args)); } diff --git a/src/coro_rpc/examples/helloworld/client/main.cpp b/src/coro_rpc/examples/helloworld/client/main.cpp index d1c9cb2a0..968c79779 100644 --- a/src/coro_rpc/examples/helloworld/client/main.cpp +++ b/src/coro_rpc/examples/helloworld/client/main.cpp @@ -31,25 +31,37 @@ Lazy show_rpc_call(coro_rpc_client &client) { if (!ret) { std::cout << "err: " << ret.error().msg << std::endl; } - assert(ret.value() == "hello world"s); + assert(ret.value() == "hello_world"s); - ret = co_await client.call("hello coro_rpc"); + auto ret_int = co_await client.call(12, 30); + if (!ret_int) { + std::cout << "err: " << ret_int.error().msg << std::endl; + } + assert(ret_int.value() == 42); + + ret = co_await client.call("coro_echo"); if (!ret) { std::cout << "err: " << ret.error().msg << std::endl; } - assert(ret.value() == "hello coro_rpc"s); + assert(ret.value() == "coro_echo"s); ret = co_await client.call(); if (!ret) { std::cout << "err: " << ret.error().msg << std::endl; } - assert(ret.value() == "hello coro_rpc"s); + assert(ret.value() == "hello_with_delay"s); ret = co_await client.call<&HelloService::hello>(); if (!ret) { std::cout << "err: " << ret.error().msg << std::endl; } - assert(ret.value() == "hello"s); + assert(ret.value() == "HelloService::hello"s); + + ret = co_await client.call<&HelloService::hello_with_delay>(); + if (!ret) { + std::cout << "err: " << ret.error().msg << std::endl; + } + assert(ret.value() == "HelloService::hello_with_delay"s); } int main() { diff --git a/src/coro_rpc/examples/helloworld/rpc_service/rpc_service.h b/src/coro_rpc/examples/helloworld/rpc_service/rpc_service.h index 61c8e835f..f74da81cc 100644 --- a/src/coro_rpc/examples/helloworld/rpc_service/rpc_service.h +++ b/src/coro_rpc/examples/helloworld/rpc_service/rpc_service.h @@ -18,14 +18,19 @@ #include #include +#include + +#include "async_simple/coro/Lazy.h" std::string hello_world(); -std::string echo(std::string str); +int A_add_B(int a, int b); void hello_with_delay(coro_rpc::connection conn); +async_simple::coro::Lazy coro_echo(std::string_view sv); class HelloService { public: std::string hello(); + void hello_with_delay(coro_rpc::connection conn); private: }; diff --git a/src/coro_rpc/examples/helloworld/server/main.cpp b/src/coro_rpc/examples/helloworld/server/main.cpp index e600d47d8..b78e9357b 100644 --- a/src/coro_rpc/examples/helloworld/server/main.cpp +++ b/src/coro_rpc/examples/helloworld/server/main.cpp @@ -24,11 +24,12 @@ int main() { coro_rpc_server server(2, 8801); // regist normal function for rpc - server.regist_handler(); + server.regist_handler(); // regist member function for rpc HelloService hello_service; - server.regist_handler<&HelloService::hello>(&hello_service); + server.regist_handler<&HelloService::hello, &HelloService::hello_with_delay>( + &hello_service); auto ec = server.start(); return ec == std::errc{}; diff --git a/src/coro_rpc/examples/helloworld/server/rpc_service.cpp b/src/coro_rpc/examples/helloworld/server/rpc_service.cpp index dacd47281..7173da6e0 100644 --- a/src/coro_rpc/examples/helloworld/server/rpc_service.cpp +++ b/src/coro_rpc/examples/helloworld/server/rpc_service.cpp @@ -19,22 +19,42 @@ #include +#include "asio_util/asio_coro_util.hpp" + using namespace coro_rpc; std::string hello_world() { ELOGV(INFO, "call helloworld"); - return "hello world"; + return "hello_world"; +} + +int A_add_B(int a, int b) { + ELOGV(INFO, "call A+B"); + return a + b; } -std::string echo(std::string str) { return str; } +async_simple::coro::Lazy coro_echo(std::string_view sv) { + ELOGV(INFO, "call coro_echo"); + co_return std::string{sv}; +} -void hello_with_delay(connection conn) { +void hello_with_delay(connection conn) { + ELOGV(INFO, "call HelloServer hello_with_delay"); std::thread([conn]() mutable { - conn.response_msg("hello coro_rpc"); + conn.response_msg("hello_with_delay"); }).detach(); } std::string HelloService::hello() { - ELOGV(INFO, "call HelloServer hello"); - return "hello"; + ELOGV(INFO, "call HelloServer::hello"); + return "HelloService::hello"; +} + +void HelloService::hello_with_delay( + coro_rpc::connection conn) { + ELOGV(INFO, "call HelloServer::hello_with_delay"); + std::thread([conn]() mutable { + conn.response_msg("HelloService::hello_with_delay"); + }).detach(); + return; }