codeaviator
codeaviator

Reputation: 2565

Which stream is suitable for serialization over UDP?

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:

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:

stream types

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

Answers (2)

rustyx
rustyx

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:

  • Datagrams may get lost in transit.
  • Datagrams may arrive more than once.
  • Datagrams may become corrupted in transit (there is a checksum but it's optional and weak).
  • Datagrams may arrive out-of-order.
  • The bandwidth of the network or the recipient may be insufficient for the rate at which the sender is sending.

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

Maxim Egorushkin
Maxim Egorushkin

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

Related Questions