Protocol Definition Language
What protocol definition do you recommend? I evaluated Google's protocol buffers, but it does not allow me to control the placement of fields in the packet being built. I assume the same is true for Thrift. My requirements are:
- specify the location of fields in the packet
- allow for bit fields
- conditionals: a flag (bit field) = true means that data can appear at a later location in the packet
- ability to define a packet structure by referring to another packet definition
Thank you.
("Flavor" on 开发者_开发问答SourceForge, used for defining MPEG-4 might be a candidate, but I am looking for something that seems to have more of a community and preferably works in a .NET environment.)
Have a look to ASN.1 http://es.wikipedia.org/wiki/ASN.1
FooProtocol DEFINITIONS ::= BEGIN
FooQuestion ::= SEQUENCE {
trackingNumber INTEGER,
question IA5String
}
FooAnswer ::= SEQUENCE {
questionNumber INTEGER,
answer BOOLEAN
}
END
It seems to cover your main requirements:
- Bit detail
- ordered content
- type references
- not sure, about conditions
Is widely used, and you can find some implementations on java and python
I'd be interested in the reasons for your requirements. Why do you need to control the position of the fields? Why are bitfields important? Conditionals?
It sounds like you have a (more or less) fixed wire format for which you need to write a parser for, and in that case none of the existing popular protocol/serialization formats (Protobufs, Thrift, JSON, Yaml, etc.) will work for you.
A somewhat unorthodox approach is to use Erlang or Haskell, both of which have good support for parsing binary protocols.
How about C# itself?
eg
class MySimplePDLData {
// format: name (or blank if padding), bit length, value (or blank if data),
// name of presence flag field (or blank if no presence flag), C# type
// one packet type per string, fields separated by pipes (|)
string[] pdl = {
// MY-SIMPLE-PDL-START
",8,0xf8,|version,8,,Int32|type,8,,Int32|id1,64,,Int64",
...
// MY-SIMPLE-PDL-END
};
}
If the data is already in memory, you don't need to do IO on a file format. From here you can either dynamically interpret packes or generate the necessary C# source code for packet recognition/pack/unpack, again using C# itself.
精彩评论