ZMQ: use random port for internal use

This commit is contained in:
SChernykh 2023-05-29 14:37:20 +02:00
parent 9d6f232dd1
commit b96dfa602b
2 changed files with 5 additions and 6 deletions

View file

@ -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) {

View file

@ -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 };