From a2d736c14f1f0c56e1d687aba06838bccab2bec0 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Wed, 15 Aug 2018 12:59:27 +0200 Subject: [PATCH] Problem: functionality around inprocs_t is scattered Solution: extract into functions of new inprocs_t class --- src/socket_base.cpp | 58 ++++++++++++++++++++++++++++----------------- src/socket_base.hpp | 12 +++++++++- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/socket_base.cpp b/src/socket_base.cpp index 03393e25..58451f02 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -97,6 +97,36 @@ #include "scatter.hpp" #include "dgram.hpp" +void zmq::socket_base_t::inprocs_t::emplace (const char *addr_, pipe_t *pipe_) +{ + _inprocs.ZMQ_MAP_INSERT_OR_EMPLACE (std::string (addr_), pipe_); +} + +int zmq::socket_base_t::inprocs_t::erase_pipes (const std::string &addr_str_) +{ + const std::pair range = + _inprocs.equal_range (addr_str_); + if (range.first == range.second) { + errno = ENOENT; + return -1; + } + + for (map_t::iterator it = range.first; it != range.second; ++it) + it->second->terminate (true); + _inprocs.erase (range.first, range.second); + return 0; +} + +void zmq::socket_base_t::inprocs_t::erase_pipe (pipe_t *pipe_) +{ + for (map_t::iterator it = _inprocs.begin (), end = _inprocs.end (); + it != end; ++it) + if (it->second == pipe_) { + _inprocs.erase (it); + break; + } +} + bool zmq::socket_base_t::check_tag () const { return _tag == 0xbaddecaf; @@ -749,7 +779,7 @@ int zmq::socket_base_t::connect (const char *addr_) _last_endpoint.assign (addr_); // remember inproc connections for disconnect - _inprocs.ZMQ_MAP_INSERT_OR_EMPLACE (std::string (addr_), new_pipes[0]); + _inprocs.emplace (addr_, new_pipes[0]); options.connected = true; return 0; @@ -978,24 +1008,13 @@ int zmq::socket_base_t::term_endpoint (const char *addr_) return -1; } - const std::string addr_str = std::string (addr_); - // Disconnect an inproc socket if (protocol == "inproc") { - if (unregister_endpoint (addr_str, this) == 0) { - return 0; - } - const std::pair range = - _inprocs.equal_range (addr_str); - if (range.first == range.second) { - errno = ENOENT; - return -1; - } + const std::string addr_str = std::string (addr_); - for (inprocs_t::iterator it = range.first; it != range.second; ++it) - it->second->terminate (true); - _inprocs.erase (range.first, range.second); - return 0; + return unregister_endpoint (addr_str, this) == 0 + ? 0 + : _inprocs.erase_pipes (addr_str); } std::string resolved_addr = addr_; @@ -1507,12 +1526,7 @@ void zmq::socket_base_t::pipe_terminated (pipe_t *pipe_) xpipe_terminated (pipe_); // Remove pipe from inproc pipes - for (inprocs_t::iterator it = _inprocs.begin (), end = _inprocs.end (); - it != end; ++it) - if (it->second == pipe_) { - _inprocs.erase (it); - break; - } + _inprocs.erase_pipe (pipe_); // Remove the pipe from the list of attached pipes and confirm its // termination if we are already shutting down. diff --git a/src/socket_base.hpp b/src/socket_base.hpp index 9cb8b4f2..6b81f26f 100644 --- a/src/socket_base.hpp +++ b/src/socket_base.hpp @@ -200,7 +200,17 @@ class socket_base_t : public own_t, endpoints_t _endpoints; // Map of open inproc endpoints. - typedef std::multimap inprocs_t; + class inprocs_t + { + public: + void emplace (const char *addr_, pipe_t *pipe_); + int erase_pipes (const std::string &addr_str_); + void erase_pipe (pipe_t *pipe_); + + private: + typedef std::multimap map_t; + map_t _inprocs; + }; inprocs_t _inprocs; // To be called after processing commands or invoking any command