穿越电线

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

Gob over the wire

问题

我正在考虑在网络协议中使用gob("encoding/gob")来序列化数据,我一直在搜索,但似乎找不到解决这些问题的方法:

消息分帧 - gob文档给人的印象是你可以简单地将TCP连接包装在gob解码器中并进行读取。但是如果你只收到了一半的消息会怎么样?gob能否以某种方式处理这个问题,还是我必须添加消息帧,并将消息数据复制到缓冲区中以供gob反序列化?

不同类型的消息 - 协议有不同类型的消息,如何最好地处理这些消息?通过在每个gob blob之前添加一个标识符来指示数据的类型?通过将所有消息放入一个"Master"消息中,该消息包含所有不同消息的字段(将其减少为只有一种类型的消息)?
我尝试了后者(更简单的方法),但似乎有巨大的开销(>650字节)。

英文:

I'm considering using gob ("encoding/gob") for serializing data in a network protocol, I have been searching around and can't seem to find any solution to these problems:

Message framing - The gob documentation gives the impression that you can simply wrap your TCP connection in a gob decoder and read away. But what happens if you only received half a message? Can gob somehow deal with this or am I forced to add a message frame and copy over the message data into a buffer for gob to unserialize?

Different types of messages - The protocol has different types of messages, how is this best handled with gob? By having an identifier before every gob blob indicating the type of the data? By putting all messages into a "Master" message which contains fields for all the different messages (reducing it to only one type of message)?
I tried the latter (simpler) and it seems to have a HUGE overhead (>650 bytes).

答案1

得分: 3

“gob”文档给人的印象是你可以简单地将你的TCP连接包装在一个gob解码器中并进行读取。

正确。该包被设计用于在编码器和解码器之间流式传输多个值。

但是如果你只接收到一半的消息会发生什么呢?

解码器调用底层的io.Reader来获取数据。如果读取器无法返回数据,则读取器将返回一个错误。解码器将此错误返回给应用程序。

如果io.Reader返回错误,就没有办法恢复解码流。

不同类型的消息

你可以编码一对消息,其中第一个消息告诉应用程序在第二个消息中期望的类型。

你也可以像你描述的那样创建一个“主”类型。你看到的开销是每个流一次产生的,而不是每个值一次产生的。

英文:

> The gob documentation gives the impression that you can simply wrap your TCP connection in a gob decoder and read away.

Correct. The package is designed to stream multiple values between an encoder and a decoder.

> But what happens if you only received half a message?

The decoder calls the underlying io.Reader to get data. If reader cannot return data, then the reader will return an error. The decoder returns this error to the application.

There's no way to recover the decoding stream if the io.Reader returns an error.

> Different types of messages

You can encode pairs of messages where the first tells the application the type to expect in the second.

You can also create a "master" type as you describe. The overhead that you see is incurred once per stream, not once per value.

huangapple
  • 本文由 发表于 2015年6月29日 22:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/31117999.html
匿名

发表评论

匿名网友

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

确定