12 #include <mrpt/core/format.h> 19 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF) 27 class SocketMonitor :
public zmq::monitor_t
30 SocketMonitor() =
default;
33 zmq::monitor_t::abort();
34 if (runningMonitor_.joinable()) runningMonitor_.join();
37 void monitor(zmq::socket_t&
s)
39 static std::atomic_int nonce = 1000;
40 const int v = nonce++;
42 const std::string endpoint =
43 mrpt::format(
"inproc://monitor%i_%p.req", v, s.operator
void*());
45 runningMonitor_ = std::thread([&, endpoint]() {
48 zmq::monitor_t::monitor(s, endpoint, ZMQ_EVENT_ALL);
50 catch (
const std::exception& e)
52 std::cerr <<
"[MySocketMonitor] Error: " << e.what() <<
"\n";
57 void setConnected(
bool v)
59 std::lock_guard<std::mutex> lck(connectedMtx_);
63 void on_event_disconnected(
64 [[maybe_unused]]
const zmq_event_t& event_,
65 [[maybe_unused]]
const char* addr_)
override 70 void on_event_connected(
71 [[maybe_unused]]
const zmq_event_t& event_,
72 [[maybe_unused]]
const char* addr_)
override 77 bool connected()
const 79 std::lock_guard<std::mutex> lck(connectedMtx_);
84 bool connected_ =
false;
85 mutable std::mutex connectedMtx_;
87 std::thread runningMonitor_;
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1