23 #ifndef ZeroMQPacket20160413H 24 #define ZeroMQPacket20160413H 26 #include "goby/common/core_constants.h" 33 std::string zeromq_packet_make_header(MarshallingScheme marshalling_scheme,
34 const std::string& identifier)
36 std::string zmq_filter;
38 google::protobuf::uint32 marshalling_int =
39 static_cast<google::protobuf::uint32
>(marshalling_scheme);
41 for (
int i = 0, n = BITS_IN_UINT32 / BITS_IN_BYTE; i < n; ++i)
42 { zmq_filter.push_back((marshalling_int >> (BITS_IN_BYTE * (n - i - 1))) & 0xFF); }
43 zmq_filter += identifier +
'\0';
50 const std::string& identifier,
const std::string& body)
52 *raw = zeromq_packet_make_header(marshalling_scheme, identifier);
58 std::string* identifier, std::string* body)
61 const unsigned MARSHALLING_SIZE = BITS_IN_UINT32 / BITS_IN_BYTE;
63 if (raw.size() < MARSHALLING_SIZE)
64 throw(std::runtime_error(
"Message is too small"));
66 google::protobuf::uint32 marshalling_int = 0;
67 for (
int i = 0, n = MARSHALLING_SIZE; i < n; ++i)
69 marshalling_int <<= BITS_IN_BYTE;
70 marshalling_int ^= raw[i];
73 if (marshalling_int >= MARSHALLING_UNKNOWN && marshalling_int <= MARSHALLING_MAX)
74 *marshalling_scheme =
static_cast<MarshallingScheme
>(marshalling_int);
78 ss <<
"Invalid marshalling value = " << marshalling_int;
79 throw(std::runtime_error(ss.str()));
82 *identifier = raw.substr(MARSHALLING_SIZE, raw.find(
'\0', MARSHALLING_SIZE) - MARSHALLING_SIZE);
85 const int HEADER_SIZE = MARSHALLING_SIZE + identifier->size() + 1;
86 *body = raw.substr(HEADER_SIZE);
void zeromq_packet_encode(std::string *raw, MarshallingScheme marshalling_scheme, const std::string &identifier, const std::string &body)
Encodes a packet for Goby over ZeroMQ.
The global namespace for the Goby project.
void zeromq_packet_decode(const std::string &raw, MarshallingScheme *marshalling_scheme, std::string *identifier, std::string *body)
Decodes a packet for Goby over ZeroMQ.