rpclib is a RPC library for C++, providing both a client and server implementation. It is built using modern C++14, and as such, requires a recent compiler. Using the library, you can:
- Expose functions of your program to be called via RPC with minimal boilerplate
- Or call function through RPC with a syntax that is as natural as C++ permits (without code generation)
#include <iostream>
#include "rpc/server.h"
void foo() {
std::cout << "foo was called!" << std::endl;
}
int main(int argc, char *argv[]) {
// Creating a server that listens on port 8080
rpc::server srv(8080);
// Binding the name "foo" to free function foo.
// note: the signature is automatically captured
srv.bind("foo", &foo);
// Binding a lambda function to the name "add".
srv.bind("add", [](int a, int b) {
return a + b;
});
// Run the server loop.
srv.run();
return 0;
}When srv.run() is called, rpclib starts the server loop which listens to incoming connections
and tries to dispatch calls to the bound functions. The functions are called from the thread where
run was called from. There is also async_run that spawns worker threads and returns
immediately.
#include <iostream>
#include "rpc/client.h"
int main() {
// Creating a client that connects to the localhost on port 8080
rpc::client client("127.0.0.1", 8080);
// Calling a function with paramters and converting the result to int
auto result = client.call("add", 2, 3).as<int>();
std::cout << "The result is: " << result << std::endl;
return 0;
}Most planned 1.0.0 features are done and tested; what you are seeing here is a preview of the release (I'm hesitant to call it release candidate because there are known outstanding issues).
rpclib builds on the efforts of fantastic C++ projects. In no particular order:
- MessagePack implementation for C and C++ by Takatoshi Kondo (website)
- asio by Christopher Kohlhoff (website)
- cppformat (now renamed
fmtlibby Victor Zverovich (website) - googletest by Google
- wheels by Martinho Fernandes
Shoutouts to
- Appveyor
- Travis CI
- Coveralls.io
- Coverity
- ASan & TSan helped spotting and resolving many bugs.