一个高性能、易扩展的 C++20 RPC 库,支持多种通信协议和调用模式,专为现代 C++ 开发设计。 支持同步、异步调用,支持服务端和客户端,支持序列化和反序列化。
server:
void echo(RpcConnection weak, std::string sv) {
if (auto conn = weak.lock()) {
cout << "get connection!\n" << sv << "\n";
} else {
cout << "connection is nullptr\n" << sv << "\n";
}
}
std::string helloworld(RpcConnection weak, std::string_view str) {
if (auto conn = weak.lock()) {
cout << "get conn\n" << str << "\n";
} else {
cout << "conn is nullptr\n" << str << "\n";
}
return std::string(str);
}
int main() {
spdlog::set_level(spdlog::level::trace);
Server srv("0.0.0.0", 9000);
Router::reg_callback("echo", echo);
Router::reg_callback("hello", helloworld);
srv.run();
}client:
int main(int argc, char *argv[]) {
spdlog::set_level(spdlog::level::trace);
try {
Client c;
auto fu = c.async_call("echo", std::string("hello world"));
fu.get().as();
auto f2 = c.async_call("hello", std::string("hello world"));
spdlog::info("call hello");
auto r = f2.get().unwrap<std::string>();
spdlog::info("hello result: {}", r);
} catch (std::exception &e) {
spdlog::error("err: {}", e.what());
}
}通过msgpack-cxx实现,支持自定义类型的序列化和反序列化,
- C++20
- cmake
- boost::asio
- msgpack-cxx