From c49f12b41d5e8e64d868ec35c7d437631014e76c Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Sat, 10 Aug 2024 20:19:15 +0200 Subject: [PATCH] ZMQ: bind to random port for internal messages --- src/zmq_reader.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/zmq_reader.cpp b/src/zmq_reader.cpp index a1807b0..af81149 100644 --- a/src/zmq_reader.cpp +++ b/src/zmq_reader.cpp @@ -42,7 +42,10 @@ ZMQReader::ZMQReader(const std::string& address, uint32_t zmq_port, const std::s addr_buf[0] = '\0'; std::random_device rd; - for (uint32_t port = 49152 + (rd() % 16384), i = 0; i < 100; ++i, port = (port < 65535) ? (port + 1) : 49152) { + + for (uint32_t i = 0; i < 100; ++i) { + const uint32_t port = 49152 + (rd() % 16384); + try { log::Stream s(addr_buf); s << "tcp://127.0.0.1:" << port << '\0'; @@ -52,6 +55,7 @@ ZMQReader::ZMQReader(const std::string& address, uint32_t zmq_port, const std::s } catch (const std::exception& e) { LOGWARN(1, "failed to to bind port " << port << " for ZMQ publisher, error " << e.what()); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } }