# rest_rpc **Repository Path**: levanliu/rest_rpc ## Basic Information - **Project Name**: rest_rpc - **Description**: rest_rpc rest_rpc - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-01-24 - **Last Updated**: 2023-01-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # rest_rpc c++11, high performance, cross platform, easy to use rpc framework. It's so easy to love RPC. Modern C++开发的 RPC 库就是这么简单好用! # rest_rpc 简介 rest_rpc 是一个高性能、易用、跨平台、header only 的 c++11 rpc 库,它的目标是让 tcp 通信变得非常简单易用,即使不懂网络通信的人也可以直接使用它。它依赖 header-only 的 standalone [asio](https://github.com/chriskohlhoff/asio) rest_rpc 为用户提供了非常简单易用的接口,几行代码就可以实现 rpc 通信了,来看第一个例子 ### 一个加法的 rpc 服务 ``` //服务端注册加法rpc服务 struct dummy{ int add(rpc_conn conn, int a, int b) { return a + b; } }; int main(){ rpc_server server(9000, std::thread::hardware_concurrency()); dummy d; server.register_handler("add", &dummy::add, &d); server.run(); } ``` ``` //客户端调用加法的rpc服务 int main(){ rpc_client client("127.0.0.1", 9000); client.connect(); int result = client.call("add", 1, 2); client.run(); } ``` ### 获取一个对象的 rpc 服务 ``` //服务端注册获取person的rpc服务 //1.先定义person对象 struct person { int id; std::string name; int age; MSGPACK_DEFINE(id, name, age); }; //2.提供并服务 person get_person(rpc_conn conn) { return { 1, "tom", 20 }; } int main(){ //... server.register_handler("get_person", get_person); } ``` ``` //客户端调用获取person对象的rpc服务 int main(){ rpc_client client("127.0.0.1", 9000); client.connect(); person result = client.call("get_person"); std::cout << result.name << std::endl; client.run(); } ``` ## 酷 异步? 同步? future? callback? 当初为了提供什么样的接口在社区群里还争论了一番,有人希望提供 callback 接口,有人希望提供 future 接口,最后我 决定都提供,专治强迫症患者:) 现在想要的这些接口都给你提供了,你想用什么类型的接口就用什么类型的接口,够酷吧,让我们来看看怎么用这些接口吧: ``` //服务端提供echo服务 std::string echo(rpc_conn conn, const std::string& src) { return src; } server.register_handler("echo", echo); ``` 客户端同步接口 ``` auto result = client.call("echo", "hello"); ``` 客户端异步回调接口 ``` client.async_call("echo", [](asio::error_code ec, string_view data){ auto str = as(data); std::cout << "echo " << str << '\n'; }); ``` ## async_call 接口说明 有两个重载的 async_call 接口,一个是返回 future 的接口,一个是带超时的异步接口。 返回 future 的 async_call 接口: ``` std::future future = client.async_call("echo", "purecpp"); ``` 带超时的异步回调接口: ``` async_call("some_rpc_service_name", callback, service_args...); ``` 如果不显式设置超时时间的话,则会用默认的 5s 超时. ``` async_call("some_rpc_service_name", callback, args...); ``` ``` client.async_call("echo", [](asio::error_code ec, string_view data) { if (ec) { std::cout << ec.message() <<" "<< data << "\n"; return; } auto result = as(data); std::cout << result << " async\n"; }, "purecpp"); ``` 客户端异步 future 接口 ``` auto future = client->async_call("echo", "hello"); auto status = future.wait_for(std::chrono::seconds(2)); if (status == std::future_status::timeout) { std::cout << "timeout\n"; } else if (status == std::future_status::ready) { auto str = future.get().as(); std::cout << "echo " << str << '\n'; } ``` ``` sudo apt install zsh chsh -s /bin/zsh zsh -c "$(curl -fsSL 'https://api.host.mintimate.cn/fileHost/public/download/1P0R')" ```