/src/libzmq/src/socket_poller.hpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #ifndef __ZMQ_SOCKET_POLLER_HPP_INCLUDED__ |
4 | | #define __ZMQ_SOCKET_POLLER_HPP_INCLUDED__ |
5 | | |
6 | | #include "poller.hpp" |
7 | | |
8 | | #if defined ZMQ_POLL_BASED_ON_POLL && !defined ZMQ_HAVE_WINDOWS |
9 | | #include <poll.h> |
10 | | #endif |
11 | | |
12 | | #if defined ZMQ_HAVE_WINDOWS |
13 | | #include "windows.hpp" |
14 | | #elif defined ZMQ_HAVE_VXWORKS |
15 | | #include <unistd.h> |
16 | | #include <sys/time.h> |
17 | | #include <strings.h> |
18 | | #else |
19 | | #include <unistd.h> |
20 | | #endif |
21 | | |
22 | | #include <vector> |
23 | | |
24 | | #include "socket_base.hpp" |
25 | | #include "signaler.hpp" |
26 | | #include "polling_util.hpp" |
27 | | |
28 | | namespace zmq |
29 | | { |
30 | | class socket_poller_t |
31 | | { |
32 | | public: |
33 | | socket_poller_t (); |
34 | | ~socket_poller_t (); |
35 | | |
36 | | typedef zmq_poller_event_t event_t; |
37 | | |
38 | | int add (socket_base_t *socket_, void *user_data_, short events_); |
39 | | int modify (const socket_base_t *socket_, short events_); |
40 | | int remove (socket_base_t *socket_); |
41 | | |
42 | | int add_fd (fd_t fd_, void *user_data_, short events_); |
43 | | int modify_fd (fd_t fd_, short events_); |
44 | | int remove_fd (fd_t fd_); |
45 | | // Returns the signaler's fd if there is one, otherwise errors. |
46 | | int signaler_fd (fd_t *fd_) const; |
47 | | |
48 | | int wait (event_t *events_, int n_events_, long timeout_); |
49 | | |
50 | 0 | int size () const { return static_cast<int> (_items.size ()); }; |
51 | | |
52 | | // Return false if object is not a socket. |
53 | | bool check_tag () const; |
54 | | |
55 | | private: |
56 | | typedef struct item_t |
57 | | { |
58 | | socket_base_t *socket; |
59 | | fd_t fd; |
60 | | void *user_data; |
61 | | short events; |
62 | | #if defined ZMQ_POLL_BASED_ON_POLL |
63 | | int pollfd_index; |
64 | | #endif |
65 | | } item_t; |
66 | | |
67 | | static void zero_trail_events (zmq::socket_poller_t::event_t *events_, |
68 | | int n_events_, |
69 | | int found_); |
70 | | #if defined ZMQ_POLL_BASED_ON_POLL |
71 | | int check_events (zmq::socket_poller_t::event_t *events_, int n_events_); |
72 | | #elif defined ZMQ_POLL_BASED_ON_SELECT |
73 | | int check_events (zmq::socket_poller_t::event_t *events_, |
74 | | int n_events_, |
75 | | fd_set &inset_, |
76 | | fd_set &outset_, |
77 | | fd_set &errset_); |
78 | | #endif |
79 | | static int adjust_timeout (zmq::clock_t &clock_, |
80 | | long timeout_, |
81 | | uint64_t &now_, |
82 | | uint64_t &end_, |
83 | | bool &first_pass_); |
84 | | static bool is_socket (const item_t &item, const socket_base_t *socket_) |
85 | 0 | { |
86 | 0 | return item.socket == socket_; |
87 | 0 | } |
88 | | static bool is_fd (const item_t &item, fd_t fd_) |
89 | 0 | { |
90 | 0 | return !item.socket && item.fd == fd_; |
91 | 0 | } |
92 | | |
93 | | int rebuild (); |
94 | | |
95 | | // Used to check whether the object is a socket_poller. |
96 | | uint32_t _tag; |
97 | | |
98 | | // Signaler used for thread safe sockets polling |
99 | | signaler_t *_signaler; |
100 | | |
101 | | // List of sockets |
102 | | typedef std::vector<item_t> items_t; |
103 | | items_t _items; |
104 | | |
105 | | // Does the pollset needs rebuilding? |
106 | | bool _need_rebuild; |
107 | | |
108 | | // Should the signaler be used for the thread safe polling? |
109 | | bool _use_signaler; |
110 | | |
111 | | // Size of the pollset |
112 | | int _pollset_size; |
113 | | |
114 | | #if defined ZMQ_POLL_BASED_ON_POLL |
115 | | pollfd *_pollfds; |
116 | | #elif defined ZMQ_POLL_BASED_ON_SELECT |
117 | | resizable_optimized_fd_set_t _pollset_in; |
118 | | resizable_optimized_fd_set_t _pollset_out; |
119 | | resizable_optimized_fd_set_t _pollset_err; |
120 | | zmq::fd_t _max_fd; |
121 | | #endif |
122 | | |
123 | | ZMQ_NON_COPYABLE_NOR_MOVABLE (socket_poller_t) |
124 | | }; |
125 | | } |
126 | | |
127 | | #endif |