pgm_receiver.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_PGM_RECEIVER_HPP_INCLUDED__
  25. #define __ZMQ_PGM_RECEIVER_HPP_INCLUDED__
  26. #if defined ZMQ_HAVE_OPENPGM
  27. #include <map>
  28. #include <algorithm>
  29. #include "io_object.hpp"
  30. #include "i_engine.hpp"
  31. #include "options.hpp"
  32. #include "v1_decoder.hpp"
  33. #include "pgm_socket.hpp"
  34. namespace zmq
  35. {
  36. class io_thread_t;
  37. class session_base_t;
  38. class pgm_receiver_t ZMQ_FINAL : public io_object_t, public i_engine
  39. {
  40. public:
  41. pgm_receiver_t (zmq::io_thread_t *parent_, const options_t &options_);
  42. ~pgm_receiver_t ();
  43. int init (bool udp_encapsulation_, const char *network_);
  44. // i_engine interface implementation.
  45. bool has_handshake_stage () { return false; };
  46. void plug (zmq::io_thread_t *io_thread_, zmq::session_base_t *session_);
  47. void terminate ();
  48. bool restart_input ();
  49. void restart_output ();
  50. void zap_msg_available () {}
  51. const endpoint_uri_pair_t &get_endpoint () const;
  52. // i_poll_events interface implementation.
  53. void in_event ();
  54. void timer_event (int token);
  55. private:
  56. // Unplug the engine from the session.
  57. void unplug ();
  58. // Decode received data (inpos, insize) and forward decoded
  59. // messages to the session.
  60. int process_input (v1_decoder_t *decoder);
  61. // PGM is not able to move subscriptions upstream. Thus, drop all
  62. // the pending subscriptions.
  63. void drop_subscriptions ();
  64. // RX timeout timer ID.
  65. enum
  66. {
  67. rx_timer_id = 0xa1
  68. };
  69. const endpoint_uri_pair_t _empty_endpoint;
  70. // RX timer is running.
  71. bool has_rx_timer;
  72. // If joined is true we are already getting messages from the peer.
  73. // It it's false, we are getting data but still we haven't seen
  74. // beginning of a message.
  75. struct peer_info_t
  76. {
  77. bool joined;
  78. v1_decoder_t *decoder;
  79. };
  80. struct tsi_comp
  81. {
  82. bool operator() (const pgm_tsi_t &ltsi, const pgm_tsi_t &rtsi) const
  83. {
  84. uint32_t ll[2], rl[2];
  85. memcpy (ll, &ltsi, sizeof (ll));
  86. memcpy (rl, &rtsi, sizeof (rl));
  87. return (ll[0] < rl[0]) || (ll[0] == rl[0] && ll[1] < rl[1]);
  88. }
  89. };
  90. typedef std::map<pgm_tsi_t, peer_info_t, tsi_comp> peers_t;
  91. peers_t peers;
  92. // PGM socket.
  93. pgm_socket_t pgm_socket;
  94. // Socket options.
  95. options_t options;
  96. // Associated session.
  97. zmq::session_base_t *session;
  98. const pgm_tsi_t *active_tsi;
  99. // Number of bytes not consumed by the decoder due to pipe overflow.
  100. size_t insize;
  101. // Pointer to data still waiting to be processed by the decoder.
  102. const unsigned char *inpos;
  103. // Poll handle associated with PGM socket.
  104. handle_t socket_handle;
  105. // Poll handle associated with engine PGM waiting pipe.
  106. handle_t pipe_handle;
  107. ZMQ_NON_COPYABLE_NOR_MOVABLE (pgm_receiver_t)
  108. };
  109. }
  110. #endif
  111. #endif