Just like my previous Protocol Buffers post, this one is also meant as a brief introduction that will point you in the right direction rather than an exhaustive tutorial. Here we go…

Again we are in search of a portable library, this time not for serialization, but for a portable RPC mechanism. On Windows we have the WCF, but what if we want support for many platforms and programming languages? All that is answered by Thrift (also see here). Initially developed at Facebook it is now free and open source project.

Let’s start by creating a simple thrift file that defines a “service”, or a RPC server, with functions and parameters:

Here in a file service.thrift we have defined a RPC server called Service with three functions (one asynchronous) and a string parameter msg. Next we need to compile it. Just like protocol buffers, thrift is a code generator. It will produce everything needed to instantiate both the server and the client:

thrift –gen cpp -out . service.thrift

Thrift basic usage.

The above command will produce several header and source files for us. Now we are ready to implement our C++ client that will connect to the RPC server and issue remote procedure calls. The code is straight forward and easy to read and understand:

The server code is slightly more complicated, but not by much 🙂 In this post I’m using the most basic functions of thrift for illustration purposes. But know that it is quite capable of handling huge workloads and many connections. Here’s the corresponding server code:

The extra code here is the ServiceHandler class which will do the actual RPC work. Let’s put it all together now. After I start the server and execute the client program on my machine, I get the following output:

Starting the server…
ping()
Martin says hi!
async_call()

Thrift RPC server output.

It works! I hope you enjoyed this little introduction to thrift. Now go read all about it!

P.S. As always, complete source and build files available at my GitHub.

4 Replies to “Thrift: or how to RPC”

Leave a Reply