Skip to content

Raw modes: Stream buffering vs. direct

Alexander Meißner edited this page Apr 23, 2017 · 1 revision

When you are not using the MsgPack layer but want to work with the raw bytes directly you have two options:

  1. Stream buffering (std::streambuf): sputn(), sgetn(), in_avail()
  2. Direct: send(), receive(), showmanyc()

Important: Stick to one of those per socket and never mix them as using different buffers simultaneously will corrupt your data.

Stream buffering

Usually you should stick with this as it is handled automatically and enables a convenient usage of the socket in iostreams. For example: Take the pointer of a socket which is a std::streambuf and put it into the constructor of a std::iostream, then you can use the operator << and operator >> on that stream.

Stream buffering also enables you to assign more buffer to individual sockets using socket->setInputBufferSize() and socket->setOutputBufferSize() if your system buffer is to small and you're losing data that way.

Direct

Use this if you have to avoid a second buffering at all costs or if you need accurate info about the hostRemote and portRemote for each packet. For example: If you have a UDP socket which can receive packets form different remotes and has to identify those.

Important: Call socket->setInputBufferSize(0) and socket->setOutputBufferSize(0) on a individual socket or #define NETLINK_DEFAULT_INPUT_BUFFER_SIZE and NETLINK_DEFAULT_OUTPUT_BUFFER_SIZE to 0 globally.

Clone this wiki locally