Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to send binary data with Thrift

Tags:

thrift

I have a structure in c++ which stores bytes like this:

struct RemoteData 
{
    /// some other fields here

    unsigned char* buf;
    int bufLen;     
};

And I need to send this data to remote service, written in C++, via thrift. I found three ways how to map this structure to thrift idl:

  1. Using container types like this:

    struct RemoteData 
    {
        1: list<BYTE> buf,
        ...
    }
    
  2. Using binary type:

    struct RemoteData 
    {
        1: binary buf,
        ...
    }
    
  3. Storing data in string type:

    struct RemoteData 
    {
        1: string buf,
        ...
    }
    

What is the best way?

like image 688
DanilaNV Avatar asked Dec 14 '12 09:12

DanilaNV


People also ask

What is Thrift binary?

Thrift is an interface definition language and binary communication protocol used for defining and creating services for numerous programming languages.

What is Thrift IDL?

The Thrift interface definition language (IDL) allows for the definition of Thrift Types. A Thrift IDL file is processed by the Thrift code generator to produce code for the various target languages to support the defined structs and services in the IDL file.

What is Thrift type?

The Thrift type system is intended to allow programmers to use native types as much as possible, no matter what programming language they are working in. This information is based on, and supersedes, the information in the Thrift Whitepaper.


1 Answers

The value contained in thrift string type must be UTF8-encoded, otherwise some client won't be able to read it (Java thrift client for example).

Thrift list<byte> type will be converted into std::vector<int8_t> in c++, but in other languages it will be not so good (for example, in java it will be compiled into suboptimal List<Byte>.

The binary type is the best choice in terms of interoperability with clients in other languages and the correctness of protocol definition.

Since all 3 definitions produce messages of roughly the same size, I'd go with binary: even if you don't need interoperability with other languages now, you may need it in future.

like image 54
Wildfire Avatar answered Nov 15 '22 19:11

Wildfire