[coro_rpc] Add more example, fix typo (#169)

This commit is contained in:
saipubw 2023-02-03 15:26:15 +08:00 committed by GitHub
parent ead3011e1a
commit 97cd8263a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 16 deletions

View File

@ -144,7 +144,7 @@ inline auto execute(std::string_view data, rpc_conn &conn,
func, func,
std::tuple_cat( std::tuple_cat(
std::forward_as_tuple( std::forward_as_tuple(
o, connnect<conn_return_type>( o, connection<conn_return_type>(
std::get<std::shared_ptr<coro_connection>>(conn))), std::get<std::shared_ptr<coro_connection>>(conn))),
args)); args));
} }
@ -272,7 +272,7 @@ execute_coro(std::string_view data, rpc_conn &conn, Self *self = nullptr) {
func, func,
std::tuple_cat( std::tuple_cat(
std::forward_as_tuple( std::forward_as_tuple(
o, connnect<conn_return_type>( o, connection<conn_return_type>(
std::get<std::shared_ptr<coro_connection>>(conn))), std::get<std::shared_ptr<coro_connection>>(conn))),
args)); args));
} }

View File

@ -31,25 +31,37 @@ Lazy<void> show_rpc_call(coro_rpc_client &client) {
if (!ret) { if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl; 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<echo>("hello coro_rpc"); auto ret_int = co_await client.call<A_add_B>(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>("coro_echo");
if (!ret) { if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl; 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<hello_with_delay>(); ret = co_await client.call<hello_with_delay>();
if (!ret) { if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl; 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>(); ret = co_await client.call<&HelloService::hello>();
if (!ret) { if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl; 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() { int main() {

View File

@ -18,14 +18,19 @@
#include <coro_rpc/rpc_connection.hpp> #include <coro_rpc/rpc_connection.hpp>
#include <string> #include <string>
#include <string_view>
#include "async_simple/coro/Lazy.h"
std::string hello_world(); 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<std::string> conn); void hello_with_delay(coro_rpc::connection<std::string> conn);
async_simple::coro::Lazy<std::string> coro_echo(std::string_view sv);
class HelloService { class HelloService {
public: public:
std::string hello(); std::string hello();
void hello_with_delay(coro_rpc::connection<std::string> conn);
private: private:
}; };

View File

@ -24,11 +24,12 @@ int main() {
coro_rpc_server server(2, 8801); coro_rpc_server server(2, 8801);
// regist normal function for rpc // regist normal function for rpc
server.regist_handler<hello_world, echo, hello_with_delay>(); server.regist_handler<hello_world, A_add_B, hello_with_delay, coro_echo>();
// regist member function for rpc // regist member function for rpc
HelloService hello_service; 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(); auto ec = server.start();
return ec == std::errc{}; return ec == std::errc{};

View File

@ -19,22 +19,42 @@
#include <thread> #include <thread>
#include "asio_util/asio_coro_util.hpp"
using namespace coro_rpc; using namespace coro_rpc;
std::string hello_world() { std::string hello_world() {
ELOGV(INFO, "call helloworld"); ELOGV(INFO, "call helloworld");
return "hello world"; return "hello_world";
} }
std::string echo(std::string str) { return str; } int A_add_B(int a, int b) {
ELOGV(INFO, "call A+B");
return a + b;
}
void hello_with_delay(connection<std::string> conn) { async_simple::coro::Lazy<std::string> coro_echo(std::string_view sv) {
ELOGV(INFO, "call coro_echo");
co_return std::string{sv};
}
void hello_with_delay(connection</*response type:*/ std::string> conn) {
ELOGV(INFO, "call HelloServer hello_with_delay");
std::thread([conn]() mutable { std::thread([conn]() mutable {
conn.response_msg("hello coro_rpc"); conn.response_msg("hello_with_delay");
}).detach(); }).detach();
} }
std::string HelloService::hello() { std::string HelloService::hello() {
ELOGV(INFO, "call HelloServer hello"); ELOGV(INFO, "call HelloServer::hello");
return "hello"; return "HelloService::hello";
}
void HelloService::hello_with_delay(
coro_rpc::connection</*response type:*/ std::string> conn) {
ELOGV(INFO, "call HelloServer::hello_with_delay");
std::thread([conn]() mutable {
conn.response_msg("HelloService::hello_with_delay");
}).detach();
return;
} }