2009-07-29 12:07:54 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-13 18:08:18 +01:00
|
|
|
#include "platform.hpp"
|
|
|
|
|
|
|
|
// On AIX, poll.h has to be included before zmq.h 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_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
|
|
|
|
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
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-11 20:33:27 +01:00
|
|
|
#include "../include/zmq.h"
|
2010-06-17 17:09:51 +02:00
|
|
|
#include "../include/zmq_utils.h"
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2009-09-22 11:52:35 +02:00
|
|
|
#include <string.h>
|
2009-07-29 12:07:54 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <new>
|
|
|
|
|
2010-12-06 22:57:29 +01:00
|
|
|
#include "device.hpp"
|
2009-08-09 16:30:22 +02:00
|
|
|
#include "socket_base.hpp"
|
2009-08-21 14:29:22 +02:00
|
|
|
#include "msg_content.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"
|
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
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_init (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content = (zmq::msg_content_t*) ZMQ_VSM;
|
2010-03-09 16:56:53 +01:00
|
|
|
msg_->flags = 0;
|
2009-07-29 12:07:54 +02:00
|
|
|
msg_->vsm_size = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_init_size (zmq_msg_t *msg_, size_t size_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
if (size_ <= ZMQ_MAX_VSM_SIZE) {
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content = (zmq::msg_content_t*) ZMQ_VSM;
|
2010-03-09 16:56:53 +01:00
|
|
|
msg_->flags = 0;
|
2009-09-08 11:30:49 +02:00
|
|
|
msg_->vsm_size = (uint8_t) size_;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
else {
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content =
|
|
|
|
(zmq::msg_content_t*) malloc (sizeof (zmq::msg_content_t) + size_);
|
2009-07-29 12:07:54 +02:00
|
|
|
if (!msg_->content) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-09 16:56:53 +01:00
|
|
|
msg_->flags = 0;
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
|
|
|
|
content->data = (void*) (content + 1);
|
|
|
|
content->size = size_;
|
|
|
|
content->ffn = NULL;
|
2009-12-16 15:08:37 +01:00
|
|
|
content->hint = NULL;
|
2009-08-21 14:29:22 +02:00
|
|
|
new (&content->refcnt) zmq::atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
|
2009-12-16 15:08:37 +01:00
|
|
|
zmq_free_fn *ffn_, void *hint_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
msg_->content = (zmq::msg_content_t*) malloc (sizeof (zmq::msg_content_t));
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_assert (msg_->content);
|
2010-03-09 16:56:53 +01:00
|
|
|
msg_->flags = 0;
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
|
|
|
|
content->data = data_;
|
|
|
|
content->size = size_;
|
|
|
|
content->ffn = ffn_;
|
2009-12-16 15:08:37 +01:00
|
|
|
content->hint = hint_;
|
2009-08-21 14:29:22 +02:00
|
|
|
new (&content->refcnt) zmq::atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_close (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
// For VSMs and delimiters there are no resources to free.
|
2010-10-14 09:34:09 +02:00
|
|
|
if (msg_->content != (zmq::msg_content_t*) ZMQ_DELIMITER &&
|
|
|
|
msg_->content != (zmq::msg_content_t*) ZMQ_VSM) {
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-14 09:34:09 +02:00
|
|
|
// If the content is not shared, or if it is shared and the reference.
|
|
|
|
// count has dropped to zero, deallocate it.
|
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
|
|
|
|
if (!(msg_->flags & ZMQ_MSG_SHARED) || !content->refcnt.sub (1)) {
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-14 09:34:09 +02:00
|
|
|
// We used "placement new" operator to initialize the reference.
|
|
|
|
// counter so we call its destructor now.
|
|
|
|
content->refcnt.~atomic_counter_t ();
|
2009-07-29 12:07:54 +02:00
|
|
|
|
2010-10-14 09:34:09 +02:00
|
|
|
if (content->ffn)
|
|
|
|
content->ffn (content->data, content->hint);
|
|
|
|
free (content);
|
|
|
|
}
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2010-10-14 09:34:09 +02:00
|
|
|
// As a safety measure, let's make the deallocated message look like
|
|
|
|
// an empty message.
|
|
|
|
msg_->content = (zmq::msg_content_t*) ZMQ_VSM;
|
|
|
|
msg_->flags = 0;
|
|
|
|
msg_->vsm_size = 0;
|
|
|
|
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_move (zmq_msg_t *dest_, zmq_msg_t *src_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_close (dest_);
|
2009-07-29 12:07:54 +02:00
|
|
|
*dest_ = *src_;
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_init (src_);
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_msg_copy (zmq_msg_t *dest_, zmq_msg_t *src_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-03 11:30:13 +02:00
|
|
|
zmq_msg_close (dest_);
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// VSMs and delimiters require no special handling.
|
2009-09-22 10:57:46 +02:00
|
|
|
if (src_->content != (zmq::msg_content_t*) ZMQ_DELIMITER &&
|
|
|
|
src_->content != (zmq::msg_content_t*) ZMQ_VSM) {
|
2009-07-29 12:07:54 +02:00
|
|
|
|
|
|
|
// One reference is added to shared messages. Non-shared messages
|
|
|
|
// are turned into shared messages and reference count is set to 2.
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq::msg_content_t *content = (zmq::msg_content_t*) src_->content;
|
2010-03-09 16:56:53 +01:00
|
|
|
if (src_->flags & ZMQ_MSG_SHARED)
|
2009-08-21 14:29:22 +02:00
|
|
|
content->refcnt.add (1);
|
2009-07-29 12:07:54 +02:00
|
|
|
else {
|
2010-03-09 16:56:53 +01:00
|
|
|
src_->flags |= ZMQ_MSG_SHARED;
|
2009-08-21 14:29:22 +02:00
|
|
|
content->refcnt.set (2);
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest_ = *src_;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
void *zmq_msg_data (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return msg_->vsm_data;
|
2009-09-22 10:57:46 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_DELIMITER)
|
2009-07-29 12:07:54 +02:00
|
|
|
return NULL;
|
2009-08-21 14:29:22 +02:00
|
|
|
|
|
|
|
return ((zmq::msg_content_t*) msg_->content)->data;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
size_t zmq_msg_size (zmq_msg_t *msg_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2009-08-21 14:29:22 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_VSM)
|
2009-07-29 12:07:54 +02:00
|
|
|
return msg_->vsm_size;
|
2009-09-22 10:57:46 +02:00
|
|
|
if (msg_->content == (zmq::msg_content_t*) ZMQ_DELIMITER)
|
2009-07-29 12:07:54 +02:00
|
|
|
return 0;
|
2009-08-21 14:29:22 +02:00
|
|
|
|
|
|
|
return ((zmq::msg_content_t*) msg_->content)->size;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2010-06-04 15:24:06 +02:00
|
|
|
void *zmq_init (int io_threads_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
2010-05-06 10:33:01 +02:00
|
|
|
if (io_threads_ < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-29 12:07:54 +02:00
|
|
|
|
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;
|
2010-09-28 16:58:51 +02:00
|
|
|
const bool rc = pgm_init (&pgm_error);
|
2010-02-09 09:08:37 +01:00
|
|
|
if (rc != 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
|
|
|
|
|
|
|
|
// Create 0MQ context.
|
2010-05-05 14:24:54 +02:00
|
|
|
zmq::ctx_t *ctx = new (std::nothrow) zmq::ctx_t ((uint32_t) io_threads_);
|
|
|
|
zmq_assert (ctx);
|
|
|
|
return (void*) ctx;
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
int zmq_term (void *ctx_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
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
|
|
|
if (!ctx_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-26 12:14:53 +02:00
|
|
|
|
|
|
|
int rc = ((zmq::ctx_t*) ctx_)->terminate ();
|
|
|
|
int en = errno;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-05-05 14:24:54 +02:00
|
|
|
void *zmq_socket (void *ctx_, int type_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
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
|
|
|
if (!ctx_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-05 14:24:54 +02:00
|
|
|
return (void*) (((zmq::ctx_t*) ctx_)->create_socket (type_));
|
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
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
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
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->setsockopt (option_, optval_,
|
|
|
|
optvallen_));
|
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_)
|
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-09 13:04:15 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->getsockopt (option_, optval_,
|
|
|
|
optvallen_));
|
|
|
|
}
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
int zmq_bind (void *s_, const char *addr_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->bind (addr_));
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int zmq_connect (void *s_, const char *addr_)
|
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->connect (addr_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_send (void *s_, zmq_msg_t *msg_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->send (msg_, flags_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
int zmq_recv (void *s_, zmq_msg_t *msg_, int flags_)
|
2009-07-29 12:07:54 +02:00
|
|
|
{
|
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
|
|
|
if (!s_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-09 16:30:22 +02:00
|
|
|
return (((zmq::socket_base_t*) s_)->recv (msg_, flags_));
|
2009-07-29 12:07:54 +02:00
|
|
|
}
|
2009-09-08 11:30:49 +02:00
|
|
|
|
2010-09-20 16:55:46 +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 ||\
|
2009-12-10 16:46:22 +01:00
|
|
|
defined ZMQ_HAVE_OPENBSD || defined ZMQ_HAVE_SOLARIS ||\
|
|
|
|
defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_QNXNTO ||\
|
2010-02-18 19:38:15 +01:00
|
|
|
defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_AIX ||\
|
|
|
|
defined ZMQ_HAVE_NETBSD
|
2010-09-20 16:55:46 +02:00
|
|
|
#define ZMQ_POLL_BASED_ON_POLL
|
|
|
|
#elif defined ZMQ_HAVE_WINDOWS || defined ZMQ_HAVE_OPENVMS
|
|
|
|
#define ZMQ_POLL_BASED_ON_SELECT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
|
|
|
|
{
|
|
|
|
#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
|
|
|
|
Sleep (timeout_ > 0 ? timeout_ / 1000 : INFINITE);
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return usleep (timeout_);
|
|
|
|
#endif
|
|
|
|
}
|
2009-10-01 10:56:17 +02:00
|
|
|
|
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
|
|
|
if (!items_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-27 15:01:38 +02:00
|
|
|
|
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));
|
|
|
|
zmq_assert (pollfds);
|
|
|
|
|
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 ();
|
|
|
|
end = now + (timeout_ / 1000);
|
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
|
|
|
|
Sleep (timeout_ > 0 ? timeout_ / 1000 : INFINITE);
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return usleep (timeout_);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-13 21:39:20 +02:00
|
|
|
if (!items_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2010-10-13 21:39:20 +02:00
|
|
|
int rc = select (maxfd + 1, &inset, &outset, &errset, ptimeout);
|
2009-12-10 16:46:22 +01:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2010-08-27 15:01:38 +02:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
2009-12-10 16:46:22 +01:00
|
|
|
#else
|
2010-09-08 08:39:27 +02:00
|
|
|
if (rc == -1 && errno == EINTR)
|
|
|
|
return -1;
|
2010-08-27 15:01:38 +02:00
|
|
|
errno_assert (rc >= 0);
|
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 ();
|
|
|
|
end = now + (timeout_ / 1000);
|
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
|
|
|
|
|
2010-02-23 23:28:25 +01:00
|
|
|
int zmq_errno ()
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2010-04-07 08:20:01 +02:00
|
|
|
int zmq_device (int device_, void *insocket_, void *outsocket_)
|
|
|
|
{
|
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
|
|
|
if (!insocket_ || !outsocket_) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-12-06 22:57:29 +01:00
|
|
|
|
|
|
|
if (device_ != ZMQ_FORWARDER && device_ != ZMQ_QUEUE &&
|
|
|
|
device_ != ZMQ_STREAMER) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2010-04-07 08:20:01 +02:00
|
|
|
}
|
2010-12-06 22:57:29 +01:00
|
|
|
|
|
|
|
return zmq::device ((zmq::socket_base_t*) insocket_,
|
|
|
|
(zmq::socket_base_t*) outsocket_);
|
2010-04-07 08:20:01 +02:00
|
|
|
}
|
2010-06-17 16:51:53 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 0MQ utils - to be used by perf tests
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void zmq_sleep (int seconds_)
|
|
|
|
{
|
2010-09-26 16:55:54 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2010-06-17 16:51:53 +02:00
|
|
|
Sleep (seconds_ * 1000);
|
|
|
|
#else
|
|
|
|
sleep (seconds_);
|
|
|
|
#endif
|
2010-09-26 16:55:54 +02:00
|
|
|
}
|
2010-06-17 16:51:53 +02:00
|
|
|
|
|
|
|
void *zmq_stopwatch_start ()
|
|
|
|
{
|
|
|
|
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
|
|
|
|
assert (watch);
|
2010-09-26 16:55:54 +02:00
|
|
|
*watch = zmq::clock_t::now_us ();
|
2010-06-17 16:51:53 +02:00
|
|
|
return (void*) watch;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long zmq_stopwatch_stop (void *watch_)
|
|
|
|
{
|
2010-09-26 16:55:54 +02:00
|
|
|
uint64_t end = zmq::clock_t::now_us ();
|
2010-06-17 16:51:53 +02:00
|
|
|
uint64_t start = *(uint64_t*) watch_;
|
|
|
|
free (watch_);
|
|
|
|
return (unsigned long) (end - start);
|
|
|
|
}
|