Example #1
0
int XS_TEST_MAIN ()
{
    fprintf (stderr, "polltimeo test running...\n");

    void *ctx = xs_init ();
    assert (ctx);

    //  Create a disconnected socket.
    void *sb = xs_socket (ctx, XS_PULL);
    assert (sb);
    int rc = xs_bind (sb, "inproc://timeout_test");
    assert (rc == 0);

    //  Check whether timeout is honoured.
    xs_pollitem_t pi;
    pi.socket = sb;
    pi.events = XS_POLLIN;
    void *watch = xs_stopwatch_start ();
    rc = xs_poll (&pi, 1, 500);
    assert (rc == 0);
    unsigned long elapsed = xs_stopwatch_stop (watch) / 1000;
#if !defined _WIN32 || !defined _DEBUG
    assert (elapsed > 440 && elapsed < 550);
#endif

    //  Check whether connection during the wait doesn't distort the timeout.
    void *thread = thread_create (polltimeo_worker, ctx);
    assert (thread);
    watch = xs_stopwatch_start ();
    rc = xs_poll (&pi, 1, 2000);
    assert (rc == 0);
    elapsed = xs_stopwatch_stop (watch) / 1000;
#if !defined _WIN32 || !defined _DEBUG
    assert (elapsed > 1900 && elapsed < 2100);
#endif
    thread_join (thread);

    //  Clean-up.
    rc = xs_close (sb);
    assert (rc == 0);
    rc = xs_term (ctx);
    assert (rc == 0);

    return 0 ;
}
Example #2
0
int zmq_device (int device, void *frontend, void *backend)
{
    int more;
    size_t size;
    xs_msg_t msg;
    xs_pollitem_t items [2];

    int rc = xs_msg_init (&msg);
    if (rc != 0)
        return -1;

    items [0].socket = frontend;
    items [0].events = XS_POLLIN;
    items [1].socket = backend;
    items [1].events = XS_POLLIN;

    while (1) {

        rc = xs_poll (&items [0], 2, -1);
        if (rc < 0)
            return -1;

        if (items [0].revents & XS_POLLIN) {
            while (1) {

                rc = xs_recvmsg (frontend, &msg, 0);
                if (rc < 0)
                    return -1;

                size = sizeof (more);
                rc = xs_getsockopt (frontend, XS_RCVMORE, &more, &size);
                if (rc < 0)
                    return -1;

                rc = xs_sendmsg (backend, &msg, more ? XS_SNDMORE : 0);
                if (rc < 0)
                    return -1;

                if (!more)
                    break;
            }
        }

        if (items [1].revents & XS_POLLIN) {
            while (1) {

                rc = xs_recvmsg (backend, &msg, 0);
                if (rc < 0)
                    return -1;

                size = sizeof (more);
                rc = xs_getsockopt (backend, XS_RCVMORE, &more, &size);
                if (rc < 0)
                    return -1;

                rc = xs_sendmsg (frontend, &msg, more ? ZMQ_SNDMORE : 0);
                if (rc < 0)
                    return -1;

                if (!more)
                    break;
            }
        }
    }

    return 0;
}
Example #3
0
int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout)
{
    return xs_poll ((xs_pollitem_t*) items, nitems,
        timeout < 0 ? -1 : (int) (timeout / 1000));
}
Example #4
0
nmsg_res
_input_nmsg_read_container_xs(nmsg_input_t input, Nmsg__Nmsg **nmsg) {
	int ret;
	nmsg_res res;
	uint8_t *buf;
	size_t buf_len;
	ssize_t msgsize = 0;
	xs_msg_t xmsg;
	xs_pollitem_t xitems[1];

	/* poll */
	xitems[0].socket = input->stream->xs;
	xitems[0].events = XS_POLLIN;
	ret = xs_poll(xitems, 1, NMSG_RBUF_TIMEOUT);
	if (ret == 0 || (ret == -1 && errno == EINTR))
		return (nmsg_res_again);
	else if (ret == -1)
		return (nmsg_res_read_failure);

	/* initialize XS message object */
	if (xs_msg_init(&xmsg))
		return (nmsg_res_failure);

	/* read the NMSG container */
	if (xs_recvmsg(input->stream->xs, &xmsg, 0) == -1) {
		res = nmsg_res_failure;
		goto out;
	}
	nmsg_timespec_get(&input->stream->now);

	/* get buffer from the XS message */
	buf = xs_msg_data(&xmsg);
	buf_len = xs_msg_size(&xmsg);
	if (buf_len < NMSG_HDRLSZ_V2) {
		res = nmsg_res_failure;
		goto out;
	}

	/* deserialize the NMSG header */
	res = _input_nmsg_deserialize_header(buf, buf_len, &msgsize, &input->stream->flags);
	if (res != nmsg_res_success)
		goto out;
	buf += NMSG_HDRLSZ_V2;

	/* the entire message must have been read by xs_recvmsg() */
	assert((size_t) msgsize == buf_len - NMSG_HDRLSZ_V2);

	/* unpack message */
	res = _input_nmsg_unpack_container(input, nmsg, buf, msgsize);

	/* update seqsrc counts */
	if (input->stream->verify_seqsrc && *nmsg != NULL) {
		struct nmsg_seqsrc *seqsrc = _input_seqsrc_get(input, *nmsg);
		if (seqsrc != NULL)
			_input_seqsrc_update(input, seqsrc, *nmsg);
	}

	/* expire old outstanding fragments */
	_input_frag_gc(input->stream);

out:
	xs_msg_close(&xmsg);
	return (res);
}