Overview Tutorial API reference Examples Build Download | ZmqMessage 0.1 - 21 Oct 2011 |
Binary: message content pointer is interpreted as pointer to user type, and assign operator is invoked. This mode is suitable for implementing binary protocols.
Text: message content is interpreted as char array. For string types (see ZMQMESSAGE_STRING_CLASS on string concept definition) we initialize object from char array, for other types we put (>>) chars into stringstream
and read stream into instance of type.
//read text command and binary integer as value. std::string command; int value; incoming >> ZmqMessage::Text >> command << ZmqMessage::Binary << value;
std::ostream_iterator<int> out_it(std::cout, ", "); //print messages with binary integers. std::copy( incoming.begin<int>(true), incoming.end<int>(), out_it);
ZmqMessage::Outgoing<ZmqMessage::XRouting> outgoing( sock, ZmqMessage::OutOptions::NONBLOCK | ZmqMessage::OutOptions::BINARY_MODE); //then use Text and Binary manipulators: long id = 1; outgoing << id //insert binary << ZmqMessage::Text //switch to text << "SET_VALUE" << ZmqMessage::Binary << 999; //again binary
1. Declare your 'binary' types with raw_mark
field.
struct SomeBinaryData { typedef void raw_mark; int f1; char f2[20]; };
2. Declare particular types as 'binary' using ZMQMESSAGE_BINARY_TYPE or as text using ZMQMESSAGE_TEXT_TYPE macros.
struct OtherBinaryData { int aa; char[100] data; }; ZMQMESSAGE_BINARY_TYPE(OtherBinaryData); //always send/receive 'long' as text. ZMQMESSAGE_TEXT_TYPE(long);
See example zserialize.cpp for details.