src/Comms/common.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/core/exceptions.h>
11 
12 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
13 #include <google/protobuf/message.h>
14 #include <mvsim/Comms/common.h>
15 
16 #include <zmq.hpp>
17 
18 using namespace mvsim;
19 
20 void mvsim::sendMessage(
21  const google::protobuf::MessageLite& m, zmq::socket_t& socket)
22 {
23  mrpt::io::CMemoryStream buf;
24  auto arch = mrpt::serialization::archiveFrom(buf);
25 
26  arch << m.GetTypeName();
27  arch << m.SerializeAsString();
28 
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);
32 #else
33  socket.send(msg);
34 #endif
35 }
36 
37 std::tuple<std::string, std::string> mvsim::internal::parseMessageToParts(
38  const zmq::message_t& msg)
39 {
40  mrpt::io::CMemoryStream buf;
41  buf.assignMemoryNotOwn(msg.data(), msg.size());
42 
43  auto arch = mrpt::serialization::archiveFrom(buf);
44 
45  std::string typeName, serializedData;
46  arch >> typeName >> serializedData;
47  return {typeName, serializedData};
48 }
49 
50 void mvsim::parseMessage(
51  const zmq::message_t& msg, google::protobuf::MessageLite& out)
52 {
53  const auto [typeName, serializedData] = internal::parseMessageToParts(msg);
54 
55  ASSERT_EQUAL_(typeName, out.GetTypeName());
56 
57  bool ok = out.ParseFromString(serializedData);
58  if (!ok)
60  "Format error: protobuf could not decode binary message of type "
61  "'%s'",
62  typeName.c_str());
63 }
64 
65 zmq::message_t mvsim::receiveMessage(zmq::socket_t& s)
66 {
67  zmq::message_t m;
68 #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 1)
69  std::optional<size_t> msgSize = s.recv(m);
70  ASSERT_(msgSize.has_value());
71 #else
72  s.recv(&m);
73 #endif
74  return m;
75 }
76 
77 std::string mvsim::get_zmq_endpoint(const zmq::socket_t& s)
78 {
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};
84 }
85 
86 #endif
#define ASSERT_EQUAL_(__A, __B)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLdouble s
ROSCPP_DECL bool ok()
#define ASSERT_(f)
const GLdouble * m


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51