3

我正在使用现有代码,union ibv_gid通过 TCP 连接传递数据而不转换字节序。里面有一条评论:"The gid will be transfer byte by byte so no need to do "hton"。代码是正确的并且有效,但我不明白为什么数据是逐字节传递的(实际上它们传递了整个结构)以及为什么不需要字节序转换。他们传递的数据类型是:

union ibv_gid {
        uint8_t                 raw[16];
        struct {
                uint64_t        subnet_prefix;
                uint64_t        interface_id;
        } global;
};

** 对于其他数据类型(如 int 等),它们会在前后转换数据

//VL_sock_sync_data function synchronizes between the two sides 
//by exchanging data between the two sides.
//size bytes are being copied from out_buf to the other side, and being saved in in_buf.
rc = VL_sock_sync_data(sock_p, sizeof(union ibv_gid), &local_gid, &tmp_gid);

你能解释一下为什么不需要字节序转换吗?感谢您的任何帮助

4

1 回答 1

4

这里的原因似乎是这里不需要进行字节序转换,因为 GID(以其规范表示)不是两个 64 位整数。它是 16 个字节。

复杂之处在于具有不同字节序的两个系统将在subnet_prefixandinterface_id字段中看到不同的值。因此,如果他们要将这些值写入字符串,来回发送字符串并比较它们,那将是一个问题。如果他们要根据哪个具有更大的 GID 来比较 GID subnet_prefix,并期望系统之间的比较相同,那将是一个问题。如果一个只生成连续interface_id的 s,而另一个期望它们是连续的,那将是一个问题。但只要它们仅用作不透明的字节数组,就没有问题。

于 2014-06-02T13:11:35.477 回答