xsub.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_XSUB_HPP_INCLUDED__
  25. #define __ZMQ_XSUB_HPP_INCLUDED__
  26. #include "socket_base.hpp"
  27. #include "session_base.hpp"
  28. #include "dist.hpp"
  29. #include "fq.hpp"
  30. #ifdef ZMQ_USE_RADIX_TREE
  31. #include "radix_tree.hpp"
  32. #else
  33. #include "trie.hpp"
  34. #endif
  35. namespace zmq
  36. {
  37. class ctx_t;
  38. class pipe_t;
  39. class io_thread_t;
  40. class xsub_t : public socket_base_t
  41. {
  42. public:
  43. xsub_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
  44. ~xsub_t () ZMQ_OVERRIDE;
  45. protected:
  46. // Overrides of functions from socket_base_t.
  47. void xattach_pipe (zmq::pipe_t *pipe_,
  48. bool subscribe_to_all_,
  49. bool locally_initiated_) ZMQ_FINAL;
  50. int xsetsockopt (int option_,
  51. const void *optval_,
  52. size_t optvallen_) ZMQ_OVERRIDE;
  53. int xgetsockopt (int option_, void *optval_, size_t *optvallen_) ZMQ_FINAL;
  54. int xsend (zmq::msg_t *msg_) ZMQ_OVERRIDE;
  55. bool xhas_out () ZMQ_OVERRIDE;
  56. int xrecv (zmq::msg_t *msg_) ZMQ_FINAL;
  57. bool xhas_in () ZMQ_FINAL;
  58. void xread_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  59. void xwrite_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  60. void xhiccuped (pipe_t *pipe_) ZMQ_FINAL;
  61. void xpipe_terminated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  62. private:
  63. // Check whether the message matches at least one subscription.
  64. bool match (zmq::msg_t *msg_);
  65. // Function to be applied to the trie to send all the subsciptions
  66. // upstream.
  67. static void
  68. send_subscription (unsigned char *data_, size_t size_, void *arg_);
  69. // Fair queueing object for inbound pipes.
  70. fq_t _fq;
  71. // Object for distributing the subscriptions upstream.
  72. dist_t _dist;
  73. // The repository of subscriptions.
  74. #ifdef ZMQ_USE_RADIX_TREE
  75. radix_tree_t _subscriptions;
  76. #else
  77. trie_with_size_t _subscriptions;
  78. #endif
  79. // If true, send all unsubscription messages upstream, not just
  80. // unique ones
  81. bool _verbose_unsubs;
  82. // If true, 'message' contains a matching message to return on the
  83. // next recv call.
  84. bool _has_message;
  85. msg_t _message;
  86. // If true, part of a multipart message was already sent, but
  87. // there are following parts still waiting.
  88. bool _more_send;
  89. // If true, part of a multipart message was already received, but
  90. // there are following parts still waiting.
  91. bool _more_recv;
  92. // If true, subscribe and cancel messages are processed for the rest
  93. // of multipart message.
  94. bool _process_subscribe;
  95. // This option is enabled with ZMQ_ONLY_FIRST_SUBSCRIBE.
  96. // If true, messages following subscribe/unsubscribe in a multipart
  97. // message are treated as user data regardless of the first byte.
  98. bool _only_first_subscribe;
  99. ZMQ_NON_COPYABLE_NOR_MOVABLE (xsub_t)
  100. };
  101. }
  102. #endif