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:
Using container types like this:
struct RemoteData
{
1: list<BYTE> buf,
...
}
Using binary
type:
struct RemoteData
{
1: binary buf,
...
}
Storing data in string
type:
struct RemoteData
{
1: string buf,
...
}
What is the best way?
Thrift is an interface definition language and binary communication protocol used for defining and creating services for numerous programming languages.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With