RPC with native remote calls declaration and arguments types check
When we need communicate between instances of the same executable we have full calls (functions) declarations. Similarly when client and server are linked with same library which contains required calls. But we can't call function from another process directly. Usual RPC requires define call descriptors in the not native formats (SOAP, REST, DCOM, etc.) It's dictated by asymmetry client and server implementation. When both side have access to same set of native RPC calls declarations then we could generate all required boilerplate on the fly with help C++ templates.
Server:
#include <stdlib.h>
#include <rpc.hpp>
#include <rpc_streams.hpp>
//Call declaration. Implementation located in the another module
int add(int a, int b);
int main() {
//register add and stdlib.h exit() calls in the RPC registry
auto myrpc = rpc::make_rpc<rpc::stream_serializer, rpc::stdin_stdout_ipc_t>(add, exit);
//Start listening
myrpc.listen();
return 0;
}
Client:
#include <stdlib.h>
#include <rpc.hpp>
#include <rpc_streams.hpp>
//Call declaration. Implementation located in the another module
int add(int a, int b);
//Also implementation could be dummy stub. We need only address of the call on
//client side and never call it:
//int add(int a, int b){return 0;}
int main() {
//register add and stdlib.h exit() calls in the RPC registry in the same order as in server
auto myrpc = rpc::make_rpc<rpc::stream_serializer, rpc::stdin_stdout_ipc_t>(add, exit);
//Do addition on service side and print result
std::cout << myrpc(add, 2, 5) << std::endl;
//finishing server
myrpc(exit, 0);
return 0;
}
Calls library:
//Just do arguments addition
int add(int a, int b) {
return a + b;
}
rpc::make_rpc makes new rpc calls wrapper. It includes registry of calls definitions, marshaller, calls invocator and service listener Seriaalizer and IPC implementation are separated from rpc itself. You are need pass particular implemetation in the rpc::make_rpc template arguments. rpc::stream_serializer is serializer based on stringstream rpc::stdin_stdout_ipc_t is simple standard streams based input/output intended to use with pipes
loopback.cpp - Demo based on loopback IPC implementation. RPC calls invokes locally
stdpipes.cpp - Demo based on Unix fork() and pipe() calls. After run it forks and configure pipes server stdout -> client stdin and client stdout -> server stdin;
Requires c++14 compiler. Tested on G++ 7.3.0 and Ubuntu 18.04 For build just type:
$make