动态FlatBuffers分隔符

huangapple go评论82阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年5月15日 23:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/43983274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定