Skip to content

caozhanhao/qwrpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qwrpc

A easy-to-use Modern C++ header-only RPC library.

libczh License

Document Document

  • Header only
  • Easy-to-use interface
  • Runtime type checking

A Simple Example

RpcServer

qwrpc::RpcServer svr(8765);
svr.register_method("plus", std::plus<int>());

RpcClient

qwrpc::RpcClient cli("127.0.0.1:8765");
auto ret = cli.call<int>("plus", 1, 1);
// or async_call<int>("plus", 1, 1);
  • cli.call<T> returns T
  • cli.async_call<...> returns a std::future<T>

More

Type Support

qwrpc supports

  • any types that satisfied std::is_trivially_copyable
  • any types with specialization of serialize and deserialize(see below)
  • any containers(with begin(), end(), insert()) stores supported type.

It's worth noting that the last rule above indicates that something like std::vector<std::vector<...>> is also supported.

If more types are needed, add specialization qwrpc::serializer::serialize() and qwrpc::serializer::deserialize().

  • serialize and deserialize
namespace qwrpc::serializer
{
  template<>
  std::string serialize(const custom_type& b)
  {
    // do something
  }
  template<>
  custom_type deserialize(const std::string& str)
  {
    // do something
  }
}

Register

Any functions satisfied the following rule can be used directly.

  • Parameters and return value is supported types.(see above)
svr.register_method("plus", std::plus<int>());
svr.register_method("plus",[](int a, int b){ return a + b;});
svr.register_method("foo",
                      [](std::vector<qwrpc_example::C> c) -> std::vector<std::vector<qwrpc_example::D>>
                      {
                        // do something
                      });

For more examples, please see examples.

Logger

init_logger(minimum severity, output mode, filename(opt))

  • minimum severity: NONE, TRACE, DEBUG, INFO, WARN, ERR, CRITICAL.
  • output mode: file, console, file_and_console, none
  • filename: Only when output mode is file or file_and_console, filename is need.
  • If you don't want logger, just don't use init_logger() or simply set output mode to none.
qwrpc::logger::init_logger(qwrpc::logger::Severity::NONE,
                           qwrpc::logger::Output::file_and_console,
                           "qwrpc_server_log.txt");

Dependencies

Contact

  • If you have any questions or suggestions, please submit an issue or email me.
  • Email: cao2013zh at 163 dot com

Contribution

  • Any contributions are welcomed, just send a PR.

License