zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
poller.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * This file is part of zmqpp.
7  * Copyright (c) 2011-2015 Contributors as noted in the AUTHORS file.
8  */
9 
17 #ifndef ZMQPP_POLLER_HPP_
18 #define ZMQPP_POLLER_HPP_
19 
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "compatibility.hpp"
24 
25 namespace zmqpp
26 {
27 
28 class socket;
29 typedef socket socket_t;
30 
36 class poller
37 {
38 public:
39  static const long wait_forever;
41  static const short poll_none;
42  static const short poll_in;
43  static const short poll_out;
44  static const short poll_error;
46 #if ((ZMQ_VERSION_MAJOR == 4 && ZMQ_VERSION_MINOR >= 2) || ZMQ_VERSION_MAJOR > 4)
47  static const short poll_pri;
48 #endif
49 
53  poller();
54 
60  ~poller();
61 
68  void add(socket_t& socket, short const event = poll_in);
69 
76  void add(raw_socket_t const descriptor, short const event = poll_in | poll_error);
77 
85  void add(zmq_pollitem_t const& item);
86 
93  bool has(socket_t const& socket);
94 
101  bool has(raw_socket_t const descriptor);
102 
111  bool has(zmq_pollitem_t const& item);
112 
118  void remove(socket_t const& socket);
119 
125  void remove(raw_socket_t const descriptor);
126 
132  void remove(zmq_pollitem_t const& item);
133 
140  void check_for(socket_t const& socket, short const event);
141 
148  void check_for(raw_socket_t const descriptor, short const event);
149 
156  void check_for(zmq_pollitem_t const& item, short const event);
157 
169  bool poll(long timeout = wait_forever);
170 
177  short events(socket_t const& socket) const;
178 
185  short events(raw_socket_t const descriptor) const;
186 
193  short events(zmq_pollitem_t const& item) const;
194 
203  template<typename Watched>
204  bool has_input(Watched const& watchable) const { return (events(watchable) & poll_in) != 0; }
205 
214  template<typename Watched>
215  bool has_output(Watched const& watchable) const { return (events(watchable) & poll_out) != 0; }
216 
228  template<typename Watched>
229  bool has_error(Watched const& watchable) const { return (events(watchable) & poll_error) != 0; }
230 
231 private:
232  std::vector<zmq_pollitem_t> _items;
233  std::unordered_map<void *, size_t> _index;
234  std::unordered_map<raw_socket_t, size_t> _fdindex;
235 
236  void reindex(size_t const index);
237 };
238 
239 }
240 
241 #endif /* ZMQPP_POLLER_HPP_ */
static const short poll_error
Definition: poller.hpp:44
bool has_output(Watched const &watchable) const
Check either a standard socket or zmq socket for output events.
Definition: poller.hpp:215
static const short poll_out
Definition: poller.hpp:43
poller()
Construct an empty polling model.
Definition: poller.cpp:34
C++ wrapper around zmq.
Definition: actor.cpp:29
void reindex(size_t const index)
Definition: poller.cpp:91
bool has(socket_t const &socket)
Check if we are monitoring a given socket with this poller.
Definition: poller.cpp:74
The socket class represents the zmq sockets.
Definition: socket.hpp:75
socket socket_t
socket type
Definition: poller.hpp:28
int raw_socket_t
Definition: compatibility.hpp:116
void check_for(socket_t const &socket, short const event)
Update the monitored event flags for a given socket.
Definition: poller.cpp:145
static const short poll_none
Definition: poller.hpp:41
short events(socket_t const &socket) const
Get the event flags triggered for a socket.
Definition: poller.cpp:201
bool poll(long timeout=wait_forever)
Poll for monitored events.
Definition: poller.cpp:185
std::vector< zmq_pollitem_t > _items
Definition: poller.hpp:232
bool has_input(Watched const &watchable) const
Check either a standard socket or zmq socket for input events.
Definition: poller.hpp:204
static const long wait_forever
Definition: poller.hpp:39
static const short poll_pri
Definition: poller.hpp:47
void add(socket_t &socket, short const event=poll_in)
Add a socket to the polling model and set which events to monitor.
Definition: poller.cpp:49
std::unordered_map< void *, size_t > _index
Definition: poller.hpp:233
static const short poll_in
Definition: poller.hpp:42
Polling wrapper.
Definition: poller.hpp:36
~poller()
Cleanup poller.
Definition: poller.cpp:42
std::unordered_map< raw_socket_t, size_t > _fdindex
Definition: poller.hpp:234
bool has_error(Watched const &watchable) const
Check a standard socket (file descriptor or SOCKET).
Definition: poller.hpp:229