10 #include <mrpt/core/exceptions.h> 12 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF) 13 #include <google/protobuf/message.h> 18 using namespace mvsim;
20 void mvsim::sendMessage(
21 const google::protobuf::MessageLite&
m, zmq::socket_t& socket)
23 mrpt::io::CMemoryStream buf;
24 auto arch = mrpt::serialization::archiveFrom(buf);
26 arch << m.GetTypeName();
27 arch << m.SerializeAsString();
29 zmq::message_t msg(buf.getRawBufferData(), buf.getTotalBytesCount());
30 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 1) 31 socket.send(msg, zmq::send_flags::none);
37 std::tuple<std::string, std::string> mvsim::internal::parseMessageToParts(
38 const zmq::message_t& msg)
40 mrpt::io::CMemoryStream buf;
41 buf.assignMemoryNotOwn(msg.data(), msg.size());
43 auto arch = mrpt::serialization::archiveFrom(buf);
45 std::string typeName, serializedData;
46 arch >> typeName >> serializedData;
47 return {typeName, serializedData};
50 void mvsim::parseMessage(
51 const zmq::message_t& msg, google::protobuf::MessageLite& out)
53 const auto [typeName, serializedData] = internal::parseMessageToParts(msg);
57 bool ok = out.ParseFromString(serializedData);
60 "Format error: protobuf could not decode binary message of type " 65 zmq::message_t mvsim::receiveMessage(zmq::socket_t&
s)
68 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 1) 69 std::optional<size_t> msgSize = s.recv(m);
77 std::string mvsim::get_zmq_endpoint(
const zmq::socket_t& s)
79 char assignedPort[200];
80 size_t assignedPortLen =
sizeof(assignedPort);
81 s.getsockopt(ZMQ_LAST_ENDPOINT, assignedPort, &assignedPortLen);
82 assignedPort[assignedPortLen] =
'\0';
83 return {assignedPort};
#define ASSERT_EQUAL_(__A, __B)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)