IntroductionNOTE: This documentation is a work in progress and is being continually worked on to try and make using the MessageSerializer as easy as possible. I also apologize for various dead links and things of that nature that I have not had time to get to yet as well. There is also a large TODO list to make the MessageSerializer even more flexible. There are a wide variety of protocols in the world that follow a structure in a form similar to: Building an array of bytes that matches this structure can be difficult and prone to errors, especially due to the fact that the size of the data varies depending on what the variable length field is. The same holds true when the user has an array of bytes and would like to translate that into the properties of a class to then handle the message however it needs to be handled. The MessageSerializer is intended to allow the user to define a class in C# that corresponds to a message like the one above. Then, the MessageSerializer will do the conversion from the array of bytes into the class and vice-versa, while at the same time automatically calculating and verifying fields such as lengths and CRC’s. Currently the structure of the message can either be defined purely through the class definition and a series of attributes applied to the class or through the class definition and an XML definition. Note: The MessageSerializer uses Reflection to determine how to process messages. Small ExampleTake this simple message: In this message:
To use the MessageSerializer to work with this message you would define a class as follows: Then you can do something like the following: If you then looked what sampleMessageSerialization held you would see:
and if you looked at what is in sampleMessageDeserialized you would see: You can see that the MessageSerializer automatically filled in both the Length and the Crc to their appropriate values in the byte array after serialization. In fact, if you looked at the sampleMessage variable after the serialization you would see that the Length and Crc values now had the correct value as well. That was all wonderfully easy but you can probably come up with all sorts of questions, such as:
You can probably come up with plenty of other scenarios. The MessageSerializer is designed to handle a wide variety of situations with its build in functionality and does its best to make things work your way with a minimum of effort. However, if your protocol happens to require handling that isn’t built in, almost everything can be overridden to work whatever way your protocol requires. In the following pages, the default functionality, how to change the default functionality and how to add your own functionality will all be covered, along with examples that will hopefully help you make the MessageSerializer work with your protocol. In the future it is hoped that there will be a library of definitions for various protocols so that not only does the work not need to be duplicated by later users but will also provide a wide range of examples for different complicated scenarios. Let me know if you have questions on how to handle a particular situation with your protocol. |