xpub.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_XPUB_HPP_INCLUDED__
  25. #define __ZMQ_XPUB_HPP_INCLUDED__
  26. #include <deque>
  27. #include "socket_base.hpp"
  28. #include "session_base.hpp"
  29. #include "mtrie.hpp"
  30. #include "dist.hpp"
  31. namespace zmq
  32. {
  33. class ctx_t;
  34. class msg_t;
  35. class pipe_t;
  36. class io_thread_t;
  37. class xpub_t : public socket_base_t
  38. {
  39. public:
  40. xpub_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
  41. ~xpub_t () ZMQ_OVERRIDE;
  42. // Implementations of virtual functions from socket_base_t.
  43. void xattach_pipe (zmq::pipe_t *pipe_,
  44. bool subscribe_to_all_ = false,
  45. bool locally_initiated_ = false) ZMQ_OVERRIDE;
  46. int xsend (zmq::msg_t *msg_) ZMQ_FINAL;
  47. bool xhas_out () ZMQ_FINAL;
  48. int xrecv (zmq::msg_t *msg_) ZMQ_OVERRIDE;
  49. bool xhas_in () ZMQ_OVERRIDE;
  50. void xread_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  51. void xwrite_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  52. int
  53. xsetsockopt (int option_, const void *optval_, size_t optvallen_) ZMQ_FINAL;
  54. int xgetsockopt (int option_, void *optval_, size_t *optvallen_) ZMQ_FINAL;
  55. void xpipe_terminated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  56. private:
  57. // Function to be applied to the trie to send all the subscriptions
  58. // upstream.
  59. static void send_unsubscription (zmq::mtrie_t::prefix_t data_,
  60. size_t size_,
  61. xpub_t *self_);
  62. // Function to be applied to each matching pipes.
  63. static void mark_as_matching (zmq::pipe_t *pipe_, xpub_t *self_);
  64. // List of all subscriptions mapped to corresponding pipes.
  65. mtrie_t _subscriptions;
  66. // List of manual subscriptions mapped to corresponding pipes.
  67. mtrie_t _manual_subscriptions;
  68. // Distributor of messages holding the list of outbound pipes.
  69. dist_t _dist;
  70. // If true, send all subscription messages upstream, not just
  71. // unique ones
  72. bool _verbose_subs;
  73. // If true, send all unsubscription messages upstream, not just
  74. // unique ones
  75. bool _verbose_unsubs;
  76. // True if we are in the middle of sending a multi-part message.
  77. bool _more_send;
  78. // True if we are in the middle of receiving a multi-part message.
  79. bool _more_recv;
  80. // If true, subscribe and cancel messages are processed for the rest
  81. // of multipart message.
  82. bool _process_subscribe;
  83. // This option is enabled with ZMQ_ONLY_FIRST_SUBSCRIBE.
  84. // If true, messages following subscribe/unsubscribe in a multipart
  85. // message are treated as user data regardless of the first byte.
  86. bool _only_first_subscribe;
  87. // Drop messages if HWM reached, otherwise return with EAGAIN
  88. bool _lossy;
  89. // Subscriptions will not bed added automatically, only after calling set option with ZMQ_SUBSCRIBE or ZMQ_UNSUBSCRIBE
  90. bool _manual;
  91. // Send message to the last pipe, only used if xpub is on manual and after calling set option with ZMQ_SUBSCRIBE
  92. bool _send_last_pipe;
  93. // Function to be applied to match the last pipe.
  94. static void mark_last_pipe_as_matching (zmq::pipe_t *pipe_, xpub_t *self_);
  95. // Last pipe that sent subscription message, only used if xpub is on manual
  96. pipe_t *_last_pipe;
  97. // Pipes that sent subscriptions messages that have not yet been processed, only used if xpub is on manual
  98. std::deque<pipe_t *> _pending_pipes;
  99. // Welcome message to send to pipe when attached
  100. msg_t _welcome_msg;
  101. // List of pending (un)subscriptions, ie. those that were already
  102. // applied to the trie, but not yet received by the user.
  103. std::deque<blob_t> _pending_data;
  104. std::deque<metadata_t *> _pending_metadata;
  105. std::deque<unsigned char> _pending_flags;
  106. ZMQ_NON_COPYABLE_NOR_MOVABLE (xpub_t)
  107. };
  108. }
  109. #endif