Skip to content

Commit

Permalink
fix example client (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle authored Aug 16, 2023
1 parent ae4ac4b commit 3cf0b60
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
6 changes: 4 additions & 2 deletions include/ylt/coro_io/client_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class client_pool : public std::enable_shared_from_this<
std::this_thread::yield();
++cnt;
if (cnt % 10000 == 0) {
ELOG_WARN << "spinlock of client{" << client.get()
ELOG_WARN << "spinlock of client{" << client.get() << "},host:{"
<< client->get_host() << ":" << client->get_port()
<< "}cost too much time, spin count: " << cnt;
}
}
Expand Down Expand Up @@ -167,7 +168,8 @@ class client_pool : public std::enable_shared_from_this<
if (wait_time.count() > 0)
co_await coro_io::sleep_for(wait_time);
}
ELOG_WARN << "reconnect client{" << client.get()
ELOG_WARN << "reconnect client{" << client.get() << "},host:{"
<< client->get_host() << ":" << client->get_port()
<< "} out of max limit, stop retry. connect failed";
client = nullptr;
}
Expand Down
19 changes: 10 additions & 9 deletions src/coro_rpc/examples/base_examples/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,45 @@ Lazy<void> show_rpc_call() {
assert(ec == std::errc{});
auto ret = co_await client.call<hello_world>();
if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl;
std::cout << "hello_world err: " << ret.error().msg << std::endl;
}
assert(ret.value() == "hello_world"s);

auto ret_int = co_await client.call<A_add_B>(12, 30);
if (!ret_int) {
std::cout << "err: " << ret_int.error().msg << std::endl;
std::cout << "A_add_B 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;
std::cout << "coro_echo err: " << ret.error().msg << std::endl;
}
assert(ret.value() == "coro_echo"s);

ret = co_await client.call<hello_with_delay>("hello_with_delay"s);
if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl;
std::cout << "hello_with_delay err: " << ret.error().msg << std::endl;
}
assert(ret.value() == "hello_with_delay"s);

ret = co_await client.call<nested_echo>("hello_with_delay"s);
ret = co_await client.call<nested_echo>("nested_echo"s);
if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl;
std::cout << "nested_echo err: " << ret.error().msg << std::endl;
}
assert(ret.value() == "hello_with_delay"s);
assert(ret.value() == "nested_echo"s);

ret = co_await client.call<&HelloService::hello>();
if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl;
std::cout << "HelloService::hello err: " << ret.error().msg << std::endl;
}
assert(ret.value() == "HelloService::hello"s);

ret = co_await client.call<&HelloService::hello_with_delay>(
"HelloService::hello_with_delay"s);
if (!ret) {
std::cout << "err: " << ret.error().msg << std::endl;
std::cout << "HelloService::hello_with_delay err: " << ret.error().msg
<< std::endl;
}
assert(ret.value() == "HelloService::hello_with_delay"s);
}
Expand Down
16 changes: 12 additions & 4 deletions src/coro_rpc/examples/base_examples/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ using namespace coro_rpc;
using namespace async_simple;
using namespace async_simple::coro;
int main() {
// start rpc server
coro_rpc_server server(std::thread::hardware_concurrency(), 8801);
// init rpc server
coro_rpc_server server(/*thread=*/std::thread::hardware_concurrency(),
/*port=*/8801);

coro_rpc_server server2{/*thread=*/1, /*port=*/8802};

// regist normal function for rpc
server.register_handler<hello_world, A_add_B, hello_with_delay, echo,
coro_echo>();
nested_echo, coro_echo>();

// regist member function for rpc
HelloService hello_service;
server
.register_handler<&HelloService::hello, &HelloService::hello_with_delay>(
&hello_service);

// start server
server2.register_handler<echo>();
// async start server
auto res = server2.async_start();
assert(res.has_value());

// sync start server & sync await server stop
return server.start() == std::errc{};
}

0 comments on commit 3cf0b60

Please sign in to comment.