本文整理汇总了C++中zmq_poll函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_poll函数的具体用法?C++ zmq_poll怎么用?C++ zmq_poll使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmq_poll函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _zmq2agent_worker
static void
_zmq2agent_worker (struct _zmq2agent_ctx_s *ctx)
{
/* XXX(jfs): a dedicated PRNG avoids locking the glib's PRNG for each call
(such global locks are present in the GLib) and opening it with a seed
from the glib's PRNG avoids syscalls to the special file /dev/urandom */
GRand *r = g_rand_new_with_seed (g_random_int ());
gint64 last_debug = oio_ext_monotonic_time ();
zmq_pollitem_t pi[2] = {
{ctx->zpull, -1, ZMQ_POLLIN, 0},
{ctx->zagent, -1, ZMQ_POLLIN, 0},
};
for (gboolean run = TRUE; run ;) {
int rc = zmq_poll (pi, 2, 1000);
if (rc < 0) {
int err = zmq_errno();
if (err != ETERM && err != EINTR)
GRID_WARN("ZMQ poll error : (%d) %s", err, zmq_strerror(err));
if (err != EINTR)
break;
}
if (pi[1].revents)
_zmq2agent_receive_acks (ctx);
_retry_events (ctx);
if (pi[0].revents)
run = _zmq2agent_receive_events (r, ctx);
/* Periodically write stats in the log */
gint64 now = oio_ext_monotonic_time ();
if ((now - last_debug) > 2 * G_TIME_SPAN_MINUTE) {
GRID_INFO("ZMQ2AGENT recv=%"G_GINT64_FORMAT" sent=%"G_GINT64_FORMAT
" ack=%"G_GINT64_FORMAT"+%"G_GINT64_FORMAT" queue=%u",
ctx->q->counter_received, ctx->q->counter_sent,
ctx->q->counter_ack, ctx->q->counter_ack_notfound,
ctx->q->gauge_pending);
last_debug = now;
}
}
g_rand_free (r);
GRID_INFO ("Thread stopping [NOTIFY-ZMQ2AGENT]");
}
示例2: test_tcp_req
static void
test_tcp_req (void *args, zctx_t *ctx, void *pipe)
{
vtx_t *vtx = vtx_new (ctx);
int rc = vtx_tcp_load (vtx, FALSE);
assert (rc == 0);
char *port = zstr_recv (pipe);
void *client = vtx_socket (vtx, ZMQ_REQ);
assert (client);
rc = vtx_connect (vtx, client, "tcp://localhost:%s", port);
assert (rc == 0);
int sent = 0;
int recd = 0;
while (!zctx_interrupted) {
zstr_send (client, "ICANHAZ?");
sent++;
zmq_pollitem_t items [] = {
{ pipe, 0, ZMQ_POLLIN, 0 },
{ client, 0, ZMQ_POLLIN, 0 }
};
int rc = zmq_poll (items, 2, 500 * ZMQ_POLL_MSEC);
if (rc == -1)
break; // Context has been shut down
if (items [0].revents & ZMQ_POLLIN) {
free (zstr_recv (pipe));
zstr_send (pipe, "OK");
break;
}
if (items [1].revents & ZMQ_POLLIN) {
free (zstr_recv (client));
recd++;
}
else {
// No response, close socket and start a new one
vtx_close (vtx, client);
client = vtx_socket (vtx, ZMQ_REQ);
rc = vtx_connect (vtx, client, "tcp://localhost:%s", port);
}
}
zclock_log ("I: REQ: sent=%d recd=%d", sent, recd);
free (port);
vtx_destroy (&vtx);
}
示例3: assert
static flux_msg_t *op_recv (void *impl, int flags)
{
ctx_t *ctx = impl;
assert (ctx->magic == MODHANDLE_MAGIC);
zmq_pollitem_t zp = {
.events = ZMQ_POLLIN, .socket = ctx->sock, .revents = 0, .fd = -1,
};
flux_msg_t *msg = NULL;
if (connect_socket (ctx) < 0)
goto done;
if ((flags & FLUX_O_NONBLOCK)) {
int n;
if ((n = zmq_poll (&zp, 1, 0L)) <= 0) {
if (n == 0)
errno = EWOULDBLOCK;
goto done;
}
}
msg = flux_msg_recvzsock (ctx->sock);
done:
return msg;
}
static int op_event_subscribe (void *impl, const char *topic)
{
ctx_t *ctx = impl;
assert (ctx->magic == MODHANDLE_MAGIC);
json_object *in = Jnew ();
flux_rpc_t *rpc = NULL;
int rc = -1;
if (connect_socket (ctx) < 0)
goto done;
Jadd_str (in, "topic", topic);
if (!(rpc = flux_rpc (ctx->h, "cmb.sub", Jtostr (in), FLUX_NODEID_ANY, 0))
|| flux_rpc_get (rpc, NULL) < 0)
goto done;
rc = 0;
done:
Jput (in);
flux_rpc_destroy (rpc);
return rc;
}
示例4: client_task
// Request-reply client using REQ socket
// To simulate load, clients issue a burst of requests and then
// sleep for a random period.
//
static void *
client_task (void *args)
{
zctx_t *ctx = zctx_new ();
void *client = zsocket_new (ctx, ZMQ_REQ);
zsocket_connect (client, "ipc://%s-localfe.ipc", self);
void *monitor = zsocket_new (ctx, ZMQ_PUSH);
zsocket_connect (monitor, "ipc://%s-monitor.ipc", self);
while (1) {
sleep (randof (5));
int burst = randof (15);
while (burst--) {
char task_id [5];
sprintf (task_id, "%04X", randof (0x10000));
// Send request with random hex ID
zstr_send (client, task_id);
// Wait max ten seconds for a reply, then complain
zmq_pollitem_t pollset [1] = { { client, 0, ZMQ_POLLIN, 0 } };
int rc = zmq_poll (pollset, 1, 10 * 1000 * ZMQ_POLL_MSEC);
if (rc == -1)
break; // Interrupted
if (pollset [0].revents & ZMQ_POLLIN) {
char *reply = zstr_recv (client);
if (!reply)
break; // Interrupted
// Worker is supposed to answer us with our task id
puts (reply);
assert (streq (reply, task_id));
free (reply);
}
else {
zstr_sendf (monitor,
"E: CLIENT EXIT - lost task %s", task_id);
return NULL;
}
}
}
zctx_destroy (&ctx);
return NULL;
}
示例5: zpoller_wait
void *
zpoller_wait (zpoller_t *self, int timeout)
{
assert (self);
self->expired = false;
if (zsys_interrupted && !self->nonstop) {
self->terminated = true;
return NULL;
}
else
self->terminated = false;
#ifdef ZMQ_HAVE_POLLER
zmq_poller_event_t event;
if (!zmq_poller_wait (self->zmq_poller, &event, timeout * ZMQ_POLL_MSEC))
return event.user_data;
else
if (errno == ETIMEDOUT || errno == EAGAIN)
self->expired = true;
else
if (zsys_interrupted && !self->nonstop)
self->terminated = true;
return NULL;
#else
if (self->need_rebuild)
s_rebuild_poll_set (self);
int rc = zmq_poll (self->poll_set, (int) self->poll_size, timeout * ZMQ_POLL_MSEC);
if (rc > 0) {
uint reader = 0;
for (reader = 0; reader < self->poll_size; reader++)
if (self->poll_set [reader].revents & ZMQ_POLLIN)
return self->poll_readers [reader];
}
else
if (rc == -1 || (zsys_interrupted && !self->nonstop))
self->terminated = true;
else
if (rc == 0)
self->expired = true;
return NULL;
#endif
}
示例6: zmq_poll
UInt32 CZmq::PollEvent(UInt32 iEvents, Int32 iTimeout)
{
if (m_pHandle)
{
zmq_pollitem_t items[] =
{
{ m_pHandle, 0, iEvents, 0 },
};
Int32 iRet = zmq_poll(items, 1, iTimeout);
if(iRet == UTIL_ERROR)
{
FillErr();
return 0;
}
return items[0].revents;
}
return 0;
}
示例7: assert
int ZMQPollData::poll(int64_t timeout, VRefParam readable, VRefParam writable) {
errors.clear();
auto rVar = readable.getVariantOrNull();
Array rArr;
if (rVar && rVar->isArray()) {
rArr = rVar->asArrRef();
rArr.clear();
}
auto wVar = writable.getVariantOrNull();
Array wArr;
if (wVar && wVar->isArray()) {
wArr = wVar->asArrRef();
wArr.clear();
}
assert(items.size() == php_items.size());
int rc = zmq_poll(items.data(), items.size(), timeout);
if (rc == -1) {
return -1;
}
if (rc > 0) {
for (size_t i = 0; i < items.size(); i++) {
if (rVar && (items[i].revents & ZMQ_POLLIN)) {
rArr.append(php_items[i].entry);
}
if (wVar && (items[i].revents & ZMQ_POLLOUT)) {
wArr.append(php_items[i].entry);
}
if (items[i].revents & ZMQ_POLLERR) {
errors.append(php_items[i].key);
}
}
}
readable.assignIfRef(rArr);
writable.assignIfRef(wArr);
return rc;
}
示例8: mdcli_recv
zmsg_t *
mdcli_recv (mdcli_t *self)
{
assert (self);
// Poll socket for a reply, with timeout
zmq_pollitem_t items [] = { { self->client, 0, ZMQ_POLLIN, 0 } };
int rc = zmq_poll (items, 1, self->timeout * ZMQ_POLL_MSEC);
if (rc == -1)
return NULL; // Interrupted
// If we got a reply, process it
if (items [0].revents & ZMQ_POLLIN) {
zmsg_t *msg = zmsg_recv (self->client);
if (self->verbose) {
zclock_log ("I: received reply:");
zmsg_dump (msg);
}
// Don't try to handle errors, just assert noisily
assert (zmsg_size (msg) >= 4);
zframe_t *empty = zmsg_pop (msg);
assert (zframe_streq (empty, ""));
zframe_destroy (&empty);
zframe_t *header = zmsg_pop (msg);
assert (zframe_streq (header, MDPC_CLIENT));
zframe_destroy (&header);
zframe_t *service = zmsg_pop (msg);
zframe_destroy (&service);
return msg; // Success
}
if (zctx_interrupted)
printf ("W: interrupt received, killing client...\n");
else
if (self->verbose)
zclock_log ("W: permanent error, abandoning request");
return NULL;
}
示例9: s_try_request
static zmsg_t *
s_try_request (zctx_t *ctx, char *endpoint, zmsg_t *request)
{
printf ("I: trying echo service at %s...\n", endpoint);
void *client = zsocket_new (ctx, ZMQ_REQ);
zsocket_connect (client, endpoint);
// Send request, wait safely for reply
zmsg_t *msg = zmsg_dup (request);
zmsg_send (&msg, client);
zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 } };
zmq_poll (items, 1, REQUEST_TIMEOUT * ZMQ_POLL_MSEC);
zmsg_t *reply = NULL;
if (items [0].revents & ZMQ_POLLIN)
reply = zmsg_recv (client);
// Close socket in any case, we're done with it now
zsocket_destroy (ctx, client);
return reply;
}
示例10: mdcli_recv
zmsg_t *
mdcli_recv (mdcli_t *self)
{
assert (self);
// Poll socket for a reply, with timeout
zmq_pollitem_t items [] = { { self->client, 0, ZMQ_POLLIN, 0 } };
zmq_poll (items, 1, self->timeout * 1000);
// If we got a reply, process it
if (items [0].revents & ZMQ_POLLIN) {
zmsg_t *msg = zmsg_recv (self->client);
if (self->verbose) {
s_console ("I: received reply:");
zmsg_dump (msg);
}
// Don't try to handle errors, just assert noisily
assert (zmsg_parts (msg) >= 4);
char *empty = zmsg_pop (msg);
assert (streq (empty, ""));
free (empty);
char *header = zmsg_pop (msg);
assert (streq (header, MDPC_CLIENT));
free (header);
char *service = zmsg_pop (msg);
assert (streq (service, service));
free (service);
return msg; // Success
}
if (s_interrupted)
printf ("W: interrupt received, killing client...\n");
else
if (self->verbose)
s_console ("W: permanent error, abandoning request");
return NULL;
}
示例11: main
int main (void)
{
void *context = zmq_init (1);
// Connect to task ventilator
void *receiver = zmq_socket (context, ZMQ_PULL);
zmq_connect (receiver, "tcp://localhost:5557");
// Connect to weather server
void *subscriber = zmq_socket (context, ZMQ_SUB);
zmq_connect (subscriber, "tcp://localhost:5556");
zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "10001 ", 6);
// Initialize poll set
zmq_pollitem_t items [] = {
{ receiver, 0, ZMQ_POLLIN, 0 },
{ subscriber, 0, ZMQ_POLLIN, 0 }
};
// Process messages from both sockets
while (1) {
zmq_msg_t message;
zmq_poll (items, 2, -1);
if (items [0].revents & ZMQ_POLLIN) {
zmq_msg_init (&message);
zmq_recv (receiver, &message, 0);
// Process task
zmq_msg_close (&message);
}
if (items [1].revents & ZMQ_POLLIN) {
zmq_msg_init (&message);
zmq_recv (subscriber, &message, 0);
// Process weather update
zmq_msg_close (&message);
}
}
// We never get here
zmq_close (receiver);
zmq_close (subscriber);
zmq_term (context);
return 0;
}
示例12: test_tcp_router
static void
test_tcp_router (void *args, zctx_t *ctx, void *pipe)
{
vtx_t *vtx = vtx_new (ctx);
int rc = vtx_tcp_load (vtx, FALSE);
assert (rc == 0);
char *port = zstr_recv (pipe);
void *router = vtx_socket (vtx, ZMQ_ROUTER);
assert (router);
rc = vtx_bind (vtx, router, "tcp://*:%s", port);
assert (rc == 0);
int sent = 0;
while (!zctx_interrupted) {
zmq_pollitem_t items [] = {
{ pipe, 0, ZMQ_POLLIN, 0 },
{ router, 0, ZMQ_POLLIN, 0 }
};
int rc = zmq_poll (items, 2, 500 * ZMQ_POLL_MSEC);
if (rc == -1)
break; // Context has been shut down
if (items [1].revents & ZMQ_POLLIN) {
char *address = zstr_recv (router);
free (zstr_recv (router));
zstr_sendm (router, address);
zstr_send (router, "CHEEZBURGER");
free (address);
sent++;
}
if (items [0].revents & ZMQ_POLLIN) {
free (zstr_recv (pipe));
zstr_send (pipe, "OK");
break;
}
}
zclock_log ("I: ROUTER: sent=%d", sent);
free (port);
vtx_destroy (&vtx);
}
示例13: assert
static flux_msg_t *op_recv (void *impl, int flags)
{
ctx_t *ctx = impl;
assert (ctx->magic == MODHANDLE_MAGIC);
zmq_pollitem_t zp = {
.events = ZMQ_POLLIN, .socket = ctx->sock, .revents = 0, .fd = -1,
};
flux_msg_t *msg = NULL;
if (connect_socket (ctx) < 0)
goto done;
if ((flags & FLUX_O_NONBLOCK)) {
int n;
if ((n = zmq_poll (&zp, 1, 0L)) < 0)
goto done; /* likely: EWOULDBLOCK | EAGAIN */
assert (n == 1);
assert (zp.revents == ZMQ_POLLIN);
}
msg = zmsg_recv (ctx->sock);
done:
return msg;
}
static int op_event_subscribe (void *impl, const char *topic)
{
ctx_t *ctx = impl;
assert (ctx->magic == MODHANDLE_MAGIC);
JSON in = Jnew ();
int rc = -1;
if (connect_socket (ctx) < 0)
goto done;
Jadd_str (in, "topic", topic);
if (flux_json_rpc (ctx->h, FLUX_NODEID_ANY, "cmb.sub", in, NULL) < 0)
goto done;
rc = 0;
done:
Jput (in);
return rc;
}
示例14: zbeacon
void
zbeacon (zsock_t *pipe, void *args)
{
self_t *self = s_self_new (pipe);
assert (self);
// Signal successful initialization
zsock_signal (pipe, 0);
while (!self->terminated) {
// Poll on API pipe and on UDP socket
zmq_pollitem_t pollitems [] = {
{ zsock_resolve (self->pipe), 0, ZMQ_POLLIN, 0 },
{ NULL, self->udpsock, ZMQ_POLLIN, 0 }
};
long timeout = -1;
if (self->transmit) {
timeout = (long) (self->ping_at - zclock_mono ());
if (timeout < 0)
timeout = 0;
}
int pollset_size = self->udpsock? 2: 1;
if (zmq_poll (pollitems, pollset_size, timeout * ZMQ_POLL_MSEC) == -1)
break; // Interrupted
if (pollitems [0].revents & ZMQ_POLLIN)
s_self_handle_pipe (self);
if (pollitems [1].revents & ZMQ_POLLIN)
s_self_handle_udp (self);
if (self->transmit
&& zclock_mono () >= self->ping_at) {
// Send beacon to any listening peers
if (zsys_udp_send (self->udpsock, self->transmit, &self->broadcast, sizeof (inaddr_t)))
// Try to recreate UDP socket on interface
s_self_prepare_udp (self);
self->ping_at = zclock_mono () + self->interval;
}
}
s_self_destroy (&self);
}
示例15: my_zmqpoll
/* wrapper around zmq_poll which may return zero without reaching the
* specified timeout */
int
my_zmqpoll(zmq_pollitem_t *items, const int nitems, const long timeout)
{
struct timeval tv, te;
int rc, ret;
long tmleft;
/* Populate te with timeout value */
te.tv_sec = timeout / 1000000;
te.tv_usec = timeout - (te.tv_sec * 1000000);
rc = gettimeofday(&tv, NULL);
assert(rc == 0);
/* Add current time to the timeout (end time) */
te.tv_sec += tv.tv_sec;
te.tv_usec += tv.tv_usec;
te.tv_sec += te.tv_usec / 1000000;
te.tv_usec %= 1000000;
/* Loop over, return either >0, or 0 after a timeout */
tmleft = timeout;
while (1) {
ret = zmq_poll(items, nitems, tmleft);
assert(ret >= 0);
rc = gettimeofday(&tv, NULL);
assert(rc == 0);
if (ret == 0) {
/* Keep on looping unless time's up */
if (te.tv_sec < tv.tv_sec ||
(te.tv_sec == tv.tv_sec && te.tv_usec <= tv.tv_usec))
return ret;
tmleft = ((te.tv_sec - tv.tv_sec) * 1000000) + (te.tv_usec - tv.tv_usec);
} else {
return ret;
}
}
}