Protocol specification in XML

Posted by Mathijs on Stack Overflow See other posts from Stack Overflow or by Mathijs
Published on 2010-06-11T17:04:34Z Indexed on 2010/06/12 6:03 UTC
Read the original article Hit count: 181

Filed under:
|
|
|

Is there a way to specify a packet-based protocol in XML, so (de)serialization can happen automatically?

The context is as follows. I have a device that communicates through a serial port. It sends and receives a byte stream consisting of 'packets'. A packet is a collection of elementary data types and (sometimes) other packets. Some elements of packets are conditional; their inclusion depends on earlier elements.

I have a C# application that communicates with this device. Naturally, I don't want to work on a byte-level throughout my application; I want to separate the protocol from my application code. Therefore I need to translate the byte stream to structures (classes).

Currently I have implemented the protocol in C# by defining a class for each packet. These classes define the order and type of elements for each packet. Making class members conditional is difficult, so protocol information ends up in functions.

I imagine XML that looks like this (note that my experience designing XML is limited):

<packet>
  <field name="Author" type="int32" />
  <field name="Nickname" type="bytes" size="4">
    <condition type="range">
      <field>Author</field>
      <min>3</min>
      <max>6</min>
    </condition>
  </field>
</packet>

.NET has something called a 'binary serializer', but I don't think that's what I'm looking for.

Is there a way to separate protocol and code, even if packets 'include' other packets and have conditional elements?

© Stack Overflow or respective owner

Related posts about c#

Related posts about design