router.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_ROUTER_HPP_INCLUDED__
  25. #define __ZMQ_ROUTER_HPP_INCLUDED__
  26. #include <map>
  27. #include "socket_base.hpp"
  28. #include "session_base.hpp"
  29. #include "stdint.hpp"
  30. #include "blob.hpp"
  31. #include "msg.hpp"
  32. #include "fq.hpp"
  33. namespace zmq
  34. {
  35. class ctx_t;
  36. class pipe_t;
  37. // TODO: This class uses O(n) scheduling. Rewrite it to use O(1) algorithm.
  38. class router_t : public routing_socket_base_t
  39. {
  40. public:
  41. router_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
  42. ~router_t () ZMQ_OVERRIDE;
  43. // Overrides of functions from socket_base_t.
  44. void xattach_pipe (zmq::pipe_t *pipe_,
  45. bool subscribe_to_all_,
  46. bool locally_initiated_) ZMQ_FINAL;
  47. int
  48. xsetsockopt (int option_, const void *optval_, size_t optvallen_) ZMQ_FINAL;
  49. int xsend (zmq::msg_t *msg_) ZMQ_OVERRIDE;
  50. int xrecv (zmq::msg_t *msg_) ZMQ_OVERRIDE;
  51. bool xhas_in () ZMQ_OVERRIDE;
  52. bool xhas_out () ZMQ_OVERRIDE;
  53. void xread_activated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  54. void xpipe_terminated (zmq::pipe_t *pipe_) ZMQ_FINAL;
  55. int get_peer_state (const void *routing_id_,
  56. size_t routing_id_size_) const ZMQ_FINAL;
  57. protected:
  58. // Rollback any message parts that were sent but not yet flushed.
  59. int rollback ();
  60. private:
  61. // Receive peer id and update lookup map
  62. bool identify_peer (pipe_t *pipe_, bool locally_initiated_);
  63. // Fair queueing object for inbound pipes.
  64. fq_t _fq;
  65. // True iff there is a message held in the pre-fetch buffer.
  66. bool _prefetched;
  67. // If true, the receiver got the message part with
  68. // the peer's identity.
  69. bool _routing_id_sent;
  70. // Holds the prefetched identity.
  71. msg_t _prefetched_id;
  72. // Holds the prefetched message.
  73. msg_t _prefetched_msg;
  74. // The pipe we are currently reading from
  75. zmq::pipe_t *_current_in;
  76. // Should current_in should be terminate after all parts received?
  77. bool _terminate_current_in;
  78. // If true, more incoming message parts are expected.
  79. bool _more_in;
  80. // We keep a set of pipes that have not been identified yet.
  81. std::set<pipe_t *> _anonymous_pipes;
  82. // The pipe we are currently writing to.
  83. zmq::pipe_t *_current_out;
  84. // If true, more outgoing message parts are expected.
  85. bool _more_out;
  86. // Routing IDs are generated. It's a simple increment and wrap-over
  87. // algorithm. This value is the next ID to use (if not used already).
  88. uint32_t _next_integral_routing_id;
  89. // If true, report EAGAIN to the caller instead of silently dropping
  90. // the message targeting an unknown peer.
  91. bool _mandatory;
  92. bool _raw_socket;
  93. // if true, send an empty message to every connected router peer
  94. bool _probe_router;
  95. // If true, the router will reassign an identity upon encountering a
  96. // name collision. The new pipe will take the identity, the old pipe
  97. // will be terminated.
  98. bool _handover;
  99. ZMQ_NON_COPYABLE_NOR_MOVABLE (router_t)
  100. };
  101. }
  102. #endif