Reputation: 2565
I am trying to serialize and recover objects over UDP using the Boost.Serialization and Boost.Asio libraries. The following points sum up what I know so far:
boost::archive::text_oarchive
serializes data as a text stream, and the class boost::archive::text_iarchive
restores data from such a text stream.source: https://theboostcpplibraries.com/boost.serialization-archive
I understand that I must pass a stream as a parameter to the archive. However, there are a few different types of streams which are suitable candidates. See the following digram:
source: https://stackoverflow.com/a/8116698/3599179
I have seen online serialization examples that used ostream
and istream
, other examples used ostringstream
and istringstream
and some others used streambuf
, which acts as both an input and output buffer if I am not mistaken.
(File streams are out of the equation because I need to write/read from a socket not from a file.)
Upvotes: 0
Views: 961
Reputation: 85471
To add to Maxim's answer, if you really want to use UDP, you need to take care of splitting your stream into datagrams and ensure consistency of the data stream yourself. UDP is datagram-oriented and comes with no guarantees of data consistency.
What needs to be taken into account when using UDP:
If any of these are an issue, you need to implement appropriate countermeasures as part of your protocol, e.g. a packet sequence number and a way to request packet retransmission.
Large datagrams may get fragmented and severely reduce performance. Some people recommend a max datagram size of 512 bytes.
So, given these restrictions, I would suggest to use a compact, binary serialization format. For example, protobuf or msgpack. Boost isn't very compact, though can be good enough (source: cpp-serializers).
Upvotes: 2
Reputation: 136465
Take time to read descriptions of the streams you mentioned on cppreference.com Input/output library, it is quite instructive.
If you want to serialize into memory only one stream works for you: ostringstream
. Then you extract the string from it and send it however you please. For deserialization use istringstream
. Alternatively, stringstream
for both cases.
Upvotes: 4