Serializing network messages
Posted
by
mtsvetkov
on Programmers
See other posts from Programmers
or by mtsvetkov
Published on 2012-09-24T11:54:04Z
Indexed on
2012/09/24
15:49 UTC
Read the original article
Hit count: 243
I am writing a network wrapper around boost::asio
and was wondering what is a good and simple way to serialize my messages. I have a message factory which can take care of dispatching the data to the correct builder, but I want to know if there are any established solutions for getting the binary data on the sender side and consequently passing the data for deserialization on the receiver end.
Some options I've explored are: passing a pointer to a char[]
to the serialize/deserialize functions (for serialize to write to, and deserialize to read from), but it's difficult to enforce buffer size this way; building on that, I decided to have the serialize function return a boost::asio::mutable_buffer
, however ownership of the memory gets blurred between multiple classes, as the network wrapper needs to clean up the memory allocated by the message builder.
I have also seen solutions involving streambuf
's and stringstream
's, but manipulating binary data in terms of its string representation is something I want to avoid. Is there some sort of binary stream I can use instead? What I am looking for is a solution (preferrably using boost libs) that lets the message builder dictate the amount of memory allocated during serialization and what that would look like in terms of passing the data around between the wrapper and message factory/message builders.
PS. Messages contain almost exclusively built-in types and PODs and form a shallow but wide hierarchy for the sake of going through a factory.
Note: a link to examples of using boost::serialization
for something like this would be appreciated as I'm having difficulties figuring out the relation between it and buffers.
© Programmers or respective owner