Application of thrift in C++ language

Articles Catalogue

brief introduction

Thrift was originally the rpc framework of facebook. It generates multi-language interfaces according to data structure and interface description. It is convenient to develop in multi-language. Details are not described here. Here is a simple code (C++) example to illustrate the use of thrift.

Version information used by the example:
thrift: 0.10.0
boost: 1.61.0
Qt: MinGW5.5.1

This test code can be downloaded here:
Note: Some common libraries, such as boost, do not provide compilation and need to compile themselves.

thrift data structure

The data structures of different languages are basically the same, but there are also subtle differences, which can not be used universally. Thift uses its own unique grammar to describe the need for interfaces, and uses specified tools to generate the corresponding language interfaces.

The data structure of thrift is as follows:

//Basic types
bool: Boolean type, 4 bits
byte/i8: Symbolized integers, 8 bits, recommended for use in the new edition i8
i16	: Signed integer, 18 bits
i32	: Signed integer, 32 bits
i64	: Signed integer, 64 bits
double: 64 Potential floating point type
string: UTF-8 Coded string
//Special types
binary: Uncoded byte stream
//structural morphology
struct: Public object, not inheritance
struct test{
	1: string name
enum test{
	OK = 0,
	Fail = 1
list<T>: 	Ordered list
set<T>:	 	 No collection required
map<T, K>:	 Mapping data
//Abnormal type
//Service type
service: Classes of corresponding services

Introduction to Use

1. Writing thrift scripts

thrift scripts are used to describe interfaces

//File name: test.thrift
//Request data structure
struct SendData{
	1: i32 id,
	2: string message
//Return data structure
struct RespondData{
	1:string message
//Receiving and receiving service class name Service and interface RespondData
service Service{
	RespondData dataSend(1: SendData s),

2. Compiling scripts

Compiling scripts using thrift tools

./thrift-0.10.0.exe -r --gen cpp test.thrift

After completion, the gen-cpp directory is generated under the directory, and the interface code of the specified language is generated, in which xxxx_server. skeleton. CPP is the program entry.

3. Use thrift in Engineering

In order to facilitate cross-platform, Qt framework is used.

(1) Server

New project, add all files generated to the project

At this point, the build must fail, especially if a large number of files do not exist or undefined reference to error:

  1. To use thrift, you must use the boost on which it depends, compile the boost with the same compiler, and add the header and library files to the project (how to compile the boost, please refer to the relevant articles by yourself);
  2. When adding thrift files to the project, be careful not to need all of them (thrift's dependency library is not only boost, but also other functions, such as libevent/openssl, but not necessarily. Unused functions can be added without any need.

After compiling, the server is basically completed, modifying the interface method:

We set up the interface method dataSend, find the corresponding interface (generally above the main function), modify it.

void dataSend(RespondData& _return, const SendData& s) {
    // Your implementation goes here
    printf("Receive data: %d, %s\n",, s.message.c_str());//Print the client's input parameters
    _return.message = "This is respond message";//Data returned to the client

The main entry code remains unchanged:

int main(int argc, char **argv) {
    int port = 9090;//Ports for Server Listening
    shared_ptr<ServiceHandler> handler(new ServiceHandler());
    shared_ptr<TProcessor> processor(new ServiceProcessor(handler));
    shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    //Create a server, which can be executed in a single thread and only receive one request at a time. Multithreading can be used
    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    return 0;

(2) Client

New project, add the interface code to the project, in addition to xxx_server.skeleton.cpp, here is the server-side code

Add code and corresponding class libraries, similar to the server side

#include "gen-cpp/Service.h"
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using boost::shared_ptr;

int main(int argc, char **argv) {
    //To create sockets, the interface should be consistent with the server. Thift is essentially socket. Here, socket short links are used.
    boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    //transport protocol
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    transport->open();//Start the transmission and close it at last.

    //Sending data structure
    SendData send_data;
    send_data.__set_message("This is test message");
    ServiceClient client(protocol);//Create Client

    //Response data structure
    RespondData respond_data;
    client.dataSend(respond_data, send_data);//Start Interaction
    printf("Respond:%s", respond_data.message.c_str());

    return 0;

(3) Operation

Finally, the results are as follows.

Server side:

[External Link Picture Transfer Failure (img-ztu91HSq-1567650334744)(1567499596155.png)]


[External link picture transfer failure (img-wofFE7FM-1567650334756)(1567499573543.png)]

Since then, the use of thrift to complete a data interaction, you can see from the code, the client/server as a call function for data interaction, which is the convenience of RPC.


There is no PosixThreadFactory class under windows and BoostThreadFactory can be used instead.

Tags: socket Apache Qt OpenSSL

Posted on Thu, 05 Sep 2019 00:05:28 -0700 by Greaser9780