zmqpp  4.1.2
C++ bindings for 0mq (libzmq)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
zmqpp::reactor Class Reference

Reactor object that helps to manage multiple socket by calling a user-defined handler for each socket when a watched event occurs. More...

#include <reactor.hpp>

Collaboration diagram for zmqpp::reactor:
Collaboration graph
[legend]

Public Types

typedef std::function< void(void) > Callable
 
typedef std::pair
< zmq_pollitem_t, Callable
PollItemCallablePair
 

Public Member Functions

 reactor ()
 Construct an empty polling model. More...
 
 ~reactor ()
 Cleanup reactor. More...
 
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. More...
 
void add (raw_socket_t const descriptor, Callable callable, short const event=poller::poll_in|poller::poll_error)
 
bool has (socket_t const &socket)
 Check if we are monitoring a given socket with this reactor. More...
 
bool has (raw_socket_t const descriptor)
 Check if we are monitoring a given standard socket with this reactor. More...
 
void remove (socket_t const &socket)
 Stop monitoring a socket. More...
 
void remove (raw_socket_t const descriptor)
 Stop monitoring a standard socket. More...
 
void check_for (socket_t const &socket, short const event)
 Update the monitored event flags for a given socket. More...
 
void check_for (raw_socket_t const descriptor, short const event)
 
bool poll (long timeout=poller::wait_forever)
 Poll for monitored events and call associated handler when needed. More...
 
short events (socket_t const &socket) const
 Get the event flags triggered for a socket. More...
 
short events (raw_socket_t const descriptor) const
 Get the event flags triggered for a standard socket. More...
 
pollerget_poller ()
 Get a reference to the underlying poller object used by the reactor. More...
 
const pollerget_poller () const
 Get a reference to the underlying poller object used by the reactor (const version). More...
 

Protected Member Functions

void add (const zmq_pollitem_t &item, Callable callable)
 

Private Member Functions

void flush_remove_later ()
 Flush the fdRemoveLater_ and sockRemoveLater_ vector, effectively removing the item for the reactor and poller. More...
 

Private Attributes

std::vector< PollItemCallablePairitems_
 
std::vector< const socket_t * > sockRemoveLater_
 
std::vector< raw_socket_tfdRemoveLater_
 
poller poller_
 
bool dispatching_
 

Detailed Description

Reactor object that helps to manage multiple socket by calling a user-defined handler for each socket when a watched event occurs.

It uses zmq::poller as the underlying polling mechanism.

Member Typedef Documentation

typedef std::function<void (void) > zmqpp::reactor::Callable
typedef std::pair<zmq_pollitem_t, Callable> zmqpp::reactor::PollItemCallablePair

Constructor & Destructor Documentation

zmqpp::reactor::reactor ( )

Construct an empty polling model.

zmqpp::reactor::~reactor ( )

Cleanup reactor.

Any sockets will need to be closed separately.

Member Function Documentation

void zmqpp::reactor::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.

Parameters
socketthe socket to monitor.
callablethe function that will be called by the reactor when a registered event occurs on socket.
eventthe event flags to monitor on the socket.
void zmqpp::reactor::add ( raw_socket_t const  descriptor,
Callable  callable,
short const  event = poller::poll_in | poller::poll_error 
)

Add a standard socket to the reactor, providing a handler that will be called when the monitored events occur.

Parameters
descriptorthe standard socket to monitor (SOCKET under Windows, a file descriptor otherwise).
callablethe function that will be called by the reactor when a registered event occurs on fd.
eventthe event flags to monitor.
void zmqpp::reactor::add ( const zmq_pollitem_t &  item,
Callable  callable 
)
protected
void zmqpp::reactor::check_for ( socket_t const &  socket,
short const  event 
)

Update the monitored event flags for a given socket.

Parameters
socketthe socket to update event flags.
eventthe event flags to monitor on the socket.
void zmqpp::reactor::check_for ( raw_socket_t const  descriptor,
short const  event 
)

Update the monitored event flags for a given standard socket.

Parameters
descriptorthe raw socket to update event flags (SOCKET under Windows, a file descriptor otherwise).
eventthe event flags to monitor on the socket.
short zmqpp::reactor::events ( socket_t const &  socket) const

Get the event flags triggered for a socket.

Parameters
socketthe socket to get triggered event flags for.
Returns
the event flags.
short zmqpp::reactor::events ( raw_socket_t const  descriptor) const

Get the event flags triggered for a standard socket.

Parameters
descriptorthe raw socket to get triggered event flags for (SOCKET under Windows, a file descriptor otherwise).
Returns
the event flags.
void zmqpp::reactor::flush_remove_later ( )
private

Flush the fdRemoveLater_ and sockRemoveLater_ vector, effectively removing the item for the reactor and poller.

poller & zmqpp::reactor::get_poller ( )

Get a reference to the underlying poller object used by the reactor.

Not sure this is useful.

const poller & zmqpp::reactor::get_poller ( ) const

Get a reference to the underlying poller object used by the reactor (const version).

Not sure this is useful either.

bool zmqpp::reactor::has ( socket_t const &  socket)

Check if we are monitoring a given socket with this reactor.

Parameters
socketthe socket to check.
Returns
true if it is there.
bool zmqpp::reactor::has ( raw_socket_t const  descriptor)

Check if we are monitoring a given standard socket with this reactor.

Parameters
descriptorthe raw socket to check (SOCKET under Windows, a file descriptor otherwise).
Returns
true if it is there.
bool zmqpp::reactor::poll ( long  timeout = poller::wait_forever)

Poll for monitored events and call associated handler when needed.

By default this method will block forever or until at least one of the monitored sockets or file descriptors has events.

If a timeout is set and was reached then this function returns false.

Parameters
timeoutmilliseconds to timeout.
Returns
true if there is an event..
void zmqpp::reactor::remove ( socket_t const &  socket)

Stop monitoring a socket.

Parameters
socketthe socket to stop monitoring.
void zmqpp::reactor::remove ( raw_socket_t const  descriptor)

Stop monitoring a standard socket.

Parameters
descriptorthe standard socket to stop monitoring.

Member Data Documentation

bool zmqpp::reactor::dispatching_
private
std::vector<raw_socket_t> zmqpp::reactor::fdRemoveLater_
private
std::vector<PollItemCallablePair> zmqpp::reactor::items_
private
poller zmqpp::reactor::poller_
private
std::vector<const socket_t *> zmqpp::reactor::sockRemoveLater_
private

The documentation for this class was generated from the following files: