ZMQ: use random port for internal use

pull/271/head
SChernykh 1 year ago
parent 9d6f232dd1
commit b96dfa602b

@ -38,14 +38,13 @@ ZMQReader::ZMQReader(const std::string& address, uint32_t zmq_port, const std::s
m_proxy.clear(); m_proxy.clear();
} }
for (uint32_t i = m_publisherPort; i < std::numeric_limits<uint16_t>::max(); ++i) { std::random_device rd;
for (uint32_t port = 49152 + (rd() % 16384), i = 0; i < 100; ++i, port = (port < 65535) ? (port + 1) : 49152) {
try { try {
m_publisherPort = 0;
char addr[32]; char addr[32];
snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", i); snprintf(addr, sizeof(addr), "tcp://127.0.0.1:%u", port);
m_publisher.bind(addr); m_publisher.bind(addr);
m_publisherPort = static_cast<uint16_t>(i); m_publisherPort = static_cast<uint16_t>(port);
break; break;
} }
catch (const std::exception& e) { catch (const std::exception& e) {

@ -45,7 +45,7 @@ private:
zmq::context_t m_context{ 1 }; zmq::context_t m_context{ 1 };
zmq::socket_t m_publisher{ m_context, ZMQ_PUB }; zmq::socket_t m_publisher{ m_context, ZMQ_PUB };
zmq::socket_t m_subscriber{ m_context, ZMQ_SUB }; zmq::socket_t m_subscriber{ m_context, ZMQ_SUB };
uint16_t m_publisherPort = 37891; uint16_t m_publisherPort = 0;
std::atomic<bool> m_finished{ false }; std::atomic<bool> m_finished{ false };
std::atomic<bool> m_threadRunning{ false }; std::atomic<bool> m_threadRunning{ false };

Loading…
Cancel
Save