Skip to content

Commit

Permalink
[coro_rpc] Add more example, fix typo (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle authored Feb 3, 2023
1 parent ead3011 commit 97cd826
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
4 changes: 2 additions & 2 deletions include/coro_rpc/coro_rpc/rpc_execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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));
}
Expand Down
22 changes: 17 additions & 5 deletions src/coro_rpc/examples/helloworld/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 6 additions & 1 deletion src/coro_rpc/examples/helloworld/rpc_service/rpc_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
};
Expand Down
5 changes: 3 additions & 2 deletions src/coro_rpc/examples/helloworld/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
32 changes: 26 additions & 6 deletions src/coro_rpc/examples/helloworld/server/rpc_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

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<std::string> coro_echo(std::string_view sv) {
ELOGV(INFO, "call coro_echo");
co_return std::string{sv};
}

void hello_with_delay(connection<std::string> conn) {
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;
}

0 comments on commit 97cd826

Please sign in to comment.