libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
select.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_SELECT_HPP_INCLUDED__
31 #define __ZMQ_SELECT_HPP_INCLUDED__
32 
33 // poller.hpp decides which polling mechanism to use.
34 #include "poller.hpp"
35 #if defined ZMQ_USE_SELECT
36 
37 #include "platform.hpp"
38 
39 #include <stddef.h>
40 #include <vector>
41 #include <map>
42 
43 #if defined ZMQ_HAVE_WINDOWS
44 #include "windows.hpp"
45 #elif defined ZMQ_HAVE_OPENVMS
46 #include <sys/types.h>
47 #include <sys/time.h>
48 #else
49 #include <sys/select.h>
50 #endif
51 
52 #include "ctx.hpp"
53 #include "fd.hpp"
54 #include "thread.hpp"
55 #include "poller_base.hpp"
56 
57 namespace zmq
58 {
59 
60  struct i_poll_events;
61 
62  // Implements socket polling mechanism using POSIX.1-2001 select()
63  // function.
64 
65  class select_t : public poller_base_t
66  {
67  public:
68 
69  typedef fd_t handle_t;
70 
71  select_t (const ctx_t &ctx_);
72  ~select_t ();
73 
74  // "poller" concept.
75  handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
76  void rm_fd (handle_t handle_);
77  void set_pollin (handle_t handle_);
78  void reset_pollin (handle_t handle_);
79  void set_pollout (handle_t handle_);
80  void reset_pollout (handle_t handle_);
81  void start ();
82  void stop ();
83 
84  static int max_fds ();
85 
86  private:
87 
88  // Main worker thread routine.
89  static void worker_routine (void *arg_);
90 
91  // Main event loop.
92  void loop ();
93 
94  // Reference to ZMQ context.
95  const ctx_t &ctx;
96 
97  // Internal state.
98  struct fds_set_t
99  {
100  fds_set_t ();
101  fds_set_t (const fds_set_t& other_);
102  fds_set_t& operator=(const fds_set_t& other_);
103  // Convinient method to descriptor from all sets.
104  void remove_fd (const fd_t& fd_);
105 
106  fd_set read;
107  fd_set write;
108  fd_set error;
109  };
110 
111  struct fd_entry_t
112  {
113  fd_t fd;
114  zmq::i_poll_events* events;
115  };
116  typedef std::vector<fd_entry_t> fd_entries_t;
117 
118 #if defined ZMQ_HAVE_WINDOWS
119  struct family_entry_t
120  {
121  family_entry_t ();
122 
123  fd_entries_t fd_entries;
124  fds_set_t fds_set;
125  bool retired;
126  };
127  typedef std::map<u_short, family_entry_t> family_entries_t;
128 
129  struct wsa_events_t
130  {
131  wsa_events_t ();
132  ~wsa_events_t ();
133 
134  // read, write, error and readwrite
135  WSAEVENT events [4];
136  };
137 #endif
138 
139 #if defined ZMQ_HAVE_WINDOWS
140  family_entries_t family_entries;
141  // See loop for details.
142  family_entries_t::iterator current_family_entry_it;
143 #else
144  fd_entries_t fd_entries;
145  fds_set_t fds_set;
146  fd_t maxfd;
147  bool retired;
148 #endif
149 
150 #if defined ZMQ_HAVE_WINDOWS
151  // Socket's family or AF_UNSPEC on error.
152  static u_short get_fd_family (fd_t fd_);
153 #endif
154  // Checks if an fd_entry_t is retired.
155  static bool is_retired_fd (const fd_entry_t &entry);
156 
157  // If true, thread is shutting down.
158  bool stopping;
159 
160  // Handle of the physical thread doing the I/O work.
161  thread_t worker;
162 
163  select_t (const select_t&);
164  const select_t &operator = (const select_t&);
165  };
166 
167  typedef select_t poller_t;
168 
169 }
170 
171 #endif
172 
173 #endif
int fd_t
Definition: fd.hpp:50
static void worker(void *s)
Definition: address.hpp:35
Definition: command.hpp:80