[zeromq-dev] How to quit from zmq_proxy gracefully

Shun Zhang aaashun at gmail.com
Wed Oct 24 15:23:54 CEST 2012


Hi, all

I want to use the new build-in function zmq_proxy, but the api reference
page for zmq_proxy(3) <http://api.zeromq.org/3-2:zmq-proxy> has not been
written.

Here is a nice implemtation using zmq_proxy. How to quit gracefully???
{{{

static void * broker1(void *context) {
void *frontend, *backend;

frontend = zmq_socket(context, ZMQ_PULL);
backend = zmq_socket(context, ZMQ_PUSH);

zmq_bind(frontend, "inproc://broker");
zmq_connect(backend, "tcp://localhost:5560");
zmq_proxy(frontend, backend, NULL);

/* I'll never get here */
zmq_close(frontend);
zmq_close(backend); return NULL;
}
}}}

The following is my own broker, I can send a message "!q" to quit it. but I
don't know how to control zmq_proxy.
{{{
static void * broker2(void *context) {

int quit = 0;
void *frontend, *backend;

zmq_pollitem_t items [1];

frontend = zmq_socket(context, ZMQ_PULL);
backend = zmq_socket(context, ZMQ_PUSH);

zmq_bind(frontend, "inproc://broker");
zmq_connect(backend, "tcp://localhost:5560");

items[0].socket = frontend;
items[0].events = ZMQ_POLLIN;

while (1) {
int rc;

rc = zmq_poll(items, 1, 1000);
if (rc == 0 && quit) break; /* make sure no msg in queue while quit */
if (rc == 0) continue;

while (1) {

int more;
zmq_msg_t message;
zmq_msg_init(&message);
zmq_msg_recv(&message, frontend, 0);

if (zmq_msg_size(&message) == 2
&& !strncmp("!q", (char *)zmq_msg_data(&message), 2)) {
zmq_msg_close(&message);
quit = 1;
break;
}

more = zmq_msg_more(&message);
zmq_msg_send(&message, backend, more ? ZMQ_SNDMORE : 0);
zmq_msg_close(&message);
if (!more) break;
}
}

zmq_close(frontend);
zmq_close(backend);

return NULL;
}

}}}

Thanks a lot
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20121024/a6a29f2c/attachment.htm>


More information about the zeromq-dev mailing list