Introduction
Thrift is an interface definition language and binary communication protocol that is used to define and create services for numerous languages. It is used as a remote procedure call (RPC) framework and was developed at Facebook for "scalable cross-language services development".
Facebook re-open sourced the thrift with various enhancements. It includes a new code generator and framework for c++ called cpp2. The cpp2 framework looks great but there is almost no documentation on how to use the cpp2. At the time of writing this article, I could not even get any documentation on how to use the thrift compiler to generate cpp2 style code. The thrift compiler(thrift1) does not even recognize -gen cpp2 compiler option. After googling a little bit I found that the python version of the compiler can be used to generate cpp2 style code.
To know more about the cpp2, follow this link.
About this tutorial
This tutorial will help you to understand and use the fbthrift cpp2 framework. This tutorial is an incomplete guide because:
- The fbthrift supports many different languages but this tutorial only covers c++ with cpp2 framework.
- It also does not cover all aspects of the cpp2 framework. For example there are various compiler options for cpp2 code generation.
I came up with the content of this tutorial by mostly reading the thrift source code. At the time of writing this tutorial the thrift version is 15.0. The content may go outdated in future if the thrift design changes.
Prerequisite
- You should know C++ with some of the C++11 constructs e.g. lambda expression
- Event driven programming concepts. If you have never done any event driven programming before, then it is highly recommended that try out at least one libevent example
- At the time of writing this article, the cpp2 framework is only supported on Linux so you should have a Linux box to try out examples in this tutorial
Installing thrift
Get the code from Github and follow the instructions in README file