2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2012-02-15 18:41:09 -06:00
|
|
|
Copyright (c) 2007-2012 iMatix Corporation
|
2011-10-31 16:20:30 +01:00
|
|
|
Copyright (c) 2009-2011 250bpm s.r.o.
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
2010-10-30 15:08:28 +02:00
|
|
|
the terms of the GNU Lesser General Public License as published by
|
2009-07-29 12:07:54 +02:00
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-10-30 15:08:28 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-30 15:08:28 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2009-07-29 12:07:54 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-02-12 10:15:51 +11:00
|
|
|
#define ZMQ_TYPE_UNSAFE
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-11-13 18:08:18 +01:00
|
|
|
#include "platform.hpp"
|
|
|
|
|
2011-07-18 11:39:38 +02:00
|
|
|
#if defined ZMQ_FORCE_SELECT
|
|
|
|
#define ZMQ_POLL_BASED_ON_SELECT
|
|
|
|
#elif defined ZMQ_FORCE_POLL
|
|
|
|
#define ZMQ_POLL_BASED_ON_POLL
|
|
|
|
#elif defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
|
2010-11-13 18:08:18 +01:00
|
|
|
defined ZMQ_HAVE_OPENBSD || defined ZMQ_HAVE_SOLARIS ||\
|
|
|
|
defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_QNXNTO ||\
|
|
|
|
defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_AIX ||\
|
|
|
|
defined ZMQ_HAVE_NETBSD
|
2011-07-18 11:39:38 +02:00
|
|
|
#define ZMQ_POLL_BASED_ON_POLL
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS || defined ZMQ_HAVE_OPENVMS ||\
|
2012-02-17 09:48:04 +00:00
|
|
|
defined ZMQ_HAVE_CYGWIN
|
2011-07-18 11:39:38 +02:00
|
|
|
#define ZMQ_POLL_BASED_ON_SELECT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// On AIX platform, poll.h has to be included first to get consistent
|
|
|
|
// definition of pollfd structure (AIX uses 'reqevents' and 'retnevents'
|
|
|
|
// instead of 'events' and 'revents' and defines macros to map from POSIX-y
|
|
|
|
// names to AIX-specific names).
|
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
2010-11-13 18:08:18 +01:00
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
2012-06-05 09:15:37 -05:00
|
|
|
// zmq.h must be included *after* poll.h for AIX to build properly
|
|
|
|
#include "../include/zmq.h"
|
|
|
|
|
2011-07-22 08:35:01 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2011-07-18 11:39:38 +02:00
|
|
|
#include "windows.hpp"
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-02-08 03:20:25 +11:00
|
|
|
|
2012-02-08 13:15:47 +11:00
|
|
|
// XSI vector I/O
|
|
|
|
#if ZMQ_HAVE_UIO
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#else
|
2012-02-15 16:47:24 -06:00
|
|
|
struct iovec {
|
2012-02-08 13:15:47 +11:00
|
|
|
void *iov_base;
|
|
|
|
size_t iov_len;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-09-22 11:52:35 +02:00
|
|
|
#include <string.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <new>
|
|
|
|
|
2012-03-16 16:39:11 -05:00
|
|
|
#include "device.hpp"
|
2009-08-09 16:30:22 +02:00
|
|
|
#include "socket_base.hpp"
|
2009-09-08 11:30:49 +02:00
|
|
|
#include "stdint.hpp"
|
2010-02-11 10:29:33 +01:00
|
|
|
#include "config.hpp"
|
2011-02-08 14:46:27 +01:00
|
|
|
#include "likely.hpp"
|
2010-09-26 16:55:54 +02:00
|
|
|
#include "clock.hpp"
|
2010-05-05 14:24:54 +02:00
|
|
|
#include "ctx.hpp"
|
2009-10-01 10:56:17 +02:00
|
|
|
#include "err.hpp"
|
2011-04-21 22:27:48 +02:00
|
|
|
#include "msg.hpp"
|
2009-12-10 16:46:22 +01:00
|
|
|
#include "fd.hpp"
|
2009-10-01 10:56:17 +02:00
|
|
|
|
2009-09-08 11:30:49 +02:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-02-09 09:08:37 +01:00
|
|
|
#if defined ZMQ_HAVE_OPENPGM
|
2010-10-23 14:35:02 +02:00
|
|
|
#define __PGM_WININT_H__
|
2010-02-09 09:08:37 +01:00
|
|
|
#include <pgm/pgm.h>
|
|
|
|
#endif
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
// Compile time check whether msg_t fits into zmq_msg_t.
|
|
|
|
typedef char check_msg_t_size
|
|
|
|
[sizeof (zmq::msg_t) == sizeof (zmq_msg_t) ? 1 : -1];
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
|
2010-02-11 10:29:33 +01:00
|
|
|
void zmq_version (int *major_, int *minor_, int *patch_)
|
|
|
|
{
|
2010-10-09 07:53:24 +02:00
|
|
|
*major_ = ZMQ_VERSION_MAJOR;
|
|
|
|
*minor_ = ZMQ_VERSION_MINOR;
|
|
|
|
*patch_ = ZMQ_VERSION_PATCH;
|
2010-02-11 10:29:33 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
|
2009-09-22 11:52:35 +02:00
|
|
|
const char *zmq_strerror (int errnum_)
|
|
|
|
{
|
2010-10-16 16:05:34 +02:00
|
|
|
return zmq::errno_to_string (errnum_);
|
2009-09-22 11:52:35 +02:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
int zmq_errno ()
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
// New context API
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
void *zmq_ctx_new (void)
|
|
|
|
{
|
2010-02-09 09:08:37 +01:00
|
|
|
#if defined ZMQ_HAVE_OPENPGM
|
|
|
|
|
|
|
|
// Init PGM transport. Ensure threading and timer are enabled. Find PGM
|
|
|
|
// protocol ID. Note that if you want to use gettimeofday and sleep for
|
|
|
|
// openPGM timing, set environment variables PGM_TIMER to "GTOD" and
|
|
|
|
// PGM_SLEEP to "USLEEP".
|
2010-09-28 16:35:29 +02:00
|
|
|
pgm_error_t *pgm_error = NULL;
|
2011-02-13 10:07:10 +01:00
|
|
|
const bool ok = pgm_init (&pgm_error);
|
|
|
|
if (ok != TRUE) {
|
2010-09-28 16:58:51 +02:00
|
|
|
|
|
|
|
// Invalid parameters don't set pgm_error_t
|
|
|
|
zmq_assert (pgm_error != NULL);
|
|
|
|
if (pgm_error->domain == PGM_ERROR_DOMAIN_TIME && (
|
|
|
|
pgm_error->code == PGM_ERROR_FAILED)) {
|
|
|
|
|
|
|
|
// Failed to access RTC or HPET device.
|
2010-09-28 16:35:29 +02:00
|
|
|
pgm_error_free (pgm_error);
|
2010-02-09 09:08:37 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-09-28 16:58:51 +02:00
|
|
|
|
|
|
|
// PGM_ERROR_DOMAIN_ENGINE: WSAStartup errors or missing WSARecvMsg.
|
2010-02-09 09:08:37 +01:00
|
|
|
zmq_assert (false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-12 18:36:21 +01:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
// Intialise Windows sockets. Note that WSAStartup can be called multiple
|
|
|
|
// times given that WSACleanup will be called for each WSAStartup.
|
|
|
|
// We do this before the ctx constructor since its embedded mailbox_t
|
|
|
|
// object needs Winsock to be up and running.
|
|
|
|
WORD version_requested = MAKEWORD (2, 2);
|
|
|
|
WSADATA wsa_data;
|
|
|
|
int rc = WSAStartup (version_requested, &wsa_data);
|
|
|
|
zmq_assert (rc == 0);
|
|
|
|
zmq_assert (LOBYTE (wsa_data.wVersion) == 2 &&
|
|
|
|
HIBYTE (wsa_data.wVersion) == 2);
|
|
|
|
#endif
|
|
|
|
|
2010-02-09 09:08:37 +01:00
|
|
|
// Create 0MQ context.
|
2012-03-19 19:41:20 -05:00
|
|
|
zmq::ctx_t *ctx = new (std::nothrow) zmq::ctx_t;
|
2011-02-22 16:23:36 +01:00
|
|
|
alloc_assert (ctx);
|
2012-02-04 02:17:35 +11:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
int zmq_ctx_destroy (void *ctx_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-19 19:41:20 -05:00
|
|
|
|
2010-08-26 12:14:53 +02:00
|
|
|
int rc = ((zmq::ctx_t*) ctx_)->terminate ();
|
|
|
|
int en = errno;
|
|
|
|
|
2011-02-12 18:36:21 +01:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
// On Windows, uninitialise socket layer.
|
|
|
|
rc = WSACleanup ();
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#endif
|
|
|
|
|
2010-02-09 09:08:37 +01:00
|
|
|
#if defined ZMQ_HAVE_OPENPGM
|
|
|
|
// Shut down the OpenPGM library.
|
|
|
|
if (pgm_shutdown () != TRUE)
|
|
|
|
zmq_assert (false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
errno = en;
|
|
|
|
return rc;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2012-03-19 19:41:20 -05:00
|
|
|
int zmq_ctx_set (void *ctx_, int option_, int optval_)
|
|
|
|
{
|
|
|
|
if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ((zmq::ctx_t*) ctx_)->set (option_, optval_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_ctx_get (void *ctx_, int option_)
|
|
|
|
{
|
|
|
|
if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ((zmq::ctx_t*) ctx_)->get (option_);
|
|
|
|
}
|
|
|
|
|
2012-05-22 20:15:18 +01:00
|
|
|
int zmq_ctx_set_monitor (void *ctx_, zmq_monitor_fn *monitor_)
|
2012-05-21 20:47:11 +01:00
|
|
|
{
|
|
|
|
if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ((zmq::ctx_t*) ctx_)->monitor (monitor_);
|
|
|
|
}
|
2012-03-19 19:41:20 -05:00
|
|
|
|
|
|
|
// Stable/legacy context API
|
|
|
|
|
|
|
|
void *zmq_init (int io_threads_)
|
|
|
|
{
|
2012-03-29 00:47:11 -05:00
|
|
|
if (io_threads_ >= 0) {
|
|
|
|
void *ctx = zmq_ctx_new ();
|
|
|
|
zmq_ctx_set (ctx, ZMQ_IO_THREADS, io_threads_);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
2012-03-19 19:41:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_term (void *ctx_)
|
|
|
|
{
|
|
|
|
return zmq_ctx_destroy (ctx_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Sockets
|
2012-02-04 12:34:06 +11:00
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
void *zmq_socket (void *ctx_, int type_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!ctx_ || !((zmq::ctx_t*) ctx_)->check_tag ()) {
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
errno = EFAULT;
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-04 02:17:35 +11:00
|
|
|
zmq::ctx_t *ctx = (zmq::ctx_t*) ctx_;
|
|
|
|
zmq::socket_base_t *s = ctx->create_socket (type_);
|
2012-02-15 16:47:24 -06:00
|
|
|
return (void *) s;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-03 11:30:13 +02:00
|
|
|
int zmq_close (void *s_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 16:30:22 +02:00
|
|
|
((zmq::socket_base_t*) s_)->close ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_setsockopt (void *s_, int option_, const void *optval_,
|
|
|
|
size_t optvallen_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-04 01:41:09 +11:00
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
|
|
|
int result = s->setsockopt (option_, optval_, optvallen_);
|
|
|
|
return result;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2010-04-09 13:04:15 +02:00
|
|
|
int zmq_getsockopt (void *s_, int option_, void *optval_, size_t *optvallen_)
|
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-04 01:41:09 +11:00
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
|
|
|
int result = s->getsockopt (option_, optval_, optvallen_);
|
|
|
|
return result;
|
2010-04-09 13:04:15 +02:00
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
int zmq_bind (void *s_, const char *addr_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-04 01:41:09 +11:00
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
|
|
|
int result = s->bind (addr_);
|
|
|
|
return result;
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_connect (void *s_, const char *addr_)
|
|
|
|
{
|
2011-04-09 09:35:34 +02:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
Added error checking (EFAULT) for null arguments
* Fixed zmq_term, zmq_socket, zmq_close, zmq_setsockopt,
* zmq_getsockopt, zmq_bind, zmq_connect, zmq_send,
* zmq_recv, zmq_poll, zmq_device, zmq_stopwatch_stop
* Updated Reference Manual for these methods
2010-08-08 11:43:32 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2012-02-04 01:41:09 +11:00
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
|
|
|
int result = s->connect (addr_);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-20 18:59:08 +04:00
|
|
|
int zmq_unbind (void *s_, const char *addr_)
|
2012-04-18 23:42:11 +04:00
|
|
|
{
|
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-04-20 18:59:08 +04:00
|
|
|
return s->term_endpoint (addr_);
|
2012-04-18 23:42:11 +04:00
|
|
|
}
|
|
|
|
|
2012-04-20 18:59:08 +04:00
|
|
|
int zmq_disconnect (void *s_, const char *addr_)
|
2012-04-18 23:42:11 +04:00
|
|
|
{
|
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-04-20 18:59:08 +04:00
|
|
|
return s->term_endpoint (addr_);
|
2012-04-18 23:42:11 +04:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
// Sending functions.
|
|
|
|
|
2012-02-15 16:47:24 -06:00
|
|
|
static int
|
|
|
|
s_sendmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_)
|
2012-02-04 01:41:09 +11:00
|
|
|
{
|
|
|
|
int sz = (int) zmq_msg_size (msg_);
|
|
|
|
int rc = s_->send ((zmq::msg_t*) msg_, flags_);
|
|
|
|
if (unlikely (rc < 0))
|
|
|
|
return -1;
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2012-02-15 15:37:35 -06:00
|
|
|
/* To be deprecated once zmq_msg_send() is stable */
|
2012-02-04 01:41:09 +11:00
|
|
|
int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_)
|
|
|
|
{
|
2012-02-15 15:37:35 -06:00
|
|
|
return zmq_msg_send (msg_, s_, flags_);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2011-03-24 11:53:55 +01:00
|
|
|
int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
|
|
|
|
{
|
2012-02-08 03:20:25 +11:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 11:53:55 +01:00
|
|
|
zmq_msg_t msg;
|
|
|
|
int rc = zmq_msg_init_size (&msg, len_);
|
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
memcpy (zmq_msg_data (&msg), buf_, len_);
|
|
|
|
|
2012-02-04 01:41:09 +11:00
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-02-15 16:47:24 -06:00
|
|
|
rc = s_sendmsg (s, &msg, flags_);
|
2011-03-24 11:53:55 +01:00
|
|
|
if (unlikely (rc < 0)) {
|
|
|
|
int err = errno;
|
|
|
|
int rc2 = zmq_msg_close (&msg);
|
|
|
|
errno_assert (rc2 == 0);
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note the optimisation here. We don't close the msg object as it is
|
|
|
|
// empty anyway. This may change when implementation of zmq_msg_t changes.
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-02-08 03:20:25 +11:00
|
|
|
// Send multiple messages.
|
|
|
|
//
|
|
|
|
// If flag bit ZMQ_SNDMORE is set the vector is treated as
|
|
|
|
// a single multi-part message, i.e. the last message has
|
2012-02-08 11:45:36 +11:00
|
|
|
// ZMQ_SNDMORE bit switched off.
|
2012-02-08 03:20:25 +11:00
|
|
|
//
|
2012-02-15 16:47:24 -06:00
|
|
|
int zmq_sendiov (void *s_, iovec *a_, size_t count_, int flags_)
|
2012-02-08 03:20:25 +11:00
|
|
|
{
|
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int rc = 0;
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-02-15 16:47:24 -06:00
|
|
|
|
|
|
|
for (size_t i = 0; i < count_; ++i) {
|
2012-02-08 03:20:25 +11:00
|
|
|
rc = zmq_msg_init_size (&msg, a_[i].iov_len);
|
2012-02-15 16:47:24 -06:00
|
|
|
if (rc != 0) {
|
2012-02-08 03:20:25 +11:00
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memcpy (zmq_msg_data (&msg), a_[i].iov_base, a_[i].iov_len);
|
2012-02-15 16:47:24 -06:00
|
|
|
if (i == count_ - 1)
|
|
|
|
flags_ = flags_ & ~ZMQ_SNDMORE;
|
|
|
|
rc = s_sendmsg (s, &msg, flags_);
|
2012-02-08 03:20:25 +11:00
|
|
|
if (unlikely (rc < 0)) {
|
|
|
|
int err = errno;
|
|
|
|
int rc2 = zmq_msg_close (&msg);
|
|
|
|
errno_assert (rc2 == 0);
|
|
|
|
errno = err;
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
// Receiving functions.
|
|
|
|
|
2012-02-15 16:47:24 -06:00
|
|
|
static int
|
|
|
|
s_recvmsg (zmq::socket_base_t *s_, zmq_msg_t *msg_, int flags_)
|
2012-02-04 01:41:09 +11:00
|
|
|
{
|
2012-02-14 17:14:46 -06:00
|
|
|
int rc = s_->recv ((zmq::msg_t*) msg_, flags_);
|
2012-02-04 01:41:09 +11:00
|
|
|
if (unlikely (rc < 0))
|
|
|
|
return -1;
|
|
|
|
return (int) zmq_msg_size (msg_);
|
|
|
|
}
|
|
|
|
|
2012-02-15 15:37:35 -06:00
|
|
|
/* To be deprecated once zmq_msg_recv() is stable */
|
2012-02-04 01:41:09 +11:00
|
|
|
int zmq_recvmsg (void *s_, zmq_msg_t *msg_, int flags_)
|
|
|
|
{
|
2012-02-15 15:37:35 -06:00
|
|
|
return zmq_msg_recv (msg_, s_, flags_);
|
2012-02-04 01:41:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-24 11:53:55 +01:00
|
|
|
int zmq_recv (void *s_, void *buf_, size_t len_, int flags_)
|
|
|
|
{
|
2012-02-04 01:41:09 +11:00
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 11:53:55 +01:00
|
|
|
zmq_msg_t msg;
|
|
|
|
int rc = zmq_msg_init (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2012-02-04 01:41:09 +11:00
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-02-15 16:47:24 -06:00
|
|
|
int nbytes = s_recvmsg (s, &msg, flags_);
|
2011-03-27 09:50:25 +02:00
|
|
|
if (unlikely (nbytes < 0)) {
|
2011-03-24 11:53:55 +01:00
|
|
|
int err = errno;
|
2011-03-27 09:50:25 +02:00
|
|
|
rc = zmq_msg_close (&msg);
|
|
|
|
errno_assert (rc == 0);
|
2011-03-24 11:53:55 +01:00
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At the moment an oversized message is silently truncated.
|
|
|
|
// TODO: Build in a notification mechanism to report the overflows.
|
2011-03-27 09:50:25 +02:00
|
|
|
size_t to_copy = size_t (nbytes) < len_ ? size_t (nbytes) : len_;
|
2011-03-24 11:53:55 +01:00
|
|
|
memcpy (buf_, zmq_msg_data (&msg), to_copy);
|
2011-03-26 11:05:55 +01:00
|
|
|
|
|
|
|
rc = zmq_msg_close (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2012-02-17 09:48:04 +00:00
|
|
|
return nbytes;
|
2011-03-24 11:53:55 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 11:45:36 +11:00
|
|
|
// Receive a multi-part message
|
|
|
|
//
|
|
|
|
// Receives up to *count_ parts of a multi-part message.
|
|
|
|
// Sets *count_ to the actual number of parts read.
|
|
|
|
// ZMQ_RCVMORE is set to indicate if a complete multi-part message was read.
|
|
|
|
// Returns number of message parts read, or -1 on error.
|
|
|
|
//
|
|
|
|
// Note: even if -1 is returned, some parts of the message
|
|
|
|
// may have been read. Therefore the client must consult
|
|
|
|
// *count_ to retrieve message parts successfully read,
|
|
|
|
// even if -1 is returned.
|
|
|
|
//
|
|
|
|
// The iov_base* buffers of each iovec *a_ filled in by this
|
|
|
|
// function may be freed using free().
|
|
|
|
//
|
|
|
|
// Implementation note: We assume zmq::msg_t buffer allocated
|
|
|
|
// by zmq::recvmsg can be freed by free().
|
|
|
|
// We assume it is safe to steal these buffers by simply
|
|
|
|
// not closing the zmq::msg_t.
|
|
|
|
//
|
2012-02-15 16:47:24 -06:00
|
|
|
int zmq_recviov (void *s_, iovec *a_, size_t *count_, int flags_)
|
2012-02-08 11:45:36 +11:00
|
|
|
{
|
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
|
|
|
|
2012-02-15 16:47:24 -06:00
|
|
|
size_t count = (int) *count_;
|
2012-02-08 11:45:36 +11:00
|
|
|
int nread = 0;
|
|
|
|
bool recvmore = true;
|
|
|
|
|
2012-02-15 16:47:24 -06:00
|
|
|
for (size_t i = 0; recvmore && i < count; ++i) {
|
2012-02-08 11:45:36 +11:00
|
|
|
// Cheat! We never close any msg
|
|
|
|
// because we want to steal the buffer.
|
|
|
|
zmq_msg_t msg;
|
|
|
|
int rc = zmq_msg_init (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2012-02-15 16:47:24 -06:00
|
|
|
int nbytes = s_recvmsg (s, &msg, flags_);
|
2012-02-08 11:45:36 +11:00
|
|
|
if (unlikely (nbytes < 0)) {
|
|
|
|
int err = errno;
|
|
|
|
rc = zmq_msg_close (&msg);
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
errno = err;
|
|
|
|
nread = -1;
|
|
|
|
break;
|
|
|
|
}
|
2012-02-14 17:14:46 -06:00
|
|
|
++*count_;
|
2012-02-08 11:45:36 +11:00
|
|
|
++nread;
|
|
|
|
|
|
|
|
// Cheat: acquire zmq_msg buffer.
|
2012-02-17 21:55:06 +00:00
|
|
|
a_[i].iov_base = static_cast<char *> (zmq_msg_data (&msg));
|
2012-02-08 11:45:36 +11:00
|
|
|
a_[i].iov_len = zmq_msg_size (&msg);
|
|
|
|
|
|
|
|
// Assume zmq_socket ZMQ_RVCMORE is properly set.
|
2012-02-15 16:47:24 -06:00
|
|
|
recvmore = ((zmq::msg_t*) (void *) &msg)->flags () & zmq::msg_t::more;
|
2012-02-08 11:45:36 +11:00
|
|
|
}
|
2012-02-17 09:48:04 +00:00
|
|
|
return nread;
|
2012-02-08 11:45:36 +11:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
// Message manipulators.
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int zmq_msg_init (zmq_msg_t *msg_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) msg_)->init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) msg_)->init_size (size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
|
|
|
|
zmq_free_fn *ffn_, void *hint_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) msg_)->init_data (data_, size_, ffn_, hint_);
|
|
|
|
}
|
|
|
|
|
2012-02-15 15:37:35 -06:00
|
|
|
int zmq_msg_send (zmq_msg_t *msg_, void *s_, int flags_)
|
|
|
|
{
|
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-02-16 15:55:18 -06:00
|
|
|
int result = s_sendmsg (s, msg_, flags_);
|
2012-02-15 15:37:35 -06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_msg_recv (zmq_msg_t *msg_, void *s_, int flags_)
|
|
|
|
{
|
|
|
|
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
|
|
|
|
errno = ENOTSOCK;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
|
2012-02-16 15:55:18 -06:00
|
|
|
int result = s_recvmsg (s, msg_, flags_);
|
2012-02-15 15:37:35 -06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-21 22:27:48 +02:00
|
|
|
int zmq_msg_close (zmq_msg_t *msg_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) msg_)->close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) dest_)->move (*(zmq::msg_t*) src_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) dest_)->copy (*(zmq::msg_t*) src_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *zmq_msg_data (zmq_msg_t *msg_)
|
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) msg_)->data ();
|
|
|
|
}
|
|
|
|
|
2012-02-17 09:48:04 +00:00
|
|
|
size_t zmq_msg_size (zmq_msg_t *msg_)
|
2011-04-21 22:27:48 +02:00
|
|
|
{
|
|
|
|
return ((zmq::msg_t*) msg_)->size ();
|
|
|
|
}
|
|
|
|
|
2012-02-15 18:41:09 -06:00
|
|
|
int zmq_msg_more (zmq_msg_t *msg_)
|
|
|
|
{
|
2012-03-21 14:19:40 -05:00
|
|
|
return zmq_msg_get (msg_, ZMQ_MORE);
|
2012-02-15 18:41:09 -06:00
|
|
|
}
|
|
|
|
|
2012-03-21 14:19:40 -05:00
|
|
|
int zmq_msg_get (zmq_msg_t *msg_, int option_)
|
2011-11-06 14:03:51 +01:00
|
|
|
{
|
|
|
|
switch (option_) {
|
2012-02-15 18:41:09 -06:00
|
|
|
case ZMQ_MORE:
|
2012-03-21 14:19:40 -05:00
|
|
|
return (((zmq::msg_t*) msg_)->flags () & zmq::msg_t::more)? 1: 0;
|
2012-02-15 18:41:09 -06:00
|
|
|
default:
|
2011-11-06 14:03:51 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2012-02-15 18:41:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 14:19:40 -05:00
|
|
|
int zmq_msg_set (zmq_msg_t *msg_, int option_, int optval_)
|
2012-02-15 18:41:09 -06:00
|
|
|
{
|
|
|
|
// No options supported at present
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2011-11-06 14:03:51 +01:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:34:06 +11:00
|
|
|
// Polling.
|
|
|
|
|
2010-09-20 16:55:46 +02:00
|
|
|
int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|
|
|
{
|
2012-02-15 13:03:40 -06:00
|
|
|
if (!items_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-20 16:55:46 +02:00
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
2011-02-08 14:46:27 +01:00
|
|
|
if (unlikely (nitems_ < 0)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely (nitems_ == 0)) {
|
|
|
|
if (timeout_ == 0)
|
|
|
|
return 0;
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2011-03-23 15:50:18 +01:00
|
|
|
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
|
2011-02-08 14:46:27 +01:00
|
|
|
return 0;
|
2011-08-15 19:09:04 +02:00
|
|
|
#elif defined ZMQ_HAVE_ANDROID
|
|
|
|
usleep (timeout_ * 1000);
|
|
|
|
return 0;
|
2011-02-08 14:46:27 +01:00
|
|
|
#else
|
2011-03-23 15:50:18 +01:00
|
|
|
return usleep (timeout_ * 1000);
|
2011-02-08 14:46:27 +01:00
|
|
|
#endif
|
|
|
|
}
|
2010-10-13 21:39:20 +02:00
|
|
|
zmq::clock_t clock;
|
|
|
|
uint64_t now = 0;
|
|
|
|
uint64_t end = 0;
|
|
|
|
|
2009-10-01 10:56:17 +02:00
|
|
|
pollfd *pollfds = (pollfd*) malloc (nitems_ * sizeof (pollfd));
|
2011-02-22 16:23:36 +01:00
|
|
|
alloc_assert (pollfds);
|
2009-10-01 10:56:17 +02:00
|
|
|
|
2010-08-07 18:24:12 +02:00
|
|
|
// Build pollset for poll () system call.
|
2009-10-01 10:56:17 +02:00
|
|
|
for (int i = 0; i != nitems_; i++) {
|
|
|
|
|
2010-08-07 18:24:12 +02:00
|
|
|
// If the poll item is a 0MQ socket, we poll on the file descriptor
|
|
|
|
// retrieved by the ZMQ_FD socket option.
|
2010-08-07 21:04:30 +02:00
|
|
|
if (items_ [i].socket) {
|
2010-08-07 20:35:42 +02:00
|
|
|
size_t zmq_fd_size = sizeof (zmq::fd_t);
|
2010-08-07 18:24:12 +02:00
|
|
|
if (zmq_getsockopt (items_ [i].socket, ZMQ_FD, &pollfds [i].fd,
|
|
|
|
&zmq_fd_size) == -1) {
|
|
|
|
free (pollfds);
|
|
|
|
return -1;
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
2010-08-07 21:04:30 +02:00
|
|
|
pollfds [i].events = items_ [i].events ? POLLIN : 0;
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
2010-08-07 18:24:12 +02:00
|
|
|
// Else, the poll item is a raw file descriptor. Just convert the
|
|
|
|
// events to normal POLLIN/POLLOUT for poll ().
|
2010-08-07 18:33:44 +02:00
|
|
|
else {
|
2010-08-07 18:24:12 +02:00
|
|
|
pollfds [i].fd = items_ [i].fd;
|
|
|
|
pollfds [i].events =
|
|
|
|
(items_ [i].events & ZMQ_POLLIN ? POLLIN : 0) |
|
|
|
|
(items_ [i].events & ZMQ_POLLOUT ? POLLOUT : 0);
|
2010-08-07 18:33:44 +02:00
|
|
|
}
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 07:02:22 +02:00
|
|
|
bool first_pass = true;
|
2009-10-01 10:56:17 +02:00
|
|
|
int nevents = 0;
|
2010-01-04 15:46:20 +01:00
|
|
|
|
2010-01-13 19:21:23 +01:00
|
|
|
while (true) {
|
2010-08-27 15:01:38 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// Compute the timeout for the subsequent poll.
|
|
|
|
int timeout;
|
|
|
|
if (first_pass)
|
|
|
|
timeout = 0;
|
|
|
|
else if (timeout_ < 0)
|
|
|
|
timeout = -1;
|
|
|
|
else
|
|
|
|
timeout = end - now;
|
|
|
|
|
2010-09-08 08:39:27 +02:00
|
|
|
// Wait for events.
|
2010-08-27 15:01:38 +02:00
|
|
|
while (true) {
|
2010-10-13 21:39:20 +02:00
|
|
|
int rc = poll (pollfds, nitems_, timeout);
|
2010-08-27 15:01:38 +02:00
|
|
|
if (rc == -1 && errno == EINTR) {
|
2010-09-08 08:39:27 +02:00
|
|
|
free (pollfds);
|
|
|
|
return -1;
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
2010-08-27 15:01:38 +02:00
|
|
|
errno_assert (rc >= 0);
|
|
|
|
break;
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
2010-01-13 19:21:23 +01:00
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
// Check for the events.
|
|
|
|
for (int i = 0; i != nitems_; i++) {
|
2010-01-13 19:21:23 +01:00
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
items_ [i].revents = 0;
|
2010-08-07 18:24:12 +02:00
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
// The poll item is a 0MQ socket. Retrieve pending events
|
|
|
|
// using the ZMQ_EVENTS socket option.
|
2010-08-28 07:02:22 +02:00
|
|
|
if (items_ [i].socket) {
|
2010-08-27 15:01:38 +02:00
|
|
|
size_t zmq_events_size = sizeof (uint32_t);
|
|
|
|
uint32_t zmq_events;
|
|
|
|
if (zmq_getsockopt (items_ [i].socket, ZMQ_EVENTS, &zmq_events,
|
|
|
|
&zmq_events_size) == -1) {
|
|
|
|
free (pollfds);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((items_ [i].events & ZMQ_POLLOUT) &&
|
|
|
|
(zmq_events & ZMQ_POLLOUT))
|
|
|
|
items_ [i].revents |= ZMQ_POLLOUT;
|
|
|
|
if ((items_ [i].events & ZMQ_POLLIN) &&
|
|
|
|
(zmq_events & ZMQ_POLLIN))
|
|
|
|
items_ [i].revents |= ZMQ_POLLIN;
|
2010-02-22 18:19:26 +01:00
|
|
|
}
|
2010-08-27 15:01:38 +02:00
|
|
|
// Else, the poll item is a raw file descriptor, simply convert
|
|
|
|
// the events to zmq_pollitem_t-style format.
|
|
|
|
else {
|
|
|
|
if (pollfds [i].revents & POLLIN)
|
|
|
|
items_ [i].revents |= ZMQ_POLLIN;
|
|
|
|
if (pollfds [i].revents & POLLOUT)
|
|
|
|
items_ [i].revents |= ZMQ_POLLOUT;
|
|
|
|
if (pollfds [i].revents & ~(POLLIN | POLLOUT))
|
|
|
|
items_ [i].revents |= ZMQ_POLLERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items_ [i].revents)
|
|
|
|
nevents++;
|
2010-08-07 18:24:12 +02:00
|
|
|
}
|
2010-01-13 19:21:23 +01:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// If timout is zero, exit immediately whether there are events or not.
|
|
|
|
if (timeout_ == 0)
|
|
|
|
break;
|
2010-08-28 07:02:22 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// If there are events to return, we can exit immediately.
|
|
|
|
if (nevents)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// At this point we are meant to wait for events but there are none.
|
|
|
|
// If timeout is infinite we can just loop until we get some events.
|
2010-10-16 17:56:25 +02:00
|
|
|
if (timeout_ < 0) {
|
|
|
|
if (first_pass)
|
|
|
|
first_pass = false;
|
2010-08-27 15:01:38 +02:00
|
|
|
continue;
|
2010-10-16 17:56:25 +02:00
|
|
|
}
|
2010-08-27 15:01:38 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// The timeout is finite and there are no events. In the first pass
|
|
|
|
// we get a timestamp of when the polling have begun. (We assume that
|
|
|
|
// first pass have taken negligible time). We also compute the time
|
|
|
|
// when the polling should time out.
|
|
|
|
if (first_pass) {
|
|
|
|
now = clock.now_ms ();
|
2011-03-23 15:50:18 +01:00
|
|
|
end = now + timeout_;
|
2010-10-16 17:56:25 +02:00
|
|
|
if (now == end)
|
|
|
|
break;
|
|
|
|
first_pass = false;
|
2010-10-13 21:39:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-09-08 08:39:27 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// Find out whether timeout have expired.
|
|
|
|
now = clock.now_ms ();
|
|
|
|
if (now >= end)
|
|
|
|
break;
|
2009-10-01 10:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free (pollfds);
|
|
|
|
return nevents;
|
|
|
|
|
2010-09-20 16:55:46 +02:00
|
|
|
#elif defined ZMQ_POLL_BASED_ON_SELECT
|
2009-12-10 16:46:22 +01:00
|
|
|
|
2011-02-08 14:46:27 +01:00
|
|
|
if (unlikely (nitems_ < 0)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unlikely (nitems_ == 0)) {
|
|
|
|
if (timeout_ == 0)
|
|
|
|
return 0;
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2011-03-23 15:50:18 +01:00
|
|
|
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
|
2011-02-08 14:46:27 +01:00
|
|
|
return 0;
|
|
|
|
#else
|
2011-03-23 15:50:18 +01:00
|
|
|
return usleep (timeout_ * 1000);
|
2011-02-08 14:46:27 +01:00
|
|
|
#endif
|
|
|
|
}
|
2010-10-13 21:39:20 +02:00
|
|
|
zmq::clock_t clock;
|
|
|
|
uint64_t now = 0;
|
|
|
|
uint64_t end = 0;
|
|
|
|
|
|
|
|
// Ensure we do not attempt to select () on more than FD_SETSIZE
|
|
|
|
// file descriptors.
|
|
|
|
zmq_assert (nitems_ <= FD_SETSIZE);
|
|
|
|
|
2009-12-10 16:46:22 +01:00
|
|
|
fd_set pollset_in;
|
|
|
|
FD_ZERO (&pollset_in);
|
|
|
|
fd_set pollset_out;
|
|
|
|
FD_ZERO (&pollset_out);
|
|
|
|
fd_set pollset_err;
|
|
|
|
FD_ZERO (&pollset_err);
|
|
|
|
|
2010-08-07 20:35:42 +02:00
|
|
|
zmq::fd_t maxfd = 0;
|
2009-12-10 16:46:22 +01:00
|
|
|
|
2010-08-07 20:35:42 +02:00
|
|
|
// Build the fd_sets for passing to select ().
|
2009-12-10 16:46:22 +01:00
|
|
|
for (int i = 0; i != nitems_; i++) {
|
|
|
|
|
2010-08-07 20:35:42 +02:00
|
|
|
// If the poll item is a 0MQ socket we are interested in input on the
|
|
|
|
// notification file descriptor retrieved by the ZMQ_FD socket option.
|
2010-08-07 21:04:30 +02:00
|
|
|
if (items_ [i].socket) {
|
2010-08-07 20:35:42 +02:00
|
|
|
size_t zmq_fd_size = sizeof (zmq::fd_t);
|
|
|
|
zmq::fd_t notify_fd;
|
|
|
|
if (zmq_getsockopt (items_ [i].socket, ZMQ_FD, ¬ify_fd,
|
|
|
|
&zmq_fd_size) == -1)
|
|
|
|
return -1;
|
2010-08-07 21:04:30 +02:00
|
|
|
if (items_ [i].events) {
|
|
|
|
FD_SET (notify_fd, &pollset_in);
|
|
|
|
if (maxfd < notify_fd)
|
|
|
|
maxfd = notify_fd;
|
|
|
|
}
|
2009-12-10 16:46:22 +01:00
|
|
|
}
|
2010-08-07 20:35:42 +02:00
|
|
|
// Else, the poll item is a raw file descriptor. Convert the poll item
|
|
|
|
// events to the appropriate fd_sets.
|
|
|
|
else {
|
|
|
|
if (items_ [i].events & ZMQ_POLLIN)
|
|
|
|
FD_SET (items_ [i].fd, &pollset_in);
|
|
|
|
if (items_ [i].events & ZMQ_POLLOUT)
|
|
|
|
FD_SET (items_ [i].fd, &pollset_out);
|
|
|
|
if (items_ [i].events & ZMQ_POLLERR)
|
|
|
|
FD_SET (items_ [i].fd, &pollset_err);
|
|
|
|
if (maxfd < items_ [i].fd)
|
|
|
|
maxfd = items_ [i].fd;
|
2009-12-10 16:46:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 07:02:22 +02:00
|
|
|
bool first_pass = true;
|
2009-12-10 16:46:22 +01:00
|
|
|
int nevents = 0;
|
2010-02-10 10:42:54 +01:00
|
|
|
fd_set inset, outset, errset;
|
2010-08-07 20:35:42 +02:00
|
|
|
|
|
|
|
while (true) {
|
2010-08-27 15:01:38 +02:00
|
|
|
|
2010-11-12 19:16:00 +01:00
|
|
|
// Compute the timeout for the subsequent poll.
|
|
|
|
timeval timeout;
|
|
|
|
timeval *ptimeout;
|
|
|
|
if (first_pass) {
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
ptimeout = &timeout;
|
|
|
|
}
|
|
|
|
else if (timeout_ < 0)
|
|
|
|
ptimeout = NULL;
|
|
|
|
else {
|
|
|
|
timeout.tv_sec = (long) ((end - now) / 1000);
|
|
|
|
timeout.tv_usec = (long) ((end - now) % 1000 * 1000);
|
|
|
|
ptimeout = &timeout;
|
|
|
|
}
|
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
// Wait for events. Ignore interrupts if there's infinite timeout.
|
|
|
|
while (true) {
|
|
|
|
memcpy (&inset, &pollset_in, sizeof (fd_set));
|
|
|
|
memcpy (&outset, &pollset_out, sizeof (fd_set));
|
|
|
|
memcpy (&errset, &pollset_err, sizeof (fd_set));
|
2009-12-10 16:46:22 +01:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2011-05-15 13:12:09 +02:00
|
|
|
int rc = select (0, &inset, &outset, &errset, ptimeout);
|
2011-08-14 14:23:16 +02:00
|
|
|
if (unlikely (rc == SOCKET_ERROR)) {
|
2012-05-27 16:10:19 +02:00
|
|
|
errno = zmq::wsa_error_to_errno (WSAGetLastError ());
|
|
|
|
wsa_assert (errno == ENOTSOCK);
|
|
|
|
return -1;
|
2011-08-14 14:23:16 +02:00
|
|
|
}
|
2009-12-10 16:46:22 +01:00
|
|
|
#else
|
2011-05-15 13:12:09 +02:00
|
|
|
int rc = select (maxfd + 1, &inset, &outset, &errset, ptimeout);
|
2011-10-27 17:11:28 +02:00
|
|
|
if (unlikely (rc == -1)) {
|
2012-05-27 16:10:19 +02:00
|
|
|
errno_assert (errno == EINTR || errno == EBADF);
|
|
|
|
return -1;
|
2011-08-14 14:23:16 +02:00
|
|
|
}
|
2009-12-10 16:46:22 +01:00
|
|
|
#endif
|
2010-08-27 15:01:38 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-01-04 15:46:20 +01:00
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
// Check for the events.
|
|
|
|
for (int i = 0; i != nitems_; i++) {
|
2009-12-10 16:46:22 +01:00
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
items_ [i].revents = 0;
|
2009-12-10 16:46:22 +01:00
|
|
|
|
2010-08-27 15:01:38 +02:00
|
|
|
// The poll item is a 0MQ socket. Retrieve pending events
|
|
|
|
// using the ZMQ_EVENTS socket option.
|
|
|
|
if (items_ [i].socket) {
|
2010-09-20 17:25:04 +02:00
|
|
|
size_t zmq_events_size = sizeof (uint32_t);
|
|
|
|
uint32_t zmq_events;
|
|
|
|
if (zmq_getsockopt (items_ [i].socket, ZMQ_EVENTS, &zmq_events,
|
|
|
|
&zmq_events_size) == -1)
|
2010-08-07 20:35:42 +02:00
|
|
|
return -1;
|
2010-09-20 17:25:04 +02:00
|
|
|
if ((items_ [i].events & ZMQ_POLLOUT) &&
|
|
|
|
(zmq_events & ZMQ_POLLOUT))
|
|
|
|
items_ [i].revents |= ZMQ_POLLOUT;
|
|
|
|
if ((items_ [i].events & ZMQ_POLLIN) &&
|
|
|
|
(zmq_events & ZMQ_POLLIN))
|
|
|
|
items_ [i].revents |= ZMQ_POLLIN;
|
2010-08-27 15:01:38 +02:00
|
|
|
}
|
|
|
|
// Else, the poll item is a raw file descriptor, simply convert
|
|
|
|
// the events to zmq_pollitem_t-style format.
|
|
|
|
else {
|
|
|
|
if (FD_ISSET (items_ [i].fd, &inset))
|
2010-08-07 20:35:42 +02:00
|
|
|
items_ [i].revents |= ZMQ_POLLIN;
|
2010-08-27 15:01:38 +02:00
|
|
|
if (FD_ISSET (items_ [i].fd, &outset))
|
|
|
|
items_ [i].revents |= ZMQ_POLLOUT;
|
|
|
|
if (FD_ISSET (items_ [i].fd, &errset))
|
|
|
|
items_ [i].revents |= ZMQ_POLLERR;
|
2009-12-10 16:46:22 +01:00
|
|
|
}
|
2010-08-27 15:01:38 +02:00
|
|
|
|
|
|
|
if (items_ [i].revents)
|
|
|
|
nevents++;
|
2010-08-07 20:35:42 +02:00
|
|
|
}
|
2010-01-14 15:50:12 +01:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// If timout is zero, exit immediately whether there are events or not.
|
|
|
|
if (timeout_ == 0)
|
|
|
|
break;
|
2010-08-28 07:02:22 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// If there are events to return, we can exit immediately.
|
|
|
|
if (nevents)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// At this point we are meant to wait for events but there are none.
|
|
|
|
// If timeout is infinite we can just loop until we get some events.
|
2010-10-16 17:56:25 +02:00
|
|
|
if (timeout_ < 0) {
|
|
|
|
if (first_pass)
|
|
|
|
first_pass = false;
|
2010-08-27 15:01:38 +02:00
|
|
|
continue;
|
2010-10-16 17:56:25 +02:00
|
|
|
}
|
2010-08-27 15:01:38 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// The timeout is finite and there are no events. In the first pass
|
|
|
|
// we get a timestamp of when the polling have begun. (We assume that
|
|
|
|
// first pass have taken negligible time). We also compute the time
|
|
|
|
// when the polling should time out.
|
|
|
|
if (first_pass) {
|
|
|
|
now = clock.now_ms ();
|
2011-03-23 15:50:18 +01:00
|
|
|
end = now + timeout_;
|
2010-10-16 17:56:25 +02:00
|
|
|
if (now == end)
|
|
|
|
break;
|
|
|
|
first_pass = false;
|
2010-10-13 21:39:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-09-08 08:39:27 +02:00
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
// Find out whether timeout have expired.
|
|
|
|
now = clock.now_ms ();
|
|
|
|
if (now >= end)
|
|
|
|
break;
|
2009-12-10 16:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nevents;
|
|
|
|
|
|
|
|
#else
|
2010-08-27 15:01:38 +02:00
|
|
|
// Exotic platforms that support neither poll() nor select().
|
2009-12-10 16:46:22 +01:00
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
2009-10-01 10:56:17 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-09-20 16:55:46 +02:00
|
|
|
#if defined ZMQ_POLL_BASED_ON_SELECT
|
|
|
|
#undef ZMQ_POLL_BASED_ON_SELECT
|
|
|
|
#endif
|
|
|
|
#if defined ZMQ_POLL_BASED_ON_POLL
|
|
|
|
#undef ZMQ_POLL_BASED_ON_POLL
|
|
|
|
#endif
|
2012-03-16 16:39:11 -05:00
|
|
|
|
|
|
|
int zmq_device (int device_, void *insocket_, void *outsocket_)
|
|
|
|
{
|
|
|
|
if (!insocket_ || !outsocket_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_ != ZMQ_FORWARDER && device_ != ZMQ_QUEUE &&
|
|
|
|
device_ != ZMQ_STREAMER) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return zmq::device ((zmq::socket_base_t*) insocket_,
|
|
|
|
(zmq::socket_base_t*) outsocket_);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 0MQ utils - to be used by perf tests
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void zmq_sleep (int seconds_)
|
|
|
|
{
|
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
|
|
|
Sleep (seconds_ * 1000);
|
|
|
|
#else
|
|
|
|
sleep (seconds_);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *zmq_stopwatch_start ()
|
|
|
|
{
|
|
|
|
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
|
|
|
|
alloc_assert (watch);
|
|
|
|
*watch = zmq::clock_t::now_us ();
|
|
|
|
return (void*) watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long zmq_stopwatch_stop (void *watch_)
|
|
|
|
{
|
|
|
|
uint64_t end = zmq::clock_t::now_us ();
|
|
|
|
uint64_t start = *(uint64_t*) watch_;
|
|
|
|
free (watch_);
|
|
|
|
return (unsigned long) (end - start);
|
|
|
|
}
|