IoT
IoT

Reputation: 639

Best way of serializing/deserializing a simple protocol in C++

I want to build a simple application protocol using Berkeley sockets in C++ using on Linux. The transport layer should be UDP, and the protocols will contain the following two parts:

The first part:

It is a fixed part which is the protocol Header with the following fields:

1. int HeaderType
2. int TransactionID
3. unsigned char Source[4]
4. unsigned char Destination[4]
5. int numberoftlvs

The second part

It will contain variable number of TLVs, each TLV will contain the following fields:

1. int type
2. int length
3. unsigned char *data "Variable length"

My question is for preparing the message to be sent over the wire,what's the best way to do serialization and deserialization, to be portable on all the systems like little Endian and big Endian?

Should I prepare a big buffer of "unsigned char", and start copying the fields one by one to it? And after that, just call send command?

If I am going to follow the previous way, how can I keep tracking the pointer to where to copy my fields, my guess would be to build for each datatype a function which will know how many bytes to move the pointer, correct?

If someone can provide me with a well explained example ,it will much appreciated.

Upvotes: 0

Views: 934

Answers (2)

berkus
berkus

Reputation: 1563

As an example I shamelessly plug my own (de)serialization library that's packing into msgpackv5 format: flurry

Upvotes: 1

jsantander
jsantander

Reputation: 5102

some ideas... in no particular order... and probably not making sense all together

  • You can have a Buffer class. This class contains raw memory pointer where you are composing your message and it can contains counter or pointers to keep track of how much have you written, where you're writing and how far can you go.
  • Probably you would like to have one instance of the Buffer class for each thread reading/writing. No more, because you don't want to have expensive buffers like this around. Bound to a specific thread because you don't want to share them without locking (and locking is expensive)
  • Probably you would like to reuse a Buffer from one message to the next, avoiding the cost of creating and destroying it.
  • You might want to explore the idea of a Decorator a class that inherits or contains each of your data classes. In this case they idea for this decorator is to contain the methods to serialize and deserialize each of your data types.
  • One option for this is to make the Decorator a template and use class template specialization to provide the different formats.
  • Combining the Decorator methods and Buffer methods you should have all the control you need.
  • You can have in the Buffer class magical templated methods that have an arbitary object as parameter and automatically creates a Decorator for it and serializes.
  • Connversely, deserializing should get you a Decorator that should be made convertible to the decorated type.

I'm sorry I don't have the time right now to give you a full blown example, but I hope the above ideas can get you started.

Upvotes: 1

Related Questions