From f18259d73a231f04ae3556a2cf6b165f828cdd48 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Mon, 20 Apr 2020 15:06:40 +0800 Subject: [PATCH] refactor(mge/zmqrpc): zmqrpc return real port or -1 GitOrigin-RevId: 878b63c601899f80f41f2f150ada72d1cd117413 --- python_module/src/cpp/zmq_rpc.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/python_module/src/cpp/zmq_rpc.cpp b/python_module/src/cpp/zmq_rpc.cpp index e144cae08..56c6a4411 100644 --- a/python_module/src/cpp/zmq_rpc.cpp +++ b/python_module/src/cpp/zmq_rpc.cpp @@ -101,30 +101,31 @@ ZmqRpcServer::ZmqRpcServer(string address, int port, m_backend(m_ctx, ZMQ_ROUTER), m_workers(&m_ctx, m_impl.get()) { try { - char full_addr[30]; + char full_addr[100]; + size_t size = sizeof(full_addr); sprintf(full_addr, "%s:%d", m_address.c_str(), m_port); m_frontend.bind(full_addr); - } catch (...) { - char full_addr[30]; - for (int i = 1024; i < 49151; i++) { - m_port = 0; - try { - sprintf(full_addr, "%s:%d", m_address.c_str(), i); - m_frontend.bind(full_addr); - m_port = i; - break; - } catch (...) { - } + m_frontend.getsockopt(ZMQ_LAST_ENDPOINT, &full_addr, &size); + m_port = 0; + int pow = 1, len = strlen(full_addr); + for (int i = len - 1; i >= 0; i--) { + if (full_addr[i] == ':') break; + m_port += (full_addr[i] - '0') * pow; + pow *= 10; } + } catch(...) { + m_port = -1; } m_backend.bind("inproc://workers"); } void ZmqRpcServer::run() { + if(m_port == -1) return; m_main_thread = make_unique([this] { this->work(); }); } void ZmqRpcServer::close() { + if(m_port == -1) return; m_stop = true; if (m_main_thread->joinable()) m_main_thread->join(); -- GitLab