Cap'n Proto - 在 Java 中查找邮件大小
Cap'n Proto - Finding Message Size in Java
我正在使用 TCP Client/Server 将 Cap'n Proto 消息从 C++ 发送到 Java。
有时接收缓冲区可能过满或未满,为了处理这些情况,我们需要知道消息大小。
当我检查 Java 中的缓冲区大小时,我得到 208 个字节,但是调用
MyModel.MyMessage.STRUCT_SIZE.total()
returns 4(不确定这里使用的是什么计量单位)。
我注意到4分为208,52次。但我不知道使用 52 的重要转换因子。
如何查看 Java 中的邮件大小?
MyMessage.STRUCT_SIZE
表示该结构本身的常量大小(以 8 字节字为单位),但如果该结构包含重要字段(如文本、数据、列表或其他结构),则那些也占用 space,并且它们占用的 space 的数量不是恒定的(例如,文本将根据字符串的长度占用 space)。
通常您应该尝试让 Cap'n Proto 直接写入/读取适当的 ByteChannel
s,这样您就不必自己跟踪大小。然而,如果你真的必须提前计算消息的大小,你可以这样做:
ByteBuffer[] segments = message.getSegmentsForOutput();
int total = (segments.length / 2 + 1) * 8; // segment table
for (ByteBuffer segment: segments) {
total += segment.remaining();
}
// now `total` is the total number of bytes that will be
// written when the message is serialized.
在 C++ 大小上,您可以使用 serialize.h
中的 capnp::computeSerializedSizeInWords()
(并乘以 8)。
但同样,您确实应该通过使用 org.capnproto.Serialize
的方法和流 I/O.
来构建您的代码来避免这种情况
我正在使用 TCP Client/Server 将 Cap'n Proto 消息从 C++ 发送到 Java。
有时接收缓冲区可能过满或未满,为了处理这些情况,我们需要知道消息大小。
当我检查 Java 中的缓冲区大小时,我得到 208 个字节,但是调用
MyModel.MyMessage.STRUCT_SIZE.total()
returns 4(不确定这里使用的是什么计量单位)。
我注意到4分为208,52次。但我不知道使用 52 的重要转换因子。
如何查看 Java 中的邮件大小?
MyMessage.STRUCT_SIZE
表示该结构本身的常量大小(以 8 字节字为单位),但如果该结构包含重要字段(如文本、数据、列表或其他结构),则那些也占用 space,并且它们占用的 space 的数量不是恒定的(例如,文本将根据字符串的长度占用 space)。
通常您应该尝试让 Cap'n Proto 直接写入/读取适当的 ByteChannel
s,这样您就不必自己跟踪大小。然而,如果你真的必须提前计算消息的大小,你可以这样做:
ByteBuffer[] segments = message.getSegmentsForOutput();
int total = (segments.length / 2 + 1) * 8; // segment table
for (ByteBuffer segment: segments) {
total += segment.remaining();
}
// now `total` is the total number of bytes that will be
// written when the message is serialized.
在 C++ 大小上,您可以使用 serialize.h
中的 capnp::computeSerializedSizeInWords()
(并乘以 8)。
但同样,您确实应该通过使用 org.capnproto.Serialize
的方法和流 I/O.