select.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_SELECT_HPP_INCLUDED__
4 #define __ZMQ_SELECT_HPP_INCLUDED__
5 
6 // poller.hpp decides which polling mechanism to use.
7 #include "poller.hpp"
8 #if defined ZMQ_IOTHREAD_POLLER_USE_SELECT
9 
10 #include <stddef.h>
11 #include <vector>
12 #include <map>
13 
14 #if defined ZMQ_HAVE_WINDOWS
15 #elif defined ZMQ_HAVE_OPENVMS
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #else
19 #include <sys/select.h>
20 #endif
21 
22 #include "ctx.hpp"
23 #include "fd.hpp"
24 #include "poller_base.hpp"
25 
26 namespace zmq
27 {
28 struct i_poll_events;
29 
30 // Implements socket polling mechanism using POSIX.1-2001 select()
31 // function.
32 
33 class select_t ZMQ_FINAL : public worker_poller_base_t
34 {
35  public:
36  typedef fd_t handle_t;
37 
38  select_t (const thread_ctx_t &ctx_);
39  ~select_t () ZMQ_FINAL;
40 
41  // "poller" concept.
42  handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
43  void rm_fd (handle_t handle_);
44  void set_pollin (handle_t handle_);
45  void reset_pollin (handle_t handle_);
46  void set_pollout (handle_t handle_);
47  void reset_pollout (handle_t handle_);
48  void stop ();
49 
50  static int max_fds ();
51 
52  private:
53  // Main event loop.
54  void loop () ZMQ_FINAL;
55 
56  // Internal state.
57  struct fds_set_t
58  {
59  fds_set_t ();
60  fds_set_t (const fds_set_t &other_);
61  fds_set_t &operator= (const fds_set_t &other_);
62  // Convenience method to descriptor from all sets.
63  void remove_fd (const fd_t &fd_);
64 
65  fd_set read;
66  fd_set write;
67  fd_set error;
68  };
69 
70  struct fd_entry_t
71  {
72  fd_t fd;
73  zmq::i_poll_events *events;
74  };
75  typedef std::vector<fd_entry_t> fd_entries_t;
76 
77  void trigger_events (const fd_entries_t &fd_entries_,
78  const fds_set_t &local_fds_set_,
79  int event_count_);
80 
81  struct family_entry_t
82  {
83  family_entry_t ();
84 
85  fd_entries_t fd_entries;
86  fds_set_t fds_set;
87  bool has_retired;
88  };
89 
90  void select_family_entry (family_entry_t &family_entry_,
91  int max_fd_,
92  bool use_timeout_,
93  struct timeval &tv_);
94 
95 #if defined ZMQ_HAVE_WINDOWS
96  typedef std::map<u_short, family_entry_t> family_entries_t;
97 
98  struct wsa_events_t
99  {
100  wsa_events_t ();
101  ~wsa_events_t ();
102 
103  // read, write, error and readwrite
104  WSAEVENT events[4];
105  };
106 
107  family_entries_t _family_entries;
108  // See loop for details.
109  family_entries_t::iterator _current_family_entry_it;
110 
111  int try_retire_fd_entry (family_entries_t::iterator family_entry_it_,
112  zmq::fd_t &handle_);
113 
114  static const size_t fd_family_cache_size = 8;
115  std::pair<fd_t, u_short> _fd_family_cache[fd_family_cache_size];
116 
117  u_short get_fd_family (fd_t fd_);
118 
119  // Socket's family or AF_UNSPEC on error.
120  static u_short determine_fd_family (fd_t fd_);
121 #else
122  // on non-Windows, we can treat all fds as one family
123  family_entry_t _family_entry;
124  fd_t _max_fd;
125 #endif
126 
127  void cleanup_retired ();
128  bool cleanup_retired (family_entry_t &family_entry_);
129 
130  // Checks if an fd_entry_t is retired.
131  static bool is_retired_fd (const fd_entry_t &entry_);
132 
133  static fd_entries_t::iterator
134  find_fd_entry_by_handle (fd_entries_t &fd_entries_, handle_t handle_);
135 
137 };
138 
139 typedef select_t poller_t;
140 }
141 
142 #endif
143 
144 #endif
zmq::i_poll_events
Definition: i_poll_events.hpp:13
error
Definition: cJSON.c:88
zmq::fd_t
int fd_t
Definition: zmq.hpp:287
ctx.hpp
zmq
Definition: zmq.hpp:229
poller.hpp
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
fd.hpp
fd_t
zmq_fd_t fd_t
Definition: libzmq/tests/testutil.hpp:98
ZMQ_FINAL
Definition: unittest_ip_resolver.cpp:26
poller_base.hpp


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58