Skip to content

Instantly share code, notes, and snippets.

@vyskocilm
Created Feb 24, 2016
Embed
What would you like to do?
demo on zloop_t
#include <czmq.h>
static int
xzloop_add_fd (zloop_t *self, int fd, zloop_fn handler, void *arg)
{
assert (self);
zmq_pollitem_t *fditem = (zmq_pollitem_t*) zmalloc (sizeof (zmq_pollitem_t));
assert (fditem);
fditem->fd = fd;
fditem->events = ZMQ_POLLIN;
int r = zloop_poller (self, fditem, handler, arg);
free (fditem);
return r;
}
static int
s_handler (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
char buff [4096+1];
memset (buff, 0, 4096);
ssize_t r = read (item->fd, buff, 4096);
zsys_debug ("buf[%zd]='%s'", r, buff);
return 0;
}
static int
s_timer_handler (zloop_t *loop, int timer_id, void *args)
{
zsys_debug ("BAF10");
return -1; // stop the loop
}
int main () {
zloop_t *loop = zloop_new ();
assert (loop);
xzloop_add_fd (loop, STDIN_FILENO, s_handler, NULL);
zsys_debug ("BAF1");
zloop_timer (loop, 5000, 1, s_timer_handler, NULL);
zsys_debug ("BAF2");
zloop_start (loop);
zloop_destroy (&loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment