zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
reactor.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 
10 #pragma once
11 
12 #include <unordered_map>
13 #include <vector>
14 #include <map>
15 #include <functional>
16 
17 #include "compatibility.hpp"
18 #include "poller.hpp"
19 
20 namespace zmqpp
21 {
22 
23  class socket;
24  typedef socket socket_t;
25 
32  class reactor
33  {
34  public:
35  typedef std::function<void (void) > Callable;
36  typedef std::pair<zmq_pollitem_t, Callable> PollItemCallablePair;
40  reactor();
41 
47  ~reactor();
48 
56  void add(socket_t& socket, Callable callable, short const event = poller::poll_in);
57 
65  void add(raw_socket_t const descriptor, Callable callable, short const event = poller::poll_in | poller::poll_error);
66 
73  bool has(socket_t const& socket);
74 
81  bool has(raw_socket_t const descriptor);
82 
88  void remove(socket_t const& socket);
89 
95  void remove(raw_socket_t const descriptor);
96 
103  void check_for(socket_t const& socket, short const event);
104 
111  void check_for(raw_socket_t const descriptor, short const event);
112 
124  bool poll(long timeout = poller::wait_forever);
125 
132  short events(socket_t const& socket) const;
133 
140  short events(raw_socket_t const descriptor) const;
141 
142 
147  poller &get_poller();
148 
153  const poller &get_poller() const;
154 
155  protected:
156  void add(const zmq_pollitem_t &item, Callable callable);
157 
158  private:
159  std::vector<PollItemCallablePair> items_;
160  std::vector<const socket_t *> sockRemoveLater_;
161  std::vector<raw_socket_t> fdRemoveLater_;
162 
167  void flush_remove_later();
168 
171  };
172 
173 }
static const short poll_error
Definition: poller.hpp:44
bool has(socket_t const &socket)
Check if we are monitoring a given socket with this reactor.
Definition: reactor.cpp:53
std::vector< const socket_t * > sockRemoveLater_
Definition: reactor.hpp:160
bool poll(long timeout=poller::wait_forever)
Poll for monitored events and call associated handler when needed.
Definition: reactor.cpp:111
C++ wrapper around zmq.
Definition: actor.cpp:29
poller poller_
Definition: reactor.hpp:169
The socket class represents the zmq sockets.
Definition: socket.hpp:75
socket socket_t
socket type
Definition: poller.hpp:28
void add(socket_t &socket, Callable callable, short const event=poller::poll_in)
Add a socket to the reactor, providing a handler that will be called when the monitored events occur...
Definition: reactor.cpp:34
std::pair< zmq_pollitem_t, Callable > PollItemCallablePair
Definition: reactor.hpp:36
int raw_socket_t
Definition: compatibility.hpp:116
poller & get_poller()
Get a reference to the underlying poller object used by the reactor.
Definition: reactor.cpp:140
std::vector< raw_socket_t > fdRemoveLater_
Definition: reactor.hpp:161
std::function< void(void) > Callable
Definition: reactor.hpp:35
short events(socket_t const &socket) const
Get the event flags triggered for a socket.
Definition: reactor.cpp:130
Reactor object that helps to manage multiple socket by calling a user-defined handler for each socket...
Definition: reactor.hpp:32
~reactor()
Cleanup reactor.
Definition: reactor.cpp:30
reactor()
Construct an empty polling model.
Definition: reactor.cpp:24
std::vector< PollItemCallablePair > items_
Definition: reactor.hpp:159
void flush_remove_later()
Flush the fdRemoveLater_ and sockRemoveLater_ vector, effectively removing the item for the reactor a...
Definition: reactor.cpp:150
void check_for(socket_t const &socket, short const event)
Update the monitored event flags for a given socket.
Definition: reactor.cpp:101
static const long wait_forever
Definition: poller.hpp:39
bool dispatching_
Definition: reactor.hpp:170
static const short poll_in
Definition: poller.hpp:42
Polling wrapper.
Definition: poller.hpp:36