英文:
Dynamic FlatBuffers delimiter
问题
我正在使用FlatBuffer通过Unix套接字发送二进制数据。我发送的FlatBuffer的长度是动态的。我面临的问题是,如何知道读取一个表需要多少字节。
在发送时,是否可以附加类似于分隔符的内容,以便我可以确定FlatBuffer的结束位置。
当我尝试使用较小的大小时:
buf := make([]byte, 512)
nr, err := c.Read(buf)
if err != nil {
fmt.Println("exit echo")
return
}
如果读取的FlatBuffer大于512字节,则会导致失败。
当我通过增加缓冲区的大小来读取时,我无法找到读取的结束位置:
var n, nr int
var err error
buf := make([]byte, 0, 4096) // 大缓冲区
tmp := make([]byte, 512)
for {
n, err = c.Read(tmp)
if err != nil {
break
}
nr += n
if nr >= 4096 {
err = errOverrun
break
}
buf = append(buf, tmp[:n]...)
}
if err != nil {
fmt.Println("read error:", err)
break
}
英文:
I'm using flatbuffer to send binary data over unix socket. The flatbuffer that I send is of dynamic length. The problem I'm facing is, how to know how many bytes I have to read for one table.
Is there something like a delimiter that can be appended while sending, which I can use to determine the end of the flatbuffer.
When I tried with a smaller size
buf := make([]byte, 512)
nr, err := c.Read(buf)
if err != nil {
fmt.Println("exit echo")
return
}
And if the flatbuffer that is bigger than 512 bytes is read, then this results in failure.
When I read by growing my buffer, then I'm not able to find the end of the read
var n, nr int
var err error
buf := make([]byte, 0, 4096) // big buffer
tmp := make([]byte, 512)
for {
n, err = c.Read(tmp)
if err != nil {
break
}
nr += n
if nr >= 4096 {
err = errOverrun
break
}
buf = append(buf, tmp[:n]...)
}
if err != nil {
fmt.Println("read error:", err)
break
}
答案1
得分: 2
FlatBuffers设计上不包含长度字段,因为在大多数情况下,长度是缓冲区存储或传输的隐含部分。
如果你无法知道缓冲区的大小,或者正在流式传输缓冲区,最好的方法是在缓冲区前面添加一个32位长度字段,这样你就可以使用它来读取剩余的数据。
在C++ API中,这甚至是内置的(参见SizePrefixed
函数),但是这个功能尚未移植到Go语言,所以你需要手动实现。
英文:
FlatBuffers does not include a length field by design, since in most context the length is an implicit part of the storage or transfer of a buffer.
If you have no way to know the size of a buffer, or you are streaming buffers, the best is to simply pre-fix any buffer with a 32bit length field, so you can use that to read the rest of the data.
In the C++ API this is even built-in (see SizePrefixed
functions), but this hasn't been ported to Go yet, so you'd have to do it manually.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论