The aim is to build a minimal protobuf RPC lib using Google's Protocol Buffers.
libpbrpc is flexible as it is not bound to the http server, it's a lightweight middleware.
The only dependency is Protocol Buffers.
To use this lib is really simple. First define ServiceManager some where in your code.
ServiceManager srvMan;Then the ServiceManager only provides two functions:
-
Register a service:
void regService(Service *service);
-
Handle the RPC message:
void handleRPC(const char *data, const size_t len, string &ret);
datais the raw Protobuf binary,lenis the length of the data,retis the result generated, you can send it back directly. (Note: use ret.length() to get the correct length.)
The over all design is really simple:
- Protocol. The RPC protocol is defined in the "pbrpc.proto" file. The protocol took the JSON RPC 2.0 as a reference.
Services. The RPC server provides severalServices, and eachServicehas someMethods.Methods. EachMethodshould have input(params) and output(results).
Please take a look of my other repository pbrpc as a demo which implements a simple RPC server.
One major limitation of this library is it's synchronous. It is possible to adapt task queue for asynchronous message handling.