有线协议序列化 [英] Wire Protocol Serialization

查看:99
本文介绍了有线协议序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找所谓的二进制序列化器/反序列化器代码生成器",因为它缺少一个更好的术语,该术语专门允许您使用任意方式指定在线格式位长度,然后生成必要的C/C ++代码以打包/解压缩该格式的数据包.我开始使用带有位字段的结构,但是阅读了此内容之后帖子我想知道是否已经有一些东西可以处理所有麻烦的问题.我需要处理的示例数据结构:

I'm looking for what I'll call a 'binary serializer/deserializer code generator' for lack of a better term that specifically allows you to specify the on-the-wire format with arbitrary bit lengths and then generates the necessary C/C++ code to pack/unpack packets in that format. I started down the path of using a struct with bit fields but after reading this post I'm wondering if there's already something out there that handles all the messy problems. An example data structure I would need to deal with:

struct header {
    unsigned int val1 : 8;
    unsigned int val2 : 24;
    unsigned int val3 : 16
    unsigned int val4 : 2;
    unsigned int val5 : 3;
    unsigned int val6 : 1;
    unsigned int val7 : 10;
}

保持这样的数据结构字段的动机是,它使程序员可以轻松地根据协议中匹配的字段来设置/获取字段,例如. val5可能是一个有意义的3位标志.是的,对于整个结构,我只能有两个32位值,并且必须使用位掩码和填充物来跟踪所有内容,但是为什么呢?

The motivation for keep the fields of the data structure like that is that it makes the programmers job easier to set/get the fields based on a what they match in the protocol, ex. val5 might be a meaningful 3 bit flag. Yes I could just have two 32 bit values for the whole struct and have to use bit masks and stuff to keep track of everything but why?

我知道诸如Google Proto Buf之类的东西,但是AFAIK这些都集中在程序员端的数据结构上,不允许您指定特定的位模式-试着尝试为低级创建客户端代码二进制线格式是如何指定的协议.我找到的最接近的东西是 protlr 听起来不错,只是它似乎不是FOSS. SO上的其他帖子指向:

I'm aware of things like Google Proto Buf and the like, but AFAIK these all focus on the programmer side data structure and don't allow you to specify specific bit patterns - imagine trying to create the client code for low level protocols where the binary wire format is how it's specified. The closest thing I've found is protlr which sounds great except it doesn't appear to be FOSS. Other posts on SO point to:

  • RedBlocks 其中似乎是功能强大的嵌入式框架的一部分.
  • PADS ,它对于我的需求而言似乎非常陈旧且过于复杂.
  • binpac 听起来有趣,但我找不到使用它来解析任意位长度(例如1位,2位,17位字段)的示例,或者它也具有序列化方法,因为它似乎专注于一种用于入侵检测的反序列化.
  • RedBlocks which appears to be part of a full blown embedded framework.
  • PADS which seems extremely stale and overly complicated for my needs.
  • binpac which sounds interesting but I can't find an example of using it to parse arbitrary bit lengths (e.g. 1 bit, 2 bit, 17 bit fields) or if it also has a serialization method since it seems to be focused on one way deserialization for intrusion detection.

除了滚动还有另一种序列化格式之外,是否有符合我标准的FOSS替代方案?有人可以提供使用上述结构之一的示例作为示例吗?

Is there a FOSS alternative that meets my criteria besides rolling yet another serialization format, or can someone provide an example using one of these references for the structure above?

推荐答案

您可能为此考虑使用ASN.1并使用PER(对齐或未对齐).您可以使用限制为所需长度的BIT STRING类型,也可以使用具有约束的INTEGER类型,以将值限制为所需的位数.由于ASN.1及其编码规则与机器体系结构和编程语言无关,因此您不必担心机器是大端还是小端,或者通信的一端是Java还是C而不是Java. C ++.一个好的ASN.1工具可以为您解决所有这些问题.您可以在 ASN.1中找到有关ASN.1的更多信息.项目页面,具有链接 ASN简介. 1 以及 ASN.1的列表工具(有些是免费的,有些是商业的).我提到UNALIGNED PER的原因是,您可以按需发送字面上的确切位数,而无需在其间添加填充位.

You might consider ASN.1 for this and use PER (aligned or unaligned). You can use either BIT STRING types constrained to your needed lengths, or INTEGER types with constraints to limit values to the number of bits you would like. Since ASN.1 and its encoding rules are independent of machine architecture and programming language, you don't have to worry about whether your machine is big-endian or little-endian, or whether one end of the communications prefers Java rather than C or C++. A good ASN.1 Tool handles all of that for you. You can find out more about ASN.1 at the ASN.1 Project page which has a link Introduction to ASN.1 as well as a list of ASN.1 Tools (some free some commercial). The reason I mention UNALIGNED PER is that you can literally send exactly the number of bits across that line as you desire with no added padding bits between.

对于BIT STRINGS,您甚至可以为各个位分配名称,这些名称对您的应用程序有一定意义.

For BIT STRINGS, you can even assign names to individual bits that have some meaning to you for your application.

这篇关于有线协议序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆