我收到了一份文档,其中定义了通过串行通信通道传输和接收的一组消息。我想获取传入消息并将它们反序列化为对象,并序列化我的出站消息。线路上的编码是已建立且不可更改的,并且由标头中的各种位字段和不同的有效负载组成,例如,
class Message{
int msg_num : 7
int dest_addr : 4
bool SRR : 1
bool IDE : 1
int source_addr : 6
//... and so on...
}
我查看了使用 protobufs,但似乎建立了它们的 varint 编码方法。我还查看了 boost-serialization ,但根据我到目前为止所读到的内容,编码是如何完成的并不完全清楚。
那么,有几个问题:
- 我可以使用 boost-serialization 将字节流转换为对象吗?
- 为了不必推出我自己的序列化例程(维护混乱),是否有一种首选机制来完成我的任务(例如,自定义boost-serialization Archive,这是我已经使用过的另一种方法)没有发现)
I've been handed a document that defines a set of messages that are transmitted and received over a serial communications channel. I'd like to take the incoming messages and deserialize them into objects, and serialize my outbound messages as well. The encoding over the wire is established and not changeable, and consists of various bitfields in the header and varying payloads, e.g.,
class Message{
int msg_num : 7
int dest_addr : 4
bool SRR : 1
bool IDE : 1
int source_addr : 6
//... and so on...
}
I took a look at using protobufs, but it appears that their varint method of encoding is established. I've also looked at boost-serialization, but based on what I've read so far, how the encoding is done there is not entirely clear.
So, a few questions:
- Can I use boost-serialization to convert my bytestream to objects?
- With a goal of not having to roll my own routines for serialization (a maintenance mess), is there a preferred mechanism for accomplishing my task (e.g., a custom boost-serialization Archive, another method I've not discovered)
发布评论
评论(2)
我认为您不会找到与您正在查看的自定义协议相匹配的易于使用的序列化器。但看起来您拥有的原语集(int,bool + size)足够简单,可以编写您自己的解码器/编码器。根据收到的消息简单地生成 C/C++ 代码。生成采用此类描述的可编译代码应该是相当简单的任务。它应该是在编译时完成的自动生成 - 类似于 protobuf/Corba 正在做的事情。
示例:根据规范:
转换器可以编写一个函数,其主体类似于(抽象符号并假设 MSB):
解码器:
编码器:
这应该很容易生成,也是确保编码是自定义的最简单方法。
I think you won't find an easy-to-use serializer that will match the custom protocol you are looking at. But it look the set of primitives you have (int, bool + size ) are simple enough to be able to write your own decoder/encoder. Simply generating C/C++ code based on the message received. It should be fairly simple task to generate a compilable code taking such description. It should be an automated generation done at the compile time - similar to what protobuf/Corba are doing.
Example: from the specification:
the converter could write a function with body similar to (abstract notation and assuming MSB):
Decoder:
Encoder:
That should be pretty easy to generate and the simplest way to make sure the encoding is custom.
如果您仅限于单一平台(即仅限于单字节顺序),并且消息是 POD 类型,您可以将消息声明为
primitive
。否则,至少在 boost.serialization 的情况下,您必须编写代码,即“序列化例程”。它至少支持字节顺序转换
[编辑]
错了,这不是原始的,我迷失在序列化文档的深处
In case you're limited to single platform only (i.e. limited with single byte order), and Message is POD type, you can declare your message as
primitive
.Otherwise, in case of boost.serialization at least, you'll have to write code, i.e. 'routines for serialization'. It supports byte order conversions, at least
[Edit]
Wrong, it's not
primitive
, I'm lost in the depths of serialization docs