dwalthour

ZeroMQ fix for LIBZMQ-268 + closure args for subscriptions

Mar 28th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 15.17 KB | None | 0 0
  1. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/pgm_receiver.cpp zeromq-3.2.2.1/src/pgm_receiver.cpp
  2. --- zeromq-3.2.2/src/pgm_receiver.cpp   2012-10-25 03:30:40.000000000 -0500
  3. +++ zeromq-3.2.2.1/src/pgm_receiver.cpp 2013-03-28 08:32:49.000000000 -0500
  4. @@ -73,6 +73,8 @@
  5.  
  6.      session = session_;
  7.  
  8. +    session->set_identity_received(true);
  9. +
  10.      //  If there are any subscriptions already queued in the session, drop them.
  11.      drop_subscriptions ();
  12.  }
  13. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/pgm_sender.cpp zeromq-3.2.2.1/src/pgm_sender.cpp
  14. --- zeromq-3.2.2/src/pgm_sender.cpp 2012-10-25 03:30:40.000000000 -0500
  15. +++ zeromq-3.2.2.1/src/pgm_sender.cpp   2013-03-28 12:40:43.000000000 -0500
  16. @@ -72,6 +72,8 @@
  17.      fd_t rdata_notify_fd = retired_fd;
  18.      fd_t pending_notify_fd = retired_fd;
  19.  
  20. +    session_->set_identity_sent(true);
  21. +
  22.      encoder.set_msg_source (session_);
  23.  
  24.      //  Fill fds from PGM transport and add them to the poller.
  25. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/session_base.cpp zeromq-3.2.2.1/src/session_base.cpp
  26. --- zeromq-3.2.2/src/session_base.cpp   2012-11-13 19:09:14.000000000 -0600
  27. +++ zeromq-3.2.2.1/src/session_base.cpp 2013-03-28 12:37:35.000000000 -0500
  28. @@ -140,6 +140,17 @@
  29.          delete addr;
  30.  }
  31.  
  32. +void zmq::session_base_t::set_identity_received(bool b)
  33. +{
  34. +    identity_received = b;
  35. +}
  36. +
  37. +void zmq::session_base_t::set_identity_sent(bool b)
  38. +{
  39. +    identity_sent = b;
  40. +}
  41. +
  42. +
  43.  void zmq::session_base_t::attach_pipe (pipe_t *pipe_)
  44.  {
  45.      zmq_assert (!is_terminating ());
  46. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/session_base.hpp zeromq-3.2.2.1/src/session_base.hpp
  47. --- zeromq-3.2.2/src/session_base.hpp   2012-10-25 03:30:40.000000000 -0500
  48. +++ zeromq-3.2.2.1/src/session_base.hpp 2013-03-28 12:37:43.000000000 -0500
  49. @@ -78,6 +78,9 @@
  50.  
  51.          socket_base_t *get_socket ();
  52.  
  53. +        void set_identity_received(bool b);
  54. +        void set_identity_sent(bool b);
  55. +
  56.      protected:
  57.  
  58.          session_base_t (zmq::io_thread_t *io_thread_, bool connect_,
  59. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/socket_base.cpp zeromq-3.2.2.1/src/socket_base.cpp
  60. --- zeromq-3.2.2/src/socket_base.cpp    2012-11-23 01:54:28.000000000 -0600
  61. +++ zeromq-3.2.2.1/src/socket_base.cpp  2013-03-28 17:01:20.000000000 -0500
  62. @@ -300,6 +300,18 @@
  63.          return 0;
  64.      }
  65.  
  66. +    if (option_ == ZMQ_GET_CLOSURE) {
  67. +        if (*optvallen_ < sizeof(void*)) {
  68. +            errno = EINVAL;
  69. +            return -1;
  70. +        }
  71. +        void *cv = xget_closure();
  72. +        void **closure_ptr = (void**)optval_;
  73. +        *closure_ptr = cv;
  74. +        *optvallen_ = sizeof(void*);
  75. +        return 0;
  76. +    }
  77. +
  78.      return options.getsockopt (option_, optval_, optvallen_);
  79.  }
  80.  
  81. @@ -770,6 +782,18 @@
  82.      return xhas_out ();
  83.  }
  84.  
  85. +int zmq::socket_base_t::xset_closure (const char *filter, size_t size, void * closure)
  86. +{
  87. +    errno = EINVAL;
  88. +    return -1;
  89. +}
  90. +
  91. +void* zmq::socket_base_t::xget_closure ()
  92. +{
  93. +    return NULL;
  94. +}
  95. +
  96. +
  97.  void zmq::socket_base_t::start_reaping (poller_t *poller_)
  98.  {
  99.      //  Plug the socket to the reaper thread.
  100. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/socket_base.hpp zeromq-3.2.2.1/src/socket_base.hpp
  101. --- zeromq-3.2.2/src/socket_base.hpp    2012-11-23 01:54:28.000000000 -0600
  102. +++ zeromq-3.2.2.1/src/socket_base.hpp  2013-03-28 16:17:24.000000000 -0500
  103. @@ -150,6 +150,9 @@
  104.          virtual void xhiccuped (pipe_t *pipe_);
  105.          virtual void xterminated (pipe_t *pipe_) = 0;
  106.  
  107. +        virtual int xset_closure (const char *filter, size_t size, void *closure);
  108. +        virtual void* xget_closure ();
  109. +
  110.          //  Delay actual destruction of the socket.
  111.          void process_destroy ();
  112.  
  113. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/sub.cpp zeromq-3.2.2.1/src/sub.cpp
  114. --- zeromq-3.2.2/src/sub.cpp    2012-10-25 03:30:40.000000000 -0500
  115. +++ zeromq-3.2.2.1/src/sub.cpp  2013-03-28 17:00:34.000000000 -0500
  116. @@ -39,32 +39,39 @@
  117.  int zmq::sub_t::xsetsockopt (int option_, const void *optval_,
  118.      size_t optvallen_)
  119.  {
  120. -    if (option_ != ZMQ_SUBSCRIBE && option_ != ZMQ_UNSUBSCRIBE) {
  121. -        errno = EINVAL;
  122. -        return -1;
  123. +    if (option_ == ZMQ_SUBSCRIBE || option_ == ZMQ_UNSUBSCRIBE) {
  124. +        //  Create the subscription message.
  125. +        msg_t msg;
  126. +        int rc = msg.init_size (optvallen_ + 1);
  127. +        errno_assert (rc == 0);
  128. +        unsigned char *data = (unsigned char*) msg.data ();
  129. +        if (option_ == ZMQ_SUBSCRIBE)
  130. +            *data = 1;
  131. +        else if (option_ == ZMQ_UNSUBSCRIBE)
  132. +            *data = 0;
  133. +        memcpy (data + 1, optval_, optvallen_);
  134. +
  135. +        //  Pass it further on in the stack.
  136. +        int err = 0;
  137. +        rc = xsub_t::xsend (&msg, 0);
  138. +        if (rc != 0)
  139. +            err = errno;
  140. +        int rc2 = msg.close ();
  141. +        errno_assert (rc2 == 0);
  142. +        if (rc != 0)
  143. +            errno = err;
  144. +        return rc;
  145.      }
  146. -
  147. -    //  Create the subscription message.
  148. -    msg_t msg;
  149. -    int rc = msg.init_size (optvallen_ + 1);
  150. -    errno_assert (rc == 0);
  151. -    unsigned char *data = (unsigned char*) msg.data ();
  152. -    if (option_ == ZMQ_SUBSCRIBE)
  153. -        *data = 1;
  154. -    else if (option_ == ZMQ_UNSUBSCRIBE)
  155. -        *data = 0;
  156. -    memcpy (data + 1, optval_, optvallen_);
  157. -
  158. -    //  Pass it further on in the stack.
  159. -    int err = 0;
  160. -    rc = xsub_t::xsend (&msg, 0);
  161. -    if (rc != 0)
  162. -        err = errno;
  163. -    int rc2 = msg.close ();
  164. -    errno_assert (rc2 == 0);
  165. -    if (rc != 0)
  166. -        errno = err;
  167. -    return rc;
  168. +    else if (option_ == ZMQ_SET_CLOSURE) {
  169. +        if (optvallen_ == sizeof(zmq_closure_t)) {
  170. +            const zmq_closure_t *closure = static_cast<const zmq_closure_t*>(optval_);
  171. +            int rc = xset_closure (closure->filter, closure->size, closure->closure);
  172. +            if (!rc)
  173. +                return rc;
  174. +        }
  175. +    }
  176. +    errno = EINVAL;
  177. +    return -1;
  178.  }
  179.  
  180.  int zmq::sub_t::xsend (msg_t *, int)
  181. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/trie.cpp zeromq-3.2.2.1/src/trie.cpp
  182. --- zeromq-3.2.2/src/trie.cpp   2012-10-25 03:30:40.000000000 -0500
  183. +++ zeromq-3.2.2.1/src/trie.cpp 2013-03-28 13:14:51.000000000 -0500
  184. @@ -35,6 +35,7 @@
  185.  
  186.  zmq::trie_t::trie_t () :
  187.      refcnt (0),
  188. +    closure_value (0),
  189.      min (0),
  190.      count (0),
  191.      live_nodes (0)
  192. @@ -253,38 +254,24 @@
  193.       return ret;
  194.  }
  195.  
  196. -bool zmq::trie_t::check (unsigned char *data_, size_t size_)
  197. +bool zmq::trie_t::set_closure( const unsigned char *prefix, size_t size, void *closure)
  198.  {
  199. -    //  This function is on critical path. It deliberately doesn't use
  200. -    //  recursion to get a bit better performance.
  201. -    trie_t *current = this;
  202. -    while (true) {
  203. -
  204. -        //  We've found a corresponding subscription!
  205. -        if (current->refcnt)
  206. -            return true;
  207. -
  208. -        //  We've checked all the data and haven't found matching subscription.
  209. -        if (!size_)
  210. -            return false;
  211. -
  212. -        //  If there's no corresponding slot for the first character
  213. -        //  of the prefix, the message does not match.
  214. -        unsigned char c = *data_;
  215. -        if (c < current->min || c >= current->min + current->count)
  216. -            return false;
  217. -
  218. -        //  Move to the next character.
  219. -        if (current->count == 1)
  220. -            current = current->next.node;
  221. -        else {
  222. -            current = current->next.table [c - current->min];
  223. -            if (!current)
  224. -                return false;
  225. -        }
  226. -        data_++;
  227. -        size_--;
  228. +    trie_t *node = find_node(prefix, size);
  229. +    if (node) {
  230. +        node->closure_value = closure;
  231. +        return true;
  232. +    }
  233. +    return false;
  234. +}
  235. +
  236. +bool zmq::trie_t::check (unsigned char *data_, size_t size_, void*& closure)
  237. +{
  238. +    trie_t *node = find_node(data_, size_);
  239. +    if (node) {
  240. +        closure = node->closure_value;
  241. +        return true;
  242.      }
  243. +    return false;
  244.  }
  245.  
  246.  void zmq::trie_t::apply (void (*func_) (unsigned char *data_, size_t size_,
  247. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/trie.hpp zeromq-3.2.2.1/src/trie.hpp
  248. --- zeromq-3.2.2/src/trie.hpp   2012-10-25 03:30:40.000000000 -0500
  249. +++ zeromq-3.2.2.1/src/trie.hpp 2013-03-28 12:24:07.000000000 -0500
  250. @@ -41,12 +41,14 @@
  251.          //  rather than a duplicate.
  252.          bool add (unsigned char *prefix_, size_t size_);
  253.  
  254. +        bool set_closure( const unsigned char *prefix, size_t size, void *closure);
  255. +
  256.          //  Remove key from the trie. Returns true if the item is actually
  257.          //  removed from the trie.
  258.          bool rm (unsigned char *prefix_, size_t size_);
  259.  
  260.          //  Check whether particular key is in the trie.
  261. -        bool check (unsigned char *data_, size_t size_);
  262. +        bool check (unsigned char *data_, size_t size_, void*& closure);
  263.  
  264.          //  Apply the function supplied to each subscription in the trie.
  265.          void apply (void (*func_) (unsigned char *data_, size_t size_,
  266. @@ -60,7 +62,10 @@
  267.              void *arg_);
  268.          bool is_redundant () const;
  269.  
  270. +        trie_t *find_node (const unsigned char *data_, size_t size_);
  271. +
  272.          uint32_t refcnt;
  273. +        void *closure_value;
  274.          unsigned char min;
  275.          unsigned short count;
  276.          unsigned short live_nodes;
  277. @@ -73,6 +78,40 @@
  278.          const trie_t &operator = (const trie_t&);
  279.      };
  280.  
  281. +    inline trie_t * trie_t::find_node (const unsigned char *data_, size_t size_)
  282. +    {
  283. +        //  This function is on critical path. It deliberately doesn't use
  284. +        //  recursion to get a bit better performance.
  285. +        trie_t *current = this;
  286. +        while (true) {
  287. +            
  288. +            //  We've found a corresponding subscription!
  289. +            if (current->refcnt) {
  290. +                return current;
  291. +            }
  292. +            
  293. +            //  We've checked all the data and haven't found matching subscription.
  294. +            if (!size_)
  295. +                return NULL;
  296. +            
  297. +            //  If there's no corresponding slot for the first character
  298. +            //  of the prefix, the message does not match.
  299. +            unsigned char c = *data_;
  300. +            if (c < current->min || c >= current->min + current->count)
  301. +                return NULL;
  302. +            
  303. +            //  Move to the next character.
  304. +            if (current->count == 1)
  305. +                current = current->next.node;
  306. +            else {
  307. +                current = current->next.table [c - current->min];
  308. +                if (!current)
  309. +                    return NULL;
  310. +            }
  311. +            data_++;
  312. +            size_--;
  313. +        }
  314. +    }
  315.  }
  316.  
  317.  #endif
  318. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/xsub.cpp zeromq-3.2.2.1/src/xsub.cpp
  319. --- zeromq-3.2.2/src/xsub.cpp   2012-10-25 03:30:40.000000000 -0500
  320. +++ zeromq-3.2.2.1/src/xsub.cpp 2013-03-28 16:59:03.000000000 -0500
  321. @@ -27,6 +27,7 @@
  322.  zmq::xsub_t::xsub_t (class ctx_t *parent_, uint32_t tid_, int sid_) :
  323.      socket_base_t (parent_, tid_, sid_),
  324.      has_message (false),
  325. +    current_closure (NULL),
  326.      more (false)
  327.  {
  328.      options.type = ZMQ_XSUB;
  329. @@ -75,6 +76,16 @@
  330.      dist.terminated (pipe_);
  331.  }
  332.  
  333. +int zmq::xsub_t::xset_closure (const char *filter, size_t size, void *closure)
  334. +{
  335. +    return subscriptions.set_closure ((unsigned char*) filter, size, closure) != true;
  336. +}
  337. +
  338. +void* zmq::xsub_t::xget_closure ()
  339. +{
  340. +    return current_closure;
  341. +}
  342. +
  343.  void zmq::xsub_t::xhiccuped (pipe_t *pipe_)
  344.  {
  345.      //  Send all the cached subscriptions to the hiccuped pipe.
  346. @@ -147,7 +158,7 @@
  347.  
  348.          //  Check whether the message matches at least one subscription.
  349.          //  Non-initial parts of the message are passed
  350. -        if (more || !options.filter || match (msg_)) {
  351. +        if (more || !options.filter || match (msg_, current_closure)) {
  352.              more = msg_->flags () & msg_t::more ? true : false;
  353.              return 0;
  354.          }
  355. @@ -187,7 +198,7 @@
  356.          }
  357.  
  358.          //  Check whether the message matches at least one subscription.
  359. -        if (!options.filter || match (&message)) {
  360. +        if (!options.filter || match (&message, current_closure)) {
  361.              has_message = true;
  362.              return true;
  363.          }
  364. @@ -201,9 +212,9 @@
  365.      }
  366.  }
  367.  
  368. -bool zmq::xsub_t::match (msg_t *msg_)
  369. +bool zmq::xsub_t::match (msg_t *msg_, void*& closure)
  370.  {
  371. -    return subscriptions.check ((unsigned char*) msg_->data (), msg_->size ());
  372. +    return subscriptions.check ((unsigned char*) msg_->data (), msg_->size (), closure);
  373.  }
  374.  
  375.  void zmq::xsub_t::send_subscription (unsigned char *data_, size_t size_,
  376. diff -Nu -x '*.la' -x '*.lo' -x '*.o' -x '*.pc' -x .libs -x .deps -x Makefile -x platform.hpp -x stamp-h1 zeromq-3.2.2/src/xsub.hpp zeromq-3.2.2.1/src/xsub.hpp
  377. --- zeromq-3.2.2/src/xsub.hpp   2012-10-25 03:30:40.000000000 -0500
  378. +++ zeromq-3.2.2.1/src/xsub.hpp 2013-03-28 16:57:36.000000000 -0500
  379. @@ -54,11 +54,13 @@
  380.          void xwrite_activated (zmq::pipe_t *pipe_);
  381.          void xhiccuped (pipe_t *pipe_);
  382.          void xterminated (zmq::pipe_t *pipe_);
  383. +        int xset_closure (const char *filter, size_t size, void *closure);
  384. +        void* xget_closure ();
  385.  
  386.      private:
  387.  
  388.          //  Check whether the message matches at least one subscription.
  389. -        bool match (zmq::msg_t *msg_);
  390. +        bool match (zmq::msg_t *msg_, void*& closure);
  391.  
  392.          //  Function to be applied to the trie to send all the subsciptions
  393.          //  upstream.
  394. @@ -77,6 +79,7 @@
  395.          //  If true, 'message' contains a matching message to return on the
  396.          //  next recv call.
  397.          bool has_message;
  398. +        void* current_closure;
  399.          msg_t message;
  400.  
  401.          //  If true, part of a multipart message was already received, but
  402. diff -Nu zeromq-3.2.2/include/zmq.h zeromq-3.2.2.1/include/zmq.h
  403. --- zeromq-3.2.2/include/zmq.h  2012-11-13 19:09:14.000000000 -0600
  404. +++ zeromq-3.2.2.1/include/zmq.h    2013-03-28 16:23:04.000000000 -0500
  405. @@ -250,6 +250,8 @@
  406.  #define ZMQ_TCP_ACCEPT_FILTER 38
  407.  #define ZMQ_DELAY_ATTACH_ON_CONNECT 39
  408.  #define ZMQ_XPUB_VERBOSE 40
  409. +#define ZMQ_SET_CLOSURE 41
  410. +#define ZMQ_GET_CLOSURE 42
  411.  
  412.  
  413.  /*  Message options                                                           */
  414. @@ -353,6 +355,12 @@
  415.  ZMQ_EXPORT int zmq_sendmsg (void *s, zmq_msg_t *msg, int flags);
  416.  ZMQ_EXPORT int zmq_recvmsg (void *s, zmq_msg_t *msg, int flags);
  417.  
  418. +typedef struct {
  419. +    const char * filter;
  420. +    size_t size;
  421. +    void *closure;
  422. +} zmq_closure_t;
  423. +
  424.  /*  Experimental                                                              */
  425.  struct iovec;
Advertisement
Add Comment
Please, Sign In to add comment