[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,
std::tuple_cat(
std::forward_as_tuple(
o, connnect<conn_return_type>(
o, connection<conn_return_type>(
std::get<std::shared_ptr<coro_connection>>(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<conn_return_type>(
o, connection<conn_return_type>(
std::get<std::shared_ptr<coro_connection>>(conn))),
args));
}

View File

@ -31,25 +31,37 @@ Lazy<void> 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<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) {
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>();
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() {

View File

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

View File

@ -24,11 +24,12 @@ int main() {
coro_rpc_server server(2, 8801);
// 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
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{};

View File

@ -19,22 +19,42 @@
#include <thread>
#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";
}
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 {
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</*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;
}