| 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_SOCKET_POLLER_HPP_INCLUDED__ |
| 31 | #define __ZMQ_SOCKET_POLLER_HPP_INCLUDED__ |
| 32 | |
| 33 | #include "poller.hpp" |
| 34 | |
| 35 | #if defined ZMQ_POLL_BASED_ON_POLL && !defined ZMQ_HAVE_WINDOWS |
| 36 | #include <poll.h> |
| 37 | #endif |
| 38 | |
| 39 | #if defined ZMQ_HAVE_WINDOWS |
| 40 | #include "windows.hpp" |
| 41 | #elif defined ZMQ_HAVE_VXWORKS |
| 42 | #include <unistd.h> |
| 43 | #include <sys/time.h> |
| 44 | #include <strings.h> |
| 45 | #else |
| 46 | #include <unistd.h> |
| 47 | #endif |
| 48 | |
| 49 | #include <vector> |
| 50 | |
| 51 | #include "socket_base.hpp" |
| 52 | #include "signaler.hpp" |
| 53 | #include "polling_util.hpp" |
| 54 | |
| 55 | namespace zmq |
| 56 | { |
| 57 | class socket_poller_t |
| 58 | { |
| 59 | public: |
| 60 | socket_poller_t (); |
| 61 | ~socket_poller_t (); |
| 62 | |
| 63 | typedef struct event_t |
| 64 | { |
| 65 | socket_base_t *socket; |
| 66 | fd_t fd; |
| 67 | void *user_data; |
| 68 | short events; |
| 69 | } event_t; |
| 70 | |
| 71 | int add (socket_base_t *socket_, void *user_data_, short events_); |
| 72 | int modify (socket_base_t *socket_, short events_); |
| 73 | int remove (socket_base_t *socket_); |
| 74 | |
| 75 | int add_fd (fd_t fd_, void *user_data_, short events_); |
| 76 | int modify_fd (fd_t fd_, short events_); |
| 77 | int remove_fd (fd_t fd_); |
| 78 | // Returns the signaler's fd if there is one, otherwise errors. |
| 79 | int signaler_fd (fd_t *fd_); |
| 80 | |
| 81 | int wait (event_t *events_, int n_events_, long timeout_); |
| 82 | |
| 83 | inline int size () { return static_cast<int> (_items.size ()); }; |
| 84 | |
| 85 | // Return false if object is not a socket. |
| 86 | bool check_tag (); |
| 87 | |
| 88 | private: |
| 89 | void zero_trail_events (zmq::socket_poller_t::event_t *events_, |
| 90 | int n_events_, |
| 91 | int found_); |
| 92 | #if defined ZMQ_POLL_BASED_ON_POLL |
| 93 | int check_events (zmq::socket_poller_t::event_t *events_, int n_events_); |
| 94 | #elif defined ZMQ_POLL_BASED_ON_SELECT |
| 95 | int check_events (zmq::socket_poller_t::event_t *events_, |
| 96 | int n_events_, |
| 97 | fd_set &inset_, |
| 98 | fd_set &outset_, |
| 99 | fd_set &errset_); |
| 100 | #endif |
| 101 | int adjust_timeout (zmq::clock_t &clock_, |
| 102 | long timeout_, |
| 103 | uint64_t &now_, |
| 104 | uint64_t &end_, |
| 105 | bool &first_pass_); |
| 106 | int rebuild (); |
| 107 | |
| 108 | // Used to check whether the object is a socket_poller. |
| 109 | uint32_t _tag; |
| 110 | |
| 111 | // Signaler used for thread safe sockets polling |
| 112 | signaler_t *_signaler; |
| 113 | |
| 114 | typedef struct item_t |
| 115 | { |
| 116 | socket_base_t *socket; |
| 117 | fd_t fd; |
| 118 | void *user_data; |
| 119 | short events; |
| 120 | #if defined ZMQ_POLL_BASED_ON_POLL |
| 121 | int pollfd_index; |
| 122 | #endif |
| 123 | } item_t; |
| 124 | |
| 125 | // List of sockets |
| 126 | typedef std::vector<item_t> items_t; |
| 127 | items_t _items; |
| 128 | |
| 129 | // Does the pollset needs rebuilding? |
| 130 | bool _need_rebuild; |
| 131 | |
| 132 | // Should the signaler be used for the thread safe polling? |
| 133 | bool _use_signaler; |
| 134 | |
| 135 | // Size of the pollset |
| 136 | int _pollset_size; |
| 137 | |
| 138 | #if defined ZMQ_POLL_BASED_ON_POLL |
| 139 | pollfd *_pollfds; |
| 140 | #elif defined ZMQ_POLL_BASED_ON_SELECT |
| 141 | resizable_optimized_fd_set_t _pollset_in; |
| 142 | resizable_optimized_fd_set_t _pollset_out; |
| 143 | resizable_optimized_fd_set_t _pollset_err; |
| 144 | zmq::fd_t _max_fd; |
| 145 | #endif |
| 146 | |
| 147 | ZMQ_NON_COPYABLE_NOR_MOVABLE (socket_poller_t) |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | #endif |
| 152 | |