From 54d434fc8940e8b64067ce806d5a0c5fcee22823 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 20 Dec 2018 13:47:06 +0800 Subject: [PATCH] update --- README.md | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 491a8df..dfdb939 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,108 @@ # rest_rpc -modern, simple, easy to use rpc framework +modern c++11, simple, easy to use rpc framework. + +It's so easy to love RPC. + +Modern C++开发到RPC库就是这么简单好用! + +# quick example + +server code + + #include "rpc_server.h" + using namespace rest_rpc; + using namespace rpc_service; + + struct dummy{ + int add(connection* conn, int a, int b) { return a + b; } + }; + + std::string translate(connection* conn, const std::string& orignal) { + std::string temp = orignal; + for (auto& c : temp) { + c = std::toupper(c); + } + return temp; + } + + void hello(connection* conn, const std::string& str) { + std::cout << "hello " << str << std::endl; + } + + int main() { + rpc_server server(9000, 4); + + dummy d; + server.register_handler("add", &dummy::add, &d); + server.register_handler("translate", translate); + server.register_handler("hello", hello); + + server.run(); + + std::string str; + std::cin >> str; + } + +client code + + #include + #include "test_client.hpp" + #include "../codec.h" + + using namespace rest_rpc; + using namespace rest_rpc::rpc_service; + + void test_add() { + try{ + boost::asio::io_service io_service; + test_client client(io_service); + client.connect("127.0.0.1", "9000"); + + auto result = client.call("add", 1, 2); + + std::cout << result << std::endl; + } + catch (const std::exception& e){ + std::cout << e.what() << std::endl; + } + } + + void test_translate() { + try { + boost::asio::io_service io_service; + test_client client(io_service); + client.connect("127.0.0.1", "9000"); + + auto result = client.call("translate", "hello"); + std::cout << result << std::endl; + } + catch (const std::exception& e) { + std::cout << e.what() << std::endl; + } + } + + void test_hello() { + try { + boost::asio::io_service io_service; + test_client client(io_service); + client.connect("127.0.0.1", "9000"); + + client.call("hello", "purecpp"); + } + catch (const std::exception& e) { + std::cout << e.what() << std::endl; + } + } + + int main() { + test_hello(); + test_add(); + test_translate(); + return 0; + } + + + +# future + +make an IDL tool to genrate the client code. \ No newline at end of file