libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
pgm_receiver.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __ZMQ_PGM_RECEIVER_HPP_INCLUDED__
31 #define __ZMQ_PGM_RECEIVER_HPP_INCLUDED__
32 
33 #include "platform.hpp"
34 
35 #if defined ZMQ_HAVE_OPENPGM
36 
37 #ifdef ZMQ_HAVE_WINDOWS
38 #include "windows.hpp"
39 #endif
40 
41 #include <map>
42 #include <algorithm>
43 
44 #include "io_object.hpp"
45 #include "i_engine.hpp"
46 #include "options.hpp"
47 #include "v1_decoder.hpp"
48 #include "pgm_socket.hpp"
49 
50 namespace zmq
51 {
52 
53  class io_thread_t;
54  class session_base_t;
55 
56  class pgm_receiver_t : public io_object_t, public i_engine
57  {
58 
59  public:
60 
61  pgm_receiver_t (zmq::io_thread_t *parent_, const options_t &options_);
62  ~pgm_receiver_t ();
63 
64  int init (bool udp_encapsulation_, const char *network_);
65 
66  // i_engine interface implementation.
67  void plug (zmq::io_thread_t *io_thread_,
68  zmq::session_base_t *session_);
69  void terminate ();
70  void restart_input ();
71  void restart_output ();
72  void zap_msg_available () {}
73 
74  // i_poll_events interface implementation.
75  void in_event ();
76  void timer_event (int token);
77 
78  private:
79 
80  // Unplug the engine from the session.
81  void unplug ();
82 
83  // Decode received data (inpos, insize) and forward decoded
84  // messages to the session.
85  int process_input (v1_decoder_t *decoder);
86 
87  // PGM is not able to move subscriptions upstream. Thus, drop all
88  // the pending subscriptions.
89  void drop_subscriptions ();
90 
91  // RX timeout timer ID.
92  enum {rx_timer_id = 0xa1};
93 
94  // RX timer is running.
95  bool has_rx_timer;
96 
97  // If joined is true we are already getting messages from the peer.
98  // It it's false, we are getting data but still we haven't seen
99  // beginning of a message.
100  struct peer_info_t
101  {
102  bool joined;
103  v1_decoder_t *decoder;
104  };
105 
106  struct tsi_comp
107  {
108  bool operator () (const pgm_tsi_t &ltsi,
109  const pgm_tsi_t &rtsi) const
110  {
111  uint32_t ll[2], rl[2];
112  memcpy (ll, &ltsi, sizeof (ll));
113  memcpy (rl, &rtsi, sizeof (rl));
114  return (ll[0] < rl[0]) || (ll[0] == rl[0] && ll[1] < rl[1]);
115  }
116  };
117 
118  typedef std::map <pgm_tsi_t, peer_info_t, tsi_comp> peers_t;
119  peers_t peers;
120 
121  // PGM socket.
122  pgm_socket_t pgm_socket;
123 
124  // Socket options.
125  options_t options;
126 
127  // Associated session.
128  zmq::session_base_t *session;
129 
130  const pgm_tsi_t *active_tsi;
131 
132  // Number of bytes not consumed by the decoder due to pipe overflow.
133  size_t insize;
134 
135  // Pointer to data still waiting to be processed by the decoder.
136  const unsigned char *inpos;
137 
138  // Poll handle associated with PGM socket.
139  handle_t socket_handle;
140 
141  // Poll handle associated with engine PGM waiting pipe.
142  handle_t pipe_handle;
143 
144  pgm_receiver_t (const pgm_receiver_t&);
145  const pgm_receiver_t &operator = (const pgm_receiver_t&);
146  };
147 
148 }
149 
150 #endif
151 
152 #endif
Definition: command.hpp:81
Definition: address.hpp:35
static uint8_t decoder[96]
Definition: zmq_utils.cpp:103