使用Thrift发送二进制数据的最佳方式

Dan*_*aNV 11 thrift

我有一个c ++结构,它存储如下字节:

struct RemoteData 
{
    /// some other fields here

    unsigned char* buf;
    int bufLen;     
};
Run Code Online (Sandbox Code Playgroud)

我需要通过thrift将这些数据发送到用c ++编写的远程服务.我找到了三种方法来将这个结构映射到thrift idl:

  1. 使用这样的容器类型:

    struct RemoteData 
    {
        1: list<BYTE> buf,
        ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用binary类型:

    struct RemoteData 
    {
        1: binary buf,
        ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. string类型存储数据:

    struct RemoteData 
    {
        1: string buf,
        ...
    }
    
    Run Code Online (Sandbox Code Playgroud)

什么是最好的方法?

Wil*_*ire 14

thrift string类型中包含的值必须是UTF8编码的,否则某些客户端将无法读取它(例如Java thrift客户端).

Thrift list<byte>类型将转换为std::vector<int8_t>c ++,但在其他语言中它将不太好(例如,在java中它将被编译为次优List<Byte>.

binary就与其他语言的客户端的互操作性以及协议定义的正确性而言,该类型是最佳选择.

由于所有3个定义都会生成大小相同的消息,因此我会选择binary:即使您现在不需要与其他语言进行互操作,您将来也可能需要它.

  • @DanilaNV这个文档可能已经过时了:Thrift二进制类型和其他类型一样稳定,方便和广泛使用,所以我不明白为什么它不在基本类型列表中. (4认同)