zloop(3) ======== NAME ---- zloop - event-driven reactor SYNOPSIS -------- ---- // Callback function for reactor events typedef int (zloop_fn) (zloop_t *loop, zmq_pollitem_t *item, void *arg); // Create a new zloop reactor CZMQ_EXPORT zloop_t * zloop_new (void); // Destroy a reactor CZMQ_EXPORT void zloop_destroy (zloop_t **self_p); // Register pollitem with the reactor. When the pollitem is ready, will call // the handler, passing the arg. Returns 0 if OK, -1 if there was an error. // If you register the pollitem more than once, each instance will invoke its // corresponding handler. CZMQ_EXPORT int zloop_poller (zloop_t *self, zmq_pollitem_t *item, zloop_fn handler, void *arg); // Cancel a pollitem from the reactor, specified by socket or FD. If both // are specified, uses only socket. If multiple poll items exist for same // socket/FD, cancels ALL of them. CZMQ_EXPORT void zloop_poller_end (zloop_t *self, zmq_pollitem_t *item); // Configure a registered pollitem to ignore errors. If you do not set this, // then pollitems that have errors are removed from the reactor silently. CZMQ_EXPORT void zloop_set_tolerant (zloop_t *self, zmq_pollitem_t *item); // Register a timer that expires after some delay and repeats some number of // times. At each expiry, will call the handler, passing the arg. To // run a timer forever, use 0 times. Returns 0 if OK, -1 if there was an // error. CZMQ_EXPORT int zloop_timer (zloop_t *self, size_t delay, size_t times, zloop_fn handler, void *arg); // Cancel all timers for a specific argument (as provided in zloop_timer) CZMQ_EXPORT int zloop_timer_end (zloop_t *self, void *arg); // Set verbose tracing of reactor on/off CZMQ_EXPORT void zloop_set_verbose (zloop_t *self, bool verbose); // Start the reactor. Takes control of the thread and returns when the 0MQ // context is terminated or the process is interrupted, or any event handler // returns -1. Event handlers may register new sockets and timers, and // cancel sockets. Returns 0 if interrupted, -1 if cancelled by a handler. CZMQ_EXPORT int zloop_start (zloop_t *self); // Self test of this class CZMQ_EXPORT void zloop_test (bool verbose); ---- DESCRIPTION ----------- The zloop class provides an event-driven reactor pattern. The reactor handles zmq_pollitem_t items (pollers or writers, sockets or fds), and once-off or repeated timers. Its resolution is 1 msec. It uses a tickless timer to reduce CPU interrupts in inactive processes. EXAMPLE ------- .From zloop_test method ---- zctx_t *ctx = zctx_new (); assert (ctx); void *output = zsocket_new (ctx, ZMQ_PAIR); assert (output); zsocket_bind (output, "inproc://zloop.test"); void *input = zsocket_new (ctx, ZMQ_PAIR); assert (input); zsocket_connect (input, "inproc://zloop.test"); zloop_t *loop = zloop_new (); assert (loop); zloop_set_verbose (loop, verbose); // After 10 msecs, send a ping message to output zloop_timer (loop, 10, 1, s_timer_event, output); // When we get the ping message, end the reactor zmq_pollitem_t poll_input = { input, 0, ZMQ_POLLIN }; rc = zloop_poller (loop, &poll_input, s_socket_event, NULL); assert (rc == 0); zloop_set_tolerant (loop, &poll_input); zloop_start (loop); zloop_destroy (&loop); assert (loop == NULL); zctx_destroy (&ctx); ---- SEE ALSO -------- linkczmq:czmq[7]