dist.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_DIST_HPP_INCLUDED__
  25. #define __ZMQ_DIST_HPP_INCLUDED__
  26. #include <vector>
  27. #include "array.hpp"
  28. #include "macros.hpp"
  29. namespace zmq
  30. {
  31. class pipe_t;
  32. class msg_t;
  33. // Class manages a set of outbound pipes. It sends each messages to
  34. // each of them.
  35. class dist_t
  36. {
  37. public:
  38. dist_t ();
  39. ~dist_t ();
  40. // Adds the pipe to the distributor object.
  41. void attach (zmq::pipe_t *pipe_);
  42. // Checks if this pipe is present in the distributor.
  43. bool has_pipe (zmq::pipe_t *pipe_);
  44. // Activates pipe that have previously reached high watermark.
  45. void activated (zmq::pipe_t *pipe_);
  46. // Mark the pipe as matching. Subsequent call to send_to_matching
  47. // will send message also to this pipe.
  48. void match (zmq::pipe_t *pipe_);
  49. // Marks all pipes that are not matched as matched and vice-versa.
  50. void reverse_match ();
  51. // Mark all pipes as non-matching.
  52. void unmatch ();
  53. // Removes the pipe from the distributor object.
  54. void pipe_terminated (zmq::pipe_t *pipe_);
  55. // Send the message to the matching outbound pipes.
  56. int send_to_matching (zmq::msg_t *msg_);
  57. // Send the message to all the outbound pipes.
  58. int send_to_all (zmq::msg_t *msg_);
  59. static bool has_out ();
  60. // check HWM of all pipes matching
  61. bool check_hwm ();
  62. private:
  63. // Write the message to the pipe. Make the pipe inactive if writing
  64. // fails. In such a case false is returned.
  65. bool write (zmq::pipe_t *pipe_, zmq::msg_t *msg_);
  66. // Put the message to all active pipes.
  67. void distribute (zmq::msg_t *msg_);
  68. // List of outbound pipes.
  69. typedef array_t<zmq::pipe_t, 2> pipes_t;
  70. pipes_t _pipes;
  71. // Number of all the pipes to send the next message to.
  72. pipes_t::size_type _matching;
  73. // Number of active pipes. All the active pipes are located at the
  74. // beginning of the pipes array. These are the pipes the messages
  75. // can be sent to at the moment.
  76. pipes_t::size_type _active;
  77. // Number of pipes eligible for sending messages to. This includes all
  78. // the active pipes plus all the pipes that we can in theory send
  79. // messages to (the HWM is not yet reached), but sending a message
  80. // to them would result in partial message being delivered, ie. message
  81. // with initial parts missing.
  82. pipes_t::size_type _eligible;
  83. // True if last we are in the middle of a multipart message.
  84. bool _more;
  85. ZMQ_NON_COPYABLE_NOR_MOVABLE (dist_t)
  86. };
  87. }
  88. #endif