libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
socket_poller.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_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
36 #include <poll.h>
37 #endif
38 
39 #if defined ZMQ_HAVE_WINDOWS
40 #include "windows.hpp"
41 #else
42 #include <unistd.h>
43 #endif
44 
45 #include <vector>
46 #include <algorithm>
47 
48 #include "socket_base.hpp"
49 #include "signaler.hpp"
50 
51 namespace zmq
52 {
53 
55  {
56  public:
57  socket_poller_t ();
59 
60  typedef struct event_t
61  {
64  void *user_data;
65  short events;
66  } event_t;
67 
68  int add (socket_base_t *socket, void *user_data, short events);
69  int modify (socket_base_t *socket, short events);
70  int remove (socket_base_t *socket);
71 
72  int add_fd (fd_t fd, void *user_data, short events);
73  int modify_fd (fd_t fd, short events);
74  int remove_fd (fd_t fd);
75 
76  int wait (event_t *event, long timeout);
77 
78  // Return false if object is not a socket.
79  bool check_tag ();
80 
81  private:
82  int rebuild ();
83 
84  // Used to check whether the object is a socket_poller.
85  uint32_t tag;
86 
87  // Signaler used for thread safe sockets polling
89 
90  typedef struct item_t {
93  void *user_data;
94  short events;
95 #if defined ZMQ_POLL_BASED_ON_POLL
97 #endif
98  } item_t;
99 
100  // List of sockets
101  typedef std::vector <item_t> items_t;
102  items_t items;
103 
104  // Does the pollset needs rebuilding?
106 
107  // Should the signaler be used for the thread safe polling?
109 
110  // Size of the pollset
112 
113 #if defined ZMQ_POLL_BASED_ON_POLL
114  pollfd *pollfds;
115 #elif defined ZMQ_POLL_BASED_ON_SELECT
116  fd_set pollset_in;
117  fd_set pollset_out;
118  fd_set pollset_err;
119  zmq::fd_t maxfd;
120 #endif
121 
124  };
125 
126 }
127 
128 #endif
int wait(event_t *event, long timeout)
struct zmq::socket_poller_t::event_t event_t
std::vector< item_t > items_t
int modify(socket_base_t *socket, short events)
struct zmq::socket_poller_t::item_t item_t
int add(socket_base_t *socket, void *user_data, short events)
const socket_poller_t & operator=(const socket_poller_t &)
int modify_fd(fd_t fd, short events)
int add_fd(fd_t fd, void *user_data, short events)
Definition: address.hpp:35