Problem: formatting inconsistent

Solution: applied clang-format
This commit is contained in:
sigiesec
2018-02-01 11:46:09 +01:00
parent 6d8baea714
commit 41f459e1dc
331 changed files with 13208 additions and 13691 deletions

View File

@@ -43,10 +43,10 @@
#define ZMQ_VERSION_MINOR 2 #define ZMQ_VERSION_MINOR 2
#define ZMQ_VERSION_PATCH 4 #define ZMQ_VERSION_PATCH 4
#define ZMQ_MAKE_VERSION(major, minor, patch) \ #define ZMQ_MAKE_VERSION(major, minor, patch) \
((major) * 10000 + (minor) * 100 + (patch)) ((major) *10000 + (minor) *100 + (patch))
#define ZMQ_VERSION \ #define ZMQ_VERSION \
ZMQ_MAKE_VERSION(ZMQ_VERSION_MAJOR, ZMQ_VERSION_MINOR, ZMQ_VERSION_PATCH) ZMQ_MAKE_VERSION (ZMQ_VERSION_MAJOR, ZMQ_VERSION_MINOR, ZMQ_VERSION_PATCH)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -66,7 +66,7 @@ extern "C" {
#ifdef __MINGW32__ #ifdef __MINGW32__
// Require Windows XP or higher with MinGW for getaddrinfo(). // Require Windows XP or higher with MinGW for getaddrinfo().
#if(_WIN32_WINNT >= 0x0501) #if (_WIN32_WINNT >= 0x0501)
#else #else
#error You need at least Windows XP target #error You need at least Windows XP target
#endif #endif
@@ -76,49 +76,49 @@ extern "C" {
/* Handle DSO symbol visibility */ /* Handle DSO symbol visibility */
#if defined _WIN32 #if defined _WIN32
# if defined ZMQ_STATIC #if defined ZMQ_STATIC
# define ZMQ_EXPORT #define ZMQ_EXPORT
# elif defined DLL_EXPORT #elif defined DLL_EXPORT
# define ZMQ_EXPORT __declspec(dllexport) #define ZMQ_EXPORT __declspec(dllexport)
# else
# define ZMQ_EXPORT __declspec(dllimport)
# endif
#else #else
# if defined __SUNPRO_C || defined __SUNPRO_CC #define ZMQ_EXPORT __declspec(dllimport)
# define ZMQ_EXPORT __global #endif
# elif (defined __GNUC__ && __GNUC__ >= 4) || defined __INTEL_COMPILER #else
# define ZMQ_EXPORT __attribute__ ((visibility("default"))) #if defined __SUNPRO_C || defined __SUNPRO_CC
# else #define ZMQ_EXPORT __global
# define ZMQ_EXPORT #elif (defined __GNUC__ && __GNUC__ >= 4) || defined __INTEL_COMPILER
# endif #define ZMQ_EXPORT __attribute__ ((visibility ("default")))
#else
#define ZMQ_EXPORT
#endif
#endif #endif
/* Define integer types needed for event interface */ /* Define integer types needed for event interface */
#define ZMQ_DEFINED_STDINT 1 #define ZMQ_DEFINED_STDINT 1
#if defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_OPENVMS #if defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_OPENVMS
# include <inttypes.h> #include <inttypes.h>
#elif defined _MSC_VER && _MSC_VER < 1600 #elif defined _MSC_VER && _MSC_VER < 1600
# ifndef int32_t #ifndef int32_t
typedef __int32 int32_t; typedef __int32 int32_t;
# endif #endif
# ifndef uint32_t #ifndef uint32_t
typedef unsigned __int32 uint32_t; typedef unsigned __int32 uint32_t;
# endif #endif
# ifndef uint16_t #ifndef uint16_t
typedef unsigned __int16 uint16_t; typedef unsigned __int16 uint16_t;
# endif #endif
# ifndef uint8_t #ifndef uint8_t
typedef unsigned __int8 uint8_t; typedef unsigned __int8 uint8_t;
# endif #endif
#else #else
# include <stdint.h> #include <stdint.h>
#endif #endif
// 32-bit AIX's pollfd struct members are called reqevents and rtnevents so it // 32-bit AIX's pollfd struct members are called reqevents and rtnevents so it
// defines compatibility macros for them. Need to include that header first to // defines compatibility macros for them. Need to include that header first to
// stop build failures since zmq_pollset_t defines them as events and revents. // stop build failures since zmq_pollset_t defines them as events and revents.
#ifdef ZMQ_HAVE_AIX #ifdef ZMQ_HAVE_AIX
#include <poll.h> #include <poll.h>
#endif #endif
@@ -209,7 +209,7 @@ ZMQ_EXPORT void zmq_version (int *major, int *minor, int *patch);
/******************************************************************************/ /******************************************************************************/
/* Context options */ /* Context options */
#define ZMQ_IO_THREADS 1 #define ZMQ_IO_THREADS 1
#define ZMQ_MAX_SOCKETS 2 #define ZMQ_MAX_SOCKETS 2
#define ZMQ_SOCKET_LIMIT 3 #define ZMQ_SOCKET_LIMIT 3
#define ZMQ_THREAD_PRIORITY 3 #define ZMQ_THREAD_PRIORITY 3
@@ -217,7 +217,7 @@ ZMQ_EXPORT void zmq_version (int *major, int *minor, int *patch);
#define ZMQ_MAX_MSGSZ 5 #define ZMQ_MAX_MSGSZ 5
/* Default for new contexts */ /* Default for new contexts */
#define ZMQ_IO_THREADS_DFLT 1 #define ZMQ_IO_THREADS_DFLT 1
#define ZMQ_MAX_SOCKETS_DFLT 1023 #define ZMQ_MAX_SOCKETS_DFLT 1023
#define ZMQ_THREAD_PRIORITY_DFLT -1 #define ZMQ_THREAD_PRIORITY_DFLT -1
#define ZMQ_THREAD_SCHED_POLICY_DFLT -1 #define ZMQ_THREAD_SCHED_POLICY_DFLT -1
@@ -242,26 +242,27 @@ ZMQ_EXPORT int zmq_ctx_destroy (void *context);
* alignment and raise sigbus on violations. Make sure applications allocate * alignment and raise sigbus on violations. Make sure applications allocate
* zmq_msg_t on addresses aligned on a pointer-size boundary to avoid this issue. * zmq_msg_t on addresses aligned on a pointer-size boundary to avoid this issue.
*/ */
typedef struct zmq_msg_t { typedef struct zmq_msg_t
#if defined (__GNUC__) || defined ( __INTEL_COMPILER) || \ {
(defined (__SUNPRO_C) && __SUNPRO_C >= 0x590) || \ #if defined(__GNUC__) || defined(__INTEL_COMPILER) \
(defined (__SUNPRO_CC) && __SUNPRO_CC >= 0x590) || (defined(__SUNPRO_C) && __SUNPRO_C >= 0x590) \
unsigned char _ [64] __attribute__ ((aligned (sizeof (void *)))); || (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x590)
#elif defined (_MSC_VER) && (defined (_M_X64) || defined (_M_ARM64)) unsigned char _[64] __attribute__ ((aligned (sizeof (void *))));
__declspec (align (8)) unsigned char _ [64]; #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
#elif defined (_MSC_VER) && (defined (_M_IX86) || defined (_M_ARM_ARMV7VE)) __declspec(align (8)) unsigned char _[64];
__declspec (align (4)) unsigned char _ [64]; #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_ARM_ARMV7VE))
__declspec(align (4)) unsigned char _[64];
#else #else
unsigned char _ [64]; unsigned char _[64];
#endif #endif
} zmq_msg_t; } zmq_msg_t;
typedef void (zmq_free_fn) (void *data, void *hint); typedef void(zmq_free_fn) (void *data, void *hint);
ZMQ_EXPORT int zmq_msg_init (zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_init (zmq_msg_t *msg);
ZMQ_EXPORT int zmq_msg_init_size (zmq_msg_t *msg, size_t size); ZMQ_EXPORT int zmq_msg_init_size (zmq_msg_t *msg, size_t size);
ZMQ_EXPORT int zmq_msg_init_data (zmq_msg_t *msg, void *data, ZMQ_EXPORT int zmq_msg_init_data (
size_t size, zmq_free_fn *ffn, void *hint); zmq_msg_t *msg, void *data, size_t size, zmq_free_fn *ffn, void *hint);
ZMQ_EXPORT int zmq_msg_send (zmq_msg_t *msg, void *s, int flags); ZMQ_EXPORT int zmq_msg_send (zmq_msg_t *msg, void *s, int flags);
ZMQ_EXPORT int zmq_msg_recv (zmq_msg_t *msg, void *s, int flags); ZMQ_EXPORT int zmq_msg_recv (zmq_msg_t *msg, void *s, int flags);
ZMQ_EXPORT int zmq_msg_close (zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_close (zmq_msg_t *msg);
@@ -272,7 +273,8 @@ ZMQ_EXPORT size_t zmq_msg_size (const zmq_msg_t *msg);
ZMQ_EXPORT int zmq_msg_more (const zmq_msg_t *msg); ZMQ_EXPORT int zmq_msg_more (const zmq_msg_t *msg);
ZMQ_EXPORT int zmq_msg_get (const zmq_msg_t *msg, int property); ZMQ_EXPORT int zmq_msg_get (const zmq_msg_t *msg, int property);
ZMQ_EXPORT int zmq_msg_set (zmq_msg_t *msg, int property, int optval); ZMQ_EXPORT int zmq_msg_set (zmq_msg_t *msg, int property, int optval);
ZMQ_EXPORT const char *zmq_msg_gets (const zmq_msg_t *msg, const char *property); ZMQ_EXPORT const char *zmq_msg_gets (const zmq_msg_t *msg,
const char *property);
/******************************************************************************/ /******************************************************************************/
/* 0MQ socket definition. */ /* 0MQ socket definition. */
@@ -386,20 +388,20 @@ ZMQ_EXPORT const char *zmq_msg_gets (const zmq_msg_t *msg, const char *property)
#define ZMQ_GSSAPI 3 #define ZMQ_GSSAPI 3
/* RADIO-DISH protocol */ /* RADIO-DISH protocol */
#define ZMQ_GROUP_MAX_LENGTH 15 #define ZMQ_GROUP_MAX_LENGTH 15
/* Deprecated options and aliases */ /* Deprecated options and aliases */
#define ZMQ_IDENTITY ZMQ_ROUTING_ID #define ZMQ_IDENTITY ZMQ_ROUTING_ID
#define ZMQ_CONNECT_RID ZMQ_CONNECT_ROUTING_ID #define ZMQ_CONNECT_RID ZMQ_CONNECT_ROUTING_ID
#define ZMQ_TCP_ACCEPT_FILTER 38 #define ZMQ_TCP_ACCEPT_FILTER 38
#define ZMQ_IPC_FILTER_PID 58 #define ZMQ_IPC_FILTER_PID 58
#define ZMQ_IPC_FILTER_UID 59 #define ZMQ_IPC_FILTER_UID 59
#define ZMQ_IPC_FILTER_GID 60 #define ZMQ_IPC_FILTER_GID 60
#define ZMQ_IPV4ONLY 31 #define ZMQ_IPV4ONLY 31
#define ZMQ_DELAY_ATTACH_ON_CONNECT ZMQ_IMMEDIATE #define ZMQ_DELAY_ATTACH_ON_CONNECT ZMQ_IMMEDIATE
#define ZMQ_NOBLOCK ZMQ_DONTWAIT #define ZMQ_NOBLOCK ZMQ_DONTWAIT
#define ZMQ_FAIL_UNROUTABLE ZMQ_ROUTER_MANDATORY #define ZMQ_FAIL_UNROUTABLE ZMQ_ROUTER_MANDATORY
#define ZMQ_ROUTER_BEHAVIOR ZMQ_ROUTER_MANDATORY #define ZMQ_ROUTER_BEHAVIOR ZMQ_ROUTER_MANDATORY
/* Deprecated Message options */ /* Deprecated Message options */
#define ZMQ_SRCFD 2 #define ZMQ_SRCFD 2
@@ -410,25 +412,25 @@ ZMQ_EXPORT const char *zmq_msg_gets (const zmq_msg_t *msg, const char *property)
/* Socket transport events (TCP, IPC and TIPC only) */ /* Socket transport events (TCP, IPC and TIPC only) */
#define ZMQ_EVENT_CONNECTED 0x0001 #define ZMQ_EVENT_CONNECTED 0x0001
#define ZMQ_EVENT_CONNECT_DELAYED 0x0002 #define ZMQ_EVENT_CONNECT_DELAYED 0x0002
#define ZMQ_EVENT_CONNECT_RETRIED 0x0004 #define ZMQ_EVENT_CONNECT_RETRIED 0x0004
#define ZMQ_EVENT_LISTENING 0x0008 #define ZMQ_EVENT_LISTENING 0x0008
#define ZMQ_EVENT_BIND_FAILED 0x0010 #define ZMQ_EVENT_BIND_FAILED 0x0010
#define ZMQ_EVENT_ACCEPTED 0x0020 #define ZMQ_EVENT_ACCEPTED 0x0020
#define ZMQ_EVENT_ACCEPT_FAILED 0x0040 #define ZMQ_EVENT_ACCEPT_FAILED 0x0040
#define ZMQ_EVENT_CLOSED 0x0080 #define ZMQ_EVENT_CLOSED 0x0080
#define ZMQ_EVENT_CLOSE_FAILED 0x0100 #define ZMQ_EVENT_CLOSE_FAILED 0x0100
#define ZMQ_EVENT_DISCONNECTED 0x0200 #define ZMQ_EVENT_DISCONNECTED 0x0200
#define ZMQ_EVENT_MONITOR_STOPPED 0x0400 #define ZMQ_EVENT_MONITOR_STOPPED 0x0400
#define ZMQ_EVENT_ALL 0xFFFF #define ZMQ_EVENT_ALL 0xFFFF
ZMQ_EXPORT void *zmq_socket (void *, int type); ZMQ_EXPORT void *zmq_socket (void *, int type);
ZMQ_EXPORT int zmq_close (void *s); ZMQ_EXPORT int zmq_close (void *s);
ZMQ_EXPORT int zmq_setsockopt (void *s, int option, const void *optval, ZMQ_EXPORT int
size_t optvallen); zmq_setsockopt (void *s, int option, const void *optval, size_t optvallen);
ZMQ_EXPORT int zmq_getsockopt (void *s, int option, void *optval, ZMQ_EXPORT int
size_t *optvallen); zmq_getsockopt (void *s, int option, void *optval, size_t *optvallen);
ZMQ_EXPORT int zmq_bind (void *s, const char *addr); ZMQ_EXPORT int zmq_bind (void *s, const char *addr);
ZMQ_EXPORT int zmq_connect (void *s, const char *addr); ZMQ_EXPORT int zmq_connect (void *s, const char *addr);
ZMQ_EXPORT int zmq_unbind (void *s, const char *addr); ZMQ_EXPORT int zmq_unbind (void *s, const char *addr);
@@ -462,14 +464,17 @@ typedef struct zmq_pollitem_t
#define ZMQ_POLLITEMS_DFLT 16 #define ZMQ_POLLITEMS_DFLT 16
ZMQ_EXPORT int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout); ZMQ_EXPORT int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout);
/******************************************************************************/ /******************************************************************************/
/* Message proxying */ /* Message proxying */
/******************************************************************************/ /******************************************************************************/
ZMQ_EXPORT int zmq_proxy (void *frontend, void *backend, void *capture); ZMQ_EXPORT int zmq_proxy (void *frontend, void *backend, void *capture);
ZMQ_EXPORT int zmq_proxy_steerable (void *frontend, void *backend, void *capture, void *control); ZMQ_EXPORT int zmq_proxy_steerable (void *frontend,
void *backend,
void *capture,
void *control);
/******************************************************************************/ /******************************************************************************/
/* Probe library capabilities */ /* Probe library capabilities */
@@ -488,8 +493,10 @@ ZMQ_EXPORT int zmq_device (int type, void *frontend, void *backend);
ZMQ_EXPORT int zmq_sendmsg (void *s, zmq_msg_t *msg, int flags); ZMQ_EXPORT int zmq_sendmsg (void *s, zmq_msg_t *msg, int flags);
ZMQ_EXPORT int zmq_recvmsg (void *s, zmq_msg_t *msg, int flags); ZMQ_EXPORT int zmq_recvmsg (void *s, zmq_msg_t *msg, int flags);
struct iovec; struct iovec;
ZMQ_EXPORT int zmq_sendiov (void *s, struct iovec *iov, size_t count, int flags); ZMQ_EXPORT int
ZMQ_EXPORT int zmq_recviov (void *s, struct iovec *iov, size_t *count, int flags); zmq_sendiov (void *s, struct iovec *iov, size_t count, int flags);
ZMQ_EXPORT int
zmq_recviov (void *s, struct iovec *iov, size_t *count, int flags);
/******************************************************************************/ /******************************************************************************/
/* Encryption functions */ /* Encryption functions */
@@ -507,7 +514,8 @@ ZMQ_EXPORT int zmq_curve_keypair (char *z85_public_key, char *z85_secret_key);
/* Derive the z85-encoded public key from the z85-encoded secret key. */ /* Derive the z85-encoded public key from the z85-encoded secret key. */
/* Returns 0 on success. */ /* Returns 0 on success. */
ZMQ_EXPORT int zmq_curve_public (char *z85_public_key, const char *z85_secret_key); ZMQ_EXPORT int zmq_curve_public (char *z85_public_key,
const char *z85_secret_key);
/******************************************************************************/ /******************************************************************************/
/* Atomic utility methods */ /* Atomic utility methods */
@@ -540,13 +548,13 @@ ZMQ_EXPORT unsigned long zmq_stopwatch_stop (void *watch_);
/* Sleeps for specified number of seconds. */ /* Sleeps for specified number of seconds. */
ZMQ_EXPORT void zmq_sleep (int seconds_); ZMQ_EXPORT void zmq_sleep (int seconds_);
typedef void (zmq_thread_fn) (void*); typedef void(zmq_thread_fn) (void *);
/* Start a thread. Returns a handle to the thread. */ /* Start a thread. Returns a handle to the thread. */
ZMQ_EXPORT void *zmq_threadstart (zmq_thread_fn* func, void* arg); ZMQ_EXPORT void *zmq_threadstart (zmq_thread_fn *func, void *arg);
/* Wait for thread to complete then free up resources. */ /* Wait for thread to complete then free up resources. */
ZMQ_EXPORT void zmq_threadclose (void* thread); ZMQ_EXPORT void zmq_threadclose (void *thread);
/******************************************************************************/ /******************************************************************************/
@@ -573,16 +581,16 @@ ZMQ_EXPORT void zmq_threadclose (void* thread);
/* DRAFT 0MQ socket events and monitoring */ /* DRAFT 0MQ socket events and monitoring */
/* Unspecified system errors during handshake. Event value is an errno. */ /* Unspecified system errors during handshake. Event value is an errno. */
#define ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL 0x0800 #define ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL 0x0800
/* Handshake complete successfully with successful authentication (if * /* Handshake complete successfully with successful authentication (if *
* enabled). Event value is unused. */ * enabled). Event value is unused. */
#define ZMQ_EVENT_HANDSHAKE_SUCCEEDED 0x1000 #define ZMQ_EVENT_HANDSHAKE_SUCCEEDED 0x1000
/* Protocol errors between ZMTP peers or between server and ZAP handler. * /* Protocol errors between ZMTP peers or between server and ZAP handler. *
* Event value is one of ZMQ_PROTOCOL_ERROR_* */ * Event value is one of ZMQ_PROTOCOL_ERROR_* */
#define ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL 0x2000 #define ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL 0x2000
/* Failed authentication requests. Event value is the numeric ZAP status * /* Failed authentication requests. Event value is the numeric ZAP status *
* code, i.e. 300, 400 or 500. */ * code, i.e. 300, 400 or 500. */
#define ZMQ_EVENT_HANDSHAKE_FAILED_AUTH 0x4000 #define ZMQ_EVENT_HANDSHAKE_FAILED_AUTH 0x4000
#define ZMQ_PROTOCOL_ERROR_ZMTP_UNSPECIFIED 0x10000000 #define ZMQ_PROTOCOL_ERROR_ZMTP_UNSPECIFIED 0x10000000
#define ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND 0x10000001 #define ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND 0x10000001
@@ -601,7 +609,7 @@ ZMQ_EXPORT void zmq_threadclose (void* thread);
#define ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC 0x11000001 #define ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC 0x11000001
#define ZMQ_PROTOCOL_ERROR_ZMTP_MECHANISM_MISMATCH 0x11000002 #define ZMQ_PROTOCOL_ERROR_ZMTP_MECHANISM_MISMATCH 0x11000002
#define ZMQ_PROTOCOL_ERROR_ZAP_UNSPECIFIED 0x20000000 #define ZMQ_PROTOCOL_ERROR_ZAP_UNSPECIFIED 0x20000000
#define ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY 0x20000001 #define ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY 0x20000001
#define ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID 0x20000002 #define ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID 0x20000002
#define ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION 0x20000003 #define ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION 0x20000003
@@ -619,16 +627,16 @@ ZMQ_EXPORT int zmq_join (void *s, const char *group);
ZMQ_EXPORT int zmq_leave (void *s, const char *group); ZMQ_EXPORT int zmq_leave (void *s, const char *group);
/* DRAFT Msg methods. */ /* DRAFT Msg methods. */
ZMQ_EXPORT int zmq_msg_set_routing_id(zmq_msg_t *msg, uint32_t routing_id); ZMQ_EXPORT int zmq_msg_set_routing_id (zmq_msg_t *msg, uint32_t routing_id);
ZMQ_EXPORT uint32_t zmq_msg_routing_id(zmq_msg_t *msg); ZMQ_EXPORT uint32_t zmq_msg_routing_id (zmq_msg_t *msg);
ZMQ_EXPORT int zmq_msg_set_group(zmq_msg_t *msg, const char *group); ZMQ_EXPORT int zmq_msg_set_group (zmq_msg_t *msg, const char *group);
ZMQ_EXPORT const char *zmq_msg_group(zmq_msg_t *msg); ZMQ_EXPORT const char *zmq_msg_group (zmq_msg_t *msg);
/* DRAFT Msg property names. */ /* DRAFT Msg property names. */
#define ZMQ_MSG_PROPERTY_ROUTING_ID "Routing-Id" #define ZMQ_MSG_PROPERTY_ROUTING_ID "Routing-Id"
#define ZMQ_MSG_PROPERTY_SOCKET_TYPE "Socket-Type" #define ZMQ_MSG_PROPERTY_SOCKET_TYPE "Socket-Type"
#define ZMQ_MSG_PROPERTY_USER_ID "User-Id" #define ZMQ_MSG_PROPERTY_USER_ID "User-Id"
#define ZMQ_MSG_PROPERTY_PEER_ADDRESS "Peer-Address" #define ZMQ_MSG_PROPERTY_PEER_ADDRESS "Peer-Address"
/******************************************************************************/ /******************************************************************************/
/* Poller polling on sockets,fd and thread-safe sockets */ /* Poller polling on sockets,fd and thread-safe sockets */
@@ -649,19 +657,26 @@ typedef struct zmq_poller_event_t
} zmq_poller_event_t; } zmq_poller_event_t;
ZMQ_EXPORT void *zmq_poller_new (void); ZMQ_EXPORT void *zmq_poller_new (void);
ZMQ_EXPORT int zmq_poller_destroy (void **poller_p); ZMQ_EXPORT int zmq_poller_destroy (void **poller_p);
ZMQ_EXPORT int zmq_poller_add (void *poller, void *socket, void *user_data, short events); ZMQ_EXPORT int
ZMQ_EXPORT int zmq_poller_modify (void *poller, void *socket, short events); zmq_poller_add (void *poller, void *socket, void *user_data, short events);
ZMQ_EXPORT int zmq_poller_remove (void *poller, void *socket); ZMQ_EXPORT int zmq_poller_modify (void *poller, void *socket, short events);
ZMQ_EXPORT int zmq_poller_wait (void *poller, zmq_poller_event_t *event, long timeout); ZMQ_EXPORT int zmq_poller_remove (void *poller, void *socket);
ZMQ_EXPORT int zmq_poller_wait_all (void *poller, zmq_poller_event_t *events, int n_events, long timeout); ZMQ_EXPORT int
zmq_poller_wait (void *poller, zmq_poller_event_t *event, long timeout);
ZMQ_EXPORT int zmq_poller_wait_all (void *poller,
zmq_poller_event_t *events,
int n_events,
long timeout);
#if defined _WIN32 #if defined _WIN32
ZMQ_EXPORT int zmq_poller_add_fd (void *poller, SOCKET fd, void *user_data, short events); ZMQ_EXPORT int
zmq_poller_add_fd (void *poller, SOCKET fd, void *user_data, short events);
ZMQ_EXPORT int zmq_poller_modify_fd (void *poller, SOCKET fd, short events); ZMQ_EXPORT int zmq_poller_modify_fd (void *poller, SOCKET fd, short events);
ZMQ_EXPORT int zmq_poller_remove_fd (void *poller, SOCKET fd); ZMQ_EXPORT int zmq_poller_remove_fd (void *poller, SOCKET fd);
#else #else
ZMQ_EXPORT int zmq_poller_add_fd (void *poller, int fd, void *user_data, short events); ZMQ_EXPORT int
zmq_poller_add_fd (void *poller, int fd, void *user_data, short events);
ZMQ_EXPORT int zmq_poller_modify_fd (void *poller, int fd, short events); ZMQ_EXPORT int zmq_poller_modify_fd (void *poller, int fd, short events);
ZMQ_EXPORT int zmq_poller_remove_fd (void *poller, int fd); ZMQ_EXPORT int zmq_poller_remove_fd (void *poller, int fd);
#endif #endif
@@ -676,16 +691,18 @@ ZMQ_EXPORT int zmq_socket_get_peer_state (void *socket,
#define ZMQ_HAVE_TIMERS #define ZMQ_HAVE_TIMERS
typedef void (zmq_timer_fn)(int timer_id, void *arg); typedef void(zmq_timer_fn) (int timer_id, void *arg);
ZMQ_EXPORT void *zmq_timers_new (void); ZMQ_EXPORT void *zmq_timers_new (void);
ZMQ_EXPORT int zmq_timers_destroy (void **timers_p); ZMQ_EXPORT int zmq_timers_destroy (void **timers_p);
ZMQ_EXPORT int zmq_timers_add (void *timers, size_t interval, zmq_timer_fn handler, void *arg); ZMQ_EXPORT int
ZMQ_EXPORT int zmq_timers_cancel (void *timers, int timer_id); zmq_timers_add (void *timers, size_t interval, zmq_timer_fn handler, void *arg);
ZMQ_EXPORT int zmq_timers_set_interval (void *timers, int timer_id, size_t interval); ZMQ_EXPORT int zmq_timers_cancel (void *timers, int timer_id);
ZMQ_EXPORT int zmq_timers_reset (void *timers, int timer_id); ZMQ_EXPORT int
ZMQ_EXPORT long zmq_timers_timeout (void *timers); zmq_timers_set_interval (void *timers, int timer_id, size_t interval);
ZMQ_EXPORT int zmq_timers_execute (void *timers); ZMQ_EXPORT int zmq_timers_reset (void *timers, int timer_id);
ZMQ_EXPORT long zmq_timers_timeout (void *timers);
ZMQ_EXPORT int zmq_timers_execute (void *timers);
/******************************************************************************/ /******************************************************************************/
/* GSSAPI definitions */ /* GSSAPI definitions */

View File

@@ -34,14 +34,16 @@
compilers even have an equivalent concept. compilers even have an equivalent concept.
So in the worst case, this include file is treated as silently empty. */ So in the worst case, this include file is treated as silently empty. */
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER) #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) \
|| defined(_MSC_VER)
#if defined(__GNUC__) || defined(__GNUG__) #if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wcpp" #pragma GCC diagnostic warning "-Wcpp"
#pragma GCC diagnostic ignored "-Werror" #pragma GCC diagnostic ignored "-Werror"
#pragma GCC diagnostic ignored "-Wall" #pragma GCC diagnostic ignored "-Wall"
#endif #endif
#pragma message("Warning: zmq_utils.h is deprecated. All its functionality is provided by zmq.h.") #pragma message( \
"Warning: zmq_utils.h is deprecated. All its functionality is provided by zmq.h.")
#if defined(__GNUC__) || defined(__GNUG__) #if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif

View File

@@ -106,7 +106,7 @@ static void *worker (void *ctx_)
#endif #endif
} }
int main (int argc, char *argv []) int main (int argc, char *argv[])
{ {
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
HANDLE local_thread; HANDLE local_thread;
@@ -127,8 +127,8 @@ int main (int argc, char *argv [])
return 1; return 1;
} }
message_size = atoi (argv [1]); message_size = atoi (argv[1]);
roundtrip_count = atoi (argv [2]); roundtrip_count = atoi (argv[2]);
ctx = zmq_init (1); ctx = zmq_init (1);
if (!ctx) { if (!ctx) {
@@ -149,8 +149,7 @@ int main (int argc, char *argv [])
} }
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
local_thread = (HANDLE) _beginthreadex (NULL, 0, local_thread = (HANDLE) _beginthreadex (NULL, 0, worker, ctx, 0, NULL);
worker, ctx, 0 , NULL);
if (local_thread == 0) { if (local_thread == 0) {
printf ("error in _beginthreadex\n"); printf ("error in _beginthreadex\n");
return -1; return -1;
@@ -237,4 +236,3 @@ int main (int argc, char *argv [])
return 0; return 0;
} }

View File

@@ -71,7 +71,6 @@ static void *worker (void *ctx_)
} }
for (i = 0; i != message_count; i++) { for (i = 0; i != message_count; i++) {
rc = zmq_msg_init_size (&msg, message_size); rc = zmq_msg_init_size (&msg, message_size);
if (rc != 0) { if (rc != 0) {
printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
@@ -106,7 +105,7 @@ static void *worker (void *ctx_)
#endif #endif
} }
int main (int argc, char *argv []) int main (int argc, char *argv[])
{ {
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
HANDLE local_thread; HANDLE local_thread;
@@ -128,8 +127,8 @@ int main (int argc, char *argv [])
return 1; return 1;
} }
message_size = atoi (argv [1]); message_size = atoi (argv[1]);
message_count = atoi (argv [2]); message_count = atoi (argv[2]);
ctx = zmq_init (1); ctx = zmq_init (1);
if (!ctx) { if (!ctx) {
@@ -150,8 +149,7 @@ int main (int argc, char *argv [])
} }
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
local_thread = (HANDLE) _beginthreadex (NULL, 0, local_thread = (HANDLE) _beginthreadex (NULL, 0, worker, ctx, 0, NULL);
worker, ctx, 0 , NULL);
if (local_thread == 0) { if (local_thread == 0) {
printf ("error in _beginthreadex\n"); printf ("error in _beginthreadex\n");
return -1; return -1;
@@ -238,8 +236,8 @@ int main (int argc, char *argv [])
return -1; return -1;
} }
throughput = (unsigned long) throughput =
((double) message_count / (double) elapsed * 1000000); (unsigned long) ((double) message_count / (double) elapsed * 1000000);
megabits = (double) (throughput * message_size * 8) / 1000000; megabits = (double) (throughput * message_size * 8) / 1000000;
printf ("mean throughput: %d [msg/s]\n", (int) throughput); printf ("mean throughput: %d [msg/s]\n", (int) throughput);
@@ -247,4 +245,3 @@ int main (int argc, char *argv [])
return 0; return 0;
} }

View File

@@ -31,7 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int main (int argc, char *argv []) int main (int argc, char *argv[])
{ {
const char *bind_to; const char *bind_to;
int roundtrip_count; int roundtrip_count;
@@ -44,12 +44,12 @@ int main (int argc, char *argv [])
if (argc != 4) { if (argc != 4) {
printf ("usage: local_lat <bind-to> <message-size> " printf ("usage: local_lat <bind-to> <message-size> "
"<roundtrip-count>\n"); "<roundtrip-count>\n");
return 1; return 1;
} }
bind_to = argv [1]; bind_to = argv[1];
message_size = atoi (argv [2]); message_size = atoi (argv[2]);
roundtrip_count = atoi (argv [3]); roundtrip_count = atoi (argv[3]);
ctx = zmq_init (1); ctx = zmq_init (1);
if (!ctx) { if (!ctx) {

View File

@@ -34,7 +34,7 @@
// keys are arbitrary but must match remote_lat.cpp // keys are arbitrary but must match remote_lat.cpp
const char server_prvkey[] = "{X}#>t#jRGaQ}gMhv=30r(Mw+87YGs+5%kh=i@f8"; const char server_prvkey[] = "{X}#>t#jRGaQ}gMhv=30r(Mw+87YGs+5%kh=i@f8";
int main (int argc, char *argv []) int main (int argc, char *argv[])
{ {
const char *bind_to; const char *bind_to;
int message_count; int message_count;
@@ -51,13 +51,14 @@ int main (int argc, char *argv [])
int curve = 0; int curve = 0;
if (argc != 4 && argc != 5) { if (argc != 4 && argc != 5) {
printf ("usage: local_thr <bind-to> <message-size> <message-count> [<enable_curve>]\n"); printf ("usage: local_thr <bind-to> <message-size> <message-count> "
"[<enable_curve>]\n");
return 1; return 1;
} }
bind_to = argv [1]; bind_to = argv[1];
message_size = atoi (argv [2]); message_size = atoi (argv[2]);
message_count = atoi (argv [3]); message_count = atoi (argv[3]);
if (argc >= 5 && atoi (argv [4])) { if (argc >= 5 && atoi (argv[4])) {
curve = 1; curve = 1;
} }
@@ -76,13 +77,14 @@ int main (int argc, char *argv [])
// Add your socket options here. // Add your socket options here.
// For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
if (curve) { if (curve) {
rc = zmq_setsockopt (s, ZMQ_CURVE_SECRETKEY, server_prvkey, sizeof(server_prvkey)); rc = zmq_setsockopt (s, ZMQ_CURVE_SECRETKEY, server_prvkey,
sizeof (server_prvkey));
if (rc != 0) { if (rc != 0) {
printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno)); printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno));
return -1; return -1;
} }
int server = 1; int server = 1;
rc = zmq_setsockopt (s, ZMQ_CURVE_SERVER, &server, sizeof(int)); rc = zmq_setsockopt (s, ZMQ_CURVE_SERVER, &server, sizeof (int));
if (rc != 0) { if (rc != 0) {
printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno)); printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno));
return -1; return -1;
@@ -135,8 +137,7 @@ int main (int argc, char *argv [])
return -1; return -1;
} }
throughput = throughput = ((double) message_count / (double) elapsed * 1000000);
((double) message_count / (double) elapsed * 1000000);
megabits = ((double) throughput * message_size * 8) / 1000000; megabits = ((double) throughput * message_size * 8) / 1000000;
printf ("message size: %d [B]\n", (int) message_size); printf ("message size: %d [B]\n", (int) message_size);

View File

@@ -32,7 +32,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int main (int argc, char *argv []) int main (int argc, char *argv[])
{ {
const char *connect_to; const char *connect_to;
int roundtrip_count; int roundtrip_count;
@@ -48,12 +48,12 @@ int main (int argc, char *argv [])
if (argc != 4) { if (argc != 4) {
printf ("usage: remote_lat <connect-to> <message-size> " printf ("usage: remote_lat <connect-to> <message-size> "
"<roundtrip-count>\n"); "<roundtrip-count>\n");
return 1; return 1;
} }
connect_to = argv [1]; connect_to = argv[1];
message_size = atoi (argv [2]); message_size = atoi (argv[2]);
roundtrip_count = atoi (argv [3]); roundtrip_count = atoi (argv[3]);
ctx = zmq_init (1); ctx = zmq_init (1);
if (!ctx) { if (!ctx) {

View File

@@ -37,7 +37,7 @@ const char server_pubkey[] = "DX4nh=yUn{-9ugra0X3Src4SU-4xTgqxcYY.+<SH";
const char client_pubkey[] = "<n^oA}I:66W+*ds3tAmi1+KJzv-}k&fC2aA5Bj0K"; const char client_pubkey[] = "<n^oA}I:66W+*ds3tAmi1+KJzv-}k&fC2aA5Bj0K";
const char client_prvkey[] = "9R9bV}[6z6DC-%$!jTVTKvWc=LEL{4i4gzUe$@Zx"; const char client_prvkey[] = "9R9bV}[6z6DC-%$!jTVTKvWc=LEL{4i4gzUe$@Zx";
int main (int argc, char *argv []) int main (int argc, char *argv[])
{ {
const char *connect_to; const char *connect_to;
int message_count; int message_count;
@@ -51,13 +51,13 @@ int main (int argc, char *argv [])
if (argc != 4 && argc != 5) { if (argc != 4 && argc != 5) {
printf ("usage: remote_thr <connect-to> <message-size> " printf ("usage: remote_thr <connect-to> <message-size> "
"<message-count> [<enable_curve>]\n"); "<message-count> [<enable_curve>]\n");
return 1; return 1;
} }
connect_to = argv [1]; connect_to = argv[1];
message_size = atoi (argv [2]); message_size = atoi (argv[2]);
message_count = atoi (argv [3]); message_count = atoi (argv[3]);
if (argc >= 5 && atoi (argv [4])) { if (argc >= 5 && atoi (argv[4])) {
curve = 1; curve = 1;
} }
@@ -76,19 +76,22 @@ int main (int argc, char *argv [])
// Add your socket options here. // Add your socket options here.
// For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
if (curve) { if (curve) {
rc = zmq_setsockopt (s, ZMQ_CURVE_SECRETKEY, client_prvkey, sizeof (client_prvkey)); rc = zmq_setsockopt (s, ZMQ_CURVE_SECRETKEY, client_prvkey,
sizeof (client_prvkey));
if (rc != 0) { if (rc != 0) {
printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno)); printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno));
return -1; return -1;
} }
rc = zmq_setsockopt (s, ZMQ_CURVE_PUBLICKEY, client_pubkey, sizeof (client_pubkey)); rc = zmq_setsockopt (s, ZMQ_CURVE_PUBLICKEY, client_pubkey,
sizeof (client_pubkey));
if (rc != 0) { if (rc != 0) {
printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno)); printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno));
return -1; return -1;
} }
rc = zmq_setsockopt (s, ZMQ_CURVE_SERVERKEY, server_pubkey, sizeof (server_pubkey)); rc = zmq_setsockopt (s, ZMQ_CURVE_SERVERKEY, server_pubkey,
sizeof (server_pubkey));
if (rc != 0) { if (rc != 0) {
printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno)); printf ("error in zmq_setsockoopt: %s\n", zmq_strerror (errno));
return -1; return -1;

View File

@@ -44,11 +44,12 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
zmq::address_t::address_t ( zmq::address_t::address_t (const std::string &protocol_,
const std::string &protocol_, const std::string &address_, ctx_t *parent_) const std::string &address_,
: protocol (protocol_), ctx_t *parent_) :
address (address_), protocol (protocol_),
parent (parent_) address (address_),
parent (parent_)
{ {
memset (&resolved, 0, sizeof resolved); memset (&resolved, 0, sizeof resolved);
} }
@@ -57,35 +58,32 @@ zmq::address_t::~address_t ()
{ {
if (protocol == "tcp") { if (protocol == "tcp") {
if (resolved.tcp_addr) { if (resolved.tcp_addr) {
LIBZMQ_DELETE(resolved.tcp_addr); LIBZMQ_DELETE (resolved.tcp_addr);
} }
} }
if (protocol == "udp") { if (protocol == "udp") {
if (resolved.udp_addr) { if (resolved.udp_addr) {
LIBZMQ_DELETE(resolved.udp_addr); LIBZMQ_DELETE (resolved.udp_addr);
} }
} }
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
else else if (protocol == "ipc") {
if (protocol == "ipc") {
if (resolved.ipc_addr) { if (resolved.ipc_addr) {
LIBZMQ_DELETE(resolved.ipc_addr); LIBZMQ_DELETE (resolved.ipc_addr);
} }
} }
#endif #endif
#if defined ZMQ_HAVE_TIPC #if defined ZMQ_HAVE_TIPC
else else if (protocol == "tipc") {
if (protocol == "tipc") {
if (resolved.tipc_addr) { if (resolved.tipc_addr) {
LIBZMQ_DELETE(resolved.tipc_addr); LIBZMQ_DELETE (resolved.tipc_addr);
} }
} }
#endif #endif
#if defined ZMQ_HAVE_VMCI #if defined ZMQ_HAVE_VMCI
else else if (protocol == "vmci") {
if (protocol == "vmci") {
if (resolved.vmci_addr) { if (resolved.vmci_addr) {
LIBZMQ_DELETE(resolved.vmci_addr); LIBZMQ_DELETE (resolved.vmci_addr);
} }
} }
#endif #endif
@@ -102,22 +100,19 @@ int zmq::address_t::to_string (std::string &addr_) const
return resolved.udp_addr->to_string (addr_); return resolved.udp_addr->to_string (addr_);
} }
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
else else if (protocol == "ipc") {
if (protocol == "ipc") {
if (resolved.ipc_addr) if (resolved.ipc_addr)
return resolved.ipc_addr->to_string (addr_); return resolved.ipc_addr->to_string (addr_);
} }
#endif #endif
#if defined ZMQ_HAVE_TIPC #if defined ZMQ_HAVE_TIPC
else else if (protocol == "tipc") {
if (protocol == "tipc") {
if (resolved.tipc_addr) if (resolved.tipc_addr)
return resolved.tipc_addr->to_string (addr_); return resolved.tipc_addr->to_string (addr_);
} }
#endif #endif
#if defined ZMQ_HAVE_VMCI #if defined ZMQ_HAVE_VMCI
else else if (protocol == "vmci") {
if (protocol == "vmci") {
if (resolved.vmci_addr) if (resolved.vmci_addr)
return resolved.vmci_addr->to_string (addr_); return resolved.vmci_addr->to_string (addr_);
} }

View File

@@ -34,44 +34,48 @@
namespace zmq namespace zmq
{ {
class ctx_t; class ctx_t;
class tcp_address_t; class tcp_address_t;
class udp_address_t; class udp_address_t;
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
class ipc_address_t; class ipc_address_t;
#endif #endif
#if defined ZMQ_HAVE_LINUX #if defined ZMQ_HAVE_LINUX
class tipc_address_t; class tipc_address_t;
#endif #endif
#if defined ZMQ_HAVE_VMCI #if defined ZMQ_HAVE_VMCI
class vmci_address_t; class vmci_address_t;
#endif #endif
struct address_t { struct address_t
address_t (const std::string &protocol_, const std::string &address_, ctx_t *parent_); {
address_t (const std::string &protocol_,
const std::string &address_,
ctx_t *parent_);
~address_t (); ~address_t ();
const std::string protocol; const std::string protocol;
const std::string address; const std::string address;
ctx_t *parent; ctx_t *parent;
// Protocol specific resolved address // Protocol specific resolved address
union { union
tcp_address_t *tcp_addr; {
udp_address_t *udp_addr; tcp_address_t *tcp_addr;
udp_address_t *udp_addr;
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
ipc_address_t *ipc_addr; ipc_address_t *ipc_addr;
#endif #endif
#if defined ZMQ_HAVE_LINUX #if defined ZMQ_HAVE_LINUX
tipc_address_t *tipc_addr; tipc_address_t *tipc_addr;
#endif #endif
#if defined ZMQ_HAVE_VMCI #if defined ZMQ_HAVE_VMCI
vmci_address_t *vmci_addr; vmci_address_t *vmci_addr;
#endif #endif
} resolved; } resolved;
int to_string (std::string &addr_) const; int to_string (std::string &addr_) const;
}; };
} }
#endif #endif

View File

@@ -35,133 +35,101 @@
namespace zmq namespace zmq
{ {
// Implementation of fast arrays with O(1) access, insertion and // Implementation of fast arrays with O(1) access, insertion and
// removal. The array stores pointers rather than objects. // removal. The array stores pointers rather than objects.
// O(1) is achieved by making items inheriting from // O(1) is achieved by making items inheriting from
// array_item_t<ID> class which internally stores the position // array_item_t<ID> class which internally stores the position
// in the array. // in the array.
// The ID template argument is used to differentiate among arrays // The ID template argument is used to differentiate among arrays
// and thus let an object be stored in different arrays. // and thus let an object be stored in different arrays.
// Base class for objects stored in the array. If you want to store // Base class for objects stored in the array. If you want to store
// same object in multiple arrays, each of those arrays has to have // same object in multiple arrays, each of those arrays has to have
// different ID. The item itself has to be derived from instantiations of // different ID. The item itself has to be derived from instantiations of
// array_item_t template for all relevant IDs. // array_item_t template for all relevant IDs.
template <int ID = 0> class array_item_t template <int ID = 0> class array_item_t
{
public:
inline array_item_t () : array_index (-1) {}
// The destructor doesn't have to be virtual. It is made virtual
// just to keep ICC and code checking tools from complaining.
inline virtual ~array_item_t () {}
inline void set_array_index (int index_) { array_index = index_; }
inline int get_array_index () { return array_index; }
private:
int array_index;
array_item_t (const array_item_t &);
const array_item_t &operator= (const array_item_t &);
};
template <typename T, int ID = 0> class array_t
{
private:
typedef array_item_t<ID> item_t;
public:
typedef typename std::vector<T *>::size_type size_type;
inline array_t () {}
inline ~array_t () {}
inline size_type size () { return items.size (); }
inline bool empty () { return items.empty (); }
inline T *&operator[] (size_type index_) { return items[index_]; }
inline void push_back (T *item_)
{ {
public: if (item_)
((item_t *) item_)->set_array_index ((int) items.size ());
items.push_back (item_);
}
inline array_item_t () : inline void erase (T *item_)
array_index (-1)
{
}
// The destructor doesn't have to be virtual. It is made virtual
// just to keep ICC and code checking tools from complaining.
inline virtual ~array_item_t ()
{
}
inline void set_array_index (int index_)
{
array_index = index_;
}
inline int get_array_index ()
{
return array_index;
}
private:
int array_index;
array_item_t (const array_item_t&);
const array_item_t &operator = (const array_item_t&);
};
template <typename T, int ID = 0> class array_t
{ {
private: erase (((item_t *) item_)->get_array_index ());
}
typedef array_item_t <ID> item_t; inline void erase (size_type index_)
{
if (items.back ())
((item_t *) items.back ())->set_array_index ((int) index_);
items[index_] = items.back ();
items.pop_back ();
}
public: inline void swap (size_type index1_, size_type index2_)
{
if (items[index1_])
((item_t *) items[index1_])->set_array_index ((int) index2_);
if (items[index2_])
((item_t *) items[index2_])->set_array_index ((int) index1_);
std::swap (items[index1_], items[index2_]);
}
typedef typename std::vector <T*>::size_type size_type; inline void clear () { items.clear (); }
inline array_t () inline size_type index (T *item_)
{ {
} return (size_type) ((item_t *) item_)->get_array_index ();
}
inline ~array_t () private:
{ typedef std::vector<T *> items_t;
} items_t items;
inline size_type size ()
{
return items.size ();
}
inline bool empty ()
{
return items.empty ();
}
inline T *&operator [] (size_type index_)
{
return items [index_];
}
inline void push_back (T *item_)
{
if (item_)
((item_t*) item_)->set_array_index ((int) items.size ());
items.push_back (item_);
}
inline void erase (T *item_) {
erase (((item_t*) item_)->get_array_index ());
}
inline void erase (size_type index_) {
if (items.back ())
((item_t*) items.back ())->set_array_index ((int) index_);
items [index_] = items.back ();
items.pop_back ();
}
inline void swap (size_type index1_, size_type index2_)
{
if (items [index1_])
((item_t*) items [index1_])->set_array_index ((int) index2_);
if (items [index2_])
((item_t*) items [index2_])->set_array_index ((int) index1_);
std::swap (items [index1_], items [index2_]);
}
inline void clear ()
{
items.clear ();
}
inline size_type index (T *item_)
{
return (size_type) ((item_t*) item_)->get_array_index ();
}
private:
typedef std::vector <T*> items_t;
items_t items;
array_t (const array_t&);
const array_t &operator = (const array_t&);
};
array_t (const array_t &);
const array_t &operator= (const array_t &);
};
} }
#endif #endif

View File

@@ -44,7 +44,8 @@
#define ZMQ_ATOMIC_COUNTER_ARM #define ZMQ_ATOMIC_COUNTER_ARM
#elif defined ZMQ_HAVE_WINDOWS #elif defined ZMQ_HAVE_WINDOWS
#define ZMQ_ATOMIC_COUNTER_WINDOWS #define ZMQ_ATOMIC_COUNTER_WINDOWS
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD || defined ZMQ_HAVE_GNU) #elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD \
|| defined ZMQ_HAVE_GNU)
#define ZMQ_ATOMIC_COUNTER_ATOMIC_H #define ZMQ_ATOMIC_COUNTER_ATOMIC_H
#elif defined __tile__ #elif defined __tile__
#define ZMQ_ATOMIC_COUNTER_TILE #define ZMQ_ATOMIC_COUNTER_TILE
@@ -66,174 +67,158 @@
namespace zmq namespace zmq
{ {
// This class represents an integer that can be incremented/decremented
// in atomic fashion.
//
// In zmq::shared_message_memory_allocator a buffer with an atomic_counter_t
// at the start is allocated. If the class does not align to pointer size,
// access to pointers in structures in the buffer will cause SIGBUS on
// architectures that do not allow mis-aligned pointers (eg: SPARC).
// Force the compiler to align to pointer size, which will cause the object
// to grow from 4 bytes to 8 bytes on 64 bit architectures (when not using
// mutexes).
// This class represents an integer that can be incremented/decremented #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
// in atomic fashion. class __declspec(align (8)) atomic_counter_t
// #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_ARM_ARMV7VE))
// In zmq::shared_message_memory_allocator a buffer with an atomic_counter_t class __declspec(align (4)) atomic_counter_t
// at the start is allocated. If the class does not align to pointer size,
// access to pointers in structures in the buffer will cause SIGBUS on
// architectures that do not allow mis-aligned pointers (eg: SPARC).
// Force the compiler to align to pointer size, which will cause the object
// to grow from 4 bytes to 8 bytes on 64 bit architectures (when not using
// mutexes).
#if defined (_MSC_VER) && (defined (_M_X64) || defined (_M_ARM64))
class __declspec (align (8)) atomic_counter_t
#elif defined (_MSC_VER) && (defined (_M_IX86) || defined (_M_ARM_ARMV7VE))
class __declspec (align (4)) atomic_counter_t
#else #else
class atomic_counter_t class atomic_counter_t
#endif #endif
{
public:
typedef uint32_t integer_t;
inline atomic_counter_t (integer_t value_ = 0) : value (value_) {}
inline ~atomic_counter_t () {}
// Set counter value (not thread-safe).
inline void set (integer_t value_) { value = value_; }
// Atomic addition. Returns the old value.
inline integer_t add (integer_t increment_)
{ {
public: integer_t old_value;
typedef uint32_t integer_t;
inline atomic_counter_t (integer_t value_ = 0) :
value (value_)
{
}
inline ~atomic_counter_t ()
{
}
// Set counter value (not thread-safe).
inline void set (integer_t value_)
{
value = value_;
}
// Atomic addition. Returns the old value.
inline integer_t add (integer_t increment_)
{
integer_t old_value;
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS #if defined ZMQ_ATOMIC_COUNTER_WINDOWS
old_value = InterlockedExchangeAdd ((LONG*) &value, increment_); old_value = InterlockedExchangeAdd ((LONG *) &value, increment_);
#elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC #elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC
old_value = __atomic_fetch_add(&value, increment_, __ATOMIC_ACQ_REL); old_value = __atomic_fetch_add (&value, increment_, __ATOMIC_ACQ_REL);
#elif defined ZMQ_ATOMIC_COUNTER_CXX11 #elif defined ZMQ_ATOMIC_COUNTER_CXX11
old_value = value.fetch_add(increment_, std::memory_order_acq_rel); old_value = value.fetch_add (increment_, std::memory_order_acq_rel);
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H #elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
integer_t new_value = atomic_add_32_nv (&value, increment_); integer_t new_value = atomic_add_32_nv (&value, increment_);
old_value = new_value - increment_; old_value = new_value - increment_;
#elif defined ZMQ_ATOMIC_COUNTER_TILE #elif defined ZMQ_ATOMIC_COUNTER_TILE
old_value = arch_atomic_add (&value, increment_); old_value = arch_atomic_add (&value, increment_);
#elif defined ZMQ_ATOMIC_COUNTER_X86 #elif defined ZMQ_ATOMIC_COUNTER_X86
__asm__ volatile ( __asm__ volatile("lock; xadd %0, %1 \n\t"
"lock; xadd %0, %1 \n\t" : "=r"(old_value), "=m"(value)
: "=r" (old_value), "=m" (value) : "0"(increment_), "m"(value)
: "0" (increment_), "m" (value) : "cc", "memory");
: "cc", "memory");
#elif defined ZMQ_ATOMIC_COUNTER_ARM #elif defined ZMQ_ATOMIC_COUNTER_ARM
integer_t flag, tmp; integer_t flag, tmp;
__asm__ volatile ( __asm__ volatile(" dmb sy\n\t"
" dmb sy\n\t" "1: ldrex %0, [%5]\n\t"
"1: ldrex %0, [%5]\n\t" " add %2, %0, %4\n\t"
" add %2, %0, %4\n\t" " strex %1, %2, [%5]\n\t"
" strex %1, %2, [%5]\n\t" " teq %1, #0\n\t"
" teq %1, #0\n\t" " bne 1b\n\t"
" bne 1b\n\t" " dmb sy\n\t"
" dmb sy\n\t" : "=&r"(old_value), "=&r"(flag), "=&r"(tmp),
: "=&r"(old_value), "=&r"(flag), "=&r"(tmp), "+Qo"(value) "+Qo"(value)
: "Ir"(increment_), "r"(&value) : "Ir"(increment_), "r"(&value)
: "cc"); : "cc");
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX #elif defined ZMQ_ATOMIC_COUNTER_MUTEX
sync.lock (); sync.lock ();
old_value = value; old_value = value;
value += increment_; value += increment_;
sync.unlock (); sync.unlock ();
#else #else
#error atomic_counter is not implemented for this platform #error atomic_counter is not implemented for this platform
#endif #endif
return old_value; return old_value;
} }
// Atomic subtraction. Returns false if the counter drops to zero. // Atomic subtraction. Returns false if the counter drops to zero.
inline bool sub (integer_t decrement) inline bool sub (integer_t decrement)
{ {
#if defined ZMQ_ATOMIC_COUNTER_WINDOWS #if defined ZMQ_ATOMIC_COUNTER_WINDOWS
LONG delta = - ((LONG) decrement); LONG delta = -((LONG) decrement);
integer_t old = InterlockedExchangeAdd ((LONG*) &value, delta); integer_t old = InterlockedExchangeAdd ((LONG *) &value, delta);
return old - decrement != 0; return old - decrement != 0;
#elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC #elif defined ZMQ_ATOMIC_COUNTER_INTRINSIC
integer_t nv = __atomic_sub_fetch(&value, decrement, __ATOMIC_ACQ_REL); integer_t nv = __atomic_sub_fetch (&value, decrement, __ATOMIC_ACQ_REL);
return nv != 0; return nv != 0;
#elif defined ZMQ_ATOMIC_COUNTER_CXX11 #elif defined ZMQ_ATOMIC_COUNTER_CXX11
integer_t old = value.fetch_sub(decrement, std::memory_order_acq_rel); integer_t old = value.fetch_sub (decrement, std::memory_order_acq_rel);
return old - decrement != 0; return old - decrement != 0;
#elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H #elif defined ZMQ_ATOMIC_COUNTER_ATOMIC_H
int32_t delta = - ((int32_t) decrement); int32_t delta = -((int32_t) decrement);
integer_t nv = atomic_add_32_nv (&value, delta); integer_t nv = atomic_add_32_nv (&value, delta);
return nv != 0; return nv != 0;
#elif defined ZMQ_ATOMIC_COUNTER_TILE #elif defined ZMQ_ATOMIC_COUNTER_TILE
int32_t delta = - ((int32_t) decrement); int32_t delta = -((int32_t) decrement);
integer_t nv = arch_atomic_add (&value, delta); integer_t nv = arch_atomic_add (&value, delta);
return nv != 0; return nv != 0;
#elif defined ZMQ_ATOMIC_COUNTER_X86 #elif defined ZMQ_ATOMIC_COUNTER_X86
integer_t oldval = -decrement; integer_t oldval = -decrement;
volatile integer_t *val = &value; volatile integer_t *val = &value;
__asm__ volatile ("lock; xaddl %0,%1" __asm__ volatile("lock; xaddl %0,%1"
: "=r" (oldval), "=m" (*val) : "=r"(oldval), "=m"(*val)
: "0" (oldval), "m" (*val) : "0"(oldval), "m"(*val)
: "cc", "memory"); : "cc", "memory");
return oldval != decrement; return oldval != decrement;
#elif defined ZMQ_ATOMIC_COUNTER_ARM #elif defined ZMQ_ATOMIC_COUNTER_ARM
integer_t old_value, flag, tmp; integer_t old_value, flag, tmp;
__asm__ volatile ( __asm__ volatile(" dmb sy\n\t"
" dmb sy\n\t" "1: ldrex %0, [%5]\n\t"
"1: ldrex %0, [%5]\n\t" " sub %2, %0, %4\n\t"
" sub %2, %0, %4\n\t" " strex %1, %2, [%5]\n\t"
" strex %1, %2, [%5]\n\t" " teq %1, #0\n\t"
" teq %1, #0\n\t" " bne 1b\n\t"
" bne 1b\n\t" " dmb sy\n\t"
" dmb sy\n\t" : "=&r"(old_value), "=&r"(flag), "=&r"(tmp),
: "=&r"(old_value), "=&r"(flag), "=&r"(tmp), "+Qo"(value) "+Qo"(value)
: "Ir"(decrement), "r"(&value) : "Ir"(decrement), "r"(&value)
: "cc"); : "cc");
return old_value - decrement != 0; return old_value - decrement != 0;
#elif defined ZMQ_ATOMIC_COUNTER_MUTEX #elif defined ZMQ_ATOMIC_COUNTER_MUTEX
sync.lock (); sync.lock ();
value -= decrement; value -= decrement;
bool result = value ? true : false; bool result = value ? true : false;
sync.unlock (); sync.unlock ();
return result; return result;
#else #else
#error atomic_counter is not implemented for this platform #error atomic_counter is not implemented for this platform
#endif #endif
} }
inline integer_t get () const inline integer_t get () const { return value; }
{
return value;
}
private:
private:
#if defined ZMQ_ATOMIC_COUNTER_CXX11 #if defined ZMQ_ATOMIC_COUNTER_CXX11
std::atomic<integer_t> value; std::atomic<integer_t> value;
#else #else
volatile integer_t value; volatile integer_t value;
#endif #endif
#if defined ZMQ_ATOMIC_COUNTER_MUTEX #if defined ZMQ_ATOMIC_COUNTER_MUTEX
mutex_t sync; mutex_t sync;
#endif #endif
#if ! defined ZMQ_ATOMIC_COUNTER_CXX11 #if !defined ZMQ_ATOMIC_COUNTER_CXX11
atomic_counter_t (const atomic_counter_t&); atomic_counter_t (const atomic_counter_t &);
const atomic_counter_t& operator = (const atomic_counter_t&); const atomic_counter_t &operator= (const atomic_counter_t &);
#endif #endif
#if defined (__GNUC__) || defined ( __INTEL_COMPILER) || \ #if defined(__GNUC__) || defined(__INTEL_COMPILER) \
(defined (__SUNPRO_C) && __SUNPRO_C >= 0x590) || \ || (defined(__SUNPRO_C) && __SUNPRO_C >= 0x590) \
(defined (__SUNPRO_CC) && __SUNPRO_CC >= 0x590) || (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x590)
} __attribute__ ((aligned (sizeof (void *)))); } __attribute__ ((aligned (sizeof (void *))));
#else #else
}; };
#endif #endif
} }
// Remove macros local to this file. // Remove macros local to this file.

View File

@@ -44,7 +44,8 @@
#define ZMQ_ATOMIC_PTR_TILE #define ZMQ_ATOMIC_PTR_TILE
#elif defined ZMQ_HAVE_WINDOWS #elif defined ZMQ_HAVE_WINDOWS
#define ZMQ_ATOMIC_PTR_WINDOWS #define ZMQ_ATOMIC_PTR_WINDOWS
#elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD || defined ZMQ_HAVE_GNU) #elif (defined ZMQ_HAVE_SOLARIS || defined ZMQ_HAVE_NETBSD \
|| defined ZMQ_HAVE_GNU)
#define ZMQ_ATOMIC_PTR_ATOMIC_H #define ZMQ_ATOMIC_PTR_ATOMIC_H
#else #else
#define ZMQ_ATOMIC_PTR_MUTEX #define ZMQ_ATOMIC_PTR_MUTEX
@@ -64,154 +65,138 @@
namespace zmq namespace zmq
{ {
// This class encapsulates several atomic operations on pointers.
// This class encapsulates several atomic operations on pointers. template <typename T> class atomic_ptr_t
{
public:
// Initialise atomic pointer
inline atomic_ptr_t () { ptr = NULL; }
template <typename T> class atomic_ptr_t // Destroy atomic pointer
inline ~atomic_ptr_t () {}
// Set value of atomic pointer in a non-threadsafe way
// Use this function only when you are sure that at most one
// thread is accessing the pointer at the moment.
inline void set (T *ptr_) { this->ptr = ptr_; }
// Perform atomic 'exchange pointers' operation. Pointer is set
// to the 'val' value. Old value is returned.
inline T *xchg (T *val_)
{ {
public:
// Initialise atomic pointer
inline atomic_ptr_t ()
{
ptr = NULL;
}
// Destroy atomic pointer
inline ~atomic_ptr_t ()
{
}
// Set value of atomic pointer in a non-threadsafe way
// Use this function only when you are sure that at most one
// thread is accessing the pointer at the moment.
inline void set (T *ptr_)
{
this->ptr = ptr_;
}
// Perform atomic 'exchange pointers' operation. Pointer is set
// to the 'val' value. Old value is returned.
inline T *xchg (T *val_)
{
#if defined ZMQ_ATOMIC_PTR_WINDOWS #if defined ZMQ_ATOMIC_PTR_WINDOWS
return (T*) InterlockedExchangePointer ((PVOID*) &ptr, val_); return (T *) InterlockedExchangePointer ((PVOID *) &ptr, val_);
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC #elif defined ZMQ_ATOMIC_PTR_INTRINSIC
return (T*) __atomic_exchange_n (&ptr, val_, __ATOMIC_ACQ_REL); return (T *) __atomic_exchange_n (&ptr, val_, __ATOMIC_ACQ_REL);
#elif defined ZMQ_ATOMIC_PTR_CXX11 #elif defined ZMQ_ATOMIC_PTR_CXX11
return ptr.exchange(val_, std::memory_order_acq_rel); return ptr.exchange (val_, std::memory_order_acq_rel);
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H #elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
return (T*) atomic_swap_ptr (&ptr, val_); return (T *) atomic_swap_ptr (&ptr, val_);
#elif defined ZMQ_ATOMIC_PTR_TILE #elif defined ZMQ_ATOMIC_PTR_TILE
return (T*) arch_atomic_exchange (&ptr, val_); return (T *) arch_atomic_exchange (&ptr, val_);
#elif defined ZMQ_ATOMIC_PTR_X86 #elif defined ZMQ_ATOMIC_PTR_X86
T *old; T *old;
__asm__ volatile ( __asm__ volatile("lock; xchg %0, %2"
"lock; xchg %0, %2" : "=r"(old), "=m"(ptr)
: "=r" (old), "=m" (ptr) : "m"(ptr), "0"(val_));
: "m" (ptr), "0" (val_)); return old;
return old;
#elif defined ZMQ_ATOMIC_PTR_ARM #elif defined ZMQ_ATOMIC_PTR_ARM
T* old; T *old;
unsigned int flag; unsigned int flag;
__asm__ volatile ( __asm__ volatile(" dmb sy\n\t"
" dmb sy\n\t" "1: ldrex %1, [%3]\n\t"
"1: ldrex %1, [%3]\n\t" " strex %0, %4, [%3]\n\t"
" strex %0, %4, [%3]\n\t" " teq %0, #0\n\t"
" teq %0, #0\n\t" " bne 1b\n\t"
" bne 1b\n\t" " dmb sy\n\t"
" dmb sy\n\t" : "=&r"(flag), "=&r"(old), "+Qo"(ptr)
: "=&r"(flag), "=&r"(old), "+Qo"(ptr) : "r"(&ptr), "r"(val_)
: "r"(&ptr), "r"(val_) : "cc");
: "cc"); return old;
return old;
#elif defined ZMQ_ATOMIC_PTR_MUTEX #elif defined ZMQ_ATOMIC_PTR_MUTEX
sync.lock (); sync.lock ();
T *old = (T*) ptr; T *old = (T *) ptr;
ptr = val_;
sync.unlock ();
return old;
#else
#error atomic_ptr is not implemented for this platform
#endif
}
// Perform atomic 'compare and swap' operation on the pointer.
// The pointer is compared to 'cmp' argument and if they are
// equal, its value is set to 'val'. Old value of the pointer
// is returned.
inline T *cas (T *cmp_, T *val_)
{
#if defined ZMQ_ATOMIC_PTR_WINDOWS
return (T *) InterlockedCompareExchangePointer ((volatile PVOID *) &ptr,
val_, cmp_);
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC
T *old = cmp_;
__atomic_compare_exchange_n (&ptr, (volatile T **) &old, val_, false,
__ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
return old;
#elif defined ZMQ_ATOMIC_PTR_CXX11
ptr.compare_exchange_strong (cmp_, val_, std::memory_order_acq_rel);
return cmp_;
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
return (T *) atomic_cas_ptr (&ptr, cmp_, val_);
#elif defined ZMQ_ATOMIC_PTR_TILE
return (T *) arch_atomic_val_compare_and_exchange (&ptr, cmp_, val_);
#elif defined ZMQ_ATOMIC_PTR_X86
T *old;
__asm__ volatile("lock; cmpxchg %2, %3"
: "=a"(old), "=m"(ptr)
: "r"(val_), "m"(ptr), "0"(cmp_)
: "cc");
return old;
#elif defined ZMQ_ATOMIC_PTR_ARM
T *old;
unsigned int flag;
__asm__ volatile(" dmb sy\n\t"
"1: ldrex %1, [%3]\n\t"
" mov %0, #0\n\t"
" teq %1, %4\n\t"
" it eq\n\t"
" strexeq %0, %5, [%3]\n\t"
" teq %0, #0\n\t"
" bne 1b\n\t"
" dmb sy\n\t"
: "=&r"(flag), "=&r"(old), "+Qo"(ptr)
: "r"(&ptr), "r"(cmp_), "r"(val_)
: "cc");
return old;
#elif defined ZMQ_ATOMIC_PTR_MUTEX
sync.lock ();
T *old = (T *) ptr;
if (ptr == cmp_)
ptr = val_; ptr = val_;
sync.unlock (); sync.unlock ();
return old; return old;
#else #else
#error atomic_ptr is not implemented for this platform #error atomic_ptr is not implemented for this platform
#endif #endif
} }
// Perform atomic 'compare and swap' operation on the pointer.
// The pointer is compared to 'cmp' argument and if they are
// equal, its value is set to 'val'. Old value of the pointer
// is returned.
inline T *cas (T *cmp_, T *val_)
{
#if defined ZMQ_ATOMIC_PTR_WINDOWS
return (T*) InterlockedCompareExchangePointer (
(volatile PVOID*) &ptr, val_, cmp_);
#elif defined ZMQ_ATOMIC_PTR_INTRINSIC
T *old = cmp_;
__atomic_compare_exchange_n (&ptr, (volatile T**) &old, val_, false,
__ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
return old;
#elif defined ZMQ_ATOMIC_PTR_CXX11
ptr.compare_exchange_strong(cmp_, val_, std::memory_order_acq_rel);
return cmp_;
#elif defined ZMQ_ATOMIC_PTR_ATOMIC_H
return (T*) atomic_cas_ptr (&ptr, cmp_, val_);
#elif defined ZMQ_ATOMIC_PTR_TILE
return (T*) arch_atomic_val_compare_and_exchange (&ptr, cmp_, val_);
#elif defined ZMQ_ATOMIC_PTR_X86
T *old;
__asm__ volatile (
"lock; cmpxchg %2, %3"
: "=a" (old), "=m" (ptr)
: "r" (val_), "m" (ptr), "0" (cmp_)
: "cc");
return old;
#elif defined ZMQ_ATOMIC_PTR_ARM
T *old;
unsigned int flag;
__asm__ volatile (
" dmb sy\n\t"
"1: ldrex %1, [%3]\n\t"
" mov %0, #0\n\t"
" teq %1, %4\n\t"
" it eq\n\t"
" strexeq %0, %5, [%3]\n\t"
" teq %0, #0\n\t"
" bne 1b\n\t"
" dmb sy\n\t"
: "=&r"(flag), "=&r"(old), "+Qo"(ptr)
: "r"(&ptr), "r"(cmp_), "r"(val_)
: "cc");
return old;
#elif defined ZMQ_ATOMIC_PTR_MUTEX
sync.lock ();
T *old = (T*) ptr;
if (ptr == cmp_)
ptr = val_;
sync.unlock ();
return old;
#else
#error atomic_ptr is not implemented for this platform
#endif
}
private:
private:
#if defined ZMQ_ATOMIC_PTR_CXX11 #if defined ZMQ_ATOMIC_PTR_CXX11
std::atomic<T*> ptr; std::atomic<T *> ptr;
#else #else
volatile T *ptr; volatile T *ptr;
#endif #endif
#if defined ZMQ_ATOMIC_PTR_MUTEX #if defined ZMQ_ATOMIC_PTR_MUTEX
mutex_t sync; mutex_t sync;
#endif #endif
#if ! defined ZMQ_ATOMIC_PTR_CXX11 #if !defined ZMQ_ATOMIC_PTR_CXX11
atomic_ptr_t (const atomic_ptr_t&); atomic_ptr_t (const atomic_ptr_t &);
const atomic_ptr_t &operator = (const atomic_ptr_t&); const atomic_ptr_t &operator= (const atomic_ptr_t &);
#endif #endif
}; };
} }
// Remove macros local to this file. // Remove macros local to this file.

View File

@@ -36,7 +36,7 @@
#if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER > 1700 #if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER > 1700
#define ZMQ_HAS_MOVE_SEMANTICS #define ZMQ_HAS_MOVE_SEMANTICS
#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) emplace (k,v) #define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) emplace (k, v)
#define ZMQ_PUSH_OR_EMPLACE_BACK emplace_back #define ZMQ_PUSH_OR_EMPLACE_BACK emplace_back
#define ZMQ_MOVE(x) std::move (x) #define ZMQ_MOVE(x) std::move (x)
#else #else
@@ -47,140 +47,140 @@
namespace zmq namespace zmq
{ {
struct reference_tag_t {}; struct reference_tag_t
{
};
// Object to hold dynamically allocated opaque binary data. // Object to hold dynamically allocated opaque binary data.
// On modern compilers, it will be movable but not copyable. Copies // On modern compilers, it will be movable but not copyable. Copies
// must be explicitly created by set_deep_copy. // must be explicitly created by set_deep_copy.
// On older compilers, it is copyable for syntactical reasons. // On older compilers, it is copyable for syntactical reasons.
struct blob_t struct blob_t
{
// Creates an empty blob_t.
blob_t () : data_ (0), size_ (0), owned_ (true) {}
// Creates a blob_t of a given size, with uninitialized content.
blob_t (const size_t size) :
data_ ((unsigned char *) malloc (size)),
size_ (size),
owned_ (true)
{ {
// Creates an empty blob_t. }
blob_t () : data_ (0), size_ (0), owned_ (true) {}
// Creates a blob_t of a given size, with uninitialized content. // Creates a blob_t of a given size, an initializes content by copying
blob_t (const size_t size) // from another buffer.
: data_ ((unsigned char*)malloc (size)) blob_t (const unsigned char *const data, const size_t size) :
, size_ (size) data_ ((unsigned char *) malloc (size)),
, owned_ (true) size_ (size),
{ owned_ (true)
{
memcpy (data_, data, size_);
}
// Creates a blob_t for temporary use that only references a
// pre-allocated block of data.
// Use with caution and ensure that the blob_t will not outlive
// the referenced data.
blob_t (unsigned char *const data, const size_t size, reference_tag_t) :
data_ (data),
size_ (size),
owned_ (false)
{
}
// Returns the size of the blob_t.
size_t size () const { return size_; }
// Returns a pointer to the data of the blob_t.
const unsigned char *data () const { return data_; }
// Returns a pointer to the data of the blob_t.
unsigned char *data () { return data_; }
// Defines an order relationship on blob_t.
bool operator< (blob_t const &other) const
{
int cmpres = memcmp (data_, other.data_, std::min (size_, other.size_));
return cmpres < 0 || (cmpres == 0 && size_ < other.size_);
}
// Sets a blob_t to a deep copy of another blob_t.
void set_deep_copy (blob_t const &other)
{
clear ();
data_ = (unsigned char *) malloc (other.size_);
size_ = other.size_;
owned_ = true;
memcpy (data_, other.data_, size_);
}
// Sets a blob_t to a copy of a given buffer.
void set (const unsigned char *const data, const size_t size)
{
clear ();
data_ = (unsigned char *) malloc (size);
size_ = size;
owned_ = true;
memcpy (data_, data, size_);
}
// Empties a blob_t.
void clear ()
{
if (owned_) {
free (data_);
} }
data_ = 0;
size_ = 0;
}
// Creates a blob_t of a given size, an initializes content by copying ~blob_t ()
// from another buffer. {
blob_t(const unsigned char * const data, const size_t size) if (owned_) {
: data_ ((unsigned char*)malloc (size)) free (data_);
, size_ (size)
, owned_ (true)
{
memcpy(data_, data, size_);
}
// Creates a blob_t for temporary use that only references a
// pre-allocated block of data.
// Use with caution and ensure that the blob_t will not outlive
// the referenced data.
blob_t (unsigned char * const data, const size_t size, reference_tag_t)
: data_ (data)
, size_ (size)
, owned_ (false)
{
}
// Returns the size of the blob_t.
size_t size () const { return size_; }
// Returns a pointer to the data of the blob_t.
const unsigned char *data() const {
return data_;
}
// Returns a pointer to the data of the blob_t.
unsigned char *data() {
return data_;
}
// Defines an order relationship on blob_t.
bool operator< (blob_t const &other) const {
int cmpres = memcmp (data_, other.data_, std::min (size_, other.size_));
return cmpres < 0 || (cmpres == 0 && size_ < other.size_);
}
// Sets a blob_t to a deep copy of another blob_t.
void set_deep_copy (blob_t const &other)
{
clear ();
data_ = (unsigned char*)malloc (other.size_);
size_ = other.size_;
owned_ = true;
memcpy (data_, other.data_, size_);
}
// Sets a blob_t to a copy of a given buffer.
void set (const unsigned char * const data, const size_t size)
{
clear ();
data_ = (unsigned char*)malloc (size);
size_ = size;
owned_ = true;
memcpy (data_, data, size_);
}
// Empties a blob_t.
void clear () {
if (owned_) { free (data_); }
data_ = 0; size_ = 0;
}
~blob_t () {
if (owned_) { free (data_); }
} }
}
#ifdef ZMQ_HAS_MOVE_SEMANTICS #ifdef ZMQ_HAS_MOVE_SEMANTICS
blob_t (const blob_t &) = delete; blob_t (const blob_t &) = delete;
blob_t &operator= (const blob_t &) = delete; blob_t &operator= (const blob_t &) = delete;
blob_t (blob_t&& other) blob_t (blob_t &&other) :
: data_ (other.data_) data_ (other.data_),
, size_ (other.size_) size_ (other.size_),
, owned_ (other.owned_) owned_ (other.owned_)
{ {
other.owned_ = false;
}
blob_t &operator= (blob_t &&other)
{
if (this != &other) {
clear ();
data_ = other.data_;
size_ = other.size_;
owned_ = other.owned_;
other.owned_ = false; other.owned_ = false;
} }
blob_t &operator= (blob_t&& other) { return *this;
if (this != &other) }
{
clear ();
data_ = other.data_;
size_ = other.size_;
owned_ = other.owned_;
other.owned_ = false;
}
return *this;
}
#else #else
blob_t (const blob_t &other) blob_t (const blob_t &other) : owned_ (false) { set_deep_copy (other); }
: owned_(false) blob_t &operator= (const blob_t &other)
{ {
if (this != &other) {
clear ();
set_deep_copy (other); set_deep_copy (other);
} }
blob_t &operator= (const blob_t &other) { return *this;
if (this != &other) }
{
clear ();
set_deep_copy (other);
}
return *this;
}
#endif #endif
private: private:
unsigned char *data_; unsigned char *data_;
size_t size_; size_t size_;
bool owned_; bool owned_;
}; };
} }
#endif #endif

View File

@@ -69,7 +69,6 @@ int zmq::client_t::xrecv (msg_t *msg_)
// Drop any messages with more flag // Drop any messages with more flag
while (rc == 0 && msg_->flags () & msg_t::more) { while (rc == 0 && msg_->flags () & msg_t::more) {
// drop all frames of the current multi-frame message // drop all frames of the current multi-frame message
rc = fq.recvpipe (msg_, NULL); rc = fq.recvpipe (msg_, NULL);

View File

@@ -37,45 +37,39 @@
namespace zmq namespace zmq
{ {
class ctx_t;
class msg_t;
class pipe_t;
class io_thread_t;
class socket_base_t;
class ctx_t; class client_t : public socket_base_t
class msg_t; {
class pipe_t; public:
class io_thread_t; client_t (zmq::ctx_t *parent_, uint32_t tid_, int sid);
class socket_base_t; ~client_t ();
class client_t : protected:
public socket_base_t // Overrides of functions from socket_base_t.
{ void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
public: int xsend (zmq::msg_t *msg_);
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
bool xhas_out ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
client_t (zmq::ctx_t *parent_, uint32_t tid_, int sid); private:
~client_t (); // Messages are fair-queued from inbound pipes. And load-balanced to
// the outbound pipes.
protected: fq_t fq;
lb_t lb;
// Overrides of functions from socket_base_t.
void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
int xsend (zmq::msg_t *msg_);
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
bool xhas_out ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
private:
// Messages are fair-queued from inbound pipes. And load-balanced to
// the outbound pipes.
fq_t fq;
lb_t lb;
client_t (const client_t &);
const client_t &operator = (const client_t&);
};
client_t (const client_t &);
const client_t &operator= (const client_t &);
};
} }
#endif #endif

View File

@@ -72,58 +72,61 @@ int alt_clock_gettime (int clock_id, timespec *ts)
#endif #endif
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
typedef ULONGLONG (*f_compatible_get_tick_count64)(); typedef ULONGLONG (*f_compatible_get_tick_count64) ();
static zmq::mutex_t compatible_get_tick_count64_mutex; static zmq::mutex_t compatible_get_tick_count64_mutex;
ULONGLONG compatible_get_tick_count64() ULONGLONG compatible_get_tick_count64 ()
{ {
#ifdef ZMQ_HAVE_WINDOWS_UWP #ifdef ZMQ_HAVE_WINDOWS_UWP
const ULONGLONG result = ::GetTickCount64(); const ULONGLONG result = ::GetTickCount64 ();
return result; return result;
#else #else
zmq::scoped_lock_t locker(compatible_get_tick_count64_mutex); zmq::scoped_lock_t locker (compatible_get_tick_count64_mutex);
static DWORD s_wrap = 0; static DWORD s_wrap = 0;
static DWORD s_last_tick = 0; static DWORD s_last_tick = 0;
const DWORD current_tick = ::GetTickCount(); const DWORD current_tick = ::GetTickCount ();
if (current_tick < s_last_tick) if (current_tick < s_last_tick)
++s_wrap; ++s_wrap;
s_last_tick = current_tick; s_last_tick = current_tick;
const ULONGLONG result = (static_cast<ULONGLONG>(s_wrap) << 32) + static_cast<ULONGLONG>(current_tick); const ULONGLONG result = (static_cast<ULONGLONG> (s_wrap) << 32)
+ static_cast<ULONGLONG> (current_tick);
return result; return result;
#endif #endif
} }
f_compatible_get_tick_count64 init_compatible_get_tick_count64() f_compatible_get_tick_count64 init_compatible_get_tick_count64 ()
{ {
f_compatible_get_tick_count64 func = NULL; f_compatible_get_tick_count64 func = NULL;
#if !defined ZMQ_HAVE_WINDOWS_UWP #if !defined ZMQ_HAVE_WINDOWS_UWP
HMODULE module = ::LoadLibraryA("Kernel32.dll"); HMODULE module = ::LoadLibraryA ("Kernel32.dll");
if (module != NULL) if (module != NULL)
func = reinterpret_cast<f_compatible_get_tick_count64>(::GetProcAddress(module, "GetTickCount64")); func = reinterpret_cast<f_compatible_get_tick_count64> (
::GetProcAddress (module, "GetTickCount64"));
#endif #endif
if (func == NULL) if (func == NULL)
func = compatible_get_tick_count64; func = compatible_get_tick_count64;
#if !defined ZMQ_HAVE_WINDOWS_UWP #if !defined ZMQ_HAVE_WINDOWS_UWP
::FreeLibrary(module); ::FreeLibrary (module);
#endif #endif
return func; return func;
} }
static f_compatible_get_tick_count64 my_get_tick_count64 = init_compatible_get_tick_count64(); static f_compatible_get_tick_count64 my_get_tick_count64 =
init_compatible_get_tick_count64 ();
#endif #endif
zmq::clock_t::clock_t () : zmq::clock_t::clock_t () :
last_tsc (rdtsc ()), last_tsc (rdtsc ()),
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
last_time (static_cast<uint64_t>((*my_get_tick_count64)())) last_time (static_cast<uint64_t> ((*my_get_tick_count64) ()))
#else #else
last_time (now_us () / 1000) last_time (now_us () / 1000)
#endif #endif
@@ -156,7 +159,8 @@ uint64_t zmq::clock_t::now_us ()
// Use POSIX clock_gettime function to get precise monotonic time. // Use POSIX clock_gettime function to get precise monotonic time.
struct timespec tv; struct timespec tv;
#if defined ZMQ_HAVE_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 // less than macOS 10.12 #if defined ZMQ_HAVE_OSX \
&& __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 // less than macOS 10.12
int rc = alt_clock_gettime (SYSTEM_CLOCK, &tv); int rc = alt_clock_gettime (SYSTEM_CLOCK, &tv);
#else #else
int rc = clock_gettime (CLOCK_MONOTONIC, &tv); int rc = clock_gettime (CLOCK_MONOTONIC, &tv);
@@ -164,7 +168,7 @@ uint64_t zmq::clock_t::now_us ()
// Fix case where system has clock_gettime but CLOCK_MONOTONIC is not supported. // Fix case where system has clock_gettime but CLOCK_MONOTONIC is not supported.
// This should be a configuration check, but I looked into it and writing an // This should be a configuration check, but I looked into it and writing an
// AC_FUNC_CLOCK_MONOTONIC seems beyond my powers. // AC_FUNC_CLOCK_MONOTONIC seems beyond my powers.
if( rc != 0) { if (rc != 0) {
// Use POSIX gettimeofday function to get precise time. // Use POSIX gettimeofday function to get precise time.
struct timeval tv; struct timeval tv;
int rc = gettimeofday (&tv, NULL); int rc = gettimeofday (&tv, NULL);
@@ -193,14 +197,13 @@ uint64_t zmq::clock_t::now_ms ()
uint64_t tsc = rdtsc (); uint64_t tsc = rdtsc ();
// If TSC is not supported, get precise time and chop off the microseconds. // If TSC is not supported, get precise time and chop off the microseconds.
if (!tsc) if (!tsc) {
{
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
// Under Windows, now_us is not so reliable since QueryPerformanceCounter // Under Windows, now_us is not so reliable since QueryPerformanceCounter
// does not guarantee that it will use a hardware that offers a monotonic timer. // does not guarantee that it will use a hardware that offers a monotonic timer.
// So, lets use GetTickCount when GetTickCount64 is not available with an workaround // So, lets use GetTickCount when GetTickCount64 is not available with an workaround
// to its 32 bit limitation. // to its 32 bit limitation.
return static_cast<uint64_t>((*my_get_tick_count64)()); return static_cast<uint64_t> ((*my_get_tick_count64) ());
#else #else
return now_us () / 1000; return now_us () / 1000;
#endif #endif
@@ -214,7 +217,7 @@ uint64_t zmq::clock_t::now_ms ()
last_tsc = tsc; last_tsc = tsc;
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
last_time = static_cast<uint64_t>((*my_get_tick_count64)()); last_time = static_cast<uint64_t> ((*my_get_tick_count64) ());
#else #else
last_time = now_us () / 1000; last_time = now_us () / 1000;
#endif #endif
@@ -227,27 +230,29 @@ uint64_t zmq::clock_t::rdtsc ()
return __rdtsc (); return __rdtsc ();
#elif (defined __GNUC__ && (defined __i386__ || defined __x86_64__)) #elif (defined __GNUC__ && (defined __i386__ || defined __x86_64__))
uint32_t low, high; uint32_t low, high;
__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high)); __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
return (uint64_t) high << 32 | low; return (uint64_t) high << 32 | low;
#elif (defined __SUNPRO_CC && (__SUNPRO_CC >= 0x5100) && (defined __i386 || \ #elif (defined __SUNPRO_CC && (__SUNPRO_CC >= 0x5100) \
defined __amd64 || defined __x86_64)) && (defined __i386 || defined __amd64 || defined __x86_64))
union { union
{
uint64_t u64val; uint64_t u64val;
uint32_t u32val [2]; uint32_t u32val[2];
} tsc; } tsc;
asm("rdtsc" : "=a" (tsc.u32val [0]), "=d" (tsc.u32val [1])); asm("rdtsc" : "=a"(tsc.u32val[0]), "=d"(tsc.u32val[1]));
return tsc.u64val; return tsc.u64val;
#elif defined(__s390__) #elif defined(__s390__)
uint64_t tsc; uint64_t tsc;
asm("\tstck\t%0\n" : "=Q" (tsc) : : "cc"); asm("\tstck\t%0\n" : "=Q"(tsc) : : "cc");
return(tsc); return (tsc);
#else #else
struct timespec ts; struct timespec ts;
#if defined ZMQ_HAVE_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 // less than macOS 10.12 #if defined ZMQ_HAVE_OSX \
alt_clock_gettime (SYSTEM_CLOCK, &ts); && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 // less than macOS 10.12
#else alt_clock_gettime (SYSTEM_CLOCK, &ts);
clock_gettime (CLOCK_MONOTONIC, &ts); #else
#endif clock_gettime (CLOCK_MONOTONIC, &ts);
return (uint64_t)(ts.tv_sec) * 1000000000 + ts.tv_nsec; #endif
return (uint64_t) (ts.tv_sec) * 1000000000 + ts.tv_nsec;
#endif #endif
} }

View File

@@ -48,36 +48,32 @@ int alt_clock_gettime (int clock_id, timespec *ts);
namespace zmq namespace zmq
{ {
class clock_t
{
public:
clock_t ();
~clock_t ();
class clock_t // CPU's timestamp counter. Returns 0 if it's not available.
{ static uint64_t rdtsc ();
public:
clock_t (); // High precision timestamp.
~clock_t (); static uint64_t now_us ();
// CPU's timestamp counter. Returns 0 if it's not available. // Low precision timestamp. In tight loops generating it can be
static uint64_t rdtsc (); // 10 to 100 times faster than the high precision timestamp.
uint64_t now_ms ();
// High precision timestamp. private:
static uint64_t now_us (); // TSC timestamp of when last time measurement was made.
uint64_t last_tsc;
// Low precision timestamp. In tight loops generating it can be // Physical time corresponding to the TSC above (in milliseconds).
// 10 to 100 times faster than the high precision timestamp. uint64_t last_time;
uint64_t now_ms ();
private:
// TSC timestamp of when last time measurement was made.
uint64_t last_tsc;
// Physical time corresponding to the TSC above (in milliseconds).
uint64_t last_time;
clock_t (const clock_t&);
const clock_t &operator = (const clock_t&);
};
clock_t (const clock_t &);
const clock_t &operator= (const clock_t &);
};
} }
#endif #endif

View File

@@ -35,153 +35,169 @@
namespace zmq namespace zmq
{ {
class object_t;
class own_t;
struct i_engine;
class pipe_t;
class socket_base_t;
class object_t; // This structure defines the commands that can be sent between threads.
class own_t;
struct i_engine;
class pipe_t;
class socket_base_t;
// This structure defines the commands that can be sent between threads.
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(push) #pragma warning(push)
#pragma warning(disable: 4324) // C4324: alignment padding warnings #pragma warning(disable : 4324) // C4324: alignment padding warnings
__declspec(align(64)) __declspec(align (64))
#endif #endif
struct command_t struct command_t
{
// Object to process the command.
zmq::object_t *destination;
enum type_t
{ {
// Object to process the command. stop,
zmq::object_t *destination; plug,
own,
attach,
bind,
activate_read,
activate_write,
hiccup,
pipe_term,
pipe_term_ack,
pipe_hwm,
term_req,
term,
term_ack,
term_endpoint,
reap,
reaped,
inproc_connected,
done
} type;
enum type_t union args_t
{
// Sent to I/O thread to let it know that it should
// terminate itself.
struct
{ {
stop, } stop;
plug,
own,
attach,
bind,
activate_read,
activate_write,
hiccup,
pipe_term,
pipe_term_ack,
pipe_hwm,
term_req,
term,
term_ack,
term_endpoint,
reap,
reaped,
inproc_connected,
done
} type;
union args_t // Sent to I/O object to make it register with its I/O thread.
struct
{ {
} plug;
// Sent to I/O thread to let it know that it should // Sent to socket to let it know about the newly created object.
// terminate itself. struct
struct { {
} stop; zmq::own_t *object;
} own;
// Sent to I/O object to make it register with its I/O thread. // Attach the engine to the session. If engine is NULL, it informs
struct { // session that the connection have failed.
} plug; struct
{
struct i_engine *engine;
} attach;
// Sent to socket to let it know about the newly created object. // Sent from session to socket to establish pipe(s) between them.
struct { // Caller have used inc_seqnum beforehand sending the command.
zmq::own_t *object; struct
} own; {
zmq::pipe_t *pipe;
} bind;
// Attach the engine to the session. If engine is NULL, it informs // Sent by pipe writer to inform dormant pipe reader that there
// session that the connection have failed. // are messages in the pipe.
struct { struct
struct i_engine *engine; {
} attach; } activate_read;
// Sent from session to socket to establish pipe(s) between them. // Sent by pipe reader to inform pipe writer about how many
// Caller have used inc_seqnum beforehand sending the command. // messages it has read so far.
struct { struct
zmq::pipe_t *pipe; {
} bind; uint64_t msgs_read;
} activate_write;
// Sent by pipe writer to inform dormant pipe reader that there // Sent by pipe reader to writer after creating a new inpipe.
// are messages in the pipe. // The parameter is actually of type pipe_t::upipe_t, however,
struct { // its definition is private so we'll have to do with void*.
} activate_read; struct
{
void *pipe;
} hiccup;
// Sent by pipe reader to inform pipe writer about how many // Sent by pipe reader to pipe writer to ask it to terminate
// messages it has read so far. // its end of the pipe.
struct { struct
uint64_t msgs_read; {
} activate_write; } pipe_term;
// Sent by pipe reader to writer after creating a new inpipe. // Pipe writer acknowledges pipe_term command.
// The parameter is actually of type pipe_t::upipe_t, however, struct
// its definition is private so we'll have to do with void*. {
struct { } pipe_term_ack;
void *pipe;
} hiccup;
// Sent by pipe reader to pipe writer to ask it to terminate // Sent by one of pipe to another part for modify hwm
// its end of the pipe. struct
struct { {
} pipe_term; int inhwm;
int outhwm;
} pipe_hwm;
// Pipe writer acknowledges pipe_term command. // Sent by I/O object ot the socket to request the shutdown of
struct { // the I/O object.
} pipe_term_ack; struct
{
zmq::own_t *object;
} term_req;
// Sent by one of pipe to another part for modify hwm // Sent by socket to I/O object to start its shutdown.
struct { struct
int inhwm; {
int outhwm; int linger;
} pipe_hwm; } term;
// Sent by I/O object ot the socket to request the shutdown of // Sent by I/O object to the socket to acknowledge it has
// the I/O object. // shut down.
struct { struct
zmq::own_t *object; {
} term_req; } term_ack;
// Sent by socket to I/O object to start its shutdown. // Sent by session_base (I/O thread) to socket (application thread)
struct { // to ask to disconnect the endpoint.
int linger; struct
} term; {
std::string *endpoint;
} term_endpoint;
// Sent by I/O object to the socket to acknowledge it has // Transfers the ownership of the closed socket
// shut down. // to the reaper thread.
struct { struct
} term_ack; {
zmq::socket_base_t *socket;
} reap;
// Sent by session_base (I/O thread) to socket (application thread) // Closed socket notifies the reaper that it's already deallocated.
// to ask to disconnect the endpoint. struct
struct { {
std::string *endpoint; } reaped;
} term_endpoint;
// Transfers the ownership of the closed socket // Sent by reaper thread to the term thread when all the sockets
// to the reaper thread. // are successfully deallocated.
struct { struct
zmq::socket_base_t *socket; {
} reap; } done;
// Closed socket notifies the reaper that it's already deallocated. } args;
struct {
} reaped;
// Sent by reaper thread to the term thread when all the sockets
// are successfully deallocated.
struct {
} done;
} args;
#ifdef _MSC_VER #ifdef _MSC_VER
}; };
#pragma warning(pop) #pragma warning(pop)
#else #else
} __attribute__((aligned(64))); } __attribute__ ((aligned (64)));
#endif #endif
} }

View File

@@ -58,38 +58,26 @@
namespace zmq namespace zmq
{ {
class condition_variable_t
{
public:
inline condition_variable_t () { zmq_assert (false); }
class condition_variable_t inline ~condition_variable_t () {}
inline int wait (mutex_t *mutex_, int timeout_)
{ {
public: zmq_assert (false);
inline condition_variable_t () return -1;
{ }
zmq_assert(false);
}
inline ~condition_variable_t () inline void broadcast () { zmq_assert (false); }
{
}
inline int wait (mutex_t* mutex_, int timeout_ )
{
zmq_assert(false);
return -1;
}
inline void broadcast ()
{
zmq_assert(false);
}
private:
// Disable copy construction and assignment.
condition_variable_t (const condition_variable_t&);
void operator = (const condition_variable_t&);
};
private:
// Disable copy construction and assignment.
condition_variable_t (const condition_variable_t &);
void operator= (const condition_variable_t &);
};
} }
#else #else
@@ -103,94 +91,79 @@ namespace zmq
{ {
#if !defined(ZMQ_HAVE_WINDOWS_TARGET_XP) && _WIN32_WINNT >= 0x0600 #if !defined(ZMQ_HAVE_WINDOWS_TARGET_XP) && _WIN32_WINNT >= 0x0600
class condition_variable_t class condition_variable_t
{
public:
inline condition_variable_t () { InitializeConditionVariable (&cv); }
inline ~condition_variable_t () {}
inline int wait (mutex_t *mutex_, int timeout_)
{ {
public: int rc = SleepConditionVariableCS (&cv, mutex_->get_cs (), timeout_);
inline condition_variable_t ()
{
InitializeConditionVariable (&cv);
}
inline ~condition_variable_t () if (rc != 0)
{ return 0;
} rc = GetLastError ();
inline int wait (mutex_t* mutex_, int timeout_ ) if (rc != ERROR_TIMEOUT)
{ win_assert (rc);
int rc = SleepConditionVariableCS(&cv, mutex_->get_cs (), timeout_);
if (rc != 0) errno = EAGAIN;
return 0; return -1;
}
rc = GetLastError(); inline void broadcast () { WakeAllConditionVariable (&cv); }
if (rc != ERROR_TIMEOUT) private:
win_assert(rc); CONDITION_VARIABLE cv;
errno = EAGAIN; // Disable copy construction and assignment.
return -1; condition_variable_t (const condition_variable_t &);
} void operator= (const condition_variable_t &);
};
inline void broadcast ()
{
WakeAllConditionVariable(&cv);
}
private:
CONDITION_VARIABLE cv;
// Disable copy construction and assignment.
condition_variable_t (const condition_variable_t&);
void operator = (const condition_variable_t&);
};
#else #else
class condition_variable_t class condition_variable_t
{ {
public: public:
inline condition_variable_t() inline condition_variable_t () {}
{
} inline ~condition_variable_t () {}
inline ~condition_variable_t() inline int wait (mutex_t *mutex_, int timeout_)
{ {
std::unique_lock<std::mutex> lck (mtx); // lock mtx
mutex_->unlock (); // unlock mutex_
int res = 0;
if (timeout_ == -1) {
cv.wait (
lck); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all()
} else if (cv.wait_for (lck, std::chrono::milliseconds (timeout_))
== std::cv_status::timeout) {
// time expired
errno = EAGAIN;
res = -1;
}
lck.unlock (); // unlock mtx
mutex_->lock (); // lock mutex_
return res;
}
} inline void broadcast ()
{
std::unique_lock<std::mutex> lck (mtx); // lock mtx
cv.notify_all ();
}
inline int wait(mutex_t* mutex_, int timeout_) private:
{ std::condition_variable cv;
std::unique_lock<std::mutex> lck(mtx); // lock mtx std::mutex mtx;
mutex_->unlock(); // unlock mutex_
int res = 0;
if(timeout_ == -1) {
cv.wait(lck); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all()
} else if (cv.wait_for(lck, std::chrono::milliseconds(timeout_)) == std::cv_status::timeout) {
// time expired
errno = EAGAIN;
res = -1;
}
lck.unlock(); // unlock mtx
mutex_->lock(); // lock mutex_
return res;
}
inline void broadcast() // Disable copy construction and assignment.
{ condition_variable_t (const condition_variable_t &);
std::unique_lock<std::mutex> lck(mtx); // lock mtx void operator= (const condition_variable_t &);
cv.notify_all(); };
}
private:
std::condition_variable cv;
std::mutex mtx;
// Disable copy construction and assignment.
condition_variable_t(const condition_variable_t&);
void operator = (const condition_variable_t&);
};
#endif #endif
} }
@@ -203,74 +176,72 @@ namespace zmq
namespace zmq namespace zmq
{ {
class condition_variable_t
class condition_variable_t {
public:
inline condition_variable_t ()
{ {
public: int rc = pthread_cond_init (&cond, NULL);
inline condition_variable_t () posix_assert (rc);
{ }
int rc = pthread_cond_init (&cond, NULL);
posix_assert (rc);
}
inline ~condition_variable_t () inline ~condition_variable_t ()
{ {
int rc = pthread_cond_destroy (&cond); int rc = pthread_cond_destroy (&cond);
posix_assert (rc); posix_assert (rc);
} }
inline int wait (mutex_t* mutex_, int timeout_) inline int wait (mutex_t *mutex_, int timeout_)
{ {
int rc; int rc;
if (timeout_ != -1) { if (timeout_ != -1) {
struct timespec timeout; struct timespec timeout;
#if defined ZMQ_HAVE_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 // less than macOS 10.12 #if defined ZMQ_HAVE_OSX \
alt_clock_gettime(SYSTEM_CLOCK, &timeout); && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 // less than macOS 10.12
alt_clock_gettime (SYSTEM_CLOCK, &timeout);
#else #else
clock_gettime(CLOCK_MONOTONIC, &timeout); clock_gettime (CLOCK_MONOTONIC, &timeout);
#endif #endif
timeout.tv_sec += timeout_ / 1000; timeout.tv_sec += timeout_ / 1000;
timeout.tv_nsec += (timeout_ % 1000) * 1000000; timeout.tv_nsec += (timeout_ % 1000) * 1000000;
if (timeout.tv_nsec > 1000000000) { if (timeout.tv_nsec > 1000000000) {
timeout.tv_sec++; timeout.tv_sec++;
timeout.tv_nsec -= 1000000000; timeout.tv_nsec -= 1000000000;
}
rc = pthread_cond_timedwait (&cond, mutex_->get_mutex (), &timeout);
}
else
rc = pthread_cond_wait(&cond, mutex_->get_mutex());
if (rc == 0)
return 0;
if (rc == ETIMEDOUT){
errno= EAGAIN;
return -1;
} }
posix_assert (rc); rc = pthread_cond_timedwait (&cond, mutex_->get_mutex (), &timeout);
} else
rc = pthread_cond_wait (&cond, mutex_->get_mutex ());
if (rc == 0)
return 0;
if (rc == ETIMEDOUT) {
errno = EAGAIN;
return -1; return -1;
} }
inline void broadcast () posix_assert (rc);
{ return -1;
int rc = pthread_cond_broadcast (&cond); }
posix_assert (rc);
}
private: inline void broadcast ()
{
int rc = pthread_cond_broadcast (&cond);
posix_assert (rc);
}
pthread_cond_t cond; private:
pthread_cond_t cond;
// Disable copy construction and assignment. // Disable copy construction and assignment.
condition_variable_t (const condition_variable_t&); condition_variable_t (const condition_variable_t &);
const condition_variable_t &operator = (const condition_variable_t&); const condition_variable_t &operator= (const condition_variable_t &);
}; };
} }
#endif #endif

View File

@@ -32,67 +32,65 @@
namespace zmq namespace zmq
{ {
// Compile-time settings.
// Compile-time settings. enum
{
// Number of new messages in message pipe needed to trigger new memory
// allocation. Setting this parameter to 256 decreases the impact of
// memory allocation by approximately 99.6%
message_pipe_granularity = 256,
enum // Commands in pipe per allocation event.
{ command_pipe_granularity = 16,
// Number of new messages in message pipe needed to trigger new memory
// allocation. Setting this parameter to 256 decreases the impact of
// memory allocation by approximately 99.6%
message_pipe_granularity = 256,
// Commands in pipe per allocation event. // Determines how often does socket poll for new commands when it
command_pipe_granularity = 16, // still has unprocessed messages to handle. Thus, if it is set to 100,
// socket will process 100 inbound messages before doing the poll.
// If there are no unprocessed messages available, poll is done
// immediately. Decreasing the value trades overall latency for more
// real-time behaviour (less latency peaks).
inbound_poll_rate = 100,
// Determines how often does socket poll for new commands when it // Maximal batching size for engines with receiving functionality.
// still has unprocessed messages to handle. Thus, if it is set to 100, // So, if there are 10 messages that fit into the batch size, all of
// socket will process 100 inbound messages before doing the poll. // them may be read by a single 'recv' system call, thus avoiding
// If there are no unprocessed messages available, poll is done // unnecessary network stack traversals.
// immediately. Decreasing the value trades overall latency for more in_batch_size = 8192,
// real-time behaviour (less latency peaks).
inbound_poll_rate = 100,
// Maximal batching size for engines with receiving functionality. // Maximal batching size for engines with sending functionality.
// So, if there are 10 messages that fit into the batch size, all of // So, if there are 10 messages that fit into the batch size, all of
// them may be read by a single 'recv' system call, thus avoiding // them may be written by a single 'send' system call, thus avoiding
// unnecessary network stack traversals. // unnecessary network stack traversals.
in_batch_size = 8192, out_batch_size = 8192,
// Maximal batching size for engines with sending functionality. // Maximal delta between high and low watermark.
// So, if there are 10 messages that fit into the batch size, all of max_wm_delta = 1024,
// them may be written by a single 'send' system call, thus avoiding
// unnecessary network stack traversals.
out_batch_size = 8192,
// Maximal delta between high and low watermark. // Maximum number of events the I/O thread can process in one go.
max_wm_delta = 1024, max_io_events = 256,
// Maximum number of events the I/O thread can process in one go. // Maximal delay to process command in API thread (in CPU ticks).
max_io_events = 256, // 3,000,000 ticks equals to 1 - 2 milliseconds on current CPUs.
// Note that delay is only applied when there is continuous stream of
// messages to process. If not so, commands are processed immediately.
max_command_delay = 3000000,
// Maximal delay to process command in API thread (in CPU ticks). // Low-precision clock precision in CPU ticks. 1ms. Value of 1000000
// 3,000,000 ticks equals to 1 - 2 milliseconds on current CPUs. // should be OK for CPU frequencies above 1GHz. If should work
// Note that delay is only applied when there is continuous stream of // reasonably well for CPU frequencies above 500MHz. For lower CPU
// messages to process. If not so, commands are processed immediately. // frequencies you may consider lowering this value to get best
max_command_delay = 3000000, // possible latencies.
clock_precision = 1000000,
// Low-precision clock precision in CPU ticks. 1ms. Value of 1000000
// should be OK for CPU frequencies above 1GHz. If should work
// reasonably well for CPU frequencies above 500MHz. For lower CPU
// frequencies you may consider lowering this value to get best
// possible latencies.
clock_precision = 1000000,
// On some OSes the signaler has to be emulated using a TCP
// connection. In such cases following port is used.
// If 0, it lets the OS choose a free port without requiring use of a
// global mutex. The original implementation of a Windows signaler
// socket used port 5905 instead of letting the OS choose a free port.
// https://github.com/zeromq/libzmq/issues/1542
signaler_port = 0
};
// On some OSes the signaler has to be emulated using a TCP
// connection. In such cases following port is used.
// If 0, it lets the OS choose a free port without requiring use of a
// global mutex. The original implementation of a Windows signaler
// socket used port 5905 instead of letting the OS choose a free port.
// https://github.com/zeromq/libzmq/issues/1542
signaler_port = 0
};
} }
#endif #endif

View File

@@ -53,11 +53,12 @@
#endif #endif
#define ZMQ_CTX_TAG_VALUE_GOOD 0xabadcafe #define ZMQ_CTX_TAG_VALUE_GOOD 0xabadcafe
#define ZMQ_CTX_TAG_VALUE_BAD 0xdeadbeef #define ZMQ_CTX_TAG_VALUE_BAD 0xdeadbeef
int clipped_maxsocket (int max_requested) int clipped_maxsocket (int max_requested)
{ {
if (max_requested >= zmq::poller_t::max_fds () && zmq::poller_t::max_fds () != -1) if (max_requested >= zmq::poller_t::max_fds ()
&& zmq::poller_t::max_fds () != -1)
// -1 because we need room for the reaper mailbox. // -1 because we need room for the reaper mailbox.
max_requested = zmq::poller_t::max_fds () - 1; max_requested = zmq::poller_t::max_fds () - 1;
@@ -80,7 +81,7 @@ zmq::ctx_t::ctx_t () :
thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT) thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT)
{ {
#ifdef HAVE_FORK #ifdef HAVE_FORK
pid = getpid(); pid = getpid ();
#endif #endif
#ifdef ZMQ_HAVE_VMCI #ifdef ZMQ_HAVE_VMCI
vmci_fd = -1; vmci_fd = -1;
@@ -104,16 +105,16 @@ zmq::ctx_t::~ctx_t ()
// Ask I/O threads to terminate. If stop signal wasn't sent to I/O // Ask I/O threads to terminate. If stop signal wasn't sent to I/O
// thread subsequent invocation of destructor would hang-up. // thread subsequent invocation of destructor would hang-up.
for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) { for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) {
io_threads [i]->stop (); io_threads[i]->stop ();
} }
// Wait till I/O threads actually terminate. // Wait till I/O threads actually terminate.
for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) { for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) {
LIBZMQ_DELETE(io_threads [i]); LIBZMQ_DELETE (io_threads[i]);
} }
// Deallocate the reaper thread object. // Deallocate the reaper thread object.
LIBZMQ_DELETE(reaper); LIBZMQ_DELETE (reaper);
// Deallocate the array of mailboxes. No special work is // Deallocate the array of mailboxes. No special work is
// needed as mailboxes themselves were deallocated with their // needed as mailboxes themselves were deallocated with their
@@ -134,14 +135,15 @@ bool zmq::ctx_t::valid () const
int zmq::ctx_t::terminate () int zmq::ctx_t::terminate ()
{ {
slot_sync.lock(); slot_sync.lock ();
bool saveTerminating = terminating; bool saveTerminating = terminating;
terminating = false; terminating = false;
// Connect up any pending inproc connections, otherwise we will hang // Connect up any pending inproc connections, otherwise we will hang
pending_connections_t copy = pending_connections; pending_connections_t copy = pending_connections;
for (pending_connections_t::iterator p = copy.begin (); p != copy.end (); ++p) { for (pending_connections_t::iterator p = copy.begin (); p != copy.end ();
++p) {
zmq::socket_base_t *s = create_socket (ZMQ_PAIR); zmq::socket_base_t *s = create_socket (ZMQ_PAIR);
// create_socket might fail eg: out of memory/sockets limit reached // create_socket might fail eg: out of memory/sockets limit reached
zmq_assert (s); zmq_assert (s);
@@ -156,7 +158,7 @@ int zmq::ctx_t::terminate ()
// we are a forked child process. Close all file descriptors // we are a forked child process. Close all file descriptors
// inherited from the parent. // inherited from the parent.
for (sockets_t::size_type i = 0; i != sockets.size (); i++) for (sockets_t::size_type i = 0; i != sockets.size (); i++)
sockets [i]->get_mailbox ()->forked (); sockets[i]->get_mailbox ()->forked ();
term_mailbox.forked (); term_mailbox.forked ();
} }
@@ -173,11 +175,11 @@ int zmq::ctx_t::terminate ()
// can be interrupted. If there are no sockets we can ask reaper // can be interrupted. If there are no sockets we can ask reaper
// thread to stop. // thread to stop.
for (sockets_t::size_type i = 0; i != sockets.size (); i++) for (sockets_t::size_type i = 0; i != sockets.size (); i++)
sockets [i]->stop (); sockets[i]->stop ();
if (sockets.empty ()) if (sockets.empty ())
reaper->stop (); reaper->stop ();
} }
slot_sync.unlock(); slot_sync.unlock ();
// Wait till reaper thread closes all the sockets. // Wait till reaper thread closes all the sockets.
command_t cmd; command_t cmd;
@@ -209,7 +211,7 @@ int zmq::ctx_t::terminate ()
int zmq::ctx_t::shutdown () int zmq::ctx_t::shutdown ()
{ {
scoped_lock_t locker(slot_sync); scoped_lock_t locker (slot_sync);
if (!starting && !terminating) { if (!starting && !terminating) {
terminating = true; terminating = true;
@@ -218,7 +220,7 @@ int zmq::ctx_t::shutdown ()
// can be interrupted. If there are no sockets we can ask reaper // can be interrupted. If there are no sockets we can ask reaper
// thread to stop. // thread to stop.
for (sockets_t::size_type i = 0; i != sockets.size (); i++) for (sockets_t::size_type i = 0; i != sockets.size (); i++)
sockets [i]->stop (); sockets[i]->stop ();
if (sockets.empty ()) if (sockets.empty ())
reaper->stop (); reaper->stop ();
} }
@@ -229,65 +231,46 @@ int zmq::ctx_t::shutdown ()
int zmq::ctx_t::set (int option_, int optval_) int zmq::ctx_t::set (int option_, int optval_)
{ {
int rc = 0; int rc = 0;
if (option_ == ZMQ_MAX_SOCKETS if (option_ == ZMQ_MAX_SOCKETS && optval_ >= 1
&& optval_ >= 1 && optval_ == clipped_maxsocket (optval_)) { && optval_ == clipped_maxsocket (optval_)) {
scoped_lock_t locker(opt_sync); scoped_lock_t locker (opt_sync);
max_sockets = optval_; max_sockets = optval_;
} } else if (option_ == ZMQ_IO_THREADS && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_IO_THREADS && optval_ >= 0) {
scoped_lock_t locker(opt_sync);
io_thread_count = optval_; io_thread_count = optval_;
} } else if (option_ == ZMQ_IPV6 && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_IPV6 && optval_ >= 0) {
scoped_lock_t locker(opt_sync);
ipv6 = (optval_ != 0); ipv6 = (optval_ != 0);
} } else if (option_ == ZMQ_THREAD_PRIORITY && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_THREAD_PRIORITY && optval_ >= 0) {
scoped_lock_t locker(opt_sync);
thread_priority = optval_; thread_priority = optval_;
} } else if (option_ == ZMQ_THREAD_SCHED_POLICY && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_THREAD_SCHED_POLICY && optval_ >= 0) {
scoped_lock_t locker(opt_sync);
thread_sched_policy = optval_; thread_sched_policy = optval_;
} } else if (option_ == ZMQ_THREAD_AFFINITY_CPU_ADD && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_THREAD_AFFINITY_CPU_ADD && optval_ >= 0) { thread_affinity_cpus.insert (optval_);
scoped_lock_t locker(opt_sync); } else if (option_ == ZMQ_THREAD_AFFINITY_CPU_REMOVE && optval_ >= 0) {
thread_affinity_cpus.insert( optval_ ); scoped_lock_t locker (opt_sync);
} std::set<int>::iterator it = thread_affinity_cpus.find (optval_);
else if (it != thread_affinity_cpus.end ()) {
if (option_ == ZMQ_THREAD_AFFINITY_CPU_REMOVE && optval_ >= 0) { thread_affinity_cpus.erase (it);
scoped_lock_t locker(opt_sync);
std::set<int>::iterator it = thread_affinity_cpus.find( optval_ );
if (it != thread_affinity_cpus.end()) {
thread_affinity_cpus.erase( it );
} else { } else {
errno = EINVAL; errno = EINVAL;
rc = -1; rc = -1;
} }
} } else if (option_ == ZMQ_THREAD_NAME_PREFIX && optval_ >= 0) {
else
if (option_ == ZMQ_THREAD_NAME_PREFIX && optval_ >= 0) {
std::ostringstream s; std::ostringstream s;
s << optval_; s << optval_;
scoped_lock_t locker(opt_sync); scoped_lock_t locker (opt_sync);
thread_name_prefix = s.str(); thread_name_prefix = s.str ();
} } else if (option_ == ZMQ_BLOCKY && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_BLOCKY && optval_ >= 0) {
scoped_lock_t locker(opt_sync);
blocky = (optval_ != 0); blocky = (optval_ != 0);
} } else if (option_ == ZMQ_MAX_MSGSZ && optval_ >= 0) {
else scoped_lock_t locker (opt_sync);
if (option_ == ZMQ_MAX_MSGSZ && optval_ >= 0) { max_msgsz = optval_ < INT_MAX ? optval_ : INT_MAX;
scoped_lock_t locker(opt_sync); } else {
max_msgsz = optval_ < INT_MAX? optval_: INT_MAX;
}
else {
errno = EINVAL; errno = EINVAL;
rc = -1; rc = -1;
} }
@@ -299,23 +282,17 @@ int zmq::ctx_t::get (int option_)
int rc = 0; int rc = 0;
if (option_ == ZMQ_MAX_SOCKETS) if (option_ == ZMQ_MAX_SOCKETS)
rc = max_sockets; rc = max_sockets;
else else if (option_ == ZMQ_SOCKET_LIMIT)
if (option_ == ZMQ_SOCKET_LIMIT)
rc = clipped_maxsocket (65535); rc = clipped_maxsocket (65535);
else else if (option_ == ZMQ_IO_THREADS)
if (option_ == ZMQ_IO_THREADS)
rc = io_thread_count; rc = io_thread_count;
else else if (option_ == ZMQ_IPV6)
if (option_ == ZMQ_IPV6)
rc = ipv6; rc = ipv6;
else else if (option_ == ZMQ_BLOCKY)
if (option_ == ZMQ_BLOCKY)
rc = blocky; rc = blocky;
else else if (option_ == ZMQ_MAX_MSGSZ)
if (option_ == ZMQ_MAX_MSGSZ)
rc = max_msgsz; rc = max_msgsz;
else else if (option_ == ZMQ_MSG_T_SIZE)
if (option_ == ZMQ_MSG_T_SIZE)
rc = sizeof (zmq_msg_t); rc = sizeof (zmq_msg_t);
else { else {
errno = EINVAL; errno = EINVAL;
@@ -369,7 +346,7 @@ bool zmq::ctx_t::start ()
goto fail_cleanup_reaper; goto fail_cleanup_reaper;
} }
io_threads.push_back (io_thread); io_threads.push_back (io_thread);
slots [i] = io_thread->get_mailbox (); slots[i] = io_thread->get_mailbox ();
io_thread->start (); io_thread->start ();
} }
@@ -429,19 +406,19 @@ zmq::socket_base_t *zmq::ctx_t::create_socket (int type_)
return NULL; return NULL;
} }
sockets.push_back (s); sockets.push_back (s);
slots [slot] = s->get_mailbox (); slots[slot] = s->get_mailbox ();
return s; return s;
} }
void zmq::ctx_t::destroy_socket (class socket_base_t *socket_) void zmq::ctx_t::destroy_socket (class socket_base_t *socket_)
{ {
scoped_lock_t locker(slot_sync); scoped_lock_t locker (slot_sync);
// Free the associated thread slot. // Free the associated thread slot.
uint32_t tid = socket_->get_tid (); uint32_t tid = socket_->get_tid ();
empty_slots.push_back (tid); empty_slots.push_back (tid);
slots [tid] = NULL; slots[tid] = NULL;
// Remove the socket from the list of sockets. // Remove the socket from the list of sockets.
sockets.erase (socket_); sockets.erase (socket_);
@@ -457,25 +434,28 @@ zmq::object_t *zmq::ctx_t::get_reaper ()
return reaper; return reaper;
} }
void zmq::ctx_t::start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const void zmq::ctx_t::start_thread (thread_t &thread_,
thread_fn *tfn_,
void *arg_) const
{ {
static unsigned int nthreads_started = 0; static unsigned int nthreads_started = 0;
thread_.setSchedulingParameters(thread_priority, thread_sched_policy, thread_affinity_cpus); thread_.setSchedulingParameters (thread_priority, thread_sched_policy,
thread_.start(tfn_, arg_); thread_affinity_cpus);
thread_.start (tfn_, arg_);
#ifndef ZMQ_HAVE_ANDROID #ifndef ZMQ_HAVE_ANDROID
std::ostringstream s; std::ostringstream s;
if (!thread_name_prefix.empty()) if (!thread_name_prefix.empty ())
s << thread_name_prefix << "/"; s << thread_name_prefix << "/";
s << "ZMQbg/" << nthreads_started; s << "ZMQbg/" << nthreads_started;
thread_.setThreadName (s.str().c_str()); thread_.setThreadName (s.str ().c_str ());
#endif #endif
nthreads_started++; nthreads_started++;
} }
void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_) void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_)
{ {
slots [tid_]->send (command_); slots[tid_]->send (command_);
} }
zmq::io_thread_t *zmq::ctx_t::choose_io_thread (uint64_t affinity_) zmq::io_thread_t *zmq::ctx_t::choose_io_thread (uint64_t affinity_)
@@ -488,10 +468,10 @@ zmq::io_thread_t *zmq::ctx_t::choose_io_thread (uint64_t affinity_)
io_thread_t *selected_io_thread = NULL; io_thread_t *selected_io_thread = NULL;
for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) { for (io_threads_t::size_type i = 0; i != io_threads.size (); i++) {
if (!affinity_ || (affinity_ & (uint64_t (1) << i))) { if (!affinity_ || (affinity_ & (uint64_t (1) << i))) {
int load = io_threads [i]->get_load (); int load = io_threads[i]->get_load ();
if (selected_io_thread == NULL || load < min_load) { if (selected_io_thread == NULL || load < min_load) {
min_load = load; min_load = load;
selected_io_thread = io_threads [i]; selected_io_thread = io_threads[i];
} }
} }
} }
@@ -499,12 +479,12 @@ zmq::io_thread_t *zmq::ctx_t::choose_io_thread (uint64_t affinity_)
} }
int zmq::ctx_t::register_endpoint (const char *addr_, int zmq::ctx_t::register_endpoint (const char *addr_,
const endpoint_t &endpoint_) const endpoint_t &endpoint_)
{ {
scoped_lock_t locker(endpoints_sync); scoped_lock_t locker (endpoints_sync);
const bool inserted = endpoints.ZMQ_MAP_INSERT_OR_EMPLACE (addr_, const bool inserted =
endpoint_).second; endpoints.ZMQ_MAP_INSERT_OR_EMPLACE (addr_, endpoint_).second;
if (!inserted) { if (!inserted) {
errno = EADDRINUSE; errno = EADDRINUSE;
return -1; return -1;
@@ -512,10 +492,10 @@ int zmq::ctx_t::register_endpoint (const char *addr_,
return 0; return 0;
} }
int zmq::ctx_t::unregister_endpoint ( int zmq::ctx_t::unregister_endpoint (const std::string &addr_,
const std::string &addr_, socket_base_t *socket_) socket_base_t *socket_)
{ {
scoped_lock_t locker(endpoints_sync); scoped_lock_t locker (endpoints_sync);
const endpoints_t::iterator it = endpoints.find (addr_); const endpoints_t::iterator it = endpoints.find (addr_);
if (it == endpoints.end () || it->second.socket != socket_) { if (it == endpoints.end () || it->second.socket != socket_) {
@@ -531,7 +511,7 @@ int zmq::ctx_t::unregister_endpoint (
void zmq::ctx_t::unregister_endpoints (socket_base_t *socket_) void zmq::ctx_t::unregister_endpoints (socket_base_t *socket_)
{ {
scoped_lock_t locker(endpoints_sync); scoped_lock_t locker (endpoints_sync);
endpoints_t::iterator it = endpoints.begin (); endpoints_t::iterator it = endpoints.begin ();
while (it != endpoints.end ()) { while (it != endpoints.end ()) {
@@ -547,58 +527,69 @@ void zmq::ctx_t::unregister_endpoints (socket_base_t *socket_)
zmq::endpoint_t zmq::ctx_t::find_endpoint (const char *addr_) zmq::endpoint_t zmq::ctx_t::find_endpoint (const char *addr_)
{ {
scoped_lock_t locker(endpoints_sync); scoped_lock_t locker (endpoints_sync);
endpoints_t::iterator it = endpoints.find (addr_); endpoints_t::iterator it = endpoints.find (addr_);
if (it == endpoints.end ()) { if (it == endpoints.end ()) {
errno = ECONNREFUSED; errno = ECONNREFUSED;
endpoint_t empty = {NULL, options_t()}; endpoint_t empty = {NULL, options_t ()};
return empty; return empty;
} }
endpoint_t endpoint = it->second; endpoint_t endpoint = it->second;
// Increment the command sequence number of the peer so that it won't // Increment the command sequence number of the peer so that it won't
// get deallocated until "bind" command is issued by the caller. // get deallocated until "bind" command is issued by the caller.
// The subsequent 'bind' has to be called with inc_seqnum parameter // The subsequent 'bind' has to be called with inc_seqnum parameter
// set to false, so that the seqnum isn't incremented twice. // set to false, so that the seqnum isn't incremented twice.
endpoint.socket->inc_seqnum (); endpoint.socket->inc_seqnum ();
return endpoint; return endpoint;
} }
void zmq::ctx_t::pend_connection (const std::string &addr_, void zmq::ctx_t::pend_connection (const std::string &addr_,
const endpoint_t &endpoint_, pipe_t **pipes_) const endpoint_t &endpoint_,
pipe_t **pipes_)
{ {
scoped_lock_t locker(endpoints_sync); scoped_lock_t locker (endpoints_sync);
const pending_connection_t pending_connection = {endpoint_, pipes_ [0], pipes_ [1]}; const pending_connection_t pending_connection = {endpoint_, pipes_[0],
pipes_[1]};
endpoints_t::iterator it = endpoints.find (addr_); endpoints_t::iterator it = endpoints.find (addr_);
if (it == endpoints.end ()) { if (it == endpoints.end ()) {
// Still no bind. // Still no bind.
endpoint_.socket->inc_seqnum (); endpoint_.socket->inc_seqnum ();
pending_connections.ZMQ_MAP_INSERT_OR_EMPLACE (addr_, pending_connection); pending_connections.ZMQ_MAP_INSERT_OR_EMPLACE (addr_,
pending_connection);
} else { } else {
// Bind has happened in the mean time, connect directly // Bind has happened in the mean time, connect directly
connect_inproc_sockets(it->second.socket, it->second.options, pending_connection, connect_side); connect_inproc_sockets (it->second.socket, it->second.options,
pending_connection, connect_side);
} }
} }
void zmq::ctx_t::connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_) void zmq::ctx_t::connect_pending (const char *addr_,
zmq::socket_base_t *bind_socket_)
{ {
scoped_lock_t locker(endpoints_sync); scoped_lock_t locker (endpoints_sync);
std::pair<pending_connections_t::iterator, pending_connections_t::iterator> pending = pending_connections.equal_range(addr_); std::pair<pending_connections_t::iterator, pending_connections_t::iterator>
for (pending_connections_t::iterator p = pending.first; p != pending.second; ++p) pending = pending_connections.equal_range (addr_);
connect_inproc_sockets(bind_socket_, endpoints[addr_].options, p->second, bind_side); for (pending_connections_t::iterator p = pending.first; p != pending.second;
++p)
connect_inproc_sockets (bind_socket_, endpoints[addr_].options,
p->second, bind_side);
pending_connections.erase(pending.first, pending.second); pending_connections.erase (pending.first, pending.second);
} }
void zmq::ctx_t::connect_inproc_sockets (zmq::socket_base_t *bind_socket_, void zmq::ctx_t::connect_inproc_sockets (
options_t& bind_options, const pending_connection_t &pending_connection_, side side_) zmq::socket_base_t *bind_socket_,
options_t &bind_options,
const pending_connection_t &pending_connection_,
side side_)
{ {
bind_socket_->inc_seqnum(); bind_socket_->inc_seqnum ();
pending_connection_.bind_pipe->set_tid (bind_socket_->get_tid ()); pending_connection_.bind_pipe->set_tid (bind_socket_->get_tid ());
if (!bind_options.recv_routing_id) { if (!bind_options.recv_routing_id) {
@@ -609,23 +600,29 @@ void zmq::ctx_t::connect_inproc_sockets (zmq::socket_base_t *bind_socket_,
errno_assert (rc == 0); errno_assert (rc == 0);
} }
bool conflate = pending_connection_.endpoint.options.conflate && bool conflate =
(pending_connection_.endpoint.options.type == ZMQ_DEALER || pending_connection_.endpoint.options.conflate
pending_connection_.endpoint.options.type == ZMQ_PULL || && (pending_connection_.endpoint.options.type == ZMQ_DEALER
pending_connection_.endpoint.options.type == ZMQ_PUSH || || pending_connection_.endpoint.options.type == ZMQ_PULL
pending_connection_.endpoint.options.type == ZMQ_PUB || || pending_connection_.endpoint.options.type == ZMQ_PUSH
pending_connection_.endpoint.options.type == ZMQ_SUB); || pending_connection_.endpoint.options.type == ZMQ_PUB
|| pending_connection_.endpoint.options.type == ZMQ_SUB);
if (!conflate) { if (!conflate) {
pending_connection_.connect_pipe->set_hwms_boost(bind_options.sndhwm, bind_options.rcvhwm); pending_connection_.connect_pipe->set_hwms_boost (bind_options.sndhwm,
pending_connection_.bind_pipe->set_hwms_boost(pending_connection_.endpoint.options.sndhwm, pending_connection_.endpoint.options.rcvhwm); bind_options.rcvhwm);
pending_connection_.bind_pipe->set_hwms_boost (
pending_connection_.endpoint.options.sndhwm,
pending_connection_.endpoint.options.rcvhwm);
pending_connection_.connect_pipe->set_hwms(pending_connection_.endpoint.options.rcvhwm, pending_connection_.endpoint.options.sndhwm); pending_connection_.connect_pipe->set_hwms (
pending_connection_.bind_pipe->set_hwms(bind_options.rcvhwm, bind_options.sndhwm); pending_connection_.endpoint.options.rcvhwm,
} pending_connection_.endpoint.options.sndhwm);
else { pending_connection_.bind_pipe->set_hwms (bind_options.rcvhwm,
pending_connection_.connect_pipe->set_hwms(-1, -1); bind_options.sndhwm);
pending_connection_.bind_pipe->set_hwms(-1, -1); } else {
pending_connection_.connect_pipe->set_hwms (-1, -1);
pending_connection_.bind_pipe->set_hwms (-1, -1);
} }
if (side_ == bind_side) { if (side_ == bind_side) {
@@ -633,22 +630,24 @@ void zmq::ctx_t::connect_inproc_sockets (zmq::socket_base_t *bind_socket_,
cmd.type = command_t::bind; cmd.type = command_t::bind;
cmd.args.bind.pipe = pending_connection_.bind_pipe; cmd.args.bind.pipe = pending_connection_.bind_pipe;
bind_socket_->process_command (cmd); bind_socket_->process_command (cmd);
bind_socket_->send_inproc_connected (pending_connection_.endpoint.socket); bind_socket_->send_inproc_connected (
} pending_connection_.endpoint.socket);
else } else
pending_connection_.connect_pipe->send_bind (bind_socket_, pending_connection_.bind_pipe, false); pending_connection_.connect_pipe->send_bind (
bind_socket_, pending_connection_.bind_pipe, false);
// When a ctx is terminated all pending inproc connection will be // When a ctx is terminated all pending inproc connection will be
// connected, but the socket will already be closed and the pipe will be // connected, but the socket will already be closed and the pipe will be
// in waiting_for_delimiter state, which means no more writes can be done // in waiting_for_delimiter state, which means no more writes can be done
// and the routing id write fails and causes an assert. Check if the socket // and the routing id write fails and causes an assert. Check if the socket
// is open before sending. // is open before sending.
if (pending_connection_.endpoint.options.recv_routing_id && if (pending_connection_.endpoint.options.recv_routing_id
pending_connection_.endpoint.socket->check_tag ()) { && pending_connection_.endpoint.socket->check_tag ()) {
msg_t routing_id; msg_t routing_id;
const int rc = routing_id.init_size (bind_options.routing_id_size); const int rc = routing_id.init_size (bind_options.routing_id_size);
errno_assert (rc == 0); errno_assert (rc == 0);
memcpy (routing_id.data (), bind_options.routing_id, bind_options.routing_id_size); memcpy (routing_id.data (), bind_options.routing_id,
bind_options.routing_id_size);
routing_id.set_flags (msg_t::routing_id); routing_id.set_flags (msg_t::routing_id);
const bool written = pending_connection_.bind_pipe->write (&routing_id); const bool written = pending_connection_.bind_pipe->write (&routing_id);
zmq_assert (written); zmq_assert (written);
@@ -660,9 +659,9 @@ void zmq::ctx_t::connect_inproc_sockets (zmq::socket_base_t *bind_socket_,
int zmq::ctx_t::get_vmci_socket_family () int zmq::ctx_t::get_vmci_socket_family ()
{ {
zmq::scoped_lock_t locker(vmci_sync); zmq::scoped_lock_t locker (vmci_sync);
if (vmci_fd == -1) { if (vmci_fd == -1) {
vmci_family = VMCISock_GetAFValueFd (&vmci_fd); vmci_family = VMCISock_GetAFValueFd (&vmci_fd);
if (vmci_fd != -1) { if (vmci_fd != -1) {

View File

@@ -46,200 +46,208 @@
namespace zmq namespace zmq
{ {
class object_t;
class io_thread_t;
class socket_base_t;
class reaper_t;
class pipe_t;
class object_t; // Information associated with inproc endpoint. Note that endpoint options
class io_thread_t; // are registered as well so that the peer can access them without a need
class socket_base_t; // for synchronisation, handshaking or similar.
class reaper_t; struct endpoint_t
class pipe_t; {
socket_base_t *socket;
options_t options;
};
// Information associated with inproc endpoint. Note that endpoint options // Context object encapsulates all the global state associated with
// are registered as well so that the peer can access them without a need // the library.
// for synchronisation, handshaking or similar.
struct endpoint_t
{
socket_base_t *socket;
options_t options;
};
// Context object encapsulates all the global state associated with class ctx_t
// the library. {
public:
// Create the context object.
ctx_t ();
class ctx_t // Returns false if object is not a context.
{ bool check_tag ();
public:
// Create the context object. // This function is called when user invokes zmq_ctx_term. If there are
ctx_t (); // no more sockets open it'll cause all the infrastructure to be shut
// down. If there are open sockets still, the deallocation happens
// after the last one is closed.
int terminate ();
// Returns false if object is not a context. // This function starts the terminate process by unblocking any blocking
bool check_tag (); // operations currently in progress and stopping any more socket activity
// (except zmq_close).
// This function is non-blocking.
// terminate must still be called afterwards.
// This function is optional, terminate will unblock any current
// operations as well.
int shutdown ();
// This function is called when user invokes zmq_ctx_term. If there are // Set and get context properties.
// no more sockets open it'll cause all the infrastructure to be shut int set (int option_, int optval_);
// down. If there are open sockets still, the deallocation happens int get (int option_);
// after the last one is closed.
int terminate ();
// This function starts the terminate process by unblocking any blocking // Create and destroy a socket.
// operations currently in progress and stopping any more socket activity zmq::socket_base_t *create_socket (int type_);
// (except zmq_close). void destroy_socket (zmq::socket_base_t *socket_);
// This function is non-blocking.
// terminate must still be called afterwards.
// This function is optional, terminate will unblock any current
// operations as well.
int shutdown();
// Set and get context properties. // Start a new thread with proper scheduling parameters.
int set (int option_, int optval_); void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const;
int get (int option_);
// Create and destroy a socket. // Send command to the destination thread.
zmq::socket_base_t *create_socket (int type_); void send_command (uint32_t tid_, const command_t &command_);
void destroy_socket (zmq::socket_base_t *socket_);
// Start a new thread with proper scheduling parameters. // Returns the I/O thread that is the least busy at the moment.
void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const; // Affinity specifies which I/O threads are eligible (0 = all).
// Returns NULL if no I/O thread is available.
zmq::io_thread_t *choose_io_thread (uint64_t affinity_);
// Send command to the destination thread. // Returns reaper thread object.
void send_command (uint32_t tid_, const command_t &command_); zmq::object_t *get_reaper ();
// Returns the I/O thread that is the least busy at the moment. // Management of inproc endpoints.
// Affinity specifies which I/O threads are eligible (0 = all). int register_endpoint (const char *addr_, const endpoint_t &endpoint_);
// Returns NULL if no I/O thread is available. int unregister_endpoint (const std::string &addr_, socket_base_t *socket_);
zmq::io_thread_t *choose_io_thread (uint64_t affinity_); void unregister_endpoints (zmq::socket_base_t *socket_);
endpoint_t find_endpoint (const char *addr_);
// Returns reaper thread object. void pend_connection (const std::string &addr_,
zmq::object_t *get_reaper (); const endpoint_t &endpoint_,
pipe_t **pipes_);
// Management of inproc endpoints. void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
int register_endpoint (const char *addr_, const endpoint_t &endpoint_);
int unregister_endpoint (const std::string &addr_, socket_base_t *socket_);
void unregister_endpoints (zmq::socket_base_t *socket_);
endpoint_t find_endpoint (const char *addr_);
void pend_connection (const std::string &addr_,
const endpoint_t &endpoint_, pipe_t **pipes_);
void connect_pending (const char *addr_, zmq::socket_base_t *bind_socket_);
#ifdef ZMQ_HAVE_VMCI #ifdef ZMQ_HAVE_VMCI
// Return family for the VMCI socket or -1 if it's not available. // Return family for the VMCI socket or -1 if it's not available.
int get_vmci_socket_family (); int get_vmci_socket_family ();
#endif #endif
enum { enum
term_tid = 0, {
reaper_tid = 1 term_tid = 0,
}; reaper_tid = 1
};
~ctx_t (); ~ctx_t ();
bool valid() const; bool valid () const;
private: private:
bool start(); bool start ();
struct pending_connection_t struct pending_connection_t
{ {
endpoint_t endpoint; endpoint_t endpoint;
pipe_t* connect_pipe; pipe_t *connect_pipe;
pipe_t* bind_pipe; pipe_t *bind_pipe;
}; };
// Used to check whether the object is a context. // Used to check whether the object is a context.
uint32_t tag; uint32_t tag;
// Sockets belonging to this context. We need the list so that // Sockets belonging to this context. We need the list so that
// we can notify the sockets when zmq_ctx_term() is called. // we can notify the sockets when zmq_ctx_term() is called.
// The sockets will return ETERM then. // The sockets will return ETERM then.
typedef array_t <socket_base_t> sockets_t; typedef array_t<socket_base_t> sockets_t;
sockets_t sockets; sockets_t sockets;
// List of unused thread slots. // List of unused thread slots.
typedef std::vector <uint32_t> empty_slots_t; typedef std::vector<uint32_t> empty_slots_t;
empty_slots_t empty_slots; empty_slots_t empty_slots;
// If true, zmq_init has been called but no socket has been created // If true, zmq_init has been called but no socket has been created
// yet. Launching of I/O threads is delayed. // yet. Launching of I/O threads is delayed.
bool starting; bool starting;
// If true, zmq_ctx_term was already called. // If true, zmq_ctx_term was already called.
bool terminating; bool terminating;
// Synchronisation of accesses to global slot-related data: // Synchronisation of accesses to global slot-related data:
// sockets, empty_slots, terminating. It also synchronises // sockets, empty_slots, terminating. It also synchronises
// access to zombie sockets as such (as opposed to slots) and provides // access to zombie sockets as such (as opposed to slots) and provides
// a memory barrier to ensure that all CPU cores see the same data. // a memory barrier to ensure that all CPU cores see the same data.
mutex_t slot_sync; mutex_t slot_sync;
// The reaper thread. // The reaper thread.
zmq::reaper_t *reaper; zmq::reaper_t *reaper;
// I/O threads. // I/O threads.
typedef std::vector <zmq::io_thread_t*> io_threads_t; typedef std::vector<zmq::io_thread_t *> io_threads_t;
io_threads_t io_threads; io_threads_t io_threads;
// Array of pointers to mailboxes for both application and I/O threads. // Array of pointers to mailboxes for both application and I/O threads.
uint32_t slot_count; uint32_t slot_count;
i_mailbox **slots; i_mailbox **slots;
// Mailbox for zmq_ctx_term thread. // Mailbox for zmq_ctx_term thread.
mailbox_t term_mailbox; mailbox_t term_mailbox;
// List of inproc endpoints within this context. // List of inproc endpoints within this context.
typedef std::map <std::string, endpoint_t> endpoints_t; typedef std::map<std::string, endpoint_t> endpoints_t;
endpoints_t endpoints; endpoints_t endpoints;
// List of inproc connection endpoints pending a bind // List of inproc connection endpoints pending a bind
typedef std::multimap <std::string, pending_connection_t> pending_connections_t; typedef std::multimap<std::string, pending_connection_t>
pending_connections_t pending_connections; pending_connections_t;
pending_connections_t pending_connections;
// Synchronisation of access to the list of inproc endpoints. // Synchronisation of access to the list of inproc endpoints.
mutex_t endpoints_sync; mutex_t endpoints_sync;
// Maximum socket ID. // Maximum socket ID.
static atomic_counter_t max_socket_id; static atomic_counter_t max_socket_id;
// Maximum number of sockets that can be opened at the same time. // Maximum number of sockets that can be opened at the same time.
int max_sockets; int max_sockets;
// Maximum allowed message size // Maximum allowed message size
int max_msgsz; int max_msgsz;
// Number of I/O threads to launch. // Number of I/O threads to launch.
int io_thread_count; int io_thread_count;
// Does context wait (possibly forever) on termination? // Does context wait (possibly forever) on termination?
bool blocky; bool blocky;
// Is IPv6 enabled on this context? // Is IPv6 enabled on this context?
bool ipv6; bool ipv6;
// Thread parameters. // Thread parameters.
int thread_priority; int thread_priority;
int thread_sched_policy; int thread_sched_policy;
std::set<int> thread_affinity_cpus; std::set<int> thread_affinity_cpus;
std::string thread_name_prefix; std::string thread_name_prefix;
// Synchronisation of access to context options. // Synchronisation of access to context options.
mutex_t opt_sync; mutex_t opt_sync;
ctx_t (const ctx_t&); ctx_t (const ctx_t &);
const ctx_t &operator = (const ctx_t&); const ctx_t &operator= (const ctx_t &);
#ifdef HAVE_FORK #ifdef HAVE_FORK
// the process that created this context. Used to detect forking. // the process that created this context. Used to detect forking.
pid_t pid; pid_t pid;
#endif #endif
enum side { connect_side, bind_side }; enum side
void connect_inproc_sockets(zmq::socket_base_t *bind_socket_, options_t& bind_options, const pending_connection_t &pending_connection_, side side_); {
connect_side,
bind_side
};
void
connect_inproc_sockets (zmq::socket_base_t *bind_socket_,
options_t &bind_options,
const pending_connection_t &pending_connection_,
side side_);
#ifdef ZMQ_HAVE_VMCI #ifdef ZMQ_HAVE_VMCI
int vmci_fd; int vmci_fd;
int vmci_family; int vmci_family;
mutex_t vmci_sync; mutex_t vmci_sync;
#endif #endif
}; };
} }
#endif #endif

View File

@@ -80,7 +80,7 @@ int zmq::curve_client_t::next_handshake_command (msg_t *msg_)
int zmq::curve_client_t::process_handshake_command (msg_t *msg_) int zmq::curve_client_t::process_handshake_command (msg_t *msg_)
{ {
const unsigned char *msg_data = const unsigned char *msg_data =
static_cast <unsigned char *> (msg_->data ()); static_cast<unsigned char *> (msg_->data ());
const size_t msg_size = msg_->size (); const size_t msg_size = msg_->size ();
int rc = 0; int rc = 0;
@@ -94,8 +94,7 @@ int zmq::curve_client_t::process_handshake_command (msg_t *msg_)
rc = process_error (msg_data, msg_size); rc = process_error (msg_data, msg_size);
else { else {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
rc = -1; rc = -1;
} }
@@ -126,8 +125,7 @@ zmq::mechanism_t::status_t zmq::curve_client_t::status () const
{ {
if (state == connected) if (state == connected)
return mechanism_t::ready; return mechanism_t::ready;
else else if (state == error_received)
if (state == error_received)
return mechanism_t::error; return mechanism_t::error;
else else
return mechanism_t::handshaking; return mechanism_t::handshaking;
@@ -141,8 +139,7 @@ int zmq::curve_client_t::produce_hello (msg_t *msg_)
rc = tools.produce_hello (msg_->data (), cn_nonce); rc = tools.produce_hello (msg_->data (), cn_nonce);
if (rc == -1) { if (rc == -1) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
// TODO this is somewhat inconsistent: we call init_size, but we may // TODO this is somewhat inconsistent: we call init_size, but we may
// not close msg_; i.e. we assume that msg_ is initialized but empty // not close msg_; i.e. we assume that msg_ is initialized but empty
@@ -164,8 +161,7 @@ int zmq::curve_client_t::process_welcome (const uint8_t *msg_data,
if (rc == -1) { if (rc == -1) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
@@ -196,8 +192,7 @@ int zmq::curve_client_t::produce_initiate (msg_t *msg_)
if (-1 == rc) { if (-1 == rc) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
// TODO see comment in produce_hello // TODO see comment in produce_hello
return -1; return -1;
@@ -208,8 +203,8 @@ int zmq::curve_client_t::produce_initiate (msg_t *msg_)
return 0; return 0;
} }
int zmq::curve_client_t::process_ready ( int zmq::curve_client_t::process_ready (const uint8_t *msg_data,
const uint8_t *msg_data, size_t msg_size) size_t msg_size)
{ {
if (msg_size < 30) { if (msg_size < 30) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -221,7 +216,7 @@ int zmq::curve_client_t::process_ready (
const size_t clen = (msg_size - 14) + crypto_box_BOXZEROBYTES; const size_t clen = (msg_size - 14) + crypto_box_BOXZEROBYTES;
uint8_t ready_nonce [crypto_box_NONCEBYTES]; uint8_t ready_nonce[crypto_box_NONCEBYTES];
uint8_t *ready_plaintext = (uint8_t *) malloc (crypto_box_ZEROBYTES + clen); uint8_t *ready_plaintext = (uint8_t *) malloc (crypto_box_ZEROBYTES + clen);
alloc_assert (ready_plaintext); alloc_assert (ready_plaintext);
uint8_t *ready_box = uint8_t *ready_box =
@@ -229,21 +224,20 @@ int zmq::curve_client_t::process_ready (
alloc_assert (ready_box); alloc_assert (ready_box);
memset (ready_box, 0, crypto_box_BOXZEROBYTES); memset (ready_box, 0, crypto_box_BOXZEROBYTES);
memcpy (ready_box + crypto_box_BOXZEROBYTES, memcpy (ready_box + crypto_box_BOXZEROBYTES, msg_data + 14,
msg_data + 14, clen - crypto_box_BOXZEROBYTES); clen - crypto_box_BOXZEROBYTES);
memcpy (ready_nonce, "CurveZMQREADY---", 16); memcpy (ready_nonce, "CurveZMQREADY---", 16);
memcpy (ready_nonce + 16, msg_data + 6, 8); memcpy (ready_nonce + 16, msg_data + 6, 8);
cn_peer_nonce = get_uint64(msg_data + 6); cn_peer_nonce = get_uint64 (msg_data + 6);
int rc = crypto_box_open_afternm (ready_plaintext, ready_box, int rc = crypto_box_open_afternm (ready_plaintext, ready_box, clen,
clen, ready_nonce, cn_precom); ready_nonce, cn_precom);
free (ready_box); free (ready_box);
if (rc != 0) { if (rc != 0) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -254,8 +248,7 @@ int zmq::curve_client_t::process_ready (
if (rc == 0) if (rc == 0)
state = connected; state = connected;
else else {
{
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA); session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA);
errno = EPROTO; errno = EPROTO;
@@ -264,8 +257,8 @@ int zmq::curve_client_t::process_ready (
return rc; return rc;
} }
int zmq::curve_client_t::process_error ( int zmq::curve_client_t::process_error (const uint8_t *msg_data,
const uint8_t *msg_data, size_t msg_size) size_t msg_size)
{ {
if (state != expect_welcome && state != expect_ready) { if (state != expect_welcome && state != expect_ready) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -280,7 +273,7 @@ int zmq::curve_client_t::process_error (
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
const size_t error_reason_len = static_cast <size_t> (msg_data [6]); const size_t error_reason_len = static_cast<size_t> (msg_data[6]);
if (error_reason_len > msg_size - 7) { if (error_reason_len > msg_size - 7) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (),

View File

@@ -38,48 +38,45 @@
namespace zmq namespace zmq
{ {
class msg_t;
class session_base_t;
class msg_t; class curve_client_t : public curve_mechanism_base_t
class session_base_t; {
public:
curve_client_t (session_base_t *session_, const options_t &options_);
virtual ~curve_client_t ();
class curve_client_t : public curve_mechanism_base_t // mechanism implementation
virtual int next_handshake_command (msg_t *msg_);
virtual int process_handshake_command (msg_t *msg_);
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
virtual status_t status () const;
private:
enum state_t
{ {
public: send_hello,
expect_welcome,
curve_client_t (session_base_t *session_, const options_t &options_); send_initiate,
virtual ~curve_client_t (); expect_ready,
error_received,
// mechanism implementation connected
virtual int next_handshake_command (msg_t *msg_);
virtual int process_handshake_command (msg_t *msg_);
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
virtual status_t status () const;
private:
enum state_t {
send_hello,
expect_welcome,
send_initiate,
expect_ready,
error_received,
connected
};
// Current FSM state
state_t state;
// CURVE protocol tools
curve_client_tools_t tools;
int produce_hello (msg_t *msg_);
int process_welcome (const uint8_t *cmd_data, size_t data_size);
int produce_initiate (msg_t *msg_);
int process_ready (const uint8_t *cmd_data, size_t data_size);
int process_error (const uint8_t *cmd_data, size_t data_size);
}; };
// Current FSM state
state_t state;
// CURVE protocol tools
curve_client_tools_t tools;
int produce_hello (msg_t *msg_);
int process_welcome (const uint8_t *cmd_data, size_t data_size);
int produce_initiate (msg_t *msg_);
int process_ready (const uint8_t *cmd_data, size_t data_size);
int process_error (const uint8_t *cmd_data, size_t data_size);
};
} }
#endif #endif

View File

@@ -153,8 +153,7 @@ struct curve_client_tools_t
// Create vouch = Box [C',S](C->S') // Create vouch = Box [C',S](C->S')
memset (vouch_plaintext, 0, crypto_box_ZEROBYTES); memset (vouch_plaintext, 0, crypto_box_ZEROBYTES);
memcpy (vouch_plaintext + crypto_box_ZEROBYTES, cn_public, 32); memcpy (vouch_plaintext + crypto_box_ZEROBYTES, cn_public, 32);
memcpy (vouch_plaintext + crypto_box_ZEROBYTES + 32, server_key, memcpy (vouch_plaintext + crypto_box_ZEROBYTES + 32, server_key, 32);
32);
memcpy (vouch_nonce, "VOUCH---", 8); memcpy (vouch_nonce, "VOUCH---", 8);
randombytes (vouch_nonce + 8, 16); randombytes (vouch_nonce + 8, 16);
@@ -165,8 +164,8 @@ struct curve_client_tools_t
return -1; return -1;
uint8_t initiate_nonce[crypto_box_NONCEBYTES]; uint8_t initiate_nonce[crypto_box_NONCEBYTES];
uint8_t *initiate_box = (uint8_t *) malloc ( uint8_t *initiate_box =
crypto_box_BOXZEROBYTES + 144 + metadata_length); (uint8_t *) malloc (crypto_box_BOXZEROBYTES + 144 + metadata_length);
alloc_assert (initiate_box); alloc_assert (initiate_box);
uint8_t *initiate_plaintext = uint8_t *initiate_plaintext =
(uint8_t *) malloc (crypto_box_ZEROBYTES + 128 + metadata_length); (uint8_t *) malloc (crypto_box_ZEROBYTES + 128 + metadata_length);
@@ -174,8 +173,7 @@ struct curve_client_tools_t
// Create Box [C + vouch + metadata](C'->S') // Create Box [C + vouch + metadata](C'->S')
memset (initiate_plaintext, 0, crypto_box_ZEROBYTES); memset (initiate_plaintext, 0, crypto_box_ZEROBYTES);
memcpy (initiate_plaintext + crypto_box_ZEROBYTES, public_key, memcpy (initiate_plaintext + crypto_box_ZEROBYTES, public_key, 32);
32);
memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 32, vouch_nonce + 8, memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 32, vouch_nonce + 8,
16); 16);
memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 48, memcpy (initiate_plaintext + crypto_box_ZEROBYTES + 48,

View File

@@ -53,7 +53,7 @@ int zmq::curve_mechanism_base_t::encode (msg_t *msg_)
{ {
const size_t mlen = crypto_box_ZEROBYTES + 1 + msg_->size (); const size_t mlen = crypto_box_ZEROBYTES + 1 + msg_->size ();
uint8_t message_nonce [crypto_box_NONCEBYTES]; uint8_t message_nonce[crypto_box_NONCEBYTES];
memcpy (message_nonce, encode_nonce_prefix, 16); memcpy (message_nonce, encode_nonce_prefix, 16);
put_uint64 (message_nonce + 16, cn_nonce); put_uint64 (message_nonce + 16, cn_nonce);
@@ -63,19 +63,19 @@ int zmq::curve_mechanism_base_t::encode (msg_t *msg_)
if (msg_->flags () & msg_t::command) if (msg_->flags () & msg_t::command)
flags |= 0x02; flags |= 0x02;
uint8_t *message_plaintext = static_cast <uint8_t *> (malloc (mlen)); uint8_t *message_plaintext = static_cast<uint8_t *> (malloc (mlen));
alloc_assert (message_plaintext); alloc_assert (message_plaintext);
memset (message_plaintext, 0, crypto_box_ZEROBYTES); memset (message_plaintext, 0, crypto_box_ZEROBYTES);
message_plaintext [crypto_box_ZEROBYTES] = flags; message_plaintext[crypto_box_ZEROBYTES] = flags;
memcpy (message_plaintext + crypto_box_ZEROBYTES + 1, memcpy (message_plaintext + crypto_box_ZEROBYTES + 1, msg_->data (),
msg_->data (), msg_->size ()); msg_->size ());
uint8_t *message_box = static_cast <uint8_t *> (malloc (mlen)); uint8_t *message_box = static_cast<uint8_t *> (malloc (mlen));
alloc_assert (message_box); alloc_assert (message_box);
int rc = crypto_box_afternm (message_box, message_plaintext, int rc = crypto_box_afternm (message_box, message_plaintext, mlen,
mlen, message_nonce, cn_precom); message_nonce, cn_precom);
zmq_assert (rc == 0); zmq_assert (rc == 0);
rc = msg_->close (); rc = msg_->close ();
@@ -84,7 +84,7 @@ int zmq::curve_mechanism_base_t::encode (msg_t *msg_)
rc = msg_->init_size (16 + mlen - crypto_box_BOXZEROBYTES); rc = msg_->init_size (16 + mlen - crypto_box_BOXZEROBYTES);
zmq_assert (rc == 0); zmq_assert (rc == 0);
uint8_t *message = static_cast <uint8_t *> (msg_->data ()); uint8_t *message = static_cast<uint8_t *> (msg_->data ());
memcpy (message, "\x07MESSAGE", 8); memcpy (message, "\x07MESSAGE", 8);
memcpy (message + 8, message_nonce + 16, 8); memcpy (message + 8, message_nonce + 16, 8);
@@ -103,10 +103,10 @@ int zmq::curve_mechanism_base_t::decode (msg_t *msg_)
{ {
int rc = check_basic_command_structure (msg_); int rc = check_basic_command_structure (msg_);
if (rc == -1) if (rc == -1)
return -1; return -1;
const size_t size = msg_->size (); const size_t size = msg_->size ();
const uint8_t *message = static_cast <uint8_t *> (msg_->data ()); const uint8_t *message = static_cast<uint8_t *> (msg_->data ());
if (size < 8 || memcmp (message, "\x07MESSAGE", 8)) { if (size < 8 || memcmp (message, "\x07MESSAGE", 8)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -123,10 +123,10 @@ int zmq::curve_mechanism_base_t::decode (msg_t *msg_)
return -1; return -1;
} }
uint8_t message_nonce [crypto_box_NONCEBYTES]; uint8_t message_nonce[crypto_box_NONCEBYTES];
memcpy (message_nonce, decode_nonce_prefix, 16); memcpy (message_nonce, decode_nonce_prefix, 16);
memcpy (message_nonce + 16, message + 8, 8); memcpy (message_nonce + 16, message + 8, 8);
uint64_t nonce = get_uint64(message + 8); uint64_t nonce = get_uint64 (message + 8);
if (nonce <= cn_peer_nonce) { if (nonce <= cn_peer_nonce) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_SEQUENCE); session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_SEQUENCE);
@@ -137,15 +137,15 @@ int zmq::curve_mechanism_base_t::decode (msg_t *msg_)
const size_t clen = crypto_box_BOXZEROBYTES + msg_->size () - 16; const size_t clen = crypto_box_BOXZEROBYTES + msg_->size () - 16;
uint8_t *message_plaintext = static_cast <uint8_t *> (malloc (clen)); uint8_t *message_plaintext = static_cast<uint8_t *> (malloc (clen));
alloc_assert (message_plaintext); alloc_assert (message_plaintext);
uint8_t *message_box = static_cast <uint8_t *> (malloc (clen)); uint8_t *message_box = static_cast<uint8_t *> (malloc (clen));
alloc_assert (message_box); alloc_assert (message_box);
memset (message_box, 0, crypto_box_BOXZEROBYTES); memset (message_box, 0, crypto_box_BOXZEROBYTES);
memcpy (message_box + crypto_box_BOXZEROBYTES, memcpy (message_box + crypto_box_BOXZEROBYTES, message + 16,
message + 16, msg_->size () - 16); msg_->size () - 16);
rc = crypto_box_open_afternm (message_plaintext, message_box, clen, rc = crypto_box_open_afternm (message_plaintext, message_box, clen,
message_nonce, cn_precom); message_nonce, cn_precom);
@@ -156,17 +156,15 @@ int zmq::curve_mechanism_base_t::decode (msg_t *msg_)
rc = msg_->init_size (clen - 1 - crypto_box_ZEROBYTES); rc = msg_->init_size (clen - 1 - crypto_box_ZEROBYTES);
zmq_assert (rc == 0); zmq_assert (rc == 0);
const uint8_t flags = message_plaintext [crypto_box_ZEROBYTES]; const uint8_t flags = message_plaintext[crypto_box_ZEROBYTES];
if (flags & 0x01) if (flags & 0x01)
msg_->set_flags (msg_t::more); msg_->set_flags (msg_t::more);
if (flags & 0x02) if (flags & 0x02)
msg_->set_flags (msg_t::command); msg_->set_flags (msg_t::command);
memcpy (msg_->data (), memcpy (msg_->data (), message_plaintext + crypto_box_ZEROBYTES + 1,
message_plaintext + crypto_box_ZEROBYTES + 1,
msg_->size ()); msg_->size ());
} } else {
else {
// CURVE I : connection key used for MESSAGE is wrong // CURVE I : connection key used for MESSAGE is wrong
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC); session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);

View File

@@ -70,7 +70,7 @@ class curve_mechanism_base_t : public virtual mechanism_base_t
uint64_t cn_peer_nonce; uint64_t cn_peer_nonce;
// Intermediary buffer used to speed up boxing and unboxing. // Intermediary buffer used to speed up boxing and unboxing.
uint8_t cn_precom [crypto_box_BEFORENMBYTES]; uint8_t cn_precom[crypto_box_BEFORENMBYTES];
}; };
} }

View File

@@ -138,10 +138,10 @@ int zmq::curve_server_t::process_hello (msg_t *msg_)
{ {
int rc = check_basic_command_structure (msg_); int rc = check_basic_command_structure (msg_);
if (rc == -1) if (rc == -1)
return -1; return -1;
const size_t size = msg_->size (); const size_t size = msg_->size ();
const uint8_t * const hello = static_cast <uint8_t *> (msg_->data ()); const uint8_t *const hello = static_cast<uint8_t *> (msg_->data ());
if (size < 6 || memcmp (hello, "\x05HELLO", 6)) { if (size < 6 || memcmp (hello, "\x05HELLO", 6)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -152,18 +152,20 @@ int zmq::curve_server_t::process_hello (msg_t *msg_)
if (size != 200) { if (size != 200) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO); session->get_endpoint (),
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
const uint8_t major = hello [6]; const uint8_t major = hello[6];
const uint8_t minor = hello [7]; const uint8_t minor = hello[7];
if (major != 1 || minor != 0) { if (major != 1 || minor != 0) {
// CURVE I: client HELLO has unknown version number // CURVE I: client HELLO has unknown version number
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO); session->get_endpoint (),
ZMQ_PROTOCOL_ERROR_ZMTP_MALFORMED_COMMAND_HELLO);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -171,13 +173,13 @@ int zmq::curve_server_t::process_hello (msg_t *msg_)
// Save client's short-term public key (C') // Save client's short-term public key (C')
memcpy (cn_client, hello + 80, 32); memcpy (cn_client, hello + 80, 32);
uint8_t hello_nonce [crypto_box_NONCEBYTES]; uint8_t hello_nonce[crypto_box_NONCEBYTES];
uint8_t hello_plaintext [crypto_box_ZEROBYTES + 64]; uint8_t hello_plaintext[crypto_box_ZEROBYTES + 64];
uint8_t hello_box [crypto_box_BOXZEROBYTES + 80]; uint8_t hello_box[crypto_box_BOXZEROBYTES + 80];
memcpy (hello_nonce, "CurveZMQHELLO---", 16); memcpy (hello_nonce, "CurveZMQHELLO---", 16);
memcpy (hello_nonce + 16, hello + 112, 8); memcpy (hello_nonce + 16, hello + 112, 8);
cn_peer_nonce = get_uint64(hello + 112); cn_peer_nonce = get_uint64 (hello + 112);
memset (hello_box, 0, crypto_box_BOXZEROBYTES); memset (hello_box, 0, crypto_box_BOXZEROBYTES);
memcpy (hello_box + crypto_box_BOXZEROBYTES, hello + 120, 80); memcpy (hello_box + crypto_box_BOXZEROBYTES, hello + 120, 80);
@@ -199,9 +201,9 @@ int zmq::curve_server_t::process_hello (msg_t *msg_)
int zmq::curve_server_t::produce_welcome (msg_t *msg_) int zmq::curve_server_t::produce_welcome (msg_t *msg_)
{ {
uint8_t cookie_nonce [crypto_secretbox_NONCEBYTES]; uint8_t cookie_nonce[crypto_secretbox_NONCEBYTES];
uint8_t cookie_plaintext [crypto_secretbox_ZEROBYTES + 64]; uint8_t cookie_plaintext[crypto_secretbox_ZEROBYTES + 64];
uint8_t cookie_ciphertext [crypto_secretbox_BOXZEROBYTES + 80]; uint8_t cookie_ciphertext[crypto_secretbox_BOXZEROBYTES + 80];
// Create full nonce for encryption // Create full nonce for encryption
// 8-byte prefix plus 16-byte random nonce // 8-byte prefix plus 16-byte random nonce
@@ -210,23 +212,21 @@ int zmq::curve_server_t::produce_welcome (msg_t *msg_)
// Generate cookie = Box [C' + s'](t) // Generate cookie = Box [C' + s'](t)
memset (cookie_plaintext, 0, crypto_secretbox_ZEROBYTES); memset (cookie_plaintext, 0, crypto_secretbox_ZEROBYTES);
memcpy (cookie_plaintext + crypto_secretbox_ZEROBYTES, memcpy (cookie_plaintext + crypto_secretbox_ZEROBYTES, cn_client, 32);
cn_client, 32); memcpy (cookie_plaintext + crypto_secretbox_ZEROBYTES + 32, cn_secret, 32);
memcpy (cookie_plaintext + crypto_secretbox_ZEROBYTES + 32,
cn_secret, 32);
// Generate fresh cookie key // Generate fresh cookie key
randombytes (cookie_key, crypto_secretbox_KEYBYTES); randombytes (cookie_key, crypto_secretbox_KEYBYTES);
// Encrypt using symmetric cookie key // Encrypt using symmetric cookie key
int rc = crypto_secretbox (cookie_ciphertext, cookie_plaintext, int rc =
sizeof cookie_plaintext, crypto_secretbox (cookie_ciphertext, cookie_plaintext,
cookie_nonce, cookie_key); sizeof cookie_plaintext, cookie_nonce, cookie_key);
zmq_assert (rc == 0); zmq_assert (rc == 0);
uint8_t welcome_nonce [crypto_box_NONCEBYTES]; uint8_t welcome_nonce[crypto_box_NONCEBYTES];
uint8_t welcome_plaintext [crypto_box_ZEROBYTES + 128]; uint8_t welcome_plaintext[crypto_box_ZEROBYTES + 128];
uint8_t welcome_ciphertext [crypto_box_BOXZEROBYTES + 144]; uint8_t welcome_ciphertext[crypto_box_BOXZEROBYTES + 144];
// Create full nonce for encryption // Create full nonce for encryption
// 8-byte prefix plus 16-byte random nonce // 8-byte prefix plus 16-byte random nonce
@@ -236,8 +236,8 @@ int zmq::curve_server_t::produce_welcome (msg_t *msg_)
// Create 144-byte Box [S' + cookie](S->C') // Create 144-byte Box [S' + cookie](S->C')
memset (welcome_plaintext, 0, crypto_box_ZEROBYTES); memset (welcome_plaintext, 0, crypto_box_ZEROBYTES);
memcpy (welcome_plaintext + crypto_box_ZEROBYTES, cn_public, 32); memcpy (welcome_plaintext + crypto_box_ZEROBYTES, cn_public, 32);
memcpy (welcome_plaintext + crypto_box_ZEROBYTES + 32, memcpy (welcome_plaintext + crypto_box_ZEROBYTES + 32, cookie_nonce + 8,
cookie_nonce + 8, 16); 16);
memcpy (welcome_plaintext + crypto_box_ZEROBYTES + 48, memcpy (welcome_plaintext + crypto_box_ZEROBYTES + 48,
cookie_ciphertext + crypto_secretbox_BOXZEROBYTES, 80); cookie_ciphertext + crypto_secretbox_BOXZEROBYTES, 80);
@@ -257,7 +257,7 @@ int zmq::curve_server_t::produce_welcome (msg_t *msg_)
rc = msg_->init_size (168); rc = msg_->init_size (168);
errno_assert (rc == 0); errno_assert (rc == 0);
uint8_t * const welcome = static_cast <uint8_t *> (msg_->data ()); uint8_t *const welcome = static_cast<uint8_t *> (msg_->data ());
memcpy (welcome, "\x07WELCOME", 8); memcpy (welcome, "\x07WELCOME", 8);
memcpy (welcome + 8, welcome_nonce + 8, 16); memcpy (welcome + 8, welcome_nonce + 8, 16);
memcpy (welcome + 24, welcome_ciphertext + crypto_box_BOXZEROBYTES, 144); memcpy (welcome + 24, welcome_ciphertext + crypto_box_BOXZEROBYTES, 144);
@@ -269,10 +269,10 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
{ {
int rc = check_basic_command_structure (msg_); int rc = check_basic_command_structure (msg_);
if (rc == -1) if (rc == -1)
return -1; return -1;
const size_t size = msg_->size (); const size_t size = msg_->size ();
const uint8_t *initiate = static_cast <uint8_t *> (msg_->data ()); const uint8_t *initiate = static_cast<uint8_t *> (msg_->data ());
if (size < 9 || memcmp (initiate, "\x08INITIATE", 9)) { if (size < 9 || memcmp (initiate, "\x08INITIATE", 9)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -289,9 +289,9 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
return -1; return -1;
} }
uint8_t cookie_nonce [crypto_secretbox_NONCEBYTES]; uint8_t cookie_nonce[crypto_secretbox_NONCEBYTES];
uint8_t cookie_plaintext [crypto_secretbox_ZEROBYTES + 64]; uint8_t cookie_plaintext[crypto_secretbox_ZEROBYTES + 64];
uint8_t cookie_box [crypto_secretbox_BOXZEROBYTES + 80]; uint8_t cookie_box[crypto_secretbox_BOXZEROBYTES + 80];
// Open Box [C' + s'](t) // Open Box [C' + s'](t)
memset (cookie_box, 0, crypto_secretbox_BOXZEROBYTES); memset (cookie_box, 0, crypto_secretbox_BOXZEROBYTES);
@@ -312,7 +312,8 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
// Check cookie plain text is as expected [C' + s'] // Check cookie plain text is as expected [C' + s']
if (memcmp (cookie_plaintext + crypto_secretbox_ZEROBYTES, cn_client, 32) if (memcmp (cookie_plaintext + crypto_secretbox_ZEROBYTES, cn_client, 32)
|| memcmp (cookie_plaintext + crypto_secretbox_ZEROBYTES + 32, cn_secret, 32)) { || memcmp (cookie_plaintext + crypto_secretbox_ZEROBYTES + 32,
cn_secret, 32)) {
// TODO this case is very hard to test, as it would require a modified // TODO this case is very hard to test, as it would require a modified
// client that knows the server's secret temporary cookie key // client that knows the server's secret temporary cookie key
@@ -325,21 +326,21 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
const size_t clen = (size - 113) + crypto_box_BOXZEROBYTES; const size_t clen = (size - 113) + crypto_box_BOXZEROBYTES;
uint8_t initiate_nonce [crypto_box_NONCEBYTES]; uint8_t initiate_nonce[crypto_box_NONCEBYTES];
uint8_t initiate_plaintext [crypto_box_ZEROBYTES + 128 + 256]; uint8_t initiate_plaintext[crypto_box_ZEROBYTES + 128 + 256];
uint8_t initiate_box [crypto_box_BOXZEROBYTES + 144 + 256]; uint8_t initiate_box[crypto_box_BOXZEROBYTES + 144 + 256];
// Open Box [C + vouch + metadata](C'->S') // Open Box [C + vouch + metadata](C'->S')
memset (initiate_box, 0, crypto_box_BOXZEROBYTES); memset (initiate_box, 0, crypto_box_BOXZEROBYTES);
memcpy (initiate_box + crypto_box_BOXZEROBYTES, memcpy (initiate_box + crypto_box_BOXZEROBYTES, initiate + 113,
initiate + 113, clen - crypto_box_BOXZEROBYTES); clen - crypto_box_BOXZEROBYTES);
memcpy (initiate_nonce, "CurveZMQINITIATE", 16); memcpy (initiate_nonce, "CurveZMQINITIATE", 16);
memcpy (initiate_nonce + 16, initiate + 105, 8); memcpy (initiate_nonce + 16, initiate + 105, 8);
cn_peer_nonce = get_uint64(initiate + 105); cn_peer_nonce = get_uint64 (initiate + 105);
rc = crypto_box_open (initiate_plaintext, initiate_box, rc = crypto_box_open (initiate_plaintext, initiate_box, clen,
clen, initiate_nonce, cn_client, cn_secret); initiate_nonce, cn_client, cn_secret);
if (rc != 0) { if (rc != 0) {
// CURVE I: cannot open client INITIATE // CURVE I: cannot open client INITIATE
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -350,9 +351,9 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
const uint8_t *client_key = initiate_plaintext + crypto_box_ZEROBYTES; const uint8_t *client_key = initiate_plaintext + crypto_box_ZEROBYTES;
uint8_t vouch_nonce [crypto_box_NONCEBYTES]; uint8_t vouch_nonce[crypto_box_NONCEBYTES];
uint8_t vouch_plaintext [crypto_box_ZEROBYTES + 64]; uint8_t vouch_plaintext[crypto_box_ZEROBYTES + 64];
uint8_t vouch_box [crypto_box_BOXZEROBYTES + 80]; uint8_t vouch_box[crypto_box_BOXZEROBYTES + 80];
// Open Box Box [C',S](C->S') and check contents // Open Box Box [C',S](C->S') and check contents
memset (vouch_box, 0, crypto_box_BOXZEROBYTES); memset (vouch_box, 0, crypto_box_BOXZEROBYTES);
@@ -360,11 +361,10 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
initiate_plaintext + crypto_box_ZEROBYTES + 48, 80); initiate_plaintext + crypto_box_ZEROBYTES + 48, 80);
memcpy (vouch_nonce, "VOUCH---", 8); memcpy (vouch_nonce, "VOUCH---", 8);
memcpy (vouch_nonce + 8, memcpy (vouch_nonce + 8, initiate_plaintext + crypto_box_ZEROBYTES + 32,
initiate_plaintext + crypto_box_ZEROBYTES + 32, 16); 16);
rc = crypto_box_open (vouch_plaintext, vouch_box, rc = crypto_box_open (vouch_plaintext, vouch_box, sizeof vouch_box,
sizeof vouch_box,
vouch_nonce, client_key, cn_secret); vouch_nonce, client_key, cn_secret);
if (rc != 0) { if (rc != 0) {
// CURVE I: cannot open client INITIATE vouch // CURVE I: cannot open client INITIATE vouch
@@ -427,7 +427,7 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
int zmq::curve_server_t::produce_ready (msg_t *msg_) int zmq::curve_server_t::produce_ready (msg_t *msg_)
{ {
const size_t metadata_length = basic_properties_len (); const size_t metadata_length = basic_properties_len ();
uint8_t ready_nonce [crypto_box_NONCEBYTES]; uint8_t ready_nonce[crypto_box_NONCEBYTES];
uint8_t *ready_plaintext = uint8_t *ready_plaintext =
(uint8_t *) malloc (crypto_box_ZEROBYTES + metadata_length); (uint8_t *) malloc (crypto_box_ZEROBYTES + metadata_length);
@@ -456,7 +456,7 @@ int zmq::curve_server_t::produce_ready (msg_t *msg_)
rc = msg_->init_size (14 + mlen - crypto_box_BOXZEROBYTES); rc = msg_->init_size (14 + mlen - crypto_box_BOXZEROBYTES);
errno_assert (rc == 0); errno_assert (rc == 0);
uint8_t *ready = static_cast <uint8_t *> (msg_->data ()); uint8_t *ready = static_cast<uint8_t *> (msg_->data ());
memcpy (ready, "\x05READY", 6); memcpy (ready, "\x05READY", 6);
// Short nonce, prefixed by "CurveZMQREADY---" // Short nonce, prefixed by "CurveZMQREADY---"
@@ -477,9 +477,9 @@ int zmq::curve_server_t::produce_error (msg_t *msg_) const
zmq_assert (status_code.length () == 3); zmq_assert (status_code.length () == 3);
const int rc = msg_->init_size (6 + 1 + expected_status_code_length); const int rc = msg_->init_size (6 + 1 + expected_status_code_length);
zmq_assert (rc == 0); zmq_assert (rc == 0);
char *msg_data = static_cast <char *> (msg_->data ()); char *msg_data = static_cast<char *> (msg_->data ());
memcpy (msg_data, "\5ERROR", 6); memcpy (msg_data, "\5ERROR", 6);
msg_data [6] = expected_status_code_length; msg_data[6] = expected_status_code_length;
memcpy (msg_data + 7, status_code.c_str (), expected_status_code_length); memcpy (msg_data + 7, status_code.c_str (), expected_status_code_length);
return 0; return 0;
} }

View File

@@ -39,54 +39,51 @@
namespace zmq namespace zmq
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning (push) #pragma warning(push)
#pragma warning (disable: 4250) #pragma warning(disable : 4250)
#endif #endif
class curve_server_t : public zap_client_common_handshake_t, class curve_server_t : public zap_client_common_handshake_t,
public curve_mechanism_base_t public curve_mechanism_base_t
{ {
public: public:
curve_server_t (session_base_t *session_,
const std::string &peer_address_,
const options_t &options_);
virtual ~curve_server_t ();
curve_server_t (session_base_t *session_, // mechanism implementation
const std::string &peer_address_, virtual int next_handshake_command (msg_t *msg_);
const options_t &options_); virtual int process_handshake_command (msg_t *msg_);
virtual ~curve_server_t (); virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
// mechanism implementation private:
virtual int next_handshake_command (msg_t *msg_); // Our secret key (s)
virtual int process_handshake_command (msg_t *msg_); uint8_t secret_key[crypto_box_SECRETKEYBYTES];
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
private: // Our short-term public key (S')
uint8_t cn_public[crypto_box_PUBLICKEYBYTES];
// Our secret key (s) // Our short-term secret key (s')
uint8_t secret_key [crypto_box_SECRETKEYBYTES]; uint8_t cn_secret[crypto_box_SECRETKEYBYTES];
// Our short-term public key (S') // Client's short-term public key (C')
uint8_t cn_public [crypto_box_PUBLICKEYBYTES]; uint8_t cn_client[crypto_box_PUBLICKEYBYTES];
// Our short-term secret key (s') // Key used to produce cookie
uint8_t cn_secret [crypto_box_SECRETKEYBYTES]; uint8_t cookie_key[crypto_secretbox_KEYBYTES];
// Client's short-term public key (C') int process_hello (msg_t *msg_);
uint8_t cn_client [crypto_box_PUBLICKEYBYTES]; int produce_welcome (msg_t *msg_);
int process_initiate (msg_t *msg_);
int produce_ready (msg_t *msg_);
int produce_error (msg_t *msg_) const;
// Key used to produce cookie void send_zap_request (const uint8_t *key);
uint8_t cookie_key [crypto_secretbox_KEYBYTES]; };
int process_hello (msg_t *msg_);
int produce_welcome (msg_t *msg_);
int process_initiate (msg_t *msg_);
int produce_ready (msg_t *msg_);
int produce_error (msg_t *msg_) const;
void send_zap_request (const uint8_t *key);
};
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning (pop) #pragma warning(pop)
#endif #endif
} }
#endif #endif

View File

@@ -39,106 +39,103 @@
namespace zmq namespace zmq
{ {
// dbuffer is a single-producer single-consumer double-buffer
// implementation.
//
// The producer writes to a back buffer and then tries to swap
// pointers between the back and front buffers. If it fails,
// due to the consumer reading from the front buffer, it just
// gives up, which is ok since writes are many and redundant.
//
// The reader simply reads from the front buffer.
//
// has_msg keeps track of whether there has been a not yet read
// value written, it is used by ypipe_conflate to mimic ypipe
// functionality regarding a reader being asleep
// dbuffer is a single-producer single-consumer double-buffer template <typename T> class dbuffer_t;
// implementation.
//
// The producer writes to a back buffer and then tries to swap
// pointers between the back and front buffers. If it fails,
// due to the consumer reading from the front buffer, it just
// gives up, which is ok since writes are many and redundant.
//
// The reader simply reads from the front buffer.
//
// has_msg keeps track of whether there has been a not yet read
// value written, it is used by ypipe_conflate to mimic ypipe
// functionality regarding a reader being asleep
template <typename T> class dbuffer_t; template <> class dbuffer_t<msg_t>
{
template <> class dbuffer_t<msg_t> public:
inline dbuffer_t () :
back (&storage[0]),
front (&storage[1]),
has_msg (false)
{ {
public: back->init ();
front->init ();
}
inline dbuffer_t () inline ~dbuffer_t ()
: back (&storage[0]) {
, front (&storage[1]) back->close ();
, has_msg (false) front->close ();
{ }
back->init ();
front->init (); inline void write (const msg_t &value_)
{
msg_t &xvalue = const_cast<msg_t &> (value_);
zmq_assert (xvalue.check ());
back->move (xvalue); // cannot just overwrite, might leak
zmq_assert (back->check ());
if (sync.try_lock ()) {
std::swap (back, front);
has_msg = true;
sync.unlock ();
} }
}
inline bool read (msg_t *value_)
{
if (!value_)
return false;
inline ~dbuffer_t()
{ {
back->close (); scoped_lock_t lock (sync);
front->close (); if (!has_msg)
}
inline void write (const msg_t &value_)
{
msg_t& xvalue = const_cast<msg_t&>(value_);
zmq_assert (xvalue.check ());
back->move (xvalue); // cannot just overwrite, might leak
zmq_assert (back->check ());
if (sync.try_lock ())
{
std::swap (back, front);
has_msg = true;
sync.unlock ();
}
}
inline bool read (msg_t *value_)
{
if (!value_)
return false; return false;
{ zmq_assert (front->check ());
scoped_lock_t lock (sync);
if (!has_msg)
return false;
zmq_assert (front->check ()); *value_ = *front;
front->init (); // avoid double free
*value_ = *front; has_msg = false;
front->init (); // avoid double free return true;
has_msg = false;
return true;
}
} }
}
inline bool check_read () inline bool check_read ()
{ {
scoped_lock_t lock (sync); scoped_lock_t lock (sync);
return has_msg; return has_msg;
} }
inline bool probe (bool (*fn)(const msg_t &)) inline bool probe (bool (*fn) (const msg_t &))
{ {
scoped_lock_t lock (sync); scoped_lock_t lock (sync);
return (*fn) (*front); return (*fn) (*front);
} }
private: private:
msg_t storage[2]; msg_t storage[2];
msg_t *back, *front; msg_t *back, *front;
mutex_t sync; mutex_t sync;
bool has_msg; bool has_msg;
// Disable copying of dbuffer. // Disable copying of dbuffer.
dbuffer_t (const dbuffer_t&); dbuffer_t (const dbuffer_t &);
const dbuffer_t &operator = (const dbuffer_t&); const dbuffer_t &operator= (const dbuffer_t &);
}; };
} }
#endif #endif

View File

@@ -67,12 +67,14 @@ void zmq::dealer_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
lb.attach (pipe_); lb.attach (pipe_);
} }
int zmq::dealer_t::xsetsockopt (int option_, const void *optval_, int zmq::dealer_t::xsetsockopt (int option_,
size_t optvallen_) const void *optval_,
size_t optvallen_)
{ {
bool is_int = (optvallen_ == sizeof (int)); bool is_int = (optvallen_ == sizeof (int));
int value = 0; int value = 0;
if (is_int) memcpy(&value, optval_, sizeof (int)); if (is_int)
memcpy (&value, optval_, sizeof (int));
switch (option_) { switch (option_) {
case ZMQ_PROBE_ROUTER: case ZMQ_PROBE_ROUTER:

View File

@@ -37,53 +37,47 @@
namespace zmq namespace zmq
{ {
class ctx_t;
class msg_t;
class pipe_t;
class io_thread_t;
class socket_base_t;
class ctx_t; class dealer_t : public socket_base_t
class msg_t; {
class pipe_t; public:
class io_thread_t; dealer_t (zmq::ctx_t *parent_, uint32_t tid_, int sid);
class socket_base_t; ~dealer_t ();
class dealer_t : protected:
public socket_base_t // Overrides of functions from socket_base_t.
{ void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
public: int xsetsockopt (int option_, const void *optval_, size_t optvallen_);
int xsend (zmq::msg_t *msg_);
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
bool xhas_out ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
dealer_t (zmq::ctx_t *parent_, uint32_t tid_, int sid); // Send and recv - knowing which pipe was used.
~dealer_t (); int sendpipe (zmq::msg_t *msg_, zmq::pipe_t **pipe_);
int recvpipe (zmq::msg_t *msg_, zmq::pipe_t **pipe_);
protected: private:
// Messages are fair-queued from inbound pipes. And load-balanced to
// the outbound pipes.
fq_t fq;
lb_t lb;
// Overrides of functions from socket_base_t. // if true, send an empty message to every connected router peer
void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_); bool probe_router;
int xsetsockopt (int option_, const void *optval_, size_t optvallen_);
int xsend (zmq::msg_t *msg_);
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
bool xhas_out ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
// Send and recv - knowing which pipe was used.
int sendpipe (zmq::msg_t *msg_, zmq::pipe_t **pipe_);
int recvpipe (zmq::msg_t *msg_, zmq::pipe_t **pipe_);
private:
// Messages are fair-queued from inbound pipes. And load-balanced to
// the outbound pipes.
fq_t fq;
lb_t lb;
// if true, send an empty message to every connected router peer
bool probe_router;
dealer_t (const dealer_t&);
const dealer_t &operator = (const dealer_t&);
};
dealer_t (const dealer_t &);
const dealer_t &operator= (const dealer_t &);
};
} }
#endif #endif

View File

@@ -42,157 +42,152 @@
namespace zmq namespace zmq
{ {
// Helper base class for decoders that know the amount of data to read // Helper base class for decoders that know the amount of data to read
// in advance at any moment. Knowing the amount in advance is a property // in advance at any moment. Knowing the amount in advance is a property
// of the protocol used. 0MQ framing protocol is based size-prefixed // of the protocol used. 0MQ framing protocol is based size-prefixed
// paradigm, which qualifies it to be parsed by this class. // paradigm, which qualifies it to be parsed by this class.
// On the other hand, XML-based transports (like XMPP or SOAP) don't allow // On the other hand, XML-based transports (like XMPP or SOAP) don't allow
// for knowing the size of data to read in advance and should use different // for knowing the size of data to read in advance and should use different
// decoding algorithms. // decoding algorithms.
// //
// This class implements the state machine that parses the incoming buffer. // This class implements the state machine that parses the incoming buffer.
// Derived class should implement individual state machine actions. // Derived class should implement individual state machine actions.
// //
// Buffer management is done by an allocator policy. // Buffer management is done by an allocator policy.
template <typename T, typename A = c_single_allocator> template <typename T, typename A = c_single_allocator>
class decoder_base_t : public i_decoder class decoder_base_t : public i_decoder
{
public:
explicit decoder_base_t (A *allocator_) :
next (NULL),
read_pos (NULL),
to_read (0),
allocator (allocator_)
{ {
public: buf = allocator->allocate ();
}
explicit decoder_base_t (A *allocator_) : // The destructor doesn't have to be virtual. It is made virtual
next (NULL), // just to keep ICC and code checking tools from complaining.
read_pos (NULL), virtual ~decoder_base_t () { allocator->deallocate (); }
to_read (0),
allocator(allocator_) // Returns a buffer to be filled with binary data.
{ void get_buffer (unsigned char **data_, std::size_t *size_)
buf = allocator->allocate (); {
buf = allocator->allocate ();
// If we are expected to read large message, we'll opt for zero-
// copy, i.e. we'll ask caller to fill the data directly to the
// message. Note that subsequent read(s) are non-blocking, thus
// each single read reads at most SO_RCVBUF bytes at once not
// depending on how large is the chunk returned from here.
// As a consequence, large messages being received won't block
// other engines running in the same I/O thread for excessive
// amounts of time.
if (to_read >= allocator->size ()) {
*data_ = read_pos;
*size_ = to_read;
return;
} }
// The destructor doesn't have to be virtual. It is made virtual *data_ = buf;
// just to keep ICC and code checking tools from complaining. *size_ = allocator->size ();
virtual ~decoder_base_t () }
{
allocator->deallocate ();
}
// Returns a buffer to be filled with binary data. // Processes the data in the buffer previously allocated using
void get_buffer (unsigned char **data_, std::size_t *size_) // get_buffer function. size_ argument specifies number of bytes
{ // actually filled into the buffer. Function returns 1 when the
buf = allocator->allocate (); // whole message was decoded or 0 when more data is required.
// On error, -1 is returned and errno set accordingly.
// Number of bytes processed is returned in bytes_used_.
int decode (const unsigned char *data_,
std::size_t size_,
std::size_t &bytes_used_)
{
bytes_used_ = 0;
// If we are expected to read large message, we'll opt for zero- // In case of zero-copy simply adjust the pointers, no copying
// copy, i.e. we'll ask caller to fill the data directly to the // is required. Also, run the state machine in case all the data
// message. Note that subsequent read(s) are non-blocking, thus // were processed.
// each single read reads at most SO_RCVBUF bytes at once not if (data_ == read_pos) {
// depending on how large is the chunk returned from here. zmq_assert (size_ <= to_read);
// As a consequence, large messages being received won't block read_pos += size_;
// other engines running in the same I/O thread for excessive to_read -= size_;
// amounts of time. bytes_used_ = size_;
if (to_read >= allocator->size ()) {
*data_ = read_pos; while (!to_read) {
*size_ = to_read; const int rc =
return; (static_cast<T *> (this)->*next) (data_ + bytes_used_);
if (rc != 0)
return rc;
} }
*data_ = buf;
*size_ = allocator->size ();
}
// Processes the data in the buffer previously allocated using
// get_buffer function. size_ argument specifies number of bytes
// actually filled into the buffer. Function returns 1 when the
// whole message was decoded or 0 when more data is required.
// On error, -1 is returned and errno set accordingly.
// Number of bytes processed is returned in bytes_used_.
int decode (const unsigned char *data_, std::size_t size_,
std::size_t &bytes_used_)
{
bytes_used_ = 0;
// In case of zero-copy simply adjust the pointers, no copying
// is required. Also, run the state machine in case all the data
// were processed.
if (data_ == read_pos) {
zmq_assert (size_ <= to_read);
read_pos += size_;
to_read -= size_;
bytes_used_ = size_;
while (!to_read) {
const int rc =
(static_cast <T *> (this)->*next) (data_ + bytes_used_);
if (rc != 0)
return rc;
}
return 0;
}
while (bytes_used_ < size_) {
// Copy the data from buffer to the message.
const size_t to_copy = std::min (to_read, size_ - bytes_used_);
// Only copy when destination address is different from the
// current address in the buffer.
if (read_pos != data_ + bytes_used_) {
memcpy (read_pos, data_ + bytes_used_, to_copy);
}
read_pos += to_copy;
to_read -= to_copy;
bytes_used_ += to_copy;
// Try to get more space in the message to fill in.
// If none is available, return.
while (to_read == 0) {
// pass current address in the buffer
const int rc =
(static_cast <T *> (this)->*next) (data_ + bytes_used_);
if (rc != 0)
return rc;
}
}
return 0; return 0;
} }
virtual void resize_buffer (std::size_t new_size) while (bytes_used_ < size_) {
{ // Copy the data from buffer to the message.
allocator->resize (new_size); const size_t to_copy = std::min (to_read, size_ - bytes_used_);
// Only copy when destination address is different from the
// current address in the buffer.
if (read_pos != data_ + bytes_used_) {
memcpy (read_pos, data_ + bytes_used_, to_copy);
}
read_pos += to_copy;
to_read -= to_copy;
bytes_used_ += to_copy;
// Try to get more space in the message to fill in.
// If none is available, return.
while (to_read == 0) {
// pass current address in the buffer
const int rc =
(static_cast<T *> (this)->*next) (data_ + bytes_used_);
if (rc != 0)
return rc;
}
} }
protected: return 0;
}
// Prototype of state machine action. Action should return false if virtual void resize_buffer (std::size_t new_size)
// it is unable to push the data to the system. {
typedef int (T:: *step_t) (unsigned char const *); allocator->resize (new_size);
}
// This function should be called from derived class to read data protected:
// from the buffer and schedule next state machine action. // Prototype of state machine action. Action should return false if
void next_step (void *read_pos_, std::size_t to_read_, step_t next_) // it is unable to push the data to the system.
{ typedef int (T::*step_t) (unsigned char const *);
read_pos = static_cast <unsigned char*> (read_pos_);
to_read = to_read_;
next = next_;
}
private: // This function should be called from derived class to read data
// from the buffer and schedule next state machine action.
void next_step (void *read_pos_, std::size_t to_read_, step_t next_)
{
read_pos = static_cast<unsigned char *> (read_pos_);
to_read = to_read_;
next = next_;
}
// Next step. If set to NULL, it means that associated data stream private:
// is dead. Note that there can be still data in the process in such // Next step. If set to NULL, it means that associated data stream
// case. // is dead. Note that there can be still data in the process in such
step_t next; // case.
step_t next;
// Where to store the read data. // Where to store the read data.
unsigned char *read_pos; unsigned char *read_pos;
// How much data to read before taking next step. // How much data to read before taking next step.
std::size_t to_read; std::size_t to_read;
// The duffer for data to decode. // The duffer for data to decode.
A *allocator; A *allocator;
unsigned char *buf; unsigned char *buf;
decoder_base_t (const decoder_base_t &); decoder_base_t (const decoder_base_t &);
const decoder_base_t &operator = (const decoder_base_t &); const decoder_base_t &operator= (const decoder_base_t &);
}; };
} }
#endif #endif

View File

@@ -34,34 +34,39 @@
#include "msg.hpp" #include "msg.hpp"
zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_) : zmq::shared_message_memory_allocator::shared_message_memory_allocator (
buf(NULL), std::size_t bufsize_) :
bufsize(0), buf (NULL),
max_size(bufsize_), bufsize (0),
msg_content(NULL), max_size (bufsize_),
maxCounters (static_cast <size_t> (std::ceil (static_cast <double> (max_size) / static_cast <double> (msg_t::max_vsm_size)))) msg_content (NULL),
maxCounters (static_cast<size_t> (
std::ceil (static_cast<double> (max_size)
/ static_cast<double> (msg_t::max_vsm_size))))
{ {
} }
zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages) : zmq::shared_message_memory_allocator::shared_message_memory_allocator (
buf(NULL), std::size_t bufsize_, std::size_t maxMessages) :
bufsize(0), buf (NULL),
max_size(bufsize_), bufsize (0),
msg_content(NULL), max_size (bufsize_),
maxCounters(maxMessages) msg_content (NULL),
maxCounters (maxMessages)
{ {
} }
zmq::shared_message_memory_allocator::~shared_message_memory_allocator () zmq::shared_message_memory_allocator::~shared_message_memory_allocator ()
{ {
deallocate(); deallocate ();
} }
unsigned char* zmq::shared_message_memory_allocator::allocate () unsigned char *zmq::shared_message_memory_allocator::allocate ()
{ {
if (buf) { if (buf) {
// release reference count to couple lifetime to messages // release reference count to couple lifetime to messages
zmq::atomic_counter_t* c = reinterpret_cast<zmq::atomic_counter_t* >(buf); zmq::atomic_counter_t *c =
reinterpret_cast<zmq::atomic_counter_t *> (buf);
// if refcnt drops to 0, there are no message using the buffer // if refcnt drops to 0, there are no message using the buffer
// because either all messages have been closed or only vsm-messages // because either all messages have been closed or only vsm-messages
@@ -77,36 +82,38 @@ unsigned char* zmq::shared_message_memory_allocator::allocate ()
if (!buf) { if (!buf) {
// allocate memory for reference counters together with reception buffer // allocate memory for reference counters together with reception buffer
std::size_t const allocationsize = std::size_t const allocationsize =
max_size + sizeof (zmq::atomic_counter_t) + max_size + sizeof (zmq::atomic_counter_t)
maxCounters * sizeof (zmq::msg_t::content_t); + maxCounters * sizeof (zmq::msg_t::content_t);
buf = static_cast <unsigned char *> (std::malloc (allocationsize)); buf = static_cast<unsigned char *> (std::malloc (allocationsize));
alloc_assert (buf); alloc_assert (buf);
new (buf) atomic_counter_t (1); new (buf) atomic_counter_t (1);
} else { } else {
// release reference count to couple lifetime to messages // release reference count to couple lifetime to messages
zmq::atomic_counter_t *c = reinterpret_cast <zmq::atomic_counter_t *> (buf); zmq::atomic_counter_t *c =
reinterpret_cast<zmq::atomic_counter_t *> (buf);
c->set (1); c->set (1);
} }
bufsize = max_size; bufsize = max_size;
msg_content = reinterpret_cast <zmq::msg_t::content_t*> (buf + sizeof (atomic_counter_t) + max_size); msg_content = reinterpret_cast<zmq::msg_t::content_t *> (
buf + sizeof (atomic_counter_t) + max_size);
return buf + sizeof (zmq::atomic_counter_t); return buf + sizeof (zmq::atomic_counter_t);
} }
void zmq::shared_message_memory_allocator::deallocate () void zmq::shared_message_memory_allocator::deallocate ()
{ {
zmq::atomic_counter_t* c = reinterpret_cast<zmq::atomic_counter_t* >(buf); zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *> (buf);
if (buf && !c->sub(1)) { if (buf && !c->sub (1)) {
std::free(buf); std::free (buf);
} }
release(); release ();
} }
unsigned char* zmq::shared_message_memory_allocator::release () unsigned char *zmq::shared_message_memory_allocator::release ()
{ {
unsigned char* b = buf; unsigned char *b = buf;
buf = NULL; buf = NULL;
bufsize = 0; bufsize = 0;
msg_content = NULL; msg_content = NULL;
@@ -116,14 +123,14 @@ unsigned char* zmq::shared_message_memory_allocator::release ()
void zmq::shared_message_memory_allocator::inc_ref () void zmq::shared_message_memory_allocator::inc_ref ()
{ {
(reinterpret_cast <zmq::atomic_counter_t*> (buf))->add (1); (reinterpret_cast<zmq::atomic_counter_t *> (buf))->add (1);
} }
void zmq::shared_message_memory_allocator::call_dec_ref(void*, void* hint) void zmq::shared_message_memory_allocator::call_dec_ref (void *, void *hint)
{ {
zmq_assert (hint); zmq_assert (hint);
unsigned char* buf = static_cast <unsigned char*> (hint); unsigned char *buf = static_cast<unsigned char *> (hint);
zmq::atomic_counter_t* c = reinterpret_cast <zmq::atomic_counter_t*> (buf); zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *> (buf);
if (!c->sub (1)) { if (!c->sub (1)) {
c->~atomic_counter_t (); c->~atomic_counter_t ();
@@ -138,7 +145,7 @@ std::size_t zmq::shared_message_memory_allocator::size () const
return bufsize; return bufsize;
} }
unsigned char* zmq::shared_message_memory_allocator::data () unsigned char *zmq::shared_message_memory_allocator::data ()
{ {
return buf + sizeof (zmq::atomic_counter_t); return buf + sizeof (zmq::atomic_counter_t);
} }

View File

@@ -39,117 +39,93 @@
namespace zmq namespace zmq
{ {
// Static buffer policy. // Static buffer policy.
class c_single_allocator class c_single_allocator
{
public:
explicit c_single_allocator (std::size_t bufsize_) :
bufsize (bufsize_),
buf (static_cast<unsigned char *> (std::malloc (bufsize)))
{ {
public: alloc_assert (buf);
explicit c_single_allocator (std::size_t bufsize_) : }
bufsize(bufsize_),
buf(static_cast <unsigned char*> (std::malloc (bufsize)))
{
alloc_assert (buf);
}
~c_single_allocator () ~c_single_allocator () { std::free (buf); }
{
std::free (buf);
}
unsigned char* allocate () unsigned char *allocate () { return buf; }
{
return buf;
}
void deallocate () void deallocate () {}
{
}
std::size_t size () const std::size_t size () const { return bufsize; }
{
return bufsize;
}
void resize (std::size_t new_size) void resize (std::size_t new_size) { bufsize = new_size; }
{
bufsize = new_size;
}
private:
std::size_t bufsize;
unsigned char* buf;
c_single_allocator (c_single_allocator const&); private:
c_single_allocator& operator = (c_single_allocator const&); std::size_t bufsize;
}; unsigned char *buf;
// This allocator allocates a reference counted buffer which is used by v2_decoder_t c_single_allocator (c_single_allocator const &);
// to use zero-copy msg::init_data to create messages with memory from this buffer as c_single_allocator &operator= (c_single_allocator const &);
// data storage. };
// This allocator allocates a reference counted buffer which is used by v2_decoder_t
// to use zero-copy msg::init_data to create messages with memory from this buffer as
// data storage.
//
// The buffer is allocated with a reference count of 1 to make sure that is is alive while
// decoding messages. Otherwise, it is possible that e.g. the first message increases the count
// from zero to one, gets passed to the user application, processed in the user thread and deleted
// which would then deallocate the buffer. The drawback is that the buffer may be allocated longer
// than necessary because it is only deleted when allocate is called the next time.
class shared_message_memory_allocator
{
public:
explicit shared_message_memory_allocator (std::size_t bufsize_);
// Create an allocator for a maximum number of messages
shared_message_memory_allocator (std::size_t bufsize_,
std::size_t maxMessages);
~shared_message_memory_allocator ();
// Allocate a new buffer
// //
// The buffer is allocated with a reference count of 1 to make sure that is is alive while // This releases the current buffer to be bound to the lifetime of the messages
// decoding messages. Otherwise, it is possible that e.g. the first message increases the count // created on this buffer.
// from zero to one, gets passed to the user application, processed in the user thread and deleted unsigned char *allocate ();
// which would then deallocate the buffer. The drawback is that the buffer may be allocated longer
// than necessary because it is only deleted when allocate is called the next time.
class shared_message_memory_allocator
{
public:
explicit shared_message_memory_allocator (std::size_t bufsize_);
// Create an allocator for a maximum number of messages // force deallocation of buffer.
shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages); void deallocate ();
~shared_message_memory_allocator (); // Give up ownership of the buffer. The buffer's lifetime is now coupled to
// the messages constructed on top of it.
unsigned char *release ();
// Allocate a new buffer void inc_ref ();
//
// This releases the current buffer to be bound to the lifetime of the messages
// created on this buffer.
unsigned char* allocate ();
// force deallocation of buffer. static void call_dec_ref (void *, void *buffer);
void deallocate ();
// Give up ownership of the buffer. The buffer's lifetime is now coupled to std::size_t size () const;
// the messages constructed on top of it.
unsigned char* release ();
void inc_ref (); // Return pointer to the first message data byte.
unsigned char *data ();
static void call_dec_ref (void*, void* buffer); // Return pointer to the first byte of the buffer.
unsigned char *buffer () { return buf; }
std::size_t size () const; void resize (std::size_t new_size) { bufsize = new_size; }
// Return pointer to the first message data byte. zmq::msg_t::content_t *provide_content () { return msg_content; }
unsigned char* data ();
// Return pointer to the first byte of the buffer. void advance_content () { msg_content++; }
unsigned char* buffer ()
{
return buf;
}
void resize (std::size_t new_size) private:
{ unsigned char *buf;
bufsize = new_size; std::size_t bufsize;
} std::size_t max_size;
zmq::msg_t::content_t *msg_content;
zmq::msg_t::content_t* provide_content () std::size_t maxCounters;
{ };
return msg_content;
}
void advance_content ()
{
msg_content++;
}
private:
unsigned char* buf;
std::size_t bufsize;
std::size_t max_size;
zmq::msg_t::content_t* msg_content;
std::size_t maxCounters;
};
} }
#endif #endif

View File

@@ -47,7 +47,7 @@
#include "i_poll_events.hpp" #include "i_poll_events.hpp"
zmq::devpoll_t::devpoll_t (const zmq::ctx_t &ctx_) : zmq::devpoll_t::devpoll_t (const zmq::ctx_t &ctx_) :
ctx(ctx_), ctx (ctx_),
stopping (false) stopping (false)
{ {
devpoll_fd = open ("/dev/poll", O_RDWR); devpoll_fd = open ("/dev/poll", O_RDWR);
@@ -68,24 +68,24 @@ void zmq::devpoll_t::devpoll_ctl (fd_t fd_, short events_)
} }
zmq::devpoll_t::handle_t zmq::devpoll_t::add_fd (fd_t fd_, zmq::devpoll_t::handle_t zmq::devpoll_t::add_fd (fd_t fd_,
i_poll_events *reactor_) i_poll_events *reactor_)
{ {
// If the file descriptor table is too small expand it. // If the file descriptor table is too small expand it.
fd_table_t::size_type sz = fd_table.size (); fd_table_t::size_type sz = fd_table.size ();
if (sz <= (fd_table_t::size_type) fd_) { if (sz <= (fd_table_t::size_type) fd_) {
fd_table.resize (fd_ + 1); fd_table.resize (fd_ + 1);
while (sz != (fd_table_t::size_type) (fd_ + 1)) { while (sz != (fd_table_t::size_type) (fd_ + 1)) {
fd_table [sz].valid = false; fd_table[sz].valid = false;
++sz; ++sz;
} }
} }
zmq_assert (!fd_table [fd_].valid); zmq_assert (!fd_table[fd_].valid);
fd_table [fd_].events = 0; fd_table[fd_].events = 0;
fd_table [fd_].reactor = reactor_; fd_table[fd_].reactor = reactor_;
fd_table [fd_].valid = true; fd_table[fd_].valid = true;
fd_table [fd_].accepted = false; fd_table[fd_].accepted = false;
devpoll_ctl (fd_, 0); devpoll_ctl (fd_, 0);
pending_list.push_back (fd_); pending_list.push_back (fd_);
@@ -98,10 +98,10 @@ zmq::devpoll_t::handle_t zmq::devpoll_t::add_fd (fd_t fd_,
void zmq::devpoll_t::rm_fd (handle_t handle_) void zmq::devpoll_t::rm_fd (handle_t handle_)
{ {
zmq_assert (fd_table [handle_].valid); zmq_assert (fd_table[handle_].valid);
devpoll_ctl (handle_, POLLREMOVE); devpoll_ctl (handle_, POLLREMOVE);
fd_table [handle_].valid = false; fd_table[handle_].valid = false;
// Decrease the load metric of the thread. // Decrease the load metric of the thread.
adjust_load (-1); adjust_load (-1);
@@ -110,29 +110,29 @@ void zmq::devpoll_t::rm_fd (handle_t handle_)
void zmq::devpoll_t::set_pollin (handle_t handle_) void zmq::devpoll_t::set_pollin (handle_t handle_)
{ {
devpoll_ctl (handle_, POLLREMOVE); devpoll_ctl (handle_, POLLREMOVE);
fd_table [handle_].events |= POLLIN; fd_table[handle_].events |= POLLIN;
devpoll_ctl (handle_, fd_table [handle_].events); devpoll_ctl (handle_, fd_table[handle_].events);
} }
void zmq::devpoll_t::reset_pollin (handle_t handle_) void zmq::devpoll_t::reset_pollin (handle_t handle_)
{ {
devpoll_ctl (handle_, POLLREMOVE); devpoll_ctl (handle_, POLLREMOVE);
fd_table [handle_].events &= ~((short) POLLIN); fd_table[handle_].events &= ~((short) POLLIN);
devpoll_ctl (handle_, fd_table [handle_].events); devpoll_ctl (handle_, fd_table[handle_].events);
} }
void zmq::devpoll_t::set_pollout (handle_t handle_) void zmq::devpoll_t::set_pollout (handle_t handle_)
{ {
devpoll_ctl (handle_, POLLREMOVE); devpoll_ctl (handle_, POLLREMOVE);
fd_table [handle_].events |= POLLOUT; fd_table[handle_].events |= POLLOUT;
devpoll_ctl (handle_, fd_table [handle_].events); devpoll_ctl (handle_, fd_table[handle_].events);
} }
void zmq::devpoll_t::reset_pollout (handle_t handle_) void zmq::devpoll_t::reset_pollout (handle_t handle_)
{ {
devpoll_ctl (handle_, POLLREMOVE); devpoll_ctl (handle_, POLLREMOVE);
fd_table [handle_].events &= ~((short) POLLOUT); fd_table[handle_].events &= ~((short) POLLOUT);
devpoll_ctl (handle_, fd_table [handle_].events); devpoll_ctl (handle_, fd_table[handle_].events);
} }
void zmq::devpoll_t::start () void zmq::devpoll_t::start ()
@@ -153,12 +153,11 @@ int zmq::devpoll_t::max_fds ()
void zmq::devpoll_t::loop () void zmq::devpoll_t::loop ()
{ {
while (!stopping) { while (!stopping) {
struct pollfd ev_buf[max_io_events];
struct pollfd ev_buf [max_io_events];
struct dvpoll poll_req; struct dvpoll poll_req;
for (pending_list_t::size_type i = 0; i < pending_list.size (); i ++) for (pending_list_t::size_type i = 0; i < pending_list.size (); i++)
fd_table [pending_list [i]].accepted = true; fd_table[pending_list[i]].accepted = true;
pending_list.clear (); pending_list.clear ();
// Execute any due timers. // Execute any due timers.
@@ -166,7 +165,7 @@ void zmq::devpoll_t::loop ()
// Wait for events. // Wait for events.
// On Solaris, we can retrieve no more then (OPEN_MAX - 1) events. // On Solaris, we can retrieve no more then (OPEN_MAX - 1) events.
poll_req.dp_fds = &ev_buf [0]; poll_req.dp_fds = &ev_buf[0];
#if defined ZMQ_HAVE_SOLARIS #if defined ZMQ_HAVE_SOLARIS
poll_req.dp_nfds = std::min ((int) max_io_events, OPEN_MAX - 1); poll_req.dp_nfds = std::min ((int) max_io_events, OPEN_MAX - 1);
#else #else
@@ -178,20 +177,19 @@ void zmq::devpoll_t::loop ()
continue; continue;
errno_assert (n != -1); errno_assert (n != -1);
for (int i = 0; i < n; i ++) { for (int i = 0; i < n; i++) {
fd_entry_t *fd_ptr = &fd_table[ev_buf[i].fd];
fd_entry_t *fd_ptr = &fd_table [ev_buf [i].fd];
if (!fd_ptr->valid || !fd_ptr->accepted) if (!fd_ptr->valid || !fd_ptr->accepted)
continue; continue;
if (ev_buf [i].revents & (POLLERR | POLLHUP)) if (ev_buf[i].revents & (POLLERR | POLLHUP))
fd_ptr->reactor->in_event (); fd_ptr->reactor->in_event ();
if (!fd_ptr->valid || !fd_ptr->accepted) if (!fd_ptr->valid || !fd_ptr->accepted)
continue; continue;
if (ev_buf [i].revents & POLLOUT) if (ev_buf[i].revents & POLLOUT)
fd_ptr->reactor->out_event (); fd_ptr->reactor->out_event ();
if (!fd_ptr->valid || !fd_ptr->accepted) if (!fd_ptr->valid || !fd_ptr->accepted)
continue; continue;
if (ev_buf [i].revents & POLLIN) if (ev_buf[i].revents & POLLIN)
fd_ptr->reactor->in_event (); fd_ptr->reactor->in_event ();
} }
} }
@@ -199,7 +197,7 @@ void zmq::devpoll_t::loop ()
void zmq::devpoll_t::worker_routine (void *arg_) void zmq::devpoll_t::worker_routine (void *arg_)
{ {
((devpoll_t*) arg_)->loop (); ((devpoll_t *) arg_)->loop ();
} }
#endif #endif

View File

@@ -43,75 +43,71 @@
namespace zmq namespace zmq
{ {
struct i_poll_events;
struct i_poll_events; // Implements socket polling mechanism using the "/dev/poll" interface.
// Implements socket polling mechanism using the "/dev/poll" interface. class devpoll_t : public poller_base_t
{
public:
typedef fd_t handle_t;
class devpoll_t : public poller_base_t devpoll_t (const ctx_t &ctx_);
~devpoll_t ();
// "poller" concept.
handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void start ();
void stop ();
static int max_fds ();
private:
// Main worker thread routine.
static void worker_routine (void *arg_);
// Main event loop.
void loop ();
// Reference to ZMQ context.
const ctx_t &ctx;
// File descriptor referring to "/dev/poll" pseudo-device.
fd_t devpoll_fd;
struct fd_entry_t
{ {
public: short events;
zmq::i_poll_events *reactor;
typedef fd_t handle_t; bool valid;
bool accepted;
devpoll_t (const ctx_t &ctx_);
~devpoll_t ();
// "poller" concept.
handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void start ();
void stop ();
static int max_fds ();
private:
// Main worker thread routine.
static void worker_routine (void *arg_);
// Main event loop.
void loop ();
// Reference to ZMQ context.
const ctx_t &ctx;
// File descriptor referring to "/dev/poll" pseudo-device.
fd_t devpoll_fd;
struct fd_entry_t
{
short events;
zmq::i_poll_events *reactor;
bool valid;
bool accepted;
};
typedef std::vector <fd_entry_t> fd_table_t;
fd_table_t fd_table;
typedef std::vector <fd_t> pending_list_t;
pending_list_t pending_list;
// Pollset manipulation function.
void devpoll_ctl (fd_t fd_, short events_);
// If true, thread is in the process of shutting down.
bool stopping;
// Handle of the physical thread doing the I/O work.
thread_t worker;
devpoll_t (const devpoll_t&);
const devpoll_t &operator = (const devpoll_t&);
}; };
typedef devpoll_t poller_t; typedef std::vector<fd_entry_t> fd_table_t;
fd_table_t fd_table;
typedef std::vector<fd_t> pending_list_t;
pending_list_t pending_list;
// Pollset manipulation function.
void devpoll_ctl (fd_t fd_, short events_);
// If true, thread is in the process of shutting down.
bool stopping;
// Handle of the physical thread doing the I/O work.
thread_t worker;
devpoll_t (const devpoll_t &);
const devpoll_t &operator= (const devpoll_t &);
};
typedef devpoll_t poller_t;
} }
#endif #endif

View File

@@ -53,7 +53,7 @@ zmq::dgram_t::~dgram_t ()
void zmq::dgram_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_) void zmq::dgram_t::xattach_pipe (pipe_t *pipe_, bool subscribe_to_all_)
{ {
LIBZMQ_UNUSED(subscribe_to_all_); LIBZMQ_UNUSED (subscribe_to_all_);
zmq_assert (pipe_); zmq_assert (pipe_);
@@ -107,9 +107,7 @@ int zmq::dgram_t::xsend (msg_t *msg_)
// Expect one more message frame. // Expect one more message frame.
more_out = true; more_out = true;
} } else {
else {
// dgram messages are two part only, reject part if more is set // dgram messages are two part only, reject part if more is set
if (msg_->flags () & msg_t::more) { if (msg_->flags () & msg_t::more) {
errno = EINVAL; errno = EINVAL;
@@ -173,5 +171,5 @@ bool zmq::dgram_t::xhas_out ()
const zmq::blob_t &zmq::dgram_t::get_credential () const const zmq::blob_t &zmq::dgram_t::get_credential () const
{ {
return last_in? last_in->get_credential (): saved_credential; return last_in ? last_in->get_credential () : saved_credential;
} }

View File

@@ -36,46 +36,41 @@
namespace zmq namespace zmq
{ {
class ctx_t;
class msg_t;
class pipe_t;
class io_thread_t;
class ctx_t; class dgram_t : public socket_base_t
class msg_t; {
class pipe_t; public:
class io_thread_t; dgram_t (zmq::ctx_t *parent_, uint32_t tid_, int sid);
~dgram_t ();
class dgram_t : // Overrides of functions from socket_base_t.
public socket_base_t void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
{ int xsend (zmq::msg_t *msg_);
public: int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
bool xhas_out ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
dgram_t (zmq::ctx_t *parent_, uint32_t tid_, int sid); private:
~dgram_t (); zmq::pipe_t *pipe;
// Overrides of functions from socket_base_t. zmq::pipe_t *last_in;
void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
int xsend (zmq::msg_t *msg_);
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
bool xhas_out ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
private: blob_t saved_credential;
zmq::pipe_t *pipe; // If true, more outgoing message parts are expected.
bool more_out;
zmq::pipe_t *last_in;
blob_t saved_credential;
// If true, more outgoing message parts are expected.
bool more_out;
dgram_t (const dgram_t&);
const dgram_t &operator = (const dgram_t&);
};
dgram_t (const dgram_t &);
const dgram_t &operator= (const dgram_t &);
};
} }
#endif #endif

View File

@@ -88,7 +88,7 @@ void zmq::dish_t::xhiccuped (pipe_t *pipe_)
send_subscriptions (pipe_); send_subscriptions (pipe_);
} }
int zmq::dish_t::xjoin (const char* group_) int zmq::dish_t::xjoin (const char *group_)
{ {
std::string group = std::string (group_); std::string group = std::string (group_);
@@ -125,7 +125,7 @@ int zmq::dish_t::xjoin (const char* group_)
return rc; return rc;
} }
int zmq::dish_t::xleave (const char* group_) int zmq::dish_t::xleave (const char *group_)
{ {
std::string group = std::string (group_); std::string group = std::string (group_);
@@ -134,7 +134,8 @@ int zmq::dish_t::xleave (const char* group_)
return -1; return -1;
} }
subscriptions_t::iterator it = std::find (subscriptions.begin (), subscriptions.end (), group); subscriptions_t::iterator it =
std::find (subscriptions.begin (), subscriptions.end (), group);
if (it == subscriptions.end ()) { if (it == subscriptions.end ()) {
errno = EINVAL; errno = EINVAL;
@@ -186,7 +187,6 @@ int zmq::dish_t::xrecv (msg_t *msg_)
} }
while (true) { while (true) {
// Get a message using fair queueing algorithm. // Get a message using fair queueing algorithm.
int rc = fq.recv (msg_); int rc = fq.recv (msg_);
@@ -196,7 +196,8 @@ int zmq::dish_t::xrecv (msg_t *msg_)
return -1; return -1;
// Filtering non matching messages // Filtering non matching messages
subscriptions_t::iterator it = subscriptions.find (std::string(msg_->group ())); subscriptions_t::iterator it =
subscriptions.find (std::string (msg_->group ()));
if (it != subscriptions.end ()) if (it != subscriptions.end ())
return 0; return 0;
} }
@@ -221,7 +222,8 @@ bool zmq::dish_t::xhas_in ()
} }
// Filtering non matching messages // Filtering non matching messages
subscriptions_t::iterator it = subscriptions.find (std::string(message.group ())); subscriptions_t::iterator it =
subscriptions.find (std::string (message.group ()));
if (it != subscriptions.end ()) { if (it != subscriptions.end ()) {
has_message = true; has_message = true;
return true; return true;
@@ -236,12 +238,13 @@ const zmq::blob_t &zmq::dish_t::get_credential () const
void zmq::dish_t::send_subscriptions (pipe_t *pipe_) void zmq::dish_t::send_subscriptions (pipe_t *pipe_)
{ {
for (subscriptions_t::iterator it = subscriptions.begin (); it != subscriptions.end (); ++it) { for (subscriptions_t::iterator it = subscriptions.begin ();
it != subscriptions.end (); ++it) {
msg_t msg; msg_t msg;
int rc = msg.init_join (); int rc = msg.init_join ();
errno_assert (rc == 0); errno_assert (rc == 0);
rc = msg.set_group (it->c_str()); rc = msg.set_group (it->c_str ());
errno_assert (rc == 0); errno_assert (rc == 0);
// Send it to the pipe. // Send it to the pipe.
@@ -252,9 +255,11 @@ void zmq::dish_t::send_subscriptions (pipe_t *pipe_)
pipe_->flush (); pipe_->flush ();
} }
zmq::dish_session_t::dish_session_t (io_thread_t *io_thread_, bool connect_, zmq::dish_session_t::dish_session_t (io_thread_t *io_thread_,
socket_base_t *socket_, const options_t &options_, bool connect_,
address_t *addr_) : socket_base_t *socket_,
const options_t &options_,
address_t *addr_) :
session_base_t (io_thread_, connect_, socket_, options_, addr_), session_base_t (io_thread_, connect_, socket_, options_, addr_),
state (group) state (group)
{ {
@@ -267,12 +272,12 @@ zmq::dish_session_t::~dish_session_t ()
int zmq::dish_session_t::push_msg (msg_t *msg_) int zmq::dish_session_t::push_msg (msg_t *msg_)
{ {
if (state == group) { if (state == group) {
if ((msg_->flags() & msg_t::more) != msg_t::more) { if ((msg_->flags () & msg_t::more) != msg_t::more) {
errno = EFAULT; errno = EFAULT;
return -1; return -1;
} }
if (msg_->size() > ZMQ_GROUP_MAX_LENGTH) { if (msg_->size () > ZMQ_GROUP_MAX_LENGTH) {
errno = EFAULT; errno = EFAULT;
return -1; return -1;
} }
@@ -283,23 +288,22 @@ int zmq::dish_session_t::push_msg (msg_t *msg_)
int rc = msg_->init (); int rc = msg_->init ();
errno_assert (rc == 0); errno_assert (rc == 0);
return 0; return 0;
} } else {
else { const char *group_setting = msg_->group ();
const char *group_setting = msg_->group();
int rc; int rc;
if(group_setting[0] != 0) if (group_setting[0] != 0)
goto has_group; goto has_group;
// Set the message group // Set the message group
rc = msg_->set_group ((char*)group_msg.data (), group_msg. size()); rc = msg_->set_group ((char *) group_msg.data (), group_msg.size ());
errno_assert (rc == 0); errno_assert (rc == 0);
// We set the group, so we don't need the group_msg anymore // We set the group, so we don't need the group_msg anymore
rc = group_msg.close (); rc = group_msg.close ();
errno_assert (rc == 0); errno_assert (rc == 0);
has_group: has_group:
// Thread safe socket doesn't support multipart messages // Thread safe socket doesn't support multipart messages
if ((msg_->flags() & msg_t::more) == msg_t::more) { if ((msg_->flags () & msg_t::more) == msg_t::more) {
errno = EFAULT; errno = EFAULT;
return -1; return -1;
} }
@@ -331,19 +335,18 @@ int zmq::dish_session_t::pull_msg (msg_t *msg_)
if (msg_->is_join ()) { if (msg_->is_join ()) {
rc = command.init_size (group_length + 5); rc = command.init_size (group_length + 5);
errno_assert(rc == 0); errno_assert (rc == 0);
offset = 5; offset = 5;
memcpy (command.data (), "\4JOIN", 5); memcpy (command.data (), "\4JOIN", 5);
} } else {
else {
rc = command.init_size (group_length + 6); rc = command.init_size (group_length + 6);
errno_assert(rc == 0); errno_assert (rc == 0);
offset = 6; offset = 6;
memcpy (command.data (), "\5LEAVE", 6); memcpy (command.data (), "\5LEAVE", 6);
} }
command.set_flags (msg_t::command); command.set_flags (msg_t::command);
char* command_data = (char*)command.data (); char *command_data = (char *) command.data ();
// Copy the group // Copy the group
memcpy (command_data + offset, msg_->group (), group_length); memcpy (command_data + offset, msg_->group (), group_length);

View File

@@ -41,85 +41,81 @@
namespace zmq namespace zmq
{ {
class ctx_t;
class pipe_t;
class io_thread_t;
class ctx_t; class dish_t : public socket_base_t
class pipe_t; {
class io_thread_t; public:
dish_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
~dish_t ();
class dish_t : protected:
public socket_base_t // Overrides of functions from socket_base_t.
void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
int xsend (zmq::msg_t *msg_);
bool xhas_out ();
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xhiccuped (pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
int xjoin (const char *group_);
int xleave (const char *group_);
private:
// Send subscriptions to a pipe
void send_subscriptions (pipe_t *pipe_);
// Fair queueing object for inbound pipes.
fq_t fq;
// Object for distributing the subscriptions upstream.
dist_t dist;
// The repository of subscriptions.
typedef std::set<std::string> subscriptions_t;
subscriptions_t subscriptions;
// If true, 'message' contains a matching message to return on the
// next recv call.
bool has_message;
msg_t message;
dish_t (const dish_t &);
const dish_t &operator= (const dish_t &);
};
class dish_session_t : public session_base_t
{
public:
dish_session_t (zmq::io_thread_t *io_thread_,
bool connect_,
zmq::socket_base_t *socket_,
const options_t &options_,
address_t *addr_);
~dish_session_t ();
// Overrides of the functions from session_base_t.
int push_msg (msg_t *msg_);
int pull_msg (msg_t *msg_);
void reset ();
private:
enum
{ {
public: group,
body
} state;
dish_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_); msg_t group_msg;
~dish_t ();
protected:
// Overrides of functions from socket_base_t.
void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
int xsend (zmq::msg_t *msg_);
bool xhas_out ();
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xwrite_activated (zmq::pipe_t *pipe_);
void xhiccuped (pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
int xjoin (const char *group_);
int xleave (const char *group_);
private:
// Send subscriptions to a pipe
void send_subscriptions (pipe_t *pipe_);
// Fair queueing object for inbound pipes.
fq_t fq;
// Object for distributing the subscriptions upstream.
dist_t dist;
// The repository of subscriptions.
typedef std::set<std::string> subscriptions_t;
subscriptions_t subscriptions;
// If true, 'message' contains a matching message to return on the
// next recv call.
bool has_message;
msg_t message;
dish_t (const dish_t&);
const dish_t &operator = (const dish_t&);
};
class dish_session_t : public session_base_t
{
public:
dish_session_t (zmq::io_thread_t *io_thread_, bool connect_,
zmq::socket_base_t *socket_, const options_t &options_,
address_t *addr_);
~dish_session_t ();
// Overrides of the functions from session_base_t.
int push_msg (msg_t *msg_);
int pull_msg (msg_t *msg_);
void reset ();
private:
enum {
group,
body
} state;
msg_t group_msg;
dish_session_t (const dish_session_t&);
const dish_session_t &operator = (const dish_session_t&);
};
dish_session_t (const dish_session_t &);
const dish_session_t &operator= (const dish_session_t &);
};
} }
#endif #endif

View File

@@ -34,11 +34,7 @@
#include "msg.hpp" #include "msg.hpp"
#include "likely.hpp" #include "likely.hpp"
zmq::dist_t::dist_t () : zmq::dist_t::dist_t () : matching (0), active (0), eligible (0), more (false)
matching (0),
active (0),
eligible (0),
more (false)
{ {
} }
@@ -56,8 +52,7 @@ void zmq::dist_t::attach (pipe_t *pipe_)
pipes.push_back (pipe_); pipes.push_back (pipe_);
pipes.swap (eligible, pipes.size () - 1); pipes.swap (eligible, pipes.size () - 1);
eligible++; eligible++;
} } else {
else {
pipes.push_back (pipe_); pipes.push_back (pipe_);
pipes.swap (active, pipes.size () - 1); pipes.swap (active, pipes.size () - 1);
active++; active++;
@@ -85,14 +80,14 @@ void zmq::dist_t::reverse_match ()
pipes_t::size_type prev_matching = matching; pipes_t::size_type prev_matching = matching;
// Reset matching to 0 // Reset matching to 0
unmatch(); unmatch ();
// Mark all matching pipes as not matching and vice-versa. // Mark all matching pipes as not matching and vice-versa.
// To do this, push all pipes that are eligible but not // To do this, push all pipes that are eligible but not
// matched - i.e. between "matching" and "eligible" - // matched - i.e. between "matching" and "eligible" -
// to the beginning of the queue. // to the beginning of the queue.
for (pipes_t::size_type i = prev_matching; i < eligible; ++i) { for (pipes_t::size_type i = prev_matching; i < eligible; ++i) {
pipes.swap(i, matching++); pipes.swap (i, matching++);
} }
} }
@@ -173,9 +168,9 @@ void zmq::dist_t::distribute (msg_t *msg_)
if (msg_->is_vsm ()) { if (msg_->is_vsm ()) {
for (pipes_t::size_type i = 0; i < matching; ++i) for (pipes_t::size_type i = 0; i < matching; ++i)
if(!write (pipes [i], msg_)) if (!write (pipes[i], msg_))
--i; // Retry last write because index will have been swapped --i; // Retry last write because index will have been swapped
int rc = msg_->close(); int rc = msg_->close ();
errno_assert (rc == 0); errno_assert (rc == 0);
rc = msg_->init (); rc = msg_->init ();
errno_assert (rc == 0); errno_assert (rc == 0);
@@ -189,7 +184,7 @@ void zmq::dist_t::distribute (msg_t *msg_)
// Push copy of the message to each matching pipe. // Push copy of the message to each matching pipe.
int failed = 0; int failed = 0;
for (pipes_t::size_type i = 0; i < matching; ++i) for (pipes_t::size_type i = 0; i < matching; ++i)
if (!write (pipes [i], msg_)) { if (!write (pipes[i], msg_)) {
++failed; ++failed;
--i; // Retry last write because index will have been swapped --i; // Retry last write because index will have been swapped
} }
@@ -226,10 +221,8 @@ bool zmq::dist_t::write (pipe_t *pipe_, msg_t *msg_)
bool zmq::dist_t::check_hwm () bool zmq::dist_t::check_hwm ()
{ {
for (pipes_t::size_type i = 0; i < matching; ++i) for (pipes_t::size_type i = 0; i < matching; ++i)
if (!pipes [i]->check_hwm ()) if (!pipes[i]->check_hwm ())
return false; return false;
return true; return true;
} }

View File

@@ -37,84 +37,80 @@
namespace zmq namespace zmq
{ {
class pipe_t;
class msg_t;
class pipe_t; // Class manages a set of outbound pipes. It sends each messages to
class msg_t; // each of them.
class dist_t
{
public:
dist_t ();
~dist_t ();
// Class manages a set of outbound pipes. It sends each messages to // Adds the pipe to the distributor object.
// each of them. void attach (zmq::pipe_t *pipe_);
class dist_t
{
public:
dist_t (); // Activates pipe that have previously reached high watermark.
~dist_t (); void activated (zmq::pipe_t *pipe_);
// Adds the pipe to the distributor object. // Mark the pipe as matching. Subsequent call to send_to_matching
void attach (zmq::pipe_t *pipe_); // will send message also to this pipe.
void match (zmq::pipe_t *pipe_);
// Activates pipe that have previously reached high watermark. // Marks all pipes that are not matched as matched and vice-versa.
void activated (zmq::pipe_t *pipe_); void reverse_match ();
// Mark the pipe as matching. Subsequent call to send_to_matching // Mark all pipes as non-matching.
// will send message also to this pipe. void unmatch ();
void match (zmq::pipe_t *pipe_);
// Marks all pipes that are not matched as matched and vice-versa. // Removes the pipe from the distributor object.
void reverse_match(); void pipe_terminated (zmq::pipe_t *pipe_);
// Mark all pipes as non-matching. // Send the message to the matching outbound pipes.
void unmatch (); int send_to_matching (zmq::msg_t *msg_);
// Removes the pipe from the distributor object. // Send the message to all the outbound pipes.
void pipe_terminated (zmq::pipe_t *pipe_); int send_to_all (zmq::msg_t *msg_);
// Send the message to the matching outbound pipes. bool has_out ();
int send_to_matching (zmq::msg_t *msg_);
// Send the message to all the outbound pipes. // check HWM of all pipes matching
int send_to_all (zmq::msg_t *msg_); bool check_hwm ();
bool has_out (); private:
// Write the message to the pipe. Make the pipe inactive if writing
// fails. In such a case false is returned.
bool write (zmq::pipe_t *pipe_, zmq::msg_t *msg_);
// check HWM of all pipes matching // Put the message to all active pipes.
bool check_hwm (); void distribute (zmq::msg_t *msg_);
private: // List of outbound pipes.
typedef array_t<zmq::pipe_t, 2> pipes_t;
pipes_t pipes;
// Write the message to the pipe. Make the pipe inactive if writing // Number of all the pipes to send the next message to.
// fails. In such a case false is returned. pipes_t::size_type matching;
bool write (zmq::pipe_t *pipe_, zmq::msg_t *msg_);
// Put the message to all active pipes. // Number of active pipes. All the active pipes are located at the
void distribute (zmq::msg_t *msg_); // beginning of the pipes array. These are the pipes the messages
// can be sent to at the moment.
pipes_t::size_type active;
// List of outbound pipes. // Number of pipes eligible for sending messages to. This includes all
typedef array_t <zmq::pipe_t, 2> pipes_t; // the active pipes plus all the pipes that we can in theory send
pipes_t pipes; // messages to (the HWM is not yet reached), but sending a message
// to them would result in partial message being delivered, ie. message
// with initial parts missing.
pipes_t::size_type eligible;
// Number of all the pipes to send the next message to. // True if last we are in the middle of a multipart message.
pipes_t::size_type matching; bool more;
// Number of active pipes. All the active pipes are located at the
// beginning of the pipes array. These are the pipes the messages
// can be sent to at the moment.
pipes_t::size_type active;
// Number of pipes eligible for sending messages to. This includes all
// the active pipes plus all the pipes that we can in theory send
// messages to (the HWM is not yet reached), but sending a message
// to them would result in partial message being delivered, ie. message
// with initial parts missing.
pipes_t::size_type eligible;
// True if last we are in the middle of a multipart message.
bool more;
dist_t (const dist_t&);
const dist_t &operator = (const dist_t&);
};
dist_t (const dist_t &);
const dist_t &operator= (const dist_t &);
};
} }
#endif #endif

View File

@@ -47,143 +47,134 @@
namespace zmq namespace zmq
{ {
// Helper base class for encoders. It implements the state machine that
// fills the outgoing buffer. Derived classes should implement individual
// state machine actions.
// Helper base class for encoders. It implements the state machine that template <typename T> class encoder_base_t : public i_encoder
// fills the outgoing buffer. Derived classes should implement individual {
// state machine actions. public:
inline encoder_base_t (size_t bufsize_) :
template <typename T> class encoder_base_t : public i_encoder write_pos (0),
to_write (0),
next (NULL),
new_msg_flag (false),
bufsize (bufsize_),
in_progress (NULL)
{ {
public: buf = (unsigned char *) malloc (bufsize_);
alloc_assert (buf);
}
inline encoder_base_t (size_t bufsize_) : // The destructor doesn't have to be virtual. It is made virtual
write_pos(0), // just to keep ICC and code checking tools from complaining.
to_write(0), inline virtual ~encoder_base_t () { free (buf); }
next(NULL),
new_msg_flag(false),
bufsize (bufsize_),
in_progress (NULL)
{
buf = (unsigned char*) malloc (bufsize_);
alloc_assert (buf);
}
// The destructor doesn't have to be virtual. It is made virtual // The function returns a batch of binary data. The data
// just to keep ICC and code checking tools from complaining. // are filled to a supplied buffer. If no buffer is supplied (data_
inline virtual ~encoder_base_t () // points to NULL) decoder object will provide buffer of its own.
{ inline size_t encode (unsigned char **data_, size_t size_)
free (buf); {
} unsigned char *buffer = !*data_ ? buf : *data_;
size_t buffersize = !*data_ ? bufsize : size_;
// The function returns a batch of binary data. The data if (in_progress == NULL)
// are filled to a supplied buffer. If no buffer is supplied (data_ return 0;
// points to NULL) decoder object will provide buffer of its own.
inline size_t encode (unsigned char **data_, size_t size_)
{
unsigned char *buffer = !*data_ ? buf : *data_;
size_t buffersize = !*data_ ? bufsize : size_;
if (in_progress == NULL) size_t pos = 0;
return 0; while (pos < buffersize) {
// If there are no more data to return, run the state machine.
size_t pos = 0; // If there are still no data, return what we already have
while (pos < buffersize) { // in the buffer.
if (!to_write) {
// If there are no more data to return, run the state machine. if (new_msg_flag) {
// If there are still no data, return what we already have int rc = in_progress->close ();
// in the buffer. errno_assert (rc == 0);
if (!to_write) { rc = in_progress->init ();
if (new_msg_flag) { errno_assert (rc == 0);
int rc = in_progress->close (); in_progress = NULL;
errno_assert (rc == 0); break;
rc = in_progress->init ();
errno_assert (rc == 0);
in_progress = NULL;
break;
}
(static_cast <T*> (this)->*next) ();
} }
(static_cast<T *> (this)->*next) ();
// If there are no data in the buffer yet and we are able to
// fill whole buffer in a single go, let's use zero-copy.
// There's no disadvantage to it as we cannot stuck multiple
// messages into the buffer anyway. Note that subsequent
// write(s) are non-blocking, thus each single write writes
// at most SO_SNDBUF bytes at once not depending on how large
// is the chunk returned from here.
// As a consequence, large messages being sent won't block
// other engines running in the same I/O thread for excessive
// amounts of time.
if (!pos && !*data_ && to_write >= buffersize) {
*data_ = write_pos;
pos = to_write;
write_pos = NULL;
to_write = 0;
return pos;
}
// Copy data to the buffer. If the buffer is full, return.
size_t to_copy = std::min (to_write, buffersize - pos);
memcpy (buffer + pos, write_pos, to_copy);
pos += to_copy;
write_pos += to_copy;
to_write -= to_copy;
} }
*data_ = buffer; // If there are no data in the buffer yet and we are able to
return pos; // fill whole buffer in a single go, let's use zero-copy.
// There's no disadvantage to it as we cannot stuck multiple
// messages into the buffer anyway. Note that subsequent
// write(s) are non-blocking, thus each single write writes
// at most SO_SNDBUF bytes at once not depending on how large
// is the chunk returned from here.
// As a consequence, large messages being sent won't block
// other engines running in the same I/O thread for excessive
// amounts of time.
if (!pos && !*data_ && to_write >= buffersize) {
*data_ = write_pos;
pos = to_write;
write_pos = NULL;
to_write = 0;
return pos;
}
// Copy data to the buffer. If the buffer is full, return.
size_t to_copy = std::min (to_write, buffersize - pos);
memcpy (buffer + pos, write_pos, to_copy);
pos += to_copy;
write_pos += to_copy;
to_write -= to_copy;
} }
void load_msg (msg_t *msg_) *data_ = buffer;
{ return pos;
zmq_assert (in_progress == NULL); }
in_progress = msg_;
(static_cast <T*> (this)->*next) ();
}
protected: void load_msg (msg_t *msg_)
{
zmq_assert (in_progress == NULL);
in_progress = msg_;
(static_cast<T *> (this)->*next) ();
}
// Prototype of state machine action. protected:
typedef void (T::*step_t) (); // Prototype of state machine action.
typedef void (T::*step_t) ();
// This function should be called from derived class to write the data // This function should be called from derived class to write the data
// to the buffer and schedule next state machine action. // to the buffer and schedule next state machine action.
inline void next_step (void *write_pos_, size_t to_write_, inline void next_step (void *write_pos_,
step_t next_, bool new_msg_flag_) size_t to_write_,
{ step_t next_,
write_pos = (unsigned char*) write_pos_; bool new_msg_flag_)
to_write = to_write_; {
next = next_; write_pos = (unsigned char *) write_pos_;
new_msg_flag = new_msg_flag_; to_write = to_write_;
} next = next_;
new_msg_flag = new_msg_flag_;
}
private: private:
// Where to get the data to write from.
unsigned char *write_pos;
// Where to get the data to write from. // How much data to write before next step should be executed.
unsigned char *write_pos; size_t to_write;
// How much data to write before next step should be executed. // Next step. If set to NULL, it means that associated data stream
size_t to_write; // is dead.
step_t next;
// Next step. If set to NULL, it means that associated data stream bool new_msg_flag;
// is dead.
step_t next;
bool new_msg_flag; // The buffer for encoded data.
size_t bufsize;
unsigned char *buf;
// The buffer for encoded data. encoder_base_t (const encoder_base_t &);
size_t bufsize; void operator= (const encoder_base_t &);
unsigned char *buf;
encoder_base_t (const encoder_base_t&); protected:
void operator = (const encoder_base_t&); msg_t *in_progress;
};
protected:
msg_t *in_progress;
};
} }
#endif #endif

View File

@@ -44,9 +44,7 @@
#include "config.hpp" #include "config.hpp"
#include "i_poll_events.hpp" #include "i_poll_events.hpp"
zmq::epoll_t::epoll_t (const zmq::ctx_t &ctx_) : zmq::epoll_t::epoll_t (const zmq::ctx_t &ctx_) : ctx (ctx_), stopping (false)
ctx(ctx_),
stopping (false)
{ {
#ifdef ZMQ_USE_EPOLL_CLOEXEC #ifdef ZMQ_USE_EPOLL_CLOEXEC
// Setting this option result in sane behaviour when exec() functions // Setting this option result in sane behaviour when exec() functions
@@ -65,8 +63,9 @@ zmq::epoll_t::~epoll_t ()
worker.stop (); worker.stop ();
close (epoll_fd); close (epoll_fd);
for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) { for (retired_t::iterator it = retired.begin (); it != retired.end ();
LIBZMQ_DELETE(*it); ++it) {
LIBZMQ_DELETE (*it);
} }
} }
@@ -95,7 +94,7 @@ zmq::epoll_t::handle_t zmq::epoll_t::add_fd (fd_t fd_, i_poll_events *events_)
void zmq::epoll_t::rm_fd (handle_t handle_) void zmq::epoll_t::rm_fd (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
int rc = epoll_ctl (epoll_fd, EPOLL_CTL_DEL, pe->fd, &pe->ev); int rc = epoll_ctl (epoll_fd, EPOLL_CTL_DEL, pe->fd, &pe->ev);
errno_assert (rc != -1); errno_assert (rc != -1);
pe->fd = retired_fd; pe->fd = retired_fd;
@@ -109,7 +108,7 @@ void zmq::epoll_t::rm_fd (handle_t handle_)
void zmq::epoll_t::set_pollin (handle_t handle_) void zmq::epoll_t::set_pollin (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
pe->ev.events |= EPOLLIN; pe->ev.events |= EPOLLIN;
int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
errno_assert (rc != -1); errno_assert (rc != -1);
@@ -117,7 +116,7 @@ void zmq::epoll_t::set_pollin (handle_t handle_)
void zmq::epoll_t::reset_pollin (handle_t handle_) void zmq::epoll_t::reset_pollin (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
pe->ev.events &= ~((short) EPOLLIN); pe->ev.events &= ~((short) EPOLLIN);
int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
errno_assert (rc != -1); errno_assert (rc != -1);
@@ -125,7 +124,7 @@ void zmq::epoll_t::reset_pollin (handle_t handle_)
void zmq::epoll_t::set_pollout (handle_t handle_) void zmq::epoll_t::set_pollout (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
pe->ev.events |= EPOLLOUT; pe->ev.events |= EPOLLOUT;
int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
errno_assert (rc != -1); errno_assert (rc != -1);
@@ -133,7 +132,7 @@ void zmq::epoll_t::set_pollout (handle_t handle_)
void zmq::epoll_t::reset_pollout (handle_t handle_) void zmq::epoll_t::reset_pollout (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
pe->ev.events &= ~((short) EPOLLOUT); pe->ev.events &= ~((short) EPOLLOUT);
int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev); int rc = epoll_ctl (epoll_fd, EPOLL_CTL_MOD, pe->fd, &pe->ev);
errno_assert (rc != -1); errno_assert (rc != -1);
@@ -156,42 +155,42 @@ int zmq::epoll_t::max_fds ()
void zmq::epoll_t::loop () void zmq::epoll_t::loop ()
{ {
epoll_event ev_buf [max_io_events]; epoll_event ev_buf[max_io_events];
while (!stopping) { while (!stopping) {
// Execute any due timers. // Execute any due timers.
int timeout = (int) execute_timers (); int timeout = (int) execute_timers ();
// Wait for events. // Wait for events.
int n = epoll_wait (epoll_fd, &ev_buf [0], max_io_events, int n = epoll_wait (epoll_fd, &ev_buf[0], max_io_events,
timeout ? timeout : -1); timeout ? timeout : -1);
if (n == -1) { if (n == -1) {
errno_assert (errno == EINTR); errno_assert (errno == EINTR);
continue; continue;
} }
for (int i = 0; i < n; i ++) { for (int i = 0; i < n; i++) {
poll_entry_t *pe = ((poll_entry_t*) ev_buf [i].data.ptr); poll_entry_t *pe = ((poll_entry_t *) ev_buf[i].data.ptr);
if (pe->fd == retired_fd) if (pe->fd == retired_fd)
continue; continue;
if (ev_buf [i].events & (EPOLLERR | EPOLLHUP)) if (ev_buf[i].events & (EPOLLERR | EPOLLHUP))
pe->events->in_event (); pe->events->in_event ();
if (pe->fd == retired_fd) if (pe->fd == retired_fd)
continue; continue;
if (ev_buf [i].events & EPOLLOUT) if (ev_buf[i].events & EPOLLOUT)
pe->events->out_event (); pe->events->out_event ();
if (pe->fd == retired_fd) if (pe->fd == retired_fd)
continue; continue;
if (ev_buf [i].events & EPOLLIN) if (ev_buf[i].events & EPOLLIN)
pe->events->in_event (); pe->events->in_event ();
} }
// Destroy retired event sources. // Destroy retired event sources.
retired_sync.lock (); retired_sync.lock ();
for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) { for (retired_t::iterator it = retired.begin (); it != retired.end ();
LIBZMQ_DELETE(*it); ++it) {
LIBZMQ_DELETE (*it);
} }
retired.clear (); retired.clear ();
retired_sync.unlock (); retired_sync.unlock ();
@@ -200,7 +199,7 @@ void zmq::epoll_t::loop ()
void zmq::epoll_t::worker_routine (void *arg_) void zmq::epoll_t::worker_routine (void *arg_)
{ {
((epoll_t*) arg_)->loop (); ((epoll_t *) arg_)->loop ();
} }
#endif #endif

View File

@@ -45,73 +45,69 @@
namespace zmq namespace zmq
{ {
struct i_poll_events;
struct i_poll_events; // This class implements socket polling mechanism using the Linux-specific
// epoll mechanism.
// This class implements socket polling mechanism using the Linux-specific class epoll_t : public poller_base_t
// epoll mechanism. {
public:
typedef void *handle_t;
class epoll_t : public poller_base_t epoll_t (const ctx_t &ctx_);
~epoll_t ();
// "poller" concept.
handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void start ();
void stop ();
static int max_fds ();
private:
// Main worker thread routine.
static void worker_routine (void *arg_);
// Main event loop.
void loop ();
// Reference to ZMQ context.
const ctx_t &ctx;
// Main epoll file descriptor
fd_t epoll_fd;
struct poll_entry_t
{ {
public: fd_t fd;
epoll_event ev;
typedef void* handle_t; zmq::i_poll_events *events;
epoll_t (const ctx_t &ctx_);
~epoll_t ();
// "poller" concept.
handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void start ();
void stop ();
static int max_fds ();
private:
// Main worker thread routine.
static void worker_routine (void *arg_);
// Main event loop.
void loop ();
// Reference to ZMQ context.
const ctx_t &ctx;
// Main epoll file descriptor
fd_t epoll_fd;
struct poll_entry_t
{
fd_t fd;
epoll_event ev;
zmq::i_poll_events *events;
};
// List of retired event sources.
typedef std::vector <poll_entry_t*> retired_t;
retired_t retired;
// If true, thread is in the process of shutting down.
bool stopping;
// Handle of the physical thread doing the I/O work.
thread_t worker;
// Synchronisation of retired event sources
mutex_t retired_sync;
epoll_t (const epoll_t&);
const epoll_t &operator = (const epoll_t&);
}; };
typedef epoll_t poller_t; // List of retired event sources.
typedef std::vector<poll_entry_t *> retired_t;
retired_t retired;
// If true, thread is in the process of shutting down.
bool stopping;
// Handle of the physical thread doing the I/O work.
thread_t worker;
// Synchronisation of retired event sources
mutex_t retired_sync;
epoll_t (const epoll_t &);
const epoll_t &operator= (const epoll_t &);
};
typedef epoll_t poller_t;
} }
#endif #endif

View File

@@ -34,189 +34,209 @@ const char *zmq::errno_to_string (int errno_)
{ {
switch (errno_) { switch (errno_) {
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
case ENOTSUP: case ENOTSUP:
return "Not supported"; return "Not supported";
case EPROTONOSUPPORT: case EPROTONOSUPPORT:
return "Protocol not supported"; return "Protocol not supported";
case ENOBUFS: case ENOBUFS:
return "No buffer space available"; return "No buffer space available";
case ENETDOWN: case ENETDOWN:
return "Network is down"; return "Network is down";
case EADDRINUSE: case EADDRINUSE:
return "Address in use"; return "Address in use";
case EADDRNOTAVAIL: case EADDRNOTAVAIL:
return "Address not available"; return "Address not available";
case ECONNREFUSED: case ECONNREFUSED:
return "Connection refused"; return "Connection refused";
case EINPROGRESS: case EINPROGRESS:
return "Operation in progress"; return "Operation in progress";
#endif #endif
case EFSM: case EFSM:
return "Operation cannot be accomplished in current state"; return "Operation cannot be accomplished in current state";
case ENOCOMPATPROTO: case ENOCOMPATPROTO:
return "The protocol is not compatible with the socket type"; return "The protocol is not compatible with the socket type";
case ETERM: case ETERM:
return "Context was terminated"; return "Context was terminated";
case EMTHREAD: case EMTHREAD:
return "No thread available"; return "No thread available";
case EHOSTUNREACH: case EHOSTUNREACH:
return "Host unreachable"; return "Host unreachable";
default: default:
#if defined _MSC_VER #if defined _MSC_VER
#pragma warning (push) #pragma warning(push)
#pragma warning (disable:4996) #pragma warning(disable : 4996)
#endif #endif
return strerror (errno_); return strerror (errno_);
#if defined _MSC_VER #if defined _MSC_VER
#pragma warning (pop) #pragma warning(pop)
#endif #endif
} }
} }
void zmq::zmq_abort(const char *errmsg_) void zmq::zmq_abort (const char *errmsg_)
{ {
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
// Raise STATUS_FATAL_APP_EXIT. // Raise STATUS_FATAL_APP_EXIT.
ULONG_PTR extra_info [1]; ULONG_PTR extra_info[1];
extra_info [0] = (ULONG_PTR) errmsg_; extra_info[0] = (ULONG_PTR) errmsg_;
RaiseException (0x40000015, EXCEPTION_NONCONTINUABLE, 1, extra_info); RaiseException (0x40000015, EXCEPTION_NONCONTINUABLE, 1, extra_info);
#else #else
(void)errmsg_; (void) errmsg_;
print_backtrace(); print_backtrace ();
abort (); abort ();
#endif #endif
} }
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
const char *zmq::wsa_error() const char *zmq::wsa_error ()
{ {
return wsa_error_no (WSAGetLastError(), NULL); return wsa_error_no (WSAGetLastError (), NULL);
} }
const char *zmq::wsa_error_no (int no_, const char * wsae_wouldblock_string) const char *zmq::wsa_error_no (int no_, const char *wsae_wouldblock_string)
{ {
// TODO: It seems that list of Windows socket errors is longer than this. // TODO: It seems that list of Windows socket errors is longer than this.
// Investigate whether there's a way to convert it into the string // Investigate whether there's a way to convert it into the string
// automatically (wsaError->HRESULT->string?). // automatically (wsaError->HRESULT->string?).
return return (no_ == WSABASEERR)
(no_ == WSABASEERR) ? ? "No Error"
"No Error" : : (no_ == WSAEINTR)
(no_ == WSAEINTR) ? ? "Interrupted system call"
"Interrupted system call" : : (no_ == WSAEBADF)
(no_ == WSAEBADF) ? ? "Bad file number"
"Bad file number" : : (no_ == WSAEACCES)
(no_ == WSAEACCES) ? ? "Permission denied"
"Permission denied" : : (no_ == WSAEFAULT)
(no_ == WSAEFAULT) ? ? "Bad address"
"Bad address" : : (no_ == WSAEINVAL)
(no_ == WSAEINVAL) ? ? "Invalid argument"
"Invalid argument" : : (no_ == WSAEMFILE)
(no_ == WSAEMFILE) ? ? "Too many open files"
"Too many open files" : : (no_ == WSAEWOULDBLOCK)
(no_ == WSAEWOULDBLOCK) ? ? wsae_wouldblock_string
wsae_wouldblock_string : : (no_ == WSAEINPROGRESS)
(no_ == WSAEINPROGRESS) ? ? "Operation now in progress"
"Operation now in progress" : : (no_ == WSAEALREADY)
(no_ == WSAEALREADY) ? ? "Operation already in "
"Operation already in progress" : "progress"
(no_ == WSAENOTSOCK) ? : (no_ == WSAENOTSOCK)
"Socket operation on non-socket" : ? "Socket operation on "
(no_ == WSAEDESTADDRREQ) ? "non-socket"
"Destination address required" : : (no_ == WSAEDESTADDRREQ)
(no_ == WSAEMSGSIZE) ? ? "Destination "
"Message too long" : "address required"
(no_ == WSAEPROTOTYPE) ? : (no_ == WSAEMSGSIZE)
"Protocol wrong type for socket" : ? "Message too "
(no_ == WSAENOPROTOOPT) ? "long"
"Bad protocol option" : : (no_
(no_ == WSAEPROTONOSUPPORT) ? == WSAEPROTOTYPE)
"Protocol not supported" : ? "Protocol "
(no_ == WSAESOCKTNOSUPPORT) ? "wrong type "
"Socket type not supported" : "for socket"
(no_ == WSAEOPNOTSUPP) ? : (no_
"Operation not supported on socket" : == WSAENOPROTOOPT)
(no_ == WSAEPFNOSUPPORT) ? ? "Bad "
"Protocol family not supported" : "protoco"
(no_ == WSAEAFNOSUPPORT) ? "l "
"Address family not supported by protocol family" : "option"
(no_ == WSAEADDRINUSE) ? : (no_
"Address already in use" : == WSAEPROTONOSUPPORT)
(no_ == WSAEADDRNOTAVAIL) ? ? "Pro"
"Can't assign requested address" : "toc"
(no_ == WSAENETDOWN) ? "ol "
"Network is down" : "not"
(no_ == WSAENETUNREACH) ? " su"
"Network is unreachable" : "ppo"
(no_ == WSAENETRESET) ? "rte"
"Net dropped connection or reset" : "d"
(no_ == WSAECONNABORTED) ? : (no_
"Software caused connection abort" : == WSAESOCKTNOSUPPORT)
(no_ == WSAECONNRESET) ? ? "Socket type not supported"
"Connection reset by peer" : : (no_
(no_ == WSAENOBUFS) ? == WSAEOPNOTSUPP)
"No buffer space available" : ? "Operation not supported on socket"
(no_ == WSAEISCONN) ? : (no_
"Socket is already connected" : == WSAEPFNOSUPPORT)
(no_ == WSAENOTCONN) ? ? "Protocol family not supported"
"Socket is not connected" : : (no_
(no_ == WSAESHUTDOWN) ? == WSAEAFNOSUPPORT)
"Can't send after socket shutdown" : ? "Address family not supported by protocol family"
(no_ == WSAETOOMANYREFS) ? : (no_ == WSAEADDRINUSE) ? "Address already in use"
"Too many references can't splice" : : (no_ == WSAEADDRNOTAVAIL) ? "Can't assign requested address"
(no_ == WSAETIMEDOUT) ? : (no_ == WSAENETDOWN) ? "Network is down"
"Connection timed out" : : (no_ == WSAENETUNREACH) ? "Network is unreachable"
(no_ == WSAECONNREFUSED) ? : (no_ == WSAENETRESET) ? "Net dropped connection or reset"
"Connection refused" : : (no_ == WSAECONNABORTED) ? "Software caused connection abort"
(no_ == WSAELOOP) ? : (no_ == WSAECONNRESET) ? "Connection reset by peer"
"Too many levels of symbolic links" : : (no_
(no_ == WSAENAMETOOLONG) ? == WSAENOBUFS)
"File name too long" : ? "No buffer space available"
(no_ == WSAEHOSTDOWN) ? : (no_ == WSAEISCONN) ? "Socket is already connected"
"Host is down" : : (no_
(no_ == WSAEHOSTUNREACH) ? == WSAENOTCONN)
"No Route to Host" : ? "Socket is not connected"
(no_ == WSAENOTEMPTY) ? : (no_ == WSAESHUTDOWN) ? "Can't send after socket shutdown"
"Directory not empty" : : (no_ == WSAETOOMANYREFS) ? "Too many references can't splice"
(no_ == WSAEPROCLIM) ? : (no_ == WSAETIMEDOUT) ? "Connection timed out"
"Too many processes" : : (no_
(no_ == WSAEUSERS) ? == WSAECONNREFUSED)
"Too many users" : ? "Connection refused"
(no_ == WSAEDQUOT) ? : (no_
"Disc Quota Exceeded" : == WSAELOOP)
(no_ == WSAESTALE) ? ? "Too many levels of symbolic links"
"Stale NFS file handle" : : (no_
(no_ == WSAEREMOTE) ? == WSAENAMETOOLONG)
"Too many levels of remote in path" : ? "File name too long"
(no_ == WSASYSNOTREADY) ? : (no_ == WSAEHOSTDOWN) ? "Host is down"
"Network SubSystem is unavailable" : : (no_
(no_ == WSAVERNOTSUPPORTED) ? == WSAEHOSTUNREACH)
"WINSOCK DLL Version out of range" : ? "No Route to Host"
(no_ == WSANOTINITIALISED) ? : (no_ == WSAENOTEMPTY) ? "Directory not empty"
"Successful WSASTARTUP not yet performed" : : (no_ == WSAEPROCLIM) ? "Too many processes"
(no_ == WSAHOST_NOT_FOUND) ? : (
"Host not found" : no_
(no_ == WSATRY_AGAIN) ? == WSAEUSERS)
"Non-Authoritative Host not found" : ? "Too many users"
(no_ == WSANO_RECOVERY) ? : (no_
"Non-Recoverable errors: FORMERR REFUSED NOTIMP" : == WSAEDQUOT)
(no_ == WSANO_DATA) ? ? "Disc Quota Exceeded"
"Valid name no data record of requested" : : (no_
"error not defined"; == WSAESTALE)
? "Stale NFS file handle"
: (no_ == WSAEREMOTE) ? "Too many levels of remote in path"
: (no_
== WSASYSNOTREADY)
? "Network SubSystem is unavailable"
: (no_ == WSAVERNOTSUPPORTED) ? "WINSOCK DLL Version out of range"
: (no_
== WSANOTINITIALISED)
? "Successful WSASTARTUP not yet performed"
: (no_ == WSAHOST_NOT_FOUND) ? "Host not found"
: (no_
== WSATRY_AGAIN)
? "Non-Authoritative Host not found"
: (no_ == WSANO_RECOVERY) ? "Non-Recoverable errors: FORMERR REFUSED NOTIMP"
: (no_
== WSANO_DATA)
? "Valid name no data record of requested"
: "error not defined";
} }
void zmq::win_error (char *buffer_, size_t buffer_size_) void zmq::win_error (char *buffer_, size_t buffer_size_)
{ {
DWORD errcode = GetLastError (); DWORD errcode = GetLastError ();
#if defined _WIN32_WCE #if defined _WIN32_WCE
DWORD rc = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM | DWORD rc = FormatMessageW (
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode,
SUBLANG_DEFAULT), (LPWSTR)buffer_, buffer_size_ / sizeof(wchar_t), NULL); MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR) buffer_,
buffer_size_ / sizeof (wchar_t), NULL);
#else #else
DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | DWORD rc = FormatMessageA (
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode,
SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL); MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_,
NULL);
#endif #endif
zmq_assert (rc); zmq_assert (rc);
} }
@@ -224,158 +244,158 @@ void zmq::win_error (char *buffer_, size_t buffer_size_)
int zmq::wsa_error_to_errno (int errcode) int zmq::wsa_error_to_errno (int errcode)
{ {
switch (errcode) { switch (errcode) {
// 10004 - Interrupted system call. // 10004 - Interrupted system call.
case WSAEINTR: case WSAEINTR:
return EINTR; return EINTR;
// 10009 - File handle is not valid. // 10009 - File handle is not valid.
case WSAEBADF: case WSAEBADF:
return EBADF; return EBADF;
// 10013 - Permission denied. // 10013 - Permission denied.
case WSAEACCES: case WSAEACCES:
return EACCES; return EACCES;
// 10014 - Bad address. // 10014 - Bad address.
case WSAEFAULT: case WSAEFAULT:
return EFAULT; return EFAULT;
// 10022 - Invalid argument. // 10022 - Invalid argument.
case WSAEINVAL: case WSAEINVAL:
return EINVAL; return EINVAL;
// 10024 - Too many open files. // 10024 - Too many open files.
case WSAEMFILE: case WSAEMFILE:
return EMFILE; return EMFILE;
// 10035 - Operation would block. // 10035 - Operation would block.
case WSAEWOULDBLOCK: case WSAEWOULDBLOCK:
return EBUSY; return EBUSY;
// 10036 - Operation now in progress. // 10036 - Operation now in progress.
case WSAEINPROGRESS: case WSAEINPROGRESS:
return EAGAIN; return EAGAIN;
// 10037 - Operation already in progress. // 10037 - Operation already in progress.
case WSAEALREADY: case WSAEALREADY:
return EAGAIN; return EAGAIN;
// 10038 - Socket operation on non-socket. // 10038 - Socket operation on non-socket.
case WSAENOTSOCK: case WSAENOTSOCK:
return ENOTSOCK; return ENOTSOCK;
// 10039 - Destination address required. // 10039 - Destination address required.
case WSAEDESTADDRREQ: case WSAEDESTADDRREQ:
return EFAULT; return EFAULT;
// 10040 - Message too long. // 10040 - Message too long.
case WSAEMSGSIZE: case WSAEMSGSIZE:
return EMSGSIZE; return EMSGSIZE;
// 10041 - Protocol wrong type for socket. // 10041 - Protocol wrong type for socket.
case WSAEPROTOTYPE: case WSAEPROTOTYPE:
return EFAULT; return EFAULT;
// 10042 - Bad protocol option. // 10042 - Bad protocol option.
case WSAENOPROTOOPT: case WSAENOPROTOOPT:
return EINVAL; return EINVAL;
// 10043 - Protocol not supported. // 10043 - Protocol not supported.
case WSAEPROTONOSUPPORT: case WSAEPROTONOSUPPORT:
return EPROTONOSUPPORT; return EPROTONOSUPPORT;
// 10044 - Socket type not supported. // 10044 - Socket type not supported.
case WSAESOCKTNOSUPPORT: case WSAESOCKTNOSUPPORT:
return EFAULT; return EFAULT;
// 10045 - Operation not supported on socket. // 10045 - Operation not supported on socket.
case WSAEOPNOTSUPP: case WSAEOPNOTSUPP:
return EFAULT; return EFAULT;
// 10046 - Protocol family not supported. // 10046 - Protocol family not supported.
case WSAEPFNOSUPPORT: case WSAEPFNOSUPPORT:
return EPROTONOSUPPORT; return EPROTONOSUPPORT;
// 10047 - Address family not supported by protocol family. // 10047 - Address family not supported by protocol family.
case WSAEAFNOSUPPORT: case WSAEAFNOSUPPORT:
return EAFNOSUPPORT; return EAFNOSUPPORT;
// 10048 - Address already in use. // 10048 - Address already in use.
case WSAEADDRINUSE: case WSAEADDRINUSE:
return EADDRINUSE; return EADDRINUSE;
// 10049 - Cannot assign requested address. // 10049 - Cannot assign requested address.
case WSAEADDRNOTAVAIL: case WSAEADDRNOTAVAIL:
return EADDRNOTAVAIL; return EADDRNOTAVAIL;
// 10050 - Network is down. // 10050 - Network is down.
case WSAENETDOWN: case WSAENETDOWN:
return ENETDOWN; return ENETDOWN;
// 10051 - Network is unreachable. // 10051 - Network is unreachable.
case WSAENETUNREACH: case WSAENETUNREACH:
return ENETUNREACH; return ENETUNREACH;
// 10052 - Network dropped connection on reset. // 10052 - Network dropped connection on reset.
case WSAENETRESET: case WSAENETRESET:
return ENETRESET; return ENETRESET;
// 10053 - Software caused connection abort. // 10053 - Software caused connection abort.
case WSAECONNABORTED: case WSAECONNABORTED:
return ECONNABORTED; return ECONNABORTED;
// 10054 - Connection reset by peer. // 10054 - Connection reset by peer.
case WSAECONNRESET: case WSAECONNRESET:
return ECONNRESET; return ECONNRESET;
// 10055 - No buffer space available. // 10055 - No buffer space available.
case WSAENOBUFS: case WSAENOBUFS:
return ENOBUFS; return ENOBUFS;
// 10056 - Socket is already connected. // 10056 - Socket is already connected.
case WSAEISCONN: case WSAEISCONN:
return EFAULT; return EFAULT;
// 10057 - Socket is not connected. // 10057 - Socket is not connected.
case WSAENOTCONN: case WSAENOTCONN:
return ENOTCONN; return ENOTCONN;
// 10058 - Can't send after socket shutdown. // 10058 - Can't send after socket shutdown.
case WSAESHUTDOWN: case WSAESHUTDOWN:
return EFAULT; return EFAULT;
// 10059 - Too many references can't splice. // 10059 - Too many references can't splice.
case WSAETOOMANYREFS: case WSAETOOMANYREFS:
return EFAULT; return EFAULT;
// 10060 - Connection timed out. // 10060 - Connection timed out.
case WSAETIMEDOUT: case WSAETIMEDOUT:
return ETIMEDOUT; return ETIMEDOUT;
// 10061 - Connection refused. // 10061 - Connection refused.
case WSAECONNREFUSED: case WSAECONNREFUSED:
return ECONNREFUSED; return ECONNREFUSED;
// 10062 - Too many levels of symbolic links. // 10062 - Too many levels of symbolic links.
case WSAELOOP: case WSAELOOP:
return EFAULT; return EFAULT;
// 10063 - File name too long. // 10063 - File name too long.
case WSAENAMETOOLONG: case WSAENAMETOOLONG:
return EFAULT; return EFAULT;
// 10064 - Host is down. // 10064 - Host is down.
case WSAEHOSTDOWN: case WSAEHOSTDOWN:
return EAGAIN; return EAGAIN;
// 10065 - No route to host. // 10065 - No route to host.
case WSAEHOSTUNREACH: case WSAEHOSTUNREACH:
return EHOSTUNREACH; return EHOSTUNREACH;
// 10066 - Directory not empty. // 10066 - Directory not empty.
case WSAENOTEMPTY: case WSAENOTEMPTY:
return EFAULT; return EFAULT;
// 10067 - Too many processes. // 10067 - Too many processes.
case WSAEPROCLIM: case WSAEPROCLIM:
return EFAULT; return EFAULT;
// 10068 - Too many users. // 10068 - Too many users.
case WSAEUSERS: case WSAEUSERS:
return EFAULT; return EFAULT;
// 10069 - Disc Quota Exceeded. // 10069 - Disc Quota Exceeded.
case WSAEDQUOT: case WSAEDQUOT:
return EFAULT; return EFAULT;
// 10070 - Stale NFS file handle. // 10070 - Stale NFS file handle.
case WSAESTALE: case WSAESTALE:
return EFAULT; return EFAULT;
// 10071 - Too many levels of remote in path. // 10071 - Too many levels of remote in path.
case WSAEREMOTE: case WSAEREMOTE:
return EFAULT; return EFAULT;
// 10091 - Network SubSystem is unavailable. // 10091 - Network SubSystem is unavailable.
case WSASYSNOTREADY: case WSASYSNOTREADY:
return EFAULT; return EFAULT;
// 10092 - WINSOCK DLL Version out of range. // 10092 - WINSOCK DLL Version out of range.
case WSAVERNOTSUPPORTED: case WSAVERNOTSUPPORTED:
return EFAULT; return EFAULT;
// 10093 - Successful WSASTARTUP not yet performed. // 10093 - Successful WSASTARTUP not yet performed.
case WSANOTINITIALISED: case WSANOTINITIALISED:
return EFAULT; return EFAULT;
// 11001 - Host not found. // 11001 - Host not found.
case WSAHOST_NOT_FOUND: case WSAHOST_NOT_FOUND:
return EFAULT; return EFAULT;
// 11002 - Non-Authoritative Host not found. // 11002 - Non-Authoritative Host not found.
case WSATRY_AGAIN: case WSATRY_AGAIN:
return EFAULT; return EFAULT;
// 11003 - Non-Recoverable errors: FORMERR REFUSED NOTIMP. // 11003 - Non-Recoverable errors: FORMERR REFUSED NOTIMP.
case WSANO_RECOVERY: case WSANO_RECOVERY:
return EFAULT; return EFAULT;
// 11004 - Valid name no data record of requested. // 11004 - Valid name no data record of requested.
case WSANO_DATA: case WSANO_DATA:
return EFAULT; return EFAULT;
default: default:
wsa_assert (false); wsa_assert (false);
} }
// Not reachable // Not reachable
return 0; return 0;
@@ -417,9 +437,9 @@ void zmq::print_backtrace (void)
rc = unw_get_proc_name (&cursor, func_name, 256, &offset); rc = unw_get_proc_name (&cursor, func_name, 256, &offset);
if (rc == -UNW_ENOINFO) if (rc == -UNW_ENOINFO)
strcpy(func_name, "?"); strcpy (func_name, "?");
addr = (void *)(p_info.start_ip + offset); addr = (void *) (p_info.start_ip + offset);
if (dladdr (addr, &dl_info) && dl_info.dli_fname) if (dladdr (addr, &dl_info) && dl_info.dli_fname)
file_name = dl_info.dli_fname; file_name = dl_info.dli_fname;

View File

@@ -55,58 +55,60 @@
namespace zmq namespace zmq
{ {
const char *errno_to_string (int errno_); const char *errno_to_string (int errno_);
void zmq_abort (const char *errmsg_); void zmq_abort (const char *errmsg_);
void print_backtrace (void); void print_backtrace (void);
} }
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
namespace zmq namespace zmq
{ {
const char *wsa_error (); const char *wsa_error ();
const char *wsa_error_no (int no_, const char * wsae_wouldblock_string = "Operation would block"); const char *
void win_error (char *buffer_, size_t buffer_size_); wsa_error_no (int no_,
int wsa_error_to_errno (int errcode); const char *wsae_wouldblock_string = "Operation would block");
void win_error (char *buffer_, size_t buffer_size_);
int wsa_error_to_errno (int errcode);
} }
// Provides convenient way to check WSA-style errors on Windows. // Provides convenient way to check WSA-style errors on Windows.
#define wsa_assert(x) \ #define wsa_assert(x) \
do {\ do { \
if (unlikely (!(x))) {\ if (unlikely (!(x))) { \
const char *errstr = zmq::wsa_error ();\ const char *errstr = zmq::wsa_error (); \
if (errstr != NULL) {\ if (errstr != NULL) { \
fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \ fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
__FILE__, __LINE__);\ __FILE__, __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (errstr);\ zmq::zmq_abort (errstr); \
}\ } \
}\ } \
} while (false) } while (false)
// Provides convenient way to assert on WSA-style errors on Windows. // Provides convenient way to assert on WSA-style errors on Windows.
#define wsa_assert_no(no) \ #define wsa_assert_no(no) \
do {\ do { \
const char *errstr = zmq::wsa_error_no (no);\ const char *errstr = zmq::wsa_error_no (no); \
if (errstr != NULL) {\ if (errstr != NULL) { \
fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \ fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
__FILE__, __LINE__);\ __FILE__, __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (errstr);\ zmq::zmq_abort (errstr); \
}\ } \
} while (false) } while (false)
// Provides convenient way to check GetLastError-style errors on Windows. // Provides convenient way to check GetLastError-style errors on Windows.
#define win_assert(x) \ #define win_assert(x) \
do {\ do { \
if (unlikely (!(x))) {\ if (unlikely (!(x))) { \
char errstr [256];\ char errstr[256]; \
zmq::win_error (errstr, 256);\ zmq::win_error (errstr, 256); \
fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \ fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
__FILE__, __LINE__);\ __FILE__, __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (errstr);\ zmq::zmq_abort (errstr); \
}\ } \
} while (false) } while (false)
#endif #endif
@@ -114,60 +116,58 @@ namespace zmq
// This macro works in exactly the same way as the normal assert. It is used // This macro works in exactly the same way as the normal assert. It is used
// in its stead because standard assert on Win32 in broken - it prints nothing // in its stead because standard assert on Win32 in broken - it prints nothing
// when used within the scope of JNI library. // when used within the scope of JNI library.
#define zmq_assert(x) \ #define zmq_assert(x) \
do {\ do { \
if (unlikely (!(x))) {\ if (unlikely (!(x))) { \
fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, \ fprintf (stderr, "Assertion failed: %s (%s:%d)\n", #x, __FILE__, \
__FILE__, __LINE__);\ __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (#x);\ zmq::zmq_abort (#x); \
}\ } \
} while (false) } while (false)
// Provides convenient way to check for errno-style errors. // Provides convenient way to check for errno-style errors.
#define errno_assert(x) \ #define errno_assert(x) \
do {\ do { \
if (unlikely (!(x))) {\ if (unlikely (!(x))) { \
const char *errstr = strerror (errno);\ const char *errstr = strerror (errno); \
fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\ fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (errstr);\ zmq::zmq_abort (errstr); \
}\ } \
} while (false) } while (false)
// Provides convenient way to check for POSIX errors. // Provides convenient way to check for POSIX errors.
#define posix_assert(x) \ #define posix_assert(x) \
do {\ do { \
if (unlikely (x)) {\ if (unlikely (x)) { \
const char *errstr = strerror (x);\ const char *errstr = strerror (x); \
fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\ fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (errstr);\ zmq::zmq_abort (errstr); \
}\ } \
} while (false) } while (false)
// Provides convenient way to check for errors from getaddrinfo. // Provides convenient way to check for errors from getaddrinfo.
#define gai_assert(x) \ #define gai_assert(x) \
do {\ do { \
if (unlikely (x)) {\ if (unlikely (x)) { \
const char *errstr = gai_strerror (x);\ const char *errstr = gai_strerror (x); \
fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__);\ fprintf (stderr, "%s (%s:%d)\n", errstr, __FILE__, __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort (errstr);\ zmq::zmq_abort (errstr); \
}\ } \
} while (false) } while (false)
// Provides convenient way to check whether memory allocation have succeeded. // Provides convenient way to check whether memory allocation have succeeded.
#define alloc_assert(x) \ #define alloc_assert(x) \
do {\ do { \
if (unlikely (!x)) {\ if (unlikely (!x)) { \
fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n",\ fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n", __FILE__, \
__FILE__, __LINE__);\ __LINE__); \
fflush (stderr);\ fflush (stderr); \
zmq::zmq_abort ("FATAL ERROR: OUT OF MEMORY");\ zmq::zmq_abort ("FATAL ERROR: OUT OF MEMORY"); \
}\ } \
} while (false) } while (false)
#endif #endif

View File

@@ -37,16 +37,25 @@
namespace zmq namespace zmq
{ {
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
#if defined _MSC_VER &&_MSC_VER <= 1400 #if defined _MSC_VER && _MSC_VER <= 1400
typedef UINT_PTR fd_t; typedef UINT_PTR fd_t;
enum {retired_fd = (fd_t)(~0)}; enum
{
retired_fd = (fd_t) (~0)
};
#else #else
typedef SOCKET fd_t; typedef SOCKET fd_t;
enum {retired_fd = (fd_t)INVALID_SOCKET}; enum
{
retired_fd = (fd_t) INVALID_SOCKET
};
#endif #endif
#else #else
typedef int fd_t; typedef int fd_t;
enum {retired_fd = -1}; enum
{
retired_fd = -1
};
#endif #endif
} }
#endif #endif

View File

@@ -33,11 +33,7 @@
#include "err.hpp" #include "err.hpp"
#include "msg.hpp" #include "msg.hpp"
zmq::fq_t::fq_t () : zmq::fq_t::fq_t () : active (0), last_in (NULL), current (0), more (false)
active (0),
last_in (NULL),
current (0),
more (false)
{ {
} }
@@ -93,20 +89,19 @@ int zmq::fq_t::recvpipe (msg_t *msg_, pipe_t **pipe_)
// Round-robin over the pipes to get the next message. // Round-robin over the pipes to get the next message.
while (active > 0) { while (active > 0) {
// Try to fetch new message. If we've already read part of the message // Try to fetch new message. If we've already read part of the message
// subsequent part should be immediately available. // subsequent part should be immediately available.
bool fetched = pipes [current]->read (msg_); bool fetched = pipes[current]->read (msg_);
// Note that when message is not fetched, current pipe is deactivated // Note that when message is not fetched, current pipe is deactivated
// and replaced by another active pipe. Thus we don't have to increase // and replaced by another active pipe. Thus we don't have to increase
// the 'current' pointer. // the 'current' pointer.
if (fetched) { if (fetched) {
if (pipe_) if (pipe_)
*pipe_ = pipes [current]; *pipe_ = pipes[current];
more = msg_->flags () & msg_t::more? true: false; more = msg_->flags () & msg_t::more ? true : false;
if (!more) { if (!more) {
last_in = pipes [current]; last_in = pipes[current];
current = (current + 1) % active; current = (current + 1) % active;
} }
return 0; return 0;
@@ -142,7 +137,7 @@ bool zmq::fq_t::has_in ()
// get back to its original value. Otherwise it'll point to the first // get back to its original value. Otherwise it'll point to the first
// pipe holding messages, skipping only pipes with no messages available. // pipe holding messages, skipping only pipes with no messages available.
while (active > 0) { while (active > 0) {
if (pipes [current]->check_read ()) if (pipes[current]->check_read ())
return true; return true;
// Deactivate the pipe. // Deactivate the pipe.
@@ -157,7 +152,5 @@ bool zmq::fq_t::has_in ()
const zmq::blob_t &zmq::fq_t::get_credential () const const zmq::blob_t &zmq::fq_t::get_credential () const
{ {
return last_in? return last_in ? last_in->get_credential () : saved_credential;
last_in->get_credential (): saved_credential;
} }

View File

@@ -37,56 +37,52 @@
namespace zmq namespace zmq
{ {
// Class manages a set of inbound pipes. On receive it performs fair
// queueing so that senders gone berserk won't cause denial of
// service for decent senders.
// Class manages a set of inbound pipes. On receive it performs fair class fq_t
// queueing so that senders gone berserk won't cause denial of {
// service for decent senders. public:
fq_t ();
~fq_t ();
class fq_t void attach (pipe_t *pipe_);
{ void activated (pipe_t *pipe_);
public: void pipe_terminated (pipe_t *pipe_);
fq_t (); int recv (msg_t *msg_);
~fq_t (); int recvpipe (msg_t *msg_, pipe_t **pipe_);
bool has_in ();
const blob_t &get_credential () const;
void attach (pipe_t *pipe_); private:
void activated (pipe_t *pipe_); // Inbound pipes.
void pipe_terminated (pipe_t *pipe_); typedef array_t<pipe_t, 1> pipes_t;
pipes_t pipes;
int recv (msg_t *msg_); // Number of active pipes. All the active pipes are located at the
int recvpipe (msg_t *msg_, pipe_t **pipe_); // beginning of the pipes array.
bool has_in (); pipes_t::size_type active;
const blob_t &get_credential () const;
private: // Pointer to the last pipe we received message from.
// NULL when no message has been received or the pipe
// has terminated.
pipe_t *last_in;
// Inbound pipes. // Index of the next bound pipe to read a message from.
typedef array_t <pipe_t, 1> pipes_t; pipes_t::size_type current;
pipes_t pipes;
// Number of active pipes. All the active pipes are located at the // If true, part of a multipart message was already received, but
// beginning of the pipes array. // there are following parts still waiting in the current pipe.
pipes_t::size_type active; bool more;
// Pointer to the last pipe we received message from. // Holds credential after the last_active_pipe has terminated.
// NULL when no message has been received or the pipe blob_t saved_credential;
// has terminated.
pipe_t *last_in;
// Index of the next bound pipe to read a message from.
pipes_t::size_type current;
// If true, part of a multipart message was already received, but
// there are following parts still waiting in the current pipe.
bool more;
// Holds credential after the last_active_pipe has terminated.
blob_t saved_credential;
fq_t (const fq_t&);
const fq_t &operator = (const fq_t&);
};
fq_t (const fq_t &);
const fq_t &operator= (const fq_t &);
};
} }
#endif #endif

View File

@@ -68,7 +68,6 @@ int zmq::gather_t::xrecv (msg_t *msg_)
// Drop any messages with more flag // Drop any messages with more flag
while (rc == 0 && msg_->flags () & msg_t::more) { while (rc == 0 && msg_->flags () & msg_t::more) {
// drop all frames of the current multi-frame message // drop all frames of the current multi-frame message
rc = fq.recvpipe (msg_, NULL); rc = fq.recvpipe (msg_, NULL);

View File

@@ -36,40 +36,33 @@
namespace zmq namespace zmq
{ {
class ctx_t;
class pipe_t;
class msg_t;
class io_thread_t;
class ctx_t; class gather_t : public socket_base_t
class pipe_t; {
class msg_t; public:
class io_thread_t; gather_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_);
~gather_t ();
class gather_t : protected:
public socket_base_t // Overrides of functions from socket_base_t.
{ void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
public: int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
gather_t (zmq::ctx_t *parent_, uint32_t tid_, int sid_); private:
~gather_t (); // Fair queueing object for inbound pipes.
fq_t fq;
protected:
// Overrides of functions from socket_base_t.
void xattach_pipe (zmq::pipe_t *pipe_, bool subscribe_to_all_);
int xrecv (zmq::msg_t *msg_);
bool xhas_in ();
const blob_t &get_credential () const;
void xread_activated (zmq::pipe_t *pipe_);
void xpipe_terminated (zmq::pipe_t *pipe_);
private:
// Fair queueing object for inbound pipes.
fq_t fq;
gather_t (const gather_t&);
const gather_t &operator = (const gather_t&);
};
gather_t (const gather_t &);
const gather_t &operator= (const gather_t &);
};
} }
#endif #endif

View File

@@ -49,19 +49,22 @@ zmq::gssapi_client_t::gssapi_client_t (session_base_t *session_,
mechs (), mechs (),
security_context_established (false) security_context_established (false)
{ {
const std::string::size_type service_size = options_.gss_service_principal.size(); const std::string::size_type service_size =
service_name = static_cast <char *>(malloc(service_size+1)); options_.gss_service_principal.size ();
assert(service_name); service_name = static_cast<char *> (malloc (service_size + 1));
memcpy(service_name, options_.gss_service_principal.c_str(), service_size+1 ); assert (service_name);
memcpy (service_name, options_.gss_service_principal.c_str (),
service_size + 1);
service_name_type = convert_nametype (options_.gss_service_principal_nt); service_name_type = convert_nametype (options_.gss_service_principal_nt);
maj_stat = GSS_S_COMPLETE; maj_stat = GSS_S_COMPLETE;
if(!options_.gss_principal.empty()) if (!options_.gss_principal.empty ()) {
{ const std::string::size_type principal_size =
const std::string::size_type principal_size = options_.gss_principal.size(); options_.gss_principal.size ();
principal_name = static_cast <char *>(malloc(principal_size+1)); principal_name = static_cast<char *> (malloc (principal_size + 1));
assert(principal_name); assert (principal_name);
memcpy(principal_name, options_.gss_principal.c_str(), principal_size+1 ); memcpy (principal_name, options_.gss_principal.c_str (),
principal_size + 1);
gss_OID name_type = convert_nametype (options_.gss_principal_nt); gss_OID name_type = convert_nametype (options_.gss_principal_nt);
if (acquire_credentials (principal_name, &cred, name_type) != 0) if (acquire_credentials (principal_name, &cred, name_type) != 0)
@@ -74,16 +77,16 @@ zmq::gssapi_client_t::gssapi_client_t (session_base_t *session_,
zmq::gssapi_client_t::~gssapi_client_t () zmq::gssapi_client_t::~gssapi_client_t ()
{ {
if(service_name) if (service_name)
free (service_name); free (service_name);
if(cred) if (cred)
gss_release_cred(&min_stat, &cred); gss_release_cred (&min_stat, &cred);
} }
int zmq::gssapi_client_t::next_handshake_command (msg_t *msg_) int zmq::gssapi_client_t::next_handshake_command (msg_t *msg_)
{ {
if (state == send_ready) { if (state == send_ready) {
int rc = produce_ready(msg_); int rc = produce_ready (msg_);
if (rc == 0) if (rc == 0)
state = connected; state = connected;
@@ -107,8 +110,7 @@ int zmq::gssapi_client_t::next_handshake_command (msg_t *msg_)
if (maj_stat == GSS_S_COMPLETE) { if (maj_stat == GSS_S_COMPLETE) {
security_context_established = true; security_context_established = true;
state = recv_ready; state = recv_ready;
} } else
else
state = recv_next_token; state = recv_next_token;
return 0; return 0;
@@ -117,7 +119,7 @@ int zmq::gssapi_client_t::next_handshake_command (msg_t *msg_)
int zmq::gssapi_client_t::process_handshake_command (msg_t *msg_) int zmq::gssapi_client_t::process_handshake_command (msg_t *msg_)
{ {
if (state == recv_ready) { if (state == recv_ready) {
int rc = process_ready(msg_); int rc = process_ready (msg_);
if (rc == 0) if (rc == 0)
state = send_ready; state = send_ready;
@@ -126,8 +128,7 @@ int zmq::gssapi_client_t::process_handshake_command (msg_t *msg_)
if (state != recv_next_token) { if (state != recv_next_token) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -151,7 +152,7 @@ int zmq::gssapi_client_t::encode (msg_t *msg_)
zmq_assert (state == connected); zmq_assert (state == connected);
if (do_encryption) if (do_encryption)
return encode_message (msg_); return encode_message (msg_);
return 0; return 0;
} }
@@ -161,14 +162,14 @@ int zmq::gssapi_client_t::decode (msg_t *msg_)
zmq_assert (state == connected); zmq_assert (state == connected);
if (do_encryption) if (do_encryption)
return decode_message (msg_); return decode_message (msg_);
return 0; return 0;
} }
zmq::mechanism_t::status_t zmq::gssapi_client_t::status () const zmq::mechanism_t::status_t zmq::gssapi_client_t::status () const
{ {
return state == connected? mechanism_t::ready: mechanism_t::handshaking; return state == connected ? mechanism_t::ready : mechanism_t::handshaking;
} }
int zmq::gssapi_client_t::initialize_context () int zmq::gssapi_client_t::initialize_context ()
@@ -180,22 +181,20 @@ int zmq::gssapi_client_t::initialize_context ()
// First time through, import service_name into target_name // First time through, import service_name into target_name
if (target_name == GSS_C_NO_NAME) { if (target_name == GSS_C_NO_NAME) {
send_tok.value = service_name; send_tok.value = service_name;
send_tok.length = strlen(service_name) + 1; send_tok.length = strlen (service_name) + 1;
OM_uint32 maj = gss_import_name(&min_stat, &send_tok, OM_uint32 maj = gss_import_name (&min_stat, &send_tok,
service_name_type, service_name_type, &target_name);
&target_name);
if (maj != GSS_S_COMPLETE) if (maj != GSS_S_COMPLETE)
return -1; return -1;
} }
maj_stat = gss_init_sec_context(&init_sec_min_stat, cred, &context, maj_stat = gss_init_sec_context (
target_name, mechs.elements, &init_sec_min_stat, cred, &context, target_name, mechs.elements,
gss_flags, 0, NULL, token_ptr, NULL, gss_flags, 0, NULL, token_ptr, NULL, &send_tok, &ret_flags, NULL);
&send_tok, &ret_flags, NULL);
if (token_ptr != GSS_C_NO_BUFFER) if (token_ptr != GSS_C_NO_BUFFER)
free(recv_tok.value); free (recv_tok.value);
return 0; return 0;
} }
@@ -203,18 +202,18 @@ int zmq::gssapi_client_t::initialize_context ()
int zmq::gssapi_client_t::produce_next_token (msg_t *msg_) int zmq::gssapi_client_t::produce_next_token (msg_t *msg_)
{ {
if (send_tok.length != 0) { // Server expects another token if (send_tok.length != 0) { // Server expects another token
if (produce_initiate(msg_, send_tok.value, send_tok.length) < 0) { if (produce_initiate (msg_, send_tok.value, send_tok.length) < 0) {
gss_release_buffer(&min_stat, &send_tok); gss_release_buffer (&min_stat, &send_tok);
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
return -1; return -1;
} }
} }
gss_release_buffer(&min_stat, &send_tok); gss_release_buffer (&min_stat, &send_tok);
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) { if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
if (context != GSS_C_NO_CONTEXT) if (context != GSS_C_NO_CONTEXT)
gss_delete_sec_context(&min_stat, &context, GSS_C_NO_BUFFER); gss_delete_sec_context (&min_stat, &context, GSS_C_NO_BUFFER);
return -1; return -1;
} }
@@ -224,8 +223,8 @@ int zmq::gssapi_client_t::produce_next_token (msg_t *msg_)
int zmq::gssapi_client_t::process_next_token (msg_t *msg_) int zmq::gssapi_client_t::process_next_token (msg_t *msg_)
{ {
if (maj_stat == GSS_S_CONTINUE_NEEDED) { if (maj_stat == GSS_S_CONTINUE_NEEDED) {
if (process_initiate(msg_, &recv_tok.value, recv_tok.length) < 0) { if (process_initiate (msg_, &recv_tok.value, recv_tok.length) < 0) {
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
return -1; return -1;
} }
token_ptr = &recv_tok; token_ptr = &recv_tok;

View File

@@ -36,57 +36,55 @@
namespace zmq namespace zmq
{ {
class msg_t;
class session_base_t;
class msg_t; class gssapi_client_t : public gssapi_mechanism_base_t
class session_base_t; {
public:
gssapi_client_t (session_base_t *session_, const options_t &options_);
virtual ~gssapi_client_t ();
class gssapi_client_t : public gssapi_mechanism_base_t // mechanism implementation
virtual int next_handshake_command (msg_t *msg_);
virtual int process_handshake_command (msg_t *msg_);
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
virtual status_t status () const;
private:
enum state_t
{ {
public: call_next_init,
gssapi_client_t (session_base_t *session_, const options_t &options_); send_next_token,
virtual ~gssapi_client_t (); recv_next_token,
send_ready,
// mechanism implementation recv_ready,
virtual int next_handshake_command (msg_t *msg_); connected
virtual int process_handshake_command (msg_t *msg_);
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
virtual status_t status () const;
private:
enum state_t {
call_next_init,
send_next_token,
recv_next_token,
send_ready,
recv_ready,
connected
};
// Human-readable principal name of the service we are connecting to
char * service_name;
gss_OID service_name_type;
// Current FSM state
state_t state;
// Points to either send_tok or recv_tok
// during context initialization
gss_buffer_desc *token_ptr;
// The desired underlying mechanism
gss_OID_set_desc mechs;
// True iff client considers the server authenticated
bool security_context_established;
int initialize_context ();
int produce_next_token (msg_t *msg_);
int process_next_token (msg_t *msg_);
}; };
// Human-readable principal name of the service we are connecting to
char *service_name;
gss_OID service_name_type;
// Current FSM state
state_t state;
// Points to either send_tok or recv_tok
// during context initialization
gss_buffer_desc *token_ptr;
// The desired underlying mechanism
gss_OID_set_desc mechs;
// True iff client considers the server authenticated
bool security_context_established;
int initialize_context ();
int produce_next_token (msg_t *msg_);
int process_next_token (msg_t *msg_);
};
} }
#endif #endif

View File

@@ -41,8 +41,7 @@
#include "wire.hpp" #include "wire.hpp"
zmq::gssapi_mechanism_base_t::gssapi_mechanism_base_t ( zmq::gssapi_mechanism_base_t::gssapi_mechanism_base_t (
session_base_t *session_, session_base_t *session_, const options_t &options_) :
const options_t &options_) :
mechanism_base_t (session_, options_), mechanism_base_t (session_, options_),
send_tok (), send_tok (),
recv_tok (), recv_tok (),
@@ -62,10 +61,10 @@ zmq::gssapi_mechanism_base_t::gssapi_mechanism_base_t (
zmq::gssapi_mechanism_base_t::~gssapi_mechanism_base_t () zmq::gssapi_mechanism_base_t::~gssapi_mechanism_base_t ()
{ {
if(target_name) if (target_name)
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
if(context) if (context)
gss_delete_sec_context(&min_stat, &context, GSS_C_NO_BUFFER); gss_delete_sec_context (&min_stat, &context, GSS_C_NO_BUFFER);
} }
int zmq::gssapi_mechanism_base_t::encode_message (msg_t *msg_) int zmq::gssapi_mechanism_base_t::encode_message (msg_t *msg_)
@@ -81,17 +80,18 @@ int zmq::gssapi_mechanism_base_t::encode_message (msg_t *msg_)
if (msg_->flags () & msg_t::command) if (msg_->flags () & msg_t::command)
flags |= 0x02; flags |= 0x02;
uint8_t *plaintext_buffer = static_cast <uint8_t *>(malloc(msg_->size ()+1)); uint8_t *plaintext_buffer =
alloc_assert(plaintext_buffer); static_cast<uint8_t *> (malloc (msg_->size () + 1));
alloc_assert (plaintext_buffer);
plaintext_buffer[0] = flags; plaintext_buffer[0] = flags;
memcpy (plaintext_buffer+1, msg_->data(), msg_->size()); memcpy (plaintext_buffer + 1, msg_->data (), msg_->size ());
plaintext.value = plaintext_buffer; plaintext.value = plaintext_buffer;
plaintext.length = msg_->size ()+1; plaintext.length = msg_->size () + 1;
maj_stat = gss_wrap(&min_stat, context, 1, GSS_C_QOP_DEFAULT, maj_stat = gss_wrap (&min_stat, context, 1, GSS_C_QOP_DEFAULT, &plaintext,
&plaintext, &state, &wrapped); &state, &wrapped);
zmq_assert (maj_stat == GSS_S_COMPLETE); zmq_assert (maj_stat == GSS_S_COMPLETE);
zmq_assert (state); zmq_assert (state);
@@ -103,14 +103,14 @@ int zmq::gssapi_mechanism_base_t::encode_message (msg_t *msg_)
rc = msg_->init_size (8 + 4 + wrapped.length); rc = msg_->init_size (8 + 4 + wrapped.length);
zmq_assert (rc == 0); zmq_assert (rc == 0);
uint8_t *ptr = static_cast <uint8_t *> (msg_->data ()); uint8_t *ptr = static_cast<uint8_t *> (msg_->data ());
// Add command string // Add command string
memcpy (ptr, "\x07MESSAGE", 8); memcpy (ptr, "\x07MESSAGE", 8);
ptr += 8; ptr += 8;
// Add token length // Add token length
put_uint32 (ptr, static_cast <uint32_t> (wrapped.length)); put_uint32 (ptr, static_cast<uint32_t> (wrapped.length));
ptr += 4; ptr += 4;
// Add wrapped token value // Add wrapped token value
@@ -124,7 +124,7 @@ int zmq::gssapi_mechanism_base_t::encode_message (msg_t *msg_)
int zmq::gssapi_mechanism_base_t::decode_message (msg_t *msg_) int zmq::gssapi_mechanism_base_t::decode_message (msg_t *msg_)
{ {
const uint8_t *ptr = static_cast <uint8_t *> (msg_->data ()); const uint8_t *ptr = static_cast<uint8_t *> (msg_->data ());
size_t bytes_left = msg_->size (); size_t bytes_left = msg_->size ();
int rc = check_basic_command_structure (msg_); int rc = check_basic_command_structure (msg_);
@@ -134,8 +134,7 @@ int zmq::gssapi_mechanism_base_t::decode_message (msg_t *msg_)
// Get command string // Get command string
if (bytes_left < 8 || memcmp (ptr, "\x07MESSAGE", 8)) { if (bytes_left < 8 || memcmp (ptr, "\x07MESSAGE", 8)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -164,12 +163,12 @@ int zmq::gssapi_mechanism_base_t::decode_message (msg_t *msg_)
return -1; return -1;
} }
// TODO: instead of malloc/memcpy, can we just do: wrapped.value = ptr; // TODO: instead of malloc/memcpy, can we just do: wrapped.value = ptr;
const size_t alloc_length = wrapped.length? wrapped.length: 1; const size_t alloc_length = wrapped.length ? wrapped.length : 1;
wrapped.value = static_cast <char *> (malloc (alloc_length)); wrapped.value = static_cast<char *> (malloc (alloc_length));
alloc_assert (wrapped.value); alloc_assert (wrapped.value);
if (wrapped.length) { if (wrapped.length) {
memcpy(wrapped.value, ptr, wrapped.length); memcpy (wrapped.value, ptr, wrapped.length);
ptr += wrapped.length; ptr += wrapped.length;
bytes_left -= wrapped.length; bytes_left -= wrapped.length;
} }
@@ -177,38 +176,37 @@ int zmq::gssapi_mechanism_base_t::decode_message (msg_t *msg_)
// Unwrap the token value // Unwrap the token value
int state; int state;
gss_buffer_desc plaintext; gss_buffer_desc plaintext;
maj_stat = gss_unwrap(&min_stat, context, &wrapped, &plaintext, maj_stat = gss_unwrap (&min_stat, context, &wrapped, &plaintext, &state,
&state, (gss_qop_t *) NULL); (gss_qop_t *) NULL);
if (maj_stat != GSS_S_COMPLETE) if (maj_stat != GSS_S_COMPLETE) {
{
gss_release_buffer (&min_stat, &plaintext); gss_release_buffer (&min_stat, &plaintext);
free (wrapped.value); free (wrapped.value);
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
ZMQ_PROTOCOL_ERROR_ZMTP_CRYPTOGRAPHIC);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
zmq_assert(state); zmq_assert (state);
// Re-initialize msg_ for plaintext // Re-initialize msg_ for plaintext
rc = msg_->close (); rc = msg_->close ();
zmq_assert (rc == 0); zmq_assert (rc == 0);
rc = msg_->init_size (plaintext.length-1); rc = msg_->init_size (plaintext.length - 1);
zmq_assert (rc == 0); zmq_assert (rc == 0);
const uint8_t flags = static_cast <char *> (plaintext.value)[0]; const uint8_t flags = static_cast<char *> (plaintext.value)[0];
if (flags & 0x01) if (flags & 0x01)
msg_->set_flags (msg_t::more); msg_->set_flags (msg_t::more);
if (flags & 0x02) if (flags & 0x02)
msg_->set_flags (msg_t::command); msg_->set_flags (msg_t::command);
memcpy (msg_->data (), static_cast <char *> (plaintext.value)+1, plaintext.length-1); memcpy (msg_->data (), static_cast<char *> (plaintext.value) + 1,
plaintext.length - 1);
gss_release_buffer (&min_stat, &plaintext); gss_release_buffer (&min_stat, &plaintext);
free(wrapped.value); free (wrapped.value);
if (bytes_left > 0) { if (bytes_left > 0) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -221,7 +219,9 @@ int zmq::gssapi_mechanism_base_t::decode_message (msg_t *msg_)
return 0; return 0;
} }
int zmq::gssapi_mechanism_base_t::produce_initiate (msg_t *msg_, void *token_value_, size_t token_length_) int zmq::gssapi_mechanism_base_t::produce_initiate (msg_t *msg_,
void *token_value_,
size_t token_length_)
{ {
zmq_assert (token_value_); zmq_assert (token_value_);
zmq_assert (token_length_ <= 0xFFFFFFFFUL); zmq_assert (token_length_ <= 0xFFFFFFFFUL);
@@ -231,14 +231,14 @@ int zmq::gssapi_mechanism_base_t::produce_initiate (msg_t *msg_, void *token_val
const int rc = msg_->init_size (command_size); const int rc = msg_->init_size (command_size);
errno_assert (rc == 0); errno_assert (rc == 0);
uint8_t *ptr = static_cast <uint8_t *> (msg_->data ()); uint8_t *ptr = static_cast<uint8_t *> (msg_->data ());
// Add command string // Add command string
memcpy (ptr, "\x08INITIATE", 9); memcpy (ptr, "\x08INITIATE", 9);
ptr += 9; ptr += 9;
// Add token length // Add token length
put_uint32 (ptr, static_cast <uint32_t> (token_length_)); put_uint32 (ptr, static_cast<uint32_t> (token_length_));
ptr += 4; ptr += 4;
// Add token value // Add token value
@@ -248,11 +248,13 @@ int zmq::gssapi_mechanism_base_t::produce_initiate (msg_t *msg_, void *token_val
return 0; return 0;
} }
int zmq::gssapi_mechanism_base_t::process_initiate (msg_t *msg_, void **token_value_, size_t &token_length_) int zmq::gssapi_mechanism_base_t::process_initiate (msg_t *msg_,
void **token_value_,
size_t &token_length_)
{ {
zmq_assert (token_value_); zmq_assert (token_value_);
const uint8_t *ptr = static_cast <uint8_t *> (msg_->data ()); const uint8_t *ptr = static_cast<uint8_t *> (msg_->data ());
size_t bytes_left = msg_->size (); size_t bytes_left = msg_->size ();
int rc = check_basic_command_structure (msg_); int rc = check_basic_command_structure (msg_);
@@ -262,8 +264,7 @@ int zmq::gssapi_mechanism_base_t::process_initiate (msg_t *msg_, void **token_va
// Get command string // Get command string
if (bytes_left < 9 || memcmp (ptr, "\x08INITIATE", 9)) { if (bytes_left < 9 || memcmp (ptr, "\x08INITIATE", 9)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -291,11 +292,12 @@ int zmq::gssapi_mechanism_base_t::process_initiate (msg_t *msg_, void **token_va
return -1; return -1;
} }
*token_value_ = static_cast <char *> (malloc (token_length_ ? token_length_ : 1)); *token_value_ =
static_cast<char *> (malloc (token_length_ ? token_length_ : 1));
alloc_assert (*token_value_); alloc_assert (*token_value_);
if (token_length_) { if (token_length_) {
memcpy(*token_value_, ptr, token_length_); memcpy (*token_value_, ptr, token_length_);
ptr += token_length_; ptr += token_length_;
bytes_left -= token_length_; bytes_left -= token_length_;
} }
@@ -329,7 +331,7 @@ int zmq::gssapi_mechanism_base_t::process_ready (msg_t *msg_)
return rc; return rc;
} }
const unsigned char *ptr = static_cast <unsigned char *> (msg_->data ()); const unsigned char *ptr = static_cast<unsigned char *> (msg_->data ());
size_t bytes_left = msg_->size (); size_t bytes_left = msg_->size ();
int rc = check_basic_command_structure (msg_); int rc = check_basic_command_structure (msg_);
@@ -338,8 +340,7 @@ int zmq::gssapi_mechanism_base_t::process_ready (msg_t *msg_)
if (bytes_left < 6 || memcmp (ptr, "\x05READY", 6)) { if (bytes_left < 6 || memcmp (ptr, "\x05READY", 6)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -347,9 +348,8 @@ int zmq::gssapi_mechanism_base_t::process_ready (msg_t *msg_)
bytes_left -= 6; bytes_left -= 6;
rc = parse_metadata (ptr, bytes_left); rc = parse_metadata (ptr, bytes_left);
if (rc == -1) if (rc == -1)
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA);
ZMQ_PROTOCOL_ERROR_ZMTP_INVALID_METADATA);
return rc; return rc;
} }
@@ -363,7 +363,7 @@ const gss_OID zmq::gssapi_mechanism_base_t::convert_nametype (int zmq_nametype)
return GSS_C_NT_USER_NAME; return GSS_C_NT_USER_NAME;
case ZMQ_GSSAPI_NT_KRB5_PRINCIPAL: case ZMQ_GSSAPI_NT_KRB5_PRINCIPAL:
#ifdef GSS_KRB5_NT_PRINCIPAL_NAME #ifdef GSS_KRB5_NT_PRINCIPAL_NAME
return (gss_OID)GSS_KRB5_NT_PRINCIPAL_NAME; return (gss_OID) GSS_KRB5_NT_PRINCIPAL_NAME;
#else #else
return GSS_C_NT_USER_NAME; return GSS_C_NT_USER_NAME;
#endif #endif
@@ -371,7 +371,9 @@ const gss_OID zmq::gssapi_mechanism_base_t::convert_nametype (int zmq_nametype)
return NULL; return NULL;
} }
int zmq::gssapi_mechanism_base_t::acquire_credentials (char * service_name_, gss_cred_id_t * cred_, gss_OID name_type_) int zmq::gssapi_mechanism_base_t::acquire_credentials (char *service_name_,
gss_cred_id_t *cred_,
gss_OID name_type_)
{ {
OM_uint32 maj_stat; OM_uint32 maj_stat;
OM_uint32 min_stat; OM_uint32 min_stat;
@@ -381,20 +383,18 @@ int zmq::gssapi_mechanism_base_t::acquire_credentials (char * service_name_, gss
name_buf.value = service_name_; name_buf.value = service_name_;
name_buf.length = strlen ((char *) name_buf.value) + 1; name_buf.length = strlen ((char *) name_buf.value) + 1;
maj_stat = gss_import_name (&min_stat, &name_buf, maj_stat = gss_import_name (&min_stat, &name_buf, name_type_, &server_name);
name_type_, &server_name);
if (maj_stat != GSS_S_COMPLETE) if (maj_stat != GSS_S_COMPLETE)
return -1; return -1;
maj_stat = gss_acquire_cred (&min_stat, server_name, 0, maj_stat = gss_acquire_cred (&min_stat, server_name, 0, GSS_C_NO_OID_SET,
GSS_C_NO_OID_SET, GSS_C_BOTH, GSS_C_BOTH, cred_, NULL, NULL);
cred_, NULL, NULL);
if (maj_stat != GSS_S_COMPLETE) if (maj_stat != GSS_S_COMPLETE)
return -1; return -1;
gss_release_name(&min_stat, &server_name); gss_release_name (&min_stat, &server_name);
return 0; return 0;
} }

View File

@@ -42,91 +42,89 @@
namespace zmq namespace zmq
{ {
class msg_t;
class msg_t; /// Commonalities between clients and servers are captured here.
/// For example, clients and servers both need to produce and
/// process context-level GSSAPI tokens (via INITIATE commands)
/// and per-message GSSAPI tokens (via MESSAGE commands).
class gssapi_mechanism_base_t : public virtual mechanism_base_t
{
public:
gssapi_mechanism_base_t (session_base_t *session_,
const options_t &options_);
virtual ~gssapi_mechanism_base_t () = 0;
/// Commonalities between clients and servers are captured here. protected:
/// For example, clients and servers both need to produce and // Produce a context-level GSSAPI token (INITIATE command)
/// process context-level GSSAPI tokens (via INITIATE commands) // during security context initialization.
/// and per-message GSSAPI tokens (via MESSAGE commands). int produce_initiate (msg_t *msg_, void *data_, size_t data_len_);
class gssapi_mechanism_base_t : public virtual mechanism_base_t
{
public:
gssapi_mechanism_base_t (session_base_t *session_,
const options_t &options_);
virtual ~gssapi_mechanism_base_t () = 0;
protected: // Process a context-level GSSAPI token (INITIATE command)
// Produce a context-level GSSAPI token (INITIATE command) // during security context initialization.
// during security context initialization. int process_initiate (msg_t *msg_, void **data_, size_t &data_len_);
int produce_initiate (msg_t *msg_, void *data_, size_t data_len_);
// Process a context-level GSSAPI token (INITIATE command) // Produce a metadata ready msg (READY) to conclude handshake
// during security context initialization. int produce_ready (msg_t *msg_);
int process_initiate (msg_t *msg_, void **data_, size_t &data_len_);
// Produce a metadata ready msg (READY) to conclude handshake // Process a metadata ready msg (READY)
int produce_ready (msg_t *msg_); int process_ready (msg_t *msg_);
// Process a metadata ready msg (READY) // Encode a per-message GSSAPI token (MESSAGE command) using
int process_ready (msg_t *msg_); // the established security context.
int encode_message (msg_t *msg_);
// Encode a per-message GSSAPI token (MESSAGE command) using // Decode a per-message GSSAPI token (MESSAGE command) using
// the established security context. // the established security context.
int encode_message (msg_t *msg_); int decode_message (msg_t *msg_);
// Decode a per-message GSSAPI token (MESSAGE command) using // Convert ZMQ_GSSAPI_NT values to GSSAPI name_type
// the established security context. static const gss_OID convert_nametype (int zmq_name_type_);
int decode_message (msg_t *msg_);
// Convert ZMQ_GSSAPI_NT values to GSSAPI name_type // Acquire security context credentials from the
static const gss_OID convert_nametype (int zmq_name_type_); // underlying mechanism.
static int acquire_credentials (char *principal_name_,
gss_cred_id_t *cred_,
gss_OID name_type_);
// Acquire security context credentials from the protected:
// underlying mechanism. // Opaque GSSAPI token for outgoing data
static int acquire_credentials (char * principal_name_, gss_buffer_desc send_tok;
gss_cred_id_t * cred_,
gss_OID name_type_);
protected: // Opaque GSSAPI token for incoming data
// Opaque GSSAPI token for outgoing data gss_buffer_desc recv_tok;
gss_buffer_desc send_tok;
// Opaque GSSAPI token for incoming data // Opaque GSSAPI representation of principal
gss_buffer_desc recv_tok; gss_name_t target_name;
// Opaque GSSAPI representation of principal // Human-readable principal name
gss_name_t target_name; char *principal_name;
// Human-readable principal name // Status code returned by GSSAPI functions
char * principal_name; OM_uint32 maj_stat;
// Status code returned by GSSAPI functions // Status code returned by the underlying mechanism
OM_uint32 maj_stat; OM_uint32 min_stat;
// Status code returned by the underlying mechanism // Status code returned by the underlying mechanism
OM_uint32 min_stat; // during context initialization
OM_uint32 init_sec_min_stat;
// Status code returned by the underlying mechanism // Flags returned by GSSAPI (ignored)
// during context initialization OM_uint32 ret_flags;
OM_uint32 init_sec_min_stat;
// Flags returned by GSSAPI (ignored) // Flags returned by GSSAPI (ignored)
OM_uint32 ret_flags; OM_uint32 gss_flags;
// Flags returned by GSSAPI (ignored) // Credentials used to establish security context
OM_uint32 gss_flags; gss_cred_id_t cred;
// Credentials used to establish security context // Opaque GSSAPI representation of the security context
gss_cred_id_t cred; gss_ctx_id_t context;
// Opaque GSSAPI representation of the security context
gss_ctx_id_t context;
// If true, use gss to encrypt messages. If false, only utilize gss for auth.
bool do_encryption;
};
// If true, use gss to encrypt messages. If false, only utilize gss for auth.
bool do_encryption;
};
} }
#endif #endif

View File

@@ -54,12 +54,13 @@ zmq::gssapi_server_t::gssapi_server_t (session_base_t *session_,
security_context_established (false) security_context_established (false)
{ {
maj_stat = GSS_S_CONTINUE_NEEDED; maj_stat = GSS_S_CONTINUE_NEEDED;
if(!options_.gss_principal.empty()) if (!options_.gss_principal.empty ()) {
{ const std::string::size_type principal_size =
const std::string::size_type principal_size = options_.gss_principal.size(); options_.gss_principal.size ();
principal_name = static_cast <char *>(malloc(principal_size+1)); principal_name = static_cast<char *> (malloc (principal_size + 1));
assert(principal_name); assert (principal_name);
memcpy(principal_name, options_.gss_principal.c_str(), principal_size+1 ); memcpy (principal_name, options_.gss_principal.c_str (),
principal_size + 1);
gss_OID name_type = convert_nametype (options_.gss_principal_nt); gss_OID name_type = convert_nametype (options_.gss_principal_nt);
if (acquire_credentials (principal_name, &cred, name_type) != 0) if (acquire_credentials (principal_name, &cred, name_type) != 0)
maj_stat = GSS_S_FAILURE; maj_stat = GSS_S_FAILURE;
@@ -68,17 +69,17 @@ zmq::gssapi_server_t::gssapi_server_t (session_base_t *session_,
zmq::gssapi_server_t::~gssapi_server_t () zmq::gssapi_server_t::~gssapi_server_t ()
{ {
if(cred) if (cred)
gss_release_cred(&min_stat, &cred); gss_release_cred (&min_stat, &cred);
if(target_name) if (target_name)
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
} }
int zmq::gssapi_server_t::next_handshake_command (msg_t *msg_) int zmq::gssapi_server_t::next_handshake_command (msg_t *msg_)
{ {
if (state == send_ready) { if (state == send_ready) {
int rc = produce_ready(msg_); int rc = produce_ready (msg_);
if (rc == 0) if (rc == 0)
state = recv_ready; state = recv_ready;
@@ -108,7 +109,7 @@ int zmq::gssapi_server_t::next_handshake_command (msg_t *msg_)
int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_) int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
{ {
if (state == recv_ready) { if (state == recv_ready) {
int rc = process_ready(msg_); int rc = process_ready (msg_);
if (rc == 0) if (rc == 0)
state = connected; state = connected;
@@ -117,8 +118,7 @@ int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
if (state != recv_next_token) { if (state != recv_next_token) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
@@ -138,7 +138,7 @@ int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
expecting_zap_reply = true; expecting_zap_reply = true;
} }
} }
state = expecting_zap_reply? expect_zap_reply: send_ready; state = expecting_zap_reply ? expect_zap_reply : send_ready;
return 0; return 0;
} }
@@ -158,9 +158,9 @@ void zmq::gssapi_server_t::send_zap_request ()
{ {
gss_buffer_desc principal; gss_buffer_desc principal;
gss_display_name (&min_stat, target_name, &principal, NULL); gss_display_name (&min_stat, target_name, &principal, NULL);
zap_client_t::send_zap_request ("GSSAPI", 6, zap_client_t::send_zap_request (
reinterpret_cast<const uint8_t *> (principal.value), "GSSAPI", 6, reinterpret_cast<const uint8_t *> (principal.value),
principal.length); principal.length);
gss_release_buffer (&min_stat, &principal); gss_release_buffer (&min_stat, &principal);
} }
@@ -170,7 +170,7 @@ int zmq::gssapi_server_t::encode (msg_t *msg_)
zmq_assert (state == connected); zmq_assert (state == connected);
if (do_encryption) if (do_encryption)
return encode_message (msg_); return encode_message (msg_);
return 0; return 0;
} }
@@ -180,7 +180,7 @@ int zmq::gssapi_server_t::decode (msg_t *msg_)
zmq_assert (state == connected); zmq_assert (state == connected);
if (do_encryption) if (do_encryption)
return decode_message (msg_); return decode_message (msg_);
return 0; return 0;
} }
@@ -199,21 +199,21 @@ int zmq::gssapi_server_t::zap_msg_available ()
zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const
{ {
return state == connected? mechanism_t::ready: mechanism_t::handshaking; return state == connected ? mechanism_t::ready : mechanism_t::handshaking;
} }
int zmq::gssapi_server_t::produce_next_token (msg_t *msg_) int zmq::gssapi_server_t::produce_next_token (msg_t *msg_)
{ {
if (send_tok.length != 0) { // Client expects another token if (send_tok.length != 0) { // Client expects another token
if (produce_initiate(msg_, send_tok.value, send_tok.length) < 0) if (produce_initiate (msg_, send_tok.value, send_tok.length) < 0)
return -1; return -1;
gss_release_buffer(&min_stat, &send_tok); gss_release_buffer (&min_stat, &send_tok);
} }
if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) { if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
if (context != GSS_C_NO_CONTEXT) if (context != GSS_C_NO_CONTEXT)
gss_delete_sec_context(&min_stat, &context, GSS_C_NO_BUFFER); gss_delete_sec_context (&min_stat, &context, GSS_C_NO_BUFFER);
return -1; return -1;
} }
@@ -223,9 +223,9 @@ int zmq::gssapi_server_t::produce_next_token (msg_t *msg_)
int zmq::gssapi_server_t::process_next_token (msg_t *msg_) int zmq::gssapi_server_t::process_next_token (msg_t *msg_)
{ {
if (maj_stat == GSS_S_CONTINUE_NEEDED) { if (maj_stat == GSS_S_CONTINUE_NEEDED) {
if (process_initiate(msg_, &recv_tok.value, recv_tok.length) < 0) { if (process_initiate (msg_, &recv_tok.value, recv_tok.length) < 0) {
if (target_name != GSS_C_NO_NAME) if (target_name != GSS_C_NO_NAME)
gss_release_name(&min_stat, &target_name); gss_release_name (&min_stat, &target_name);
return -1; return -1;
} }
} }
@@ -235,10 +235,9 @@ int zmq::gssapi_server_t::process_next_token (msg_t *msg_)
void zmq::gssapi_server_t::accept_context () void zmq::gssapi_server_t::accept_context ()
{ {
maj_stat = gss_accept_sec_context(&init_sec_min_stat, &context, cred, maj_stat = gss_accept_sec_context (
&recv_tok, GSS_C_NO_CHANNEL_BINDINGS, &init_sec_min_stat, &context, cred, &recv_tok, GSS_C_NO_CHANNEL_BINDINGS,
&target_name, &doid, &send_tok, &target_name, &doid, &send_tok, &ret_flags, NULL, NULL);
&ret_flags, NULL, NULL);
if (recv_tok.value) { if (recv_tok.value) {
free (recv_tok.value); free (recv_tok.value);

View File

@@ -37,57 +37,54 @@
namespace zmq namespace zmq
{ {
class msg_t;
class session_base_t;
class msg_t; class gssapi_server_t : public gssapi_mechanism_base_t, public zap_client_t
class session_base_t; {
public:
gssapi_server_t (session_base_t *session_,
const std::string &peer_address,
const options_t &options_);
virtual ~gssapi_server_t ();
class gssapi_server_t // mechanism implementation
: public gssapi_mechanism_base_t, public zap_client_t virtual int next_handshake_command (msg_t *msg_);
virtual int process_handshake_command (msg_t *msg_);
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
virtual int zap_msg_available ();
virtual status_t status () const;
private:
enum state_t
{ {
public: send_next_token,
gssapi_server_t (session_base_t *session_, recv_next_token,
const std::string &peer_address, expect_zap_reply,
const options_t &options_); send_ready,
virtual ~gssapi_server_t (); recv_ready,
connected
// mechanism implementation
virtual int next_handshake_command (msg_t *msg_);
virtual int process_handshake_command (msg_t *msg_);
virtual int encode (msg_t *msg_);
virtual int decode (msg_t *msg_);
virtual int zap_msg_available ();
virtual status_t status () const;
private:
enum state_t {
send_next_token,
recv_next_token,
expect_zap_reply,
send_ready,
recv_ready,
connected
};
session_base_t * const session;
const std::string peer_address;
// Current FSM state
state_t state;
// True iff server considers the client authenticated
bool security_context_established;
// The underlying mechanism type (ignored)
gss_OID doid;
void accept_context ();
int produce_next_token (msg_t *msg_);
int process_next_token (msg_t *msg_);
void send_zap_request ();
}; };
session_base_t *const session;
const std::string peer_address;
// Current FSM state
state_t state;
// True iff server considers the client authenticated
bool security_context_established;
// The underlying mechanism type (ignored)
gss_OID doid;
void accept_context ();
int produce_next_token (msg_t *msg_);
int process_next_token (msg_t *msg_);
void send_zap_request ();
};
} }
#endif #endif

View File

@@ -34,31 +34,27 @@
namespace zmq namespace zmq
{ {
class msg_t;
class msg_t; // Interface to be implemented by message decoder.
// Interface to be implemented by message decoder. class i_decoder
{
public:
virtual ~i_decoder () {}
class i_decoder virtual void get_buffer (unsigned char **data_, size_t *size_) = 0;
{
public:
virtual ~i_decoder () {}
virtual void get_buffer (unsigned char **data_, size_t *size_) = 0; virtual void resize_buffer (size_t) = 0;
// Decodes data pointed to by data_.
virtual void resize_buffer(size_t) = 0; // When a message is decoded, 1 is returned.
// Decodes data pointed to by data_. // When the decoder needs more data, 0 is returned.
// When a message is decoded, 1 is returned. // On error, -1 is returned and errno is set accordingly.
// When the decoder needs more data, 0 is returned. virtual int
// On error, -1 is returned and errno is set accordingly. decode (const unsigned char *data_, size_t size_, size_t &processed) = 0;
virtual int decode (const unsigned char *data_, size_t size_,
size_t &processed) = 0;
virtual msg_t *msg () = 0;
};
virtual msg_t *msg () = 0;
};
} }
#endif #endif

View File

@@ -34,27 +34,24 @@
namespace zmq namespace zmq
{ {
// Forward declaration
class msg_t;
// Forward declaration // Interface to be implemented by message encoder.
class msg_t;
// Interface to be implemented by message encoder. struct i_encoder
{
virtual ~i_encoder () {}
struct i_encoder // The function returns a batch of binary data. The data
{ // are filled to a supplied buffer. If no buffer is supplied (data_
virtual ~i_encoder () {} // is NULL) encoder will provide buffer of its own.
// Function returns 0 when a new message is required.
// The function returns a batch of binary data. The data virtual size_t encode (unsigned char **data_, size_t size) = 0;
// are filled to a supplied buffer. If no buffer is supplied (data_
// is NULL) encoder will provide buffer of its own.
// Function returns 0 when a new message is required.
virtual size_t encode (unsigned char **data_, size_t size) = 0;
// Load a new message into encoder.
virtual void load_msg (msg_t *msg_) = 0;
};
// Load a new message into encoder.
virtual void load_msg (msg_t *msg_) = 0;
};
} }
#endif #endif

View File

@@ -32,37 +32,34 @@
namespace zmq namespace zmq
{ {
class io_thread_t;
class io_thread_t; // Abstract interface to be implemented by various engines.
// Abstract interface to be implemented by various engines. struct i_engine
{
virtual ~i_engine () {}
struct i_engine // Plug the engine to the session.
{ virtual void plug (zmq::io_thread_t *io_thread_,
virtual ~i_engine () {} class session_base_t *session_) = 0;
// Plug the engine to the session. // Terminate and deallocate the engine. Note that 'detached'
virtual void plug (zmq::io_thread_t *io_thread_, // events are not fired on termination.
class session_base_t *session_) = 0; virtual void terminate () = 0;
// Terminate and deallocate the engine. Note that 'detached' // This method is called by the session to signalise that more
// events are not fired on termination. // messages can be written to the pipe.
virtual void terminate () = 0; virtual void restart_input () = 0;
// This method is called by the session to signalise that more // This method is called by the session to signalise that there
// messages can be written to the pipe. // are messages to send available.
virtual void restart_input () = 0; virtual void restart_output () = 0;
// This method is called by the session to signalise that there virtual void zap_msg_available () = 0;
// are messages to send available.
virtual void restart_output () = 0;
virtual void zap_msg_available () = 0;
virtual const char * get_endpoint() const = 0;
};
virtual const char *get_endpoint () const = 0;
};
} }
#endif #endif

View File

@@ -34,27 +34,24 @@
namespace zmq namespace zmq
{ {
// Interface to be implemented by mailbox. // Interface to be implemented by mailbox.
class i_mailbox class i_mailbox
{ {
public: public:
virtual ~i_mailbox () {} virtual ~i_mailbox () {}
virtual void send (const command_t &cmd_) = 0; virtual void send (const command_t &cmd_) = 0;
virtual int recv (command_t *cmd_, int timeout_) = 0; virtual int recv (command_t *cmd_, int timeout_) = 0;
#ifdef HAVE_FORK #ifdef HAVE_FORK
// close the file descriptors in the signaller. This is used in a forked // close the file descriptors in the signaller. This is used in a forked
// child process to close the file descriptors so that they do not interfere // child process to close the file descriptors so that they do not interfere
// with the context in the parent process. // with the context in the parent process.
virtual void forked () = 0; virtual void forked () = 0;
#endif #endif
};
};
} }
#endif #endif

View File

@@ -32,24 +32,22 @@
namespace zmq namespace zmq
{ {
// Virtual interface to be exposed by object that want to be notified
// about events on file descriptors.
// Virtual interface to be exposed by object that want to be notified struct i_poll_events
// about events on file descriptors. {
virtual ~i_poll_events () {}
struct i_poll_events // Called by I/O thread when file descriptor is ready for reading.
{ virtual void in_event () = 0;
virtual ~i_poll_events () {}
// Called by I/O thread when file descriptor is ready for reading. // Called by I/O thread when file descriptor is ready for writing.
virtual void in_event () = 0; virtual void out_event () = 0;
// Called by I/O thread when file descriptor is ready for writing.
virtual void out_event () = 0;
// Called when timer expires.
virtual void timer_event (int id_) = 0;
};
// Called when timer expires.
virtual void timer_event (int id_) = 0;
};
} }
#endif #endif

View File

@@ -32,8 +32,7 @@
#include "io_thread.hpp" #include "io_thread.hpp"
#include "err.hpp" #include "err.hpp"
zmq::io_object_t::io_object_t (io_thread_t *io_thread_) : zmq::io_object_t::io_object_t (io_thread_t *io_thread_) : poller (NULL)
poller (NULL)
{ {
if (io_thread_) if (io_thread_)
plug (io_thread_); plug (io_thread_);

View File

@@ -38,52 +38,47 @@
namespace zmq namespace zmq
{ {
class io_thread_t;
class io_thread_t; // Simple base class for objects that live in I/O threads.
// It makes communication with the poller object easier and
// makes defining unneeded event handlers unnecessary.
// Simple base class for objects that live in I/O threads. class io_object_t : public i_poll_events
// It makes communication with the poller object easier and {
// makes defining unneeded event handlers unnecessary. public:
io_object_t (zmq::io_thread_t *io_thread_ = NULL);
~io_object_t ();
class io_object_t : public i_poll_events // When migrating an object from one I/O thread to another, first
{ // unplug it, then migrate it, then plug it to the new thread.
public: void plug (zmq::io_thread_t *io_thread_);
void unplug ();
io_object_t (zmq::io_thread_t *io_thread_ = NULL); protected:
~io_object_t (); typedef poller_t::handle_t handle_t;
// When migrating an object from one I/O thread to another, first // Methods to access underlying poller object.
// unplug it, then migrate it, then plug it to the new thread. handle_t add_fd (fd_t fd_);
void plug (zmq::io_thread_t *io_thread_); void rm_fd (handle_t handle_);
void unplug (); void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void add_timer (int timout_, int id_);
void cancel_timer (int id_);
protected: // i_poll_events interface implementation.
void in_event ();
void out_event ();
void timer_event (int id_);
typedef poller_t::handle_t handle_t; private:
poller_t *poller;
// Methods to access underlying poller object.
handle_t add_fd (fd_t fd_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void add_timer (int timout_, int id_);
void cancel_timer (int id_);
// i_poll_events interface implementation.
void in_event ();
void out_event ();
void timer_event (int id_);
private:
poller_t *poller;
io_object_t (const io_object_t&);
const io_object_t &operator = (const io_object_t&);
};
io_object_t (const io_object_t &);
const io_object_t &operator= (const io_object_t &);
};
} }
#endif #endif

View File

@@ -51,7 +51,7 @@ zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) :
zmq::io_thread_t::~io_thread_t () zmq::io_thread_t::~io_thread_t ()
{ {
LIBZMQ_DELETE(poller); LIBZMQ_DELETE (poller);
} }
void zmq::io_thread_t::start () void zmq::io_thread_t::start ()

View File

@@ -40,60 +40,56 @@
namespace zmq namespace zmq
{ {
class ctx_t;
class ctx_t; // Generic part of the I/O thread. Polling-mechanism-specific features
// are implemented in separate "polling objects".
// Generic part of the I/O thread. Polling-mechanism-specific features class io_thread_t : public object_t, public i_poll_events
// are implemented in separate "polling objects". {
public:
io_thread_t (zmq::ctx_t *ctx_, uint32_t tid_);
class io_thread_t : public object_t, public i_poll_events // Clean-up. If the thread was started, it's necessary to call 'stop'
{ // before invoking destructor. Otherwise the destructor would hang up.
public: ~io_thread_t ();
io_thread_t (zmq::ctx_t *ctx_, uint32_t tid_); // Launch the physical thread.
void start ();
// Clean-up. If the thread was started, it's necessary to call 'stop' // Ask underlying thread to stop.
// before invoking destructor. Otherwise the destructor would hang up. void stop ();
~io_thread_t ();
// Launch the physical thread. // Returns mailbox associated with this I/O thread.
void start (); mailbox_t *get_mailbox ();
// Ask underlying thread to stop. // i_poll_events implementation.
void stop (); void in_event ();
void out_event ();
void timer_event (int id_);
// Returns mailbox associated with this I/O thread. // Used by io_objects to retrieve the associated poller object.
mailbox_t *get_mailbox (); poller_t *get_poller ();
// i_poll_events implementation. // Command handlers.
void in_event (); void process_stop ();
void out_event ();
void timer_event (int id_);
// Used by io_objects to retrieve the associated poller object. // Returns load experienced by the I/O thread.
poller_t *get_poller (); int get_load ();
// Command handlers. private:
void process_stop (); // I/O thread accesses incoming commands via this mailbox.
mailbox_t mailbox;
// Returns load experienced by the I/O thread. // Handle associated with mailbox' file descriptor.
int get_load (); poller_t::handle_t mailbox_handle;
private: // I/O multiplexing is performed using a poller object.
poller_t *poller;
// I/O thread accesses incoming commands via this mailbox.
mailbox_t mailbox;
// Handle associated with mailbox' file descriptor.
poller_t::handle_t mailbox_handle;
// I/O multiplexing is performed using a poller object.
poller_t *poller;
io_thread_t (const io_thread_t&);
const io_thread_t &operator = (const io_thread_t&);
};
io_thread_t (const io_thread_t &);
const io_thread_t &operator= (const io_thread_t &);
};
} }
#endif #endif

View File

@@ -64,9 +64,9 @@ zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
return -1; return -1;
#endif #endif
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that // If there's no SOCK_CLOEXEC, let's try the second best option. Note that
// race condition can cause socket not to be closed (if fork happens // race condition can cause socket not to be closed (if fork happens
// between socket creation and this point). // between socket creation and this point).
#if !defined ZMQ_HAVE_SOCK_CLOEXEC && defined FD_CLOEXEC #if !defined ZMQ_HAVE_SOCK_CLOEXEC && defined FD_CLOEXEC
rc = fcntl (s, F_SETFD, FD_CLOEXEC); rc = fcntl (s, F_SETFD, FD_CLOEXEC);
errno_assert (rc != -1); errno_assert (rc != -1);
@@ -106,7 +106,7 @@ void zmq::unblock_socket (fd_t s_)
void zmq::enable_ipv4_mapping (fd_t s_) void zmq::enable_ipv4_mapping (fd_t s_)
{ {
(void) s_; (void) s_;
#if defined IPV6_V6ONLY && !defined ZMQ_HAVE_OPENBSD #if defined IPV6_V6ONLY && !defined ZMQ_HAVE_OPENBSD
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
@@ -114,8 +114,8 @@ void zmq::enable_ipv4_mapping (fd_t s_)
#else #else
int flag = 0; int flag = 0;
#endif #endif
int rc = setsockopt (s_, IPPROTO_IPV6, IPV6_V6ONLY, (const char*) &flag, int rc = setsockopt (s_, IPPROTO_IPV6, IPV6_V6ONLY, (const char *) &flag,
sizeof (flag)); sizeof (flag));
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);
#else #else
@@ -130,38 +130,36 @@ int zmq::get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_)
struct sockaddr_storage ss; struct sockaddr_storage ss;
#if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_WINDOWS
int addrlen = static_cast <int> (sizeof ss); int addrlen = static_cast<int> (sizeof ss);
#else #else
socklen_t addrlen = sizeof ss; socklen_t addrlen = sizeof ss;
#endif #endif
rc = getpeername (sockfd_, (struct sockaddr*) &ss, &addrlen); rc = getpeername (sockfd_, (struct sockaddr *) &ss, &addrlen);
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
if (rc == SOCKET_ERROR) { if (rc == SOCKET_ERROR) {
const int last_error = WSAGetLastError(); const int last_error = WSAGetLastError ();
wsa_assert (last_error != WSANOTINITIALISED && wsa_assert (last_error != WSANOTINITIALISED && last_error != WSAEFAULT
last_error != WSAEFAULT && && last_error != WSAEINPROGRESS
last_error != WSAEINPROGRESS && && last_error != WSAENOTSOCK);
last_error != WSAENOTSOCK);
return 0; return 0;
} }
#else #else
if (rc == -1) { if (rc == -1) {
errno_assert (errno != EBADF && errno_assert (errno != EBADF && errno != EFAULT && errno != ENOTSOCK);
errno != EFAULT &&
errno != ENOTSOCK);
return 0; return 0;
} }
#endif #endif
char host [NI_MAXHOST]; char host[NI_MAXHOST];
rc = getnameinfo ((struct sockaddr*) &ss, addrlen, host, sizeof host, rc = getnameinfo ((struct sockaddr *) &ss, addrlen, host, sizeof host, NULL,
NULL, 0, NI_NUMERICHOST); 0, NI_NUMERICHOST);
if (rc != 0) if (rc != 0)
return 0; return 0;
ip_addr_ = host; ip_addr_ = host;
union { union
{
struct sockaddr sa; struct sockaddr sa;
struct sockaddr_storage sa_stor; struct sockaddr_storage sa_stor;
} u; } u;
@@ -172,7 +170,9 @@ int zmq::get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_)
void zmq::set_ip_type_of_service (fd_t s_, int iptos) void zmq::set_ip_type_of_service (fd_t s_, int iptos)
{ {
int rc = setsockopt(s_, IPPROTO_IP, IP_TOS, reinterpret_cast<const char*>(&iptos), sizeof(iptos)); int rc =
setsockopt (s_, IPPROTO_IP, IP_TOS,
reinterpret_cast<const char *> (&iptos), sizeof (iptos));
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);
@@ -181,19 +181,14 @@ void zmq::set_ip_type_of_service (fd_t s_, int iptos)
#endif #endif
// Windows and Hurd do not support IPV6_TCLASS // Windows and Hurd do not support IPV6_TCLASS
#if !defined (ZMQ_HAVE_WINDOWS) && defined (IPV6_TCLASS) #if !defined(ZMQ_HAVE_WINDOWS) && defined(IPV6_TCLASS)
rc = setsockopt( rc = setsockopt (s_, IPPROTO_IPV6, IPV6_TCLASS,
s_, reinterpret_cast<const char *> (&iptos), sizeof (iptos));
IPPROTO_IPV6,
IPV6_TCLASS,
reinterpret_cast<const char*>(&iptos),
sizeof(iptos));
// If IPv6 is not enabled ENOPROTOOPT will be returned on Linux and // If IPv6 is not enabled ENOPROTOOPT will be returned on Linux and
// EINVAL on OSX // EINVAL on OSX
if (rc == -1) { if (rc == -1) {
errno_assert (errno == ENOPROTOOPT || errno_assert (errno == ENOPROTOOPT || errno == EINVAL);
errno == EINVAL);
} }
#endif #endif
} }
@@ -221,7 +216,8 @@ int zmq::set_nosigpipe (fd_t s_)
void zmq::bind_to_device (fd_t s_, std::string &bound_device_) void zmq::bind_to_device (fd_t s_, std::string &bound_device_)
{ {
#ifdef ZMQ_HAVE_SO_BINDTODEVICE #ifdef ZMQ_HAVE_SO_BINDTODEVICE
int rc = setsockopt(s_, SOL_SOCKET, SO_BINDTODEVICE, bound_device_.c_str (), bound_device_.length ()); int rc = setsockopt (s_, SOL_SOCKET, SO_BINDTODEVICE,
bound_device_.c_str (), bound_device_.length ());
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
wsa_assert (rc != SOCKET_ERROR); wsa_assert (rc != SOCKET_ERROR);

View File

@@ -35,30 +35,28 @@
namespace zmq namespace zmq
{ {
// Same as socket(2), but allows for transparent tweaking the options.
fd_t open_socket (int domain_, int type_, int protocol_);
// Same as socket(2), but allows for transparent tweaking the options. // Sets the socket into non-blocking mode.
fd_t open_socket (int domain_, int type_, int protocol_); void unblock_socket (fd_t s_);
// Sets the socket into non-blocking mode. // Enable IPv4-mapping of addresses in case it is disabled by default.
void unblock_socket (fd_t s_); void enable_ipv4_mapping (fd_t s_);
// Enable IPv4-mapping of addresses in case it is disabled by default. // Returns string representation of peer's address.
void enable_ipv4_mapping (fd_t s_); // Socket sockfd_ must be connected. Returns true iff successful.
int get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_);
// Returns string representation of peer's address. // Sets the IP Type-Of-Service for the underlying socket
// Socket sockfd_ must be connected. Returns true iff successful. void set_ip_type_of_service (fd_t s_, int iptos);
int get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_);
// Sets the IP Type-Of-Service for the underlying socket // Sets the SO_NOSIGPIPE option for the underlying socket.
void set_ip_type_of_service (fd_t s_, int iptos); // Return 0 on success, -1 if the connection has been closed by the peer
int set_nosigpipe (fd_t s_);
// Sets the SO_NOSIGPIPE option for the underlying socket.
// Return 0 on success, -1 if the connection has been closed by the peer
int set_nosigpipe (fd_t s_);
// Binds the underlying socket to the given device, eg. VRF or interface
void bind_to_device (fd_t s_, std::string &bound_device_);
// Binds the underlying socket to the given device, eg. VRF or interface
void bind_to_device (fd_t s_, std::string &bound_device_);
} }
#endif #endif

View File

@@ -48,7 +48,7 @@ zmq::ipc_address_t::ipc_address_t (const sockaddr *sa, socklen_t sa_len)
memset (&address, 0, sizeof address); memset (&address, 0, sizeof address);
if (sa->sa_family == AF_UNIX) if (sa->sa_family == AF_UNIX)
memcpy(&address, sa, sa_len); memcpy (&address, sa, sa_len);
} }
zmq::ipc_address_t::~ipc_address_t () zmq::ipc_address_t::~ipc_address_t ()
@@ -61,7 +61,7 @@ int zmq::ipc_address_t::resolve (const char *path_)
errno = ENAMETOOLONG; errno = ENAMETOOLONG;
return -1; return -1;
} }
if (path_ [0] == '@' && !path_ [1]) { if (path_[0] == '@' && !path_[1]) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
@@ -69,7 +69,7 @@ int zmq::ipc_address_t::resolve (const char *path_)
address.sun_family = AF_UNIX; address.sun_family = AF_UNIX;
strcpy (address.sun_path, path_); strcpy (address.sun_path, path_);
/* Abstract sockets start with '\0' */ /* Abstract sockets start with '\0' */
if (path_ [0] == '@') if (path_[0] == '@')
*address.sun_path = '\0'; *address.sun_path = '\0';
return 0; return 0;
} }
@@ -83,23 +83,24 @@ int zmq::ipc_address_t::to_string (std::string &addr_)
std::stringstream s; std::stringstream s;
s << "ipc://"; s << "ipc://";
if (!address.sun_path [0] && address.sun_path [1]) if (!address.sun_path[0] && address.sun_path[1])
s << "@" << address.sun_path + 1; s << "@" << address.sun_path + 1;
else else
s << address.sun_path; s << address.sun_path;
addr_ = s.str (); addr_ = s.str ();
return 0; return 0;
} }
const sockaddr *zmq::ipc_address_t::addr () const const sockaddr *zmq::ipc_address_t::addr () const
{ {
return (sockaddr*) &address; return (sockaddr *) &address;
} }
socklen_t zmq::ipc_address_t::addrlen () const socklen_t zmq::ipc_address_t::addrlen () const
{ {
if (!address.sun_path [0] && address.sun_path [1]) if (!address.sun_path[0] && address.sun_path[1])
return (socklen_t) strlen (address.sun_path + 1) + sizeof (sa_family_t) + 1; return (socklen_t) strlen (address.sun_path + 1) + sizeof (sa_family_t)
+ 1;
return (socklen_t) sizeof address; return (socklen_t) sizeof address;
} }

View File

@@ -39,36 +39,30 @@
namespace zmq namespace zmq
{ {
class ipc_address_t
{
public:
ipc_address_t ();
ipc_address_t (const sockaddr *sa, socklen_t sa_len);
~ipc_address_t ();
class ipc_address_t // This function sets up the address for UNIX domain transport.
{ int resolve (const char *path_);
public:
ipc_address_t (); // The opposite to resolve()
ipc_address_t (const sockaddr *sa, socklen_t sa_len); int to_string (std::string &addr_);
~ipc_address_t ();
// This function sets up the address for UNIX domain transport. const sockaddr *addr () const;
int resolve (const char *path_); socklen_t addrlen () const;
// The opposite to resolve() private:
int to_string (std::string &addr_); struct sockaddr_un address;
const sockaddr *addr () const;
socklen_t addrlen () const;
private:
struct sockaddr_un address;
ipc_address_t (const ipc_address_t&);
const ipc_address_t &operator = (const ipc_address_t&);
};
ipc_address_t (const ipc_address_t &);
const ipc_address_t &operator= (const ipc_address_t &);
};
} }
#endif #endif
#endif #endif

View File

@@ -50,8 +50,10 @@
#include <sys/un.h> #include <sys/un.h>
zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_, zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
class session_base_t *session_, const options_t &options_, class session_base_t *session_,
const address_t *addr_, bool delayed_start_) : const options_t &options_,
const address_t *addr_,
bool delayed_start_) :
own_t (io_thread_, options_), own_t (io_thread_, options_),
io_object_t (io_thread_), io_object_t (io_thread_),
addr (addr_), addr (addr_),
@@ -60,12 +62,12 @@ zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
delayed_start (delayed_start_), delayed_start (delayed_start_),
timer_started (false), timer_started (false),
session (session_), session (session_),
current_reconnect_ivl(options.reconnect_ivl) current_reconnect_ivl (options.reconnect_ivl)
{ {
zmq_assert (addr); zmq_assert (addr);
zmq_assert (addr->protocol == "ipc"); zmq_assert (addr->protocol == "ipc");
addr->to_string (endpoint); addr->to_string (endpoint);
socket = session-> get_socket(); socket = session->get_socket ();
} }
zmq::ipc_connecter_t::~ipc_connecter_t () zmq::ipc_connecter_t::~ipc_connecter_t ()
@@ -118,12 +120,12 @@ void zmq::ipc_connecter_t::out_event ()
// Handle the error condition by attempt to reconnect. // Handle the error condition by attempt to reconnect.
if (fd == retired_fd) { if (fd == retired_fd) {
close (); close ();
add_reconnect_timer(); add_reconnect_timer ();
return; return;
} }
// Create the engine object for this connection. // Create the engine object for this connection.
stream_engine_t *engine = new (std::nothrow) stream_engine_t *engine =
stream_engine_t (fd, options, endpoint); new (std::nothrow) stream_engine_t (fd, options, endpoint);
alloc_assert (engine); alloc_assert (engine);
// Attach the engine to the corresponding session object. // Attach the engine to the corresponding session object.
@@ -155,12 +157,11 @@ void zmq::ipc_connecter_t::start_connecting ()
} }
// Connection establishment may be delayed. Poll for its completion. // Connection establishment may be delayed. Poll for its completion.
else else if (rc == -1 && errno == EINPROGRESS) {
if (rc == -1 && errno == EINPROGRESS) {
handle = add_fd (s); handle = add_fd (s);
handle_valid = true; handle_valid = true;
set_pollout (handle); set_pollout (handle);
socket->event_connect_delayed (endpoint, zmq_errno()); socket->event_connect_delayed (endpoint, zmq_errno ());
} }
// Handle any other error condition by eventual reconnect. // Handle any other error condition by eventual reconnect.
@@ -171,9 +172,9 @@ void zmq::ipc_connecter_t::start_connecting ()
} }
} }
void zmq::ipc_connecter_t::add_reconnect_timer() void zmq::ipc_connecter_t::add_reconnect_timer ()
{ {
int rc_ivl = get_new_reconnect_ivl(); int rc_ivl = get_new_reconnect_ivl ();
add_timer (rc_ivl, reconnect_timer_id); add_timer (rc_ivl, reconnect_timer_id);
socket->event_connect_retried (endpoint, rc_ivl); socket->event_connect_retried (endpoint, rc_ivl);
timer_started = true; timer_started = true;
@@ -182,17 +183,16 @@ void zmq::ipc_connecter_t::add_reconnect_timer()
int zmq::ipc_connecter_t::get_new_reconnect_ivl () int zmq::ipc_connecter_t::get_new_reconnect_ivl ()
{ {
// The new interval is the current interval + random value. // The new interval is the current interval + random value.
int this_interval = current_reconnect_ivl + int this_interval =
(generate_random () % options.reconnect_ivl); current_reconnect_ivl + (generate_random () % options.reconnect_ivl);
// Only change the current reconnect interval if the maximum reconnect // Only change the current reconnect interval if the maximum reconnect
// interval was set and if it's larger than the reconnect interval. // interval was set and if it's larger than the reconnect interval.
if (options.reconnect_ivl_max > 0 && if (options.reconnect_ivl_max > 0
options.reconnect_ivl_max > options.reconnect_ivl) { && options.reconnect_ivl_max > options.reconnect_ivl) {
// Calculate the next interval // Calculate the next interval
current_reconnect_ivl = current_reconnect_ivl * 2; current_reconnect_ivl = current_reconnect_ivl * 2;
if(current_reconnect_ivl >= options.reconnect_ivl_max) { if (current_reconnect_ivl >= options.reconnect_ivl_max) {
current_reconnect_ivl = options.reconnect_ivl_max; current_reconnect_ivl = options.reconnect_ivl_max;
} }
} }
@@ -212,9 +212,8 @@ int zmq::ipc_connecter_t::open ()
unblock_socket (s); unblock_socket (s);
// Connect to the remote peer. // Connect to the remote peer.
int rc = ::connect ( int rc = ::connect (s, addr->resolved.ipc_addr->addr (),
s, addr->resolved.ipc_addr->addr (), addr->resolved.ipc_addr->addrlen ());
addr->resolved.ipc_addr->addrlen ());
// Connect was successful immediately. // Connect was successful immediately.
if (rc == 0) if (rc == 0)
@@ -251,20 +250,19 @@ zmq::fd_t zmq::ipc_connecter_t::connect ()
#else #else
socklen_t len = sizeof (err); socklen_t len = sizeof (err);
#endif #endif
int rc = getsockopt (s, SOL_SOCKET, SO_ERROR, (char*) &err, &len); int rc = getsockopt (s, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
if (rc == -1) { if (rc == -1) {
if (errno == ENOPROTOOPT) if (errno == ENOPROTOOPT)
errno = 0; errno = 0;
err = errno; err = errno;
} }
if (err != 0) { if (err != 0) {
// Assert if the error was caused by 0MQ bug. // Assert if the error was caused by 0MQ bug.
// Networking problems are OK. No need to assert. // Networking problems are OK. No need to assert.
errno = err; errno = err;
errno_assert (errno == ECONNREFUSED || errno == ECONNRESET || errno_assert (errno == ECONNREFUSED || errno == ECONNRESET
errno == ETIMEDOUT || errno == EHOSTUNREACH || || errno == ETIMEDOUT || errno == EHOSTUNREACH
errno == ENETUNREACH || errno == ENETDOWN); || errno == ENETUNREACH || errno == ENETDOWN);
return retired_fd; return retired_fd;
} }
@@ -275,4 +273,3 @@ zmq::fd_t zmq::ipc_connecter_t::connect ()
} }
#endif #endif

View File

@@ -39,97 +39,97 @@
namespace zmq namespace zmq
{ {
class io_thread_t;
class session_base_t;
struct address_t;
class io_thread_t; class ipc_connecter_t : public own_t, public io_object_t
class session_base_t; {
struct address_t; public:
// If 'delayed_start' is true connecter first waits for a while,
// then starts connection process.
ipc_connecter_t (zmq::io_thread_t *io_thread_,
zmq::session_base_t *session_,
const options_t &options_,
const address_t *addr_,
bool delayed_start_);
~ipc_connecter_t ();
class ipc_connecter_t : public own_t, public io_object_t private:
// ID of the timer used to delay the reconnection.
enum
{ {
public: reconnect_timer_id = 1
// If 'delayed_start' is true connecter first waits for a while,
// then starts connection process.
ipc_connecter_t (zmq::io_thread_t *io_thread_,
zmq::session_base_t *session_, const options_t &options_,
const address_t *addr_, bool delayed_start_);
~ipc_connecter_t ();
private:
// ID of the timer used to delay the reconnection.
enum {reconnect_timer_id = 1};
// Handlers for incoming commands.
void process_plug ();
void process_term (int linger_);
// Handlers for I/O events.
void in_event ();
void out_event ();
void timer_event (int id_);
// Internal function to start the actual connection establishment.
void start_connecting ();
// Internal function to add a reconnect timer
void add_reconnect_timer();
// Internal function to return a reconnect backoff delay.
// Will modify the current_reconnect_ivl used for next call
// Returns the currently used interval
int get_new_reconnect_ivl ();
// Open IPC connecting socket. Returns -1 in case of error,
// 0 if connect was successful immediately. Returns -1 with
// EAGAIN errno if async connect was launched.
int open ();
// Close the connecting socket.
int close ();
// Get the file descriptor of newly created connection. Returns
// retired_fd if the connection was unsuccessful.
fd_t connect ();
// Address to connect to. Owned by session_base_t.
const address_t *addr;
// Underlying socket.
fd_t s;
// Handle corresponding to the listening socket.
handle_t handle;
// If true file descriptor is registered with the poller and 'handle'
// contains valid value.
bool handle_valid;
// If true, connecter is waiting a while before trying to connect.
const bool delayed_start;
// True iff a timer has been started.
bool timer_started;
// Reference to the session we belong to.
zmq::session_base_t *session;
// Current reconnect ivl, updated for backoff strategy
int current_reconnect_ivl;
// String representation of endpoint to connect to
std::string endpoint;
// Socket
zmq::socket_base_t *socket;
ipc_connecter_t (const ipc_connecter_t&);
const ipc_connecter_t &operator = (const ipc_connecter_t&);
}; };
// Handlers for incoming commands.
void process_plug ();
void process_term (int linger_);
// Handlers for I/O events.
void in_event ();
void out_event ();
void timer_event (int id_);
// Internal function to start the actual connection establishment.
void start_connecting ();
// Internal function to add a reconnect timer
void add_reconnect_timer ();
// Internal function to return a reconnect backoff delay.
// Will modify the current_reconnect_ivl used for next call
// Returns the currently used interval
int get_new_reconnect_ivl ();
// Open IPC connecting socket. Returns -1 in case of error,
// 0 if connect was successful immediately. Returns -1 with
// EAGAIN errno if async connect was launched.
int open ();
// Close the connecting socket.
int close ();
// Get the file descriptor of newly created connection. Returns
// retired_fd if the connection was unsuccessful.
fd_t connect ();
// Address to connect to. Owned by session_base_t.
const address_t *addr;
// Underlying socket.
fd_t s;
// Handle corresponding to the listening socket.
handle_t handle;
// If true file descriptor is registered with the poller and 'handle'
// contains valid value.
bool handle_valid;
// If true, connecter is waiting a while before trying to connect.
const bool delayed_start;
// True iff a timer has been started.
bool timer_started;
// Reference to the session we belong to.
zmq::session_base_t *session;
// Current reconnect ivl, updated for backoff strategy
int current_reconnect_ivl;
// String representation of endpoint to connect to
std::string endpoint;
// Socket
zmq::socket_base_t *socket;
ipc_connecter_t (const ipc_connecter_t &);
const ipc_connecter_t &operator= (const ipc_connecter_t &);
};
} }
#endif #endif
#endif #endif

View File

@@ -52,42 +52,41 @@
#include <sys/stat.h> #include <sys/stat.h>
#ifdef ZMQ_HAVE_LOCAL_PEERCRED #ifdef ZMQ_HAVE_LOCAL_PEERCRED
# include <sys/types.h> #include <sys/types.h>
# include <sys/ucred.h> #include <sys/ucred.h>
#endif #endif
#ifdef ZMQ_HAVE_SO_PEERCRED #ifdef ZMQ_HAVE_SO_PEERCRED
# include <sys/types.h> #include <sys/types.h>
# include <pwd.h> #include <pwd.h>
# include <grp.h> #include <grp.h>
# if defined ZMQ_HAVE_OPENBSD #if defined ZMQ_HAVE_OPENBSD
# define ucred sockpeercred #define ucred sockpeercred
# endif #endif
#endif #endif
const char *zmq::ipc_listener_t::tmp_env_vars[] = { const char *zmq::ipc_listener_t::tmp_env_vars[] = {
"TMPDIR", "TMPDIR", "TEMPDIR", "TMP",
"TEMPDIR", 0 // Sentinel
"TMP",
0 // Sentinel
}; };
int zmq::ipc_listener_t::create_wildcard_address(std::string& path_, int zmq::ipc_listener_t::create_wildcard_address (std::string &path_,
std::string& file_) std::string &file_)
{ {
std::string tmp_path; std::string tmp_path;
// If TMPDIR, TEMPDIR, or TMP are available and are directories, create // If TMPDIR, TEMPDIR, or TMP are available and are directories, create
// the socket directory there. // the socket directory there.
const char **tmp_env = tmp_env_vars; const char **tmp_env = tmp_env_vars;
while ( tmp_path.empty() && *tmp_env != 0 ) { while (tmp_path.empty () && *tmp_env != 0) {
char *tmpdir = getenv(*tmp_env); char *tmpdir = getenv (*tmp_env);
struct stat statbuf; struct stat statbuf;
// Confirm it is actually a directory before trying to use // Confirm it is actually a directory before trying to use
if ( tmpdir != 0 && ::stat(tmpdir, &statbuf) == 0 && S_ISDIR(statbuf.st_mode) ) { if (tmpdir != 0 && ::stat (tmpdir, &statbuf) == 0
tmp_path.assign(tmpdir); && S_ISDIR (statbuf.st_mode)) {
if ( *(tmp_path.rbegin()) != '/' ) { tmp_path.assign (tmpdir);
tmp_path.push_back('/'); if (*(tmp_path.rbegin ()) != '/') {
tmp_path.push_back ('/');
} }
} }
@@ -96,10 +95,10 @@ int zmq::ipc_listener_t::create_wildcard_address(std::string& path_,
} }
// Append a directory name // Append a directory name
tmp_path.append("tmpXXXXXX"); tmp_path.append ("tmpXXXXXX");
// We need room for tmp_path + trailing NUL // We need room for tmp_path + trailing NUL
std::vector<char> buffer(tmp_path.length()+1); std::vector<char> buffer (tmp_path.length () + 1);
strcpy (&buffer[0], tmp_path.c_str ()); strcpy (&buffer[0], tmp_path.c_str ());
#ifdef HAVE_MKDTEMP #ifdef HAVE_MKDTEMP
@@ -121,7 +120,7 @@ int zmq::ipc_listener_t::create_wildcard_address(std::string& path_,
(void) path_; (void) path_;
int fd = mkstemp (&buffer[0]); int fd = mkstemp (&buffer[0]);
if (fd == -1) if (fd == -1)
return -1; return -1;
::close (fd); ::close (fd);
file_.assign (&buffer[0]); file_.assign (&buffer[0]);
@@ -131,7 +130,8 @@ int zmq::ipc_listener_t::create_wildcard_address(std::string& path_,
} }
zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_, zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_,
socket_base_t *socket_, const options_t &options_) : socket_base_t *socket_,
const options_t &options_) :
own_t (io_thread_, options_), own_t (io_thread_, options_),
io_object_t (io_thread_), io_object_t (io_thread_),
has_file (false), has_file (false),
@@ -166,13 +166,13 @@ void zmq::ipc_listener_t::in_event ()
// If connection was reset by the peer in the meantime, just ignore it. // If connection was reset by the peer in the meantime, just ignore it.
// TODO: Handle specific errors like ENFILE/EMFILE etc. // TODO: Handle specific errors like ENFILE/EMFILE etc.
if (fd == retired_fd) { if (fd == retired_fd) {
socket->event_accept_failed (endpoint, zmq_errno()); socket->event_accept_failed (endpoint, zmq_errno ());
return; return;
} }
// Create the engine object for this connection. // Create the engine object for this connection.
stream_engine_t *engine = new (std::nothrow) stream_engine_t *engine =
stream_engine_t (fd, options, endpoint); new (std::nothrow) stream_engine_t (fd, options, endpoint);
alloc_assert (engine); alloc_assert (engine);
// Choose I/O thread to run connecter in. Given that we are already // Choose I/O thread to run connecter in. Given that we are already
@@ -181,8 +181,8 @@ void zmq::ipc_listener_t::in_event ()
zmq_assert (io_thread); zmq_assert (io_thread);
// Create and launch a session object. // Create and launch a session object.
session_base_t *session = session_base_t::create (io_thread, false, socket, session_base_t *session =
options, NULL); session_base_t::create (io_thread, false, socket, options, NULL);
errno_assert (session); errno_assert (session);
session->inc_seqnum (); session->inc_seqnum ();
launch_child (session); launch_child (session);
@@ -214,8 +214,8 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
std::string addr (addr_); std::string addr (addr_);
// Allow wildcard file // Allow wildcard file
if (options.use_fd == -1 && addr [0] == '*') { if (options.use_fd == -1 && addr[0] == '*') {
if ( create_wildcard_address(tmp_socket_dirname, addr) < 0 ) { if (create_wildcard_address (tmp_socket_dirname, addr) < 0) {
return -1; return -1;
} }
} }
@@ -226,19 +226,19 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
// working after the first client connects. The user will take care of // working after the first client connects. The user will take care of
// cleaning up the file after the service is stopped. // cleaning up the file after the service is stopped.
if (options.use_fd == -1) { if (options.use_fd == -1) {
::unlink (addr.c_str()); ::unlink (addr.c_str ());
} }
filename.clear (); filename.clear ();
// Initialise the address structure. // Initialise the address structure.
ipc_address_t address; ipc_address_t address;
int rc = address.resolve (addr.c_str()); int rc = address.resolve (addr.c_str ());
if (rc != 0) { if (rc != 0) {
if ( !tmp_socket_dirname.empty() ) { if (!tmp_socket_dirname.empty ()) {
// We need to preserve errno to return to the user // We need to preserve errno to return to the user
int errno_ = errno; int errno_ = errno;
::rmdir(tmp_socket_dirname.c_str ()); ::rmdir (tmp_socket_dirname.c_str ());
tmp_socket_dirname.clear(); tmp_socket_dirname.clear ();
errno = errno_; errno = errno_;
} }
return -1; return -1;
@@ -252,11 +252,11 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
// Create a listening socket. // Create a listening socket.
s = open_socket (AF_UNIX, SOCK_STREAM, 0); s = open_socket (AF_UNIX, SOCK_STREAM, 0);
if (s == -1) { if (s == -1) {
if ( !tmp_socket_dirname.empty() ) { if (!tmp_socket_dirname.empty ()) {
// We need to preserve errno to return to the user // We need to preserve errno to return to the user
int errno_ = errno; int errno_ = errno;
::rmdir(tmp_socket_dirname.c_str ()); ::rmdir (tmp_socket_dirname.c_str ());
tmp_socket_dirname.clear(); tmp_socket_dirname.clear ();
errno = errno_; errno = errno_;
} }
return -1; return -1;
@@ -273,7 +273,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
goto error; goto error;
} }
filename.assign (addr.c_str()); filename.assign (addr.c_str ());
has_file = true; has_file = true;
socket->event_listening (endpoint, s); socket->event_listening (endpoint, s);
@@ -298,13 +298,13 @@ int zmq::ipc_listener_t::close ()
if (has_file && options.use_fd == -1) { if (has_file && options.use_fd == -1) {
rc = 0; rc = 0;
if ( rc == 0 && !tmp_socket_dirname.empty() ) { if (rc == 0 && !tmp_socket_dirname.empty ()) {
rc = ::rmdir(tmp_socket_dirname.c_str ()); rc = ::rmdir (tmp_socket_dirname.c_str ());
tmp_socket_dirname.clear(); tmp_socket_dirname.clear ();
} }
if (rc != 0) { if (rc != 0) {
socket->event_close_failed (endpoint, zmq_errno()); socket->event_close_failed (endpoint, zmq_errno ());
return -1; return -1;
} }
} }
@@ -317,9 +317,9 @@ int zmq::ipc_listener_t::close ()
bool zmq::ipc_listener_t::filter (fd_t sock) bool zmq::ipc_listener_t::filter (fd_t sock)
{ {
if (options.ipc_uid_accept_filters.empty () && if (options.ipc_uid_accept_filters.empty ()
options.ipc_pid_accept_filters.empty () && && options.ipc_pid_accept_filters.empty ()
options.ipc_gid_accept_filters.empty ()) && options.ipc_gid_accept_filters.empty ())
return true; return true;
struct ucred cred; struct ucred cred;
@@ -327,9 +327,12 @@ bool zmq::ipc_listener_t::filter (fd_t sock)
if (getsockopt (sock, SOL_SOCKET, SO_PEERCRED, &cred, &size)) if (getsockopt (sock, SOL_SOCKET, SO_PEERCRED, &cred, &size))
return false; return false;
if (options.ipc_uid_accept_filters.find (cred.uid) != options.ipc_uid_accept_filters.end () || if (options.ipc_uid_accept_filters.find (cred.uid)
options.ipc_gid_accept_filters.find (cred.gid) != options.ipc_gid_accept_filters.end () || != options.ipc_uid_accept_filters.end ()
options.ipc_pid_accept_filters.find (cred.pid) != options.ipc_pid_accept_filters.end ()) || options.ipc_gid_accept_filters.find (cred.gid)
!= options.ipc_gid_accept_filters.end ()
|| options.ipc_pid_accept_filters.find (cred.pid)
!= options.ipc_pid_accept_filters.end ())
return true; return true;
struct passwd *pw; struct passwd *pw;
@@ -337,8 +340,9 @@ bool zmq::ipc_listener_t::filter (fd_t sock)
if (!(pw = getpwuid (cred.uid))) if (!(pw = getpwuid (cred.uid)))
return false; return false;
for (options_t::ipc_gid_accept_filters_t::const_iterator it = options.ipc_gid_accept_filters.begin (); for (options_t::ipc_gid_accept_filters_t::const_iterator it =
it != options.ipc_gid_accept_filters.end (); it++) { options.ipc_gid_accept_filters.begin ();
it != options.ipc_gid_accept_filters.end (); it++) {
if (!(gr = getgrgid (*it))) if (!(gr = getgrgid (*it)))
continue; continue;
for (char **mem = gr->gr_mem; *mem; mem++) { for (char **mem = gr->gr_mem; *mem; mem++) {
@@ -353,8 +357,8 @@ bool zmq::ipc_listener_t::filter (fd_t sock)
bool zmq::ipc_listener_t::filter (fd_t sock) bool zmq::ipc_listener_t::filter (fd_t sock)
{ {
if (options.ipc_uid_accept_filters.empty () && if (options.ipc_uid_accept_filters.empty ()
options.ipc_gid_accept_filters.empty ()) && options.ipc_gid_accept_filters.empty ())
return true; return true;
struct xucred cred; struct xucred cred;
@@ -364,10 +368,12 @@ bool zmq::ipc_listener_t::filter (fd_t sock)
return false; return false;
if (cred.cr_version != XUCRED_VERSION) if (cred.cr_version != XUCRED_VERSION)
return false; return false;
if (options.ipc_uid_accept_filters.find (cred.cr_uid) != options.ipc_uid_accept_filters.end ()) if (options.ipc_uid_accept_filters.find (cred.cr_uid)
!= options.ipc_uid_accept_filters.end ())
return true; return true;
for (int i = 0; i < cred.cr_ngroups; i++) { for (int i = 0; i < cred.cr_ngroups; i++) {
if (options.ipc_gid_accept_filters.find (cred.cr_groups[i]) != options.ipc_gid_accept_filters.end ()) if (options.ipc_gid_accept_filters.find (cred.cr_groups[i])
!= options.ipc_gid_accept_filters.end ())
return true; return true;
} }
@@ -388,13 +394,14 @@ zmq::fd_t zmq::ipc_listener_t::accept ()
fd_t sock = ::accept (s, NULL, NULL); fd_t sock = ::accept (s, NULL, NULL);
#endif #endif
if (sock == -1) { if (sock == -1) {
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
errno == EINTR || errno == ECONNABORTED || errno == EPROTO || || errno == ECONNABORTED || errno == EPROTO
errno == ENFILE); || errno == ENFILE);
return retired_fd; return retired_fd;
} }
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) && defined FD_CLOEXEC #if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
&& defined FD_CLOEXEC
// Race condition can cause socket not to be closed (if fork happens // Race condition can cause socket not to be closed (if fork happens
// between accept and this point). // between accept and this point).
int rc = fcntl (sock, F_SETFD, FD_CLOEXEC); int rc = fcntl (sock, F_SETFD, FD_CLOEXEC);

View File

@@ -41,83 +41,78 @@
namespace zmq namespace zmq
{ {
class io_thread_t;
class socket_base_t;
class io_thread_t; class ipc_listener_t : public own_t, public io_object_t
class socket_base_t; {
public:
ipc_listener_t (zmq::io_thread_t *io_thread_,
zmq::socket_base_t *socket_,
const options_t &options_);
~ipc_listener_t ();
class ipc_listener_t : public own_t, public io_object_t // Set address to listen on.
{ int set_address (const char *addr_);
public:
ipc_listener_t (zmq::io_thread_t *io_thread_, // Get the bound address for use with wildcards
zmq::socket_base_t *socket_, const options_t &options_); int get_address (std::string &addr_);
~ipc_listener_t ();
// Set address to listen on. private:
int set_address (const char *addr_); // Handlers for incoming commands.
void process_plug ();
void process_term (int linger_);
// Get the bound address for use with wildcards // Handlers for I/O events.
int get_address (std::string &addr_); void in_event ();
private: // Close the listening socket.
int close ();
// Handlers for incoming commands. // Create wildcard path address
void process_plug (); static int create_wildcard_address (std::string &path_, std::string &file_);
void process_term (int linger_);
// Handlers for I/O events. // Filter new connections if the OS provides a mechanism to get
void in_event (); // the credentials of the peer process. Called from accept().
#if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
bool filter (fd_t sock);
#endif
// Close the listening socket. // Accept the new connection. Returns the file descriptor of the
int close (); // newly created connection. The function may return retired_fd
// if the connection was dropped while waiting in the listen backlog.
fd_t accept ();
// Create wildcard path address // True, if the underlying file for UNIX domain socket exists.
static int create_wildcard_address(std::string& path_, bool has_file;
std::string& file_);
// Filter new connections if the OS provides a mechanism to get // Name of the temporary directory (if any) that has the
// the credentials of the peer process. Called from accept(). // the UNIX domain socket
# if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED std::string tmp_socket_dirname;
bool filter (fd_t sock);
# endif
// Accept the new connection. Returns the file descriptor of the // Name of the file associated with the UNIX domain address.
// newly created connection. The function may return retired_fd std::string filename;
// if the connection was dropped while waiting in the listen backlog.
fd_t accept ();
// True, if the underlying file for UNIX domain socket exists. // Underlying socket.
bool has_file; fd_t s;
// Name of the temporary directory (if any) that has the // Handle corresponding to the listening socket.
// the UNIX domain socket handle_t handle;
std::string tmp_socket_dirname;
// Name of the file associated with the UNIX domain address. // Socket the listener belongs to.
std::string filename; zmq::socket_base_t *socket;
// Underlying socket. // String representation of endpoint to bind to
fd_t s; std::string endpoint;
// Handle corresponding to the listening socket. // Acceptable temporary directory environment variables
handle_t handle; static const char *tmp_env_vars[];
// Socket the listener belongs to.
zmq::socket_base_t *socket;
// String representation of endpoint to bind to
std::string endpoint;
// Acceptable temporary directory environment variables
static const char *tmp_env_vars[];
ipc_listener_t (const ipc_listener_t&);
const ipc_listener_t &operator = (const ipc_listener_t&);
};
ipc_listener_t (const ipc_listener_t &);
const ipc_listener_t &operator= (const ipc_listener_t &);
};
} }
#endif #endif
#endif #endif

View File

@@ -54,15 +54,13 @@
#define kevent_udata_t void * #define kevent_udata_t void *
#endif #endif
zmq::kqueue_t::kqueue_t (const zmq::ctx_t &ctx_) : zmq::kqueue_t::kqueue_t (const zmq::ctx_t &ctx_) : ctx (ctx_), stopping (false)
ctx(ctx_),
stopping (false)
{ {
// Create event queue // Create event queue
kqueue_fd = kqueue (); kqueue_fd = kqueue ();
errno_assert (kqueue_fd != -1); errno_assert (kqueue_fd != -1);
#ifdef HAVE_FORK #ifdef HAVE_FORK
pid = getpid(); pid = getpid ();
#endif #endif
} }
@@ -76,7 +74,7 @@ void zmq::kqueue_t::kevent_add (fd_t fd_, short filter_, void *udata_)
{ {
struct kevent ev; struct kevent ev;
EV_SET (&ev, fd_, filter_, EV_ADD, 0, 0, (kevent_udata_t)udata_); EV_SET (&ev, fd_, filter_, EV_ADD, 0, 0, (kevent_udata_t) udata_);
int rc = kevent (kqueue_fd, &ev, 1, NULL, 0, NULL); int rc = kevent (kqueue_fd, &ev, 1, NULL, 0, NULL);
errno_assert (rc != -1); errno_assert (rc != -1);
} }
@@ -91,7 +89,7 @@ void zmq::kqueue_t::kevent_delete (fd_t fd_, short filter_)
} }
zmq::kqueue_t::handle_t zmq::kqueue_t::add_fd (fd_t fd_, zmq::kqueue_t::handle_t zmq::kqueue_t::add_fd (fd_t fd_,
i_poll_events *reactor_) i_poll_events *reactor_)
{ {
poll_entry_t *pe = new (std::nothrow) poll_entry_t; poll_entry_t *pe = new (std::nothrow) poll_entry_t;
alloc_assert (pe); alloc_assert (pe);
@@ -108,7 +106,7 @@ zmq::kqueue_t::handle_t zmq::kqueue_t::add_fd (fd_t fd_,
void zmq::kqueue_t::rm_fd (handle_t handle_) void zmq::kqueue_t::rm_fd (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
if (pe->flag_pollin) if (pe->flag_pollin)
kevent_delete (pe->fd, EVFILT_READ); kevent_delete (pe->fd, EVFILT_READ);
if (pe->flag_pollout) if (pe->flag_pollout)
@@ -121,7 +119,7 @@ void zmq::kqueue_t::rm_fd (handle_t handle_)
void zmq::kqueue_t::set_pollin (handle_t handle_) void zmq::kqueue_t::set_pollin (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
if (likely (!pe->flag_pollin)) { if (likely (!pe->flag_pollin)) {
pe->flag_pollin = true; pe->flag_pollin = true;
kevent_add (pe->fd, EVFILT_READ, pe); kevent_add (pe->fd, EVFILT_READ, pe);
@@ -130,7 +128,7 @@ void zmq::kqueue_t::set_pollin (handle_t handle_)
void zmq::kqueue_t::reset_pollin (handle_t handle_) void zmq::kqueue_t::reset_pollin (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
if (likely (pe->flag_pollin)) { if (likely (pe->flag_pollin)) {
pe->flag_pollin = false; pe->flag_pollin = false;
kevent_delete (pe->fd, EVFILT_READ); kevent_delete (pe->fd, EVFILT_READ);
@@ -139,7 +137,7 @@ void zmq::kqueue_t::reset_pollin (handle_t handle_)
void zmq::kqueue_t::set_pollout (handle_t handle_) void zmq::kqueue_t::set_pollout (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
if (likely (!pe->flag_pollout)) { if (likely (!pe->flag_pollout)) {
pe->flag_pollout = true; pe->flag_pollout = true;
kevent_add (pe->fd, EVFILT_WRITE, pe); kevent_add (pe->fd, EVFILT_WRITE, pe);
@@ -148,11 +146,11 @@ void zmq::kqueue_t::set_pollout (handle_t handle_)
void zmq::kqueue_t::reset_pollout (handle_t handle_) void zmq::kqueue_t::reset_pollout (handle_t handle_)
{ {
poll_entry_t *pe = (poll_entry_t*) handle_; poll_entry_t *pe = (poll_entry_t *) handle_;
if (likely (pe->flag_pollout)) { if (likely (pe->flag_pollout)) {
pe->flag_pollout = false; pe->flag_pollout = false;
kevent_delete (pe->fd, EVFILT_WRITE); kevent_delete (pe->fd, EVFILT_WRITE);
} }
} }
void zmq::kqueue_t::start () void zmq::kqueue_t::start ()
@@ -173,17 +171,16 @@ int zmq::kqueue_t::max_fds ()
void zmq::kqueue_t::loop () void zmq::kqueue_t::loop ()
{ {
while (!stopping) { while (!stopping) {
// Execute any due timers. // Execute any due timers.
int timeout = (int) execute_timers (); int timeout = (int) execute_timers ();
// Wait for events. // Wait for events.
struct kevent ev_buf [max_io_events]; struct kevent ev_buf[max_io_events];
timespec ts = {timeout / 1000, (timeout % 1000) * 1000000}; timespec ts = {timeout / 1000, (timeout % 1000) * 1000000};
int n = kevent (kqueue_fd, NULL, 0, &ev_buf [0], max_io_events, int n = kevent (kqueue_fd, NULL, 0, &ev_buf[0], max_io_events,
timeout ? &ts: NULL); timeout ? &ts : NULL);
#ifdef HAVE_FORK #ifdef HAVE_FORK
if (unlikely(pid != getpid())) { if (unlikely (pid != getpid ())) {
//printf("zmq::kqueue_t::loop aborting on forked child %d\n", (int)getpid()); //printf("zmq::kqueue_t::loop aborting on forked child %d\n", (int)getpid());
// simply exit the loop in a forked process. // simply exit the loop in a forked process.
return; return;
@@ -194,26 +191,27 @@ void zmq::kqueue_t::loop ()
continue; continue;
} }
for (int i = 0; i < n; i ++) { for (int i = 0; i < n; i++) {
poll_entry_t *pe = (poll_entry_t*) ev_buf [i].udata; poll_entry_t *pe = (poll_entry_t *) ev_buf[i].udata;
if (pe->fd == retired_fd) if (pe->fd == retired_fd)
continue; continue;
if (ev_buf [i].flags & EV_EOF) if (ev_buf[i].flags & EV_EOF)
pe->reactor->in_event (); pe->reactor->in_event ();
if (pe->fd == retired_fd) if (pe->fd == retired_fd)
continue; continue;
if (ev_buf [i].filter == EVFILT_WRITE) if (ev_buf[i].filter == EVFILT_WRITE)
pe->reactor->out_event (); pe->reactor->out_event ();
if (pe->fd == retired_fd) if (pe->fd == retired_fd)
continue; continue;
if (ev_buf [i].filter == EVFILT_READ) if (ev_buf[i].filter == EVFILT_READ)
pe->reactor->in_event (); pe->reactor->in_event ();
} }
// Destroy retired event sources. // Destroy retired event sources.
for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) { for (retired_t::iterator it = retired.begin (); it != retired.end ();
LIBZMQ_DELETE(*it); ++it) {
LIBZMQ_DELETE (*it);
} }
retired.clear (); retired.clear ();
} }
@@ -221,7 +219,7 @@ void zmq::kqueue_t::loop ()
void zmq::kqueue_t::worker_routine (void *arg_) void zmq::kqueue_t::worker_routine (void *arg_)
{ {
((kqueue_t*) arg_)->loop (); ((kqueue_t *) arg_)->loop ();
} }
#endif #endif

View File

@@ -44,82 +44,78 @@
namespace zmq namespace zmq
{ {
struct i_poll_events;
struct i_poll_events; // Implements socket polling mechanism using the BSD-specific
// kqueue interface.
// Implements socket polling mechanism using the BSD-specific class kqueue_t : public poller_base_t
// kqueue interface. {
public:
typedef void *handle_t;
class kqueue_t : public poller_base_t kqueue_t (const ctx_t &ctx_);
~kqueue_t ();
// "poller" concept.
handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void start ();
void stop ();
static int max_fds ();
private:
// Main worker thread routine.
static void worker_routine (void *arg_);
// Main event loop.
void loop ();
// Reference to ZMQ context.
const ctx_t &ctx;
// File descriptor referring to the kernel event queue.
fd_t kqueue_fd;
// Adds the event to the kqueue.
void kevent_add (fd_t fd_, short filter_, void *udata_);
// Deletes the event from the kqueue.
void kevent_delete (fd_t fd_, short filter_);
struct poll_entry_t
{ {
public: fd_t fd;
bool flag_pollin;
typedef void* handle_t; bool flag_pollout;
zmq::i_poll_events *reactor;
kqueue_t (const ctx_t &ctx_);
~kqueue_t ();
// "poller" concept.
handle_t add_fd (fd_t fd_, zmq::i_poll_events *events_);
void rm_fd (handle_t handle_);
void set_pollin (handle_t handle_);
void reset_pollin (handle_t handle_);
void set_pollout (handle_t handle_);
void reset_pollout (handle_t handle_);
void start ();
void stop ();
static int max_fds ();
private:
// Main worker thread routine.
static void worker_routine (void *arg_);
// Main event loop.
void loop ();
// Reference to ZMQ context.
const ctx_t &ctx;
// File descriptor referring to the kernel event queue.
fd_t kqueue_fd;
// Adds the event to the kqueue.
void kevent_add (fd_t fd_, short filter_, void *udata_);
// Deletes the event from the kqueue.
void kevent_delete (fd_t fd_, short filter_);
struct poll_entry_t
{
fd_t fd;
bool flag_pollin;
bool flag_pollout;
zmq::i_poll_events *reactor;
};
// List of retired event sources.
typedef std::vector <poll_entry_t*> retired_t;
retired_t retired;
// If true, thread is in the process of shutting down.
bool stopping;
// Handle of the physical thread doing the I/O work.
thread_t worker;
kqueue_t (const kqueue_t&);
const kqueue_t &operator = (const kqueue_t&);
#ifdef HAVE_FORK
// the process that created this context. Used to detect forking.
pid_t pid;
#endif
}; };
typedef kqueue_t poller_t; // List of retired event sources.
typedef std::vector<poll_entry_t *> retired_t;
retired_t retired;
// If true, thread is in the process of shutting down.
bool stopping;
// Handle of the physical thread doing the I/O work.
thread_t worker;
kqueue_t (const kqueue_t &);
const kqueue_t &operator= (const kqueue_t &);
#ifdef HAVE_FORK
// the process that created this context. Used to detect forking.
pid_t pid;
#endif
};
typedef kqueue_t poller_t;
} }
#endif #endif

View File

@@ -33,11 +33,7 @@
#include "err.hpp" #include "err.hpp"
#include "msg.hpp" #include "msg.hpp"
zmq::lb_t::lb_t () : zmq::lb_t::lb_t () : active (0), current (0), more (false), dropping (false)
active (0),
current (0),
more (false),
dropping (false)
{ {
} }
@@ -89,7 +85,6 @@ int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
// Drop the message if required. If we are at the end of the message // Drop the message if required. If we are at the end of the message
// switch back to non-dropping mode. // switch back to non-dropping mode.
if (dropping) { if (dropping) {
more = msg_->flags () & msg_t::more ? true : false; more = msg_->flags () & msg_t::more ? true : false;
dropping = more; dropping = more;
@@ -101,19 +96,17 @@ int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
} }
while (active > 0) { while (active > 0) {
if (pipes [current]->write (msg_)) if (pipes[current]->write (msg_)) {
{
if (pipe_) if (pipe_)
*pipe_ = pipes [current]; *pipe_ = pipes[current];
break; break;
} }
// If send fails for multi-part msg rollback other // If send fails for multi-part msg rollback other
// parts sent earlier and return EAGAIN. // parts sent earlier and return EAGAIN.
// Application should handle this as suitable // Application should handle this as suitable
if (more) if (more) {
{ pipes[current]->rollback ();
pipes [current]->rollback ();
more = 0; more = 0;
errno = EAGAIN; errno = EAGAIN;
return -1; return -1;
@@ -134,9 +127,9 @@ int zmq::lb_t::sendpipe (msg_t *msg_, pipe_t **pipe_)
// If it's final part of the message we can flush it downstream and // If it's final part of the message we can flush it downstream and
// continue round-robining (load balance). // continue round-robining (load balance).
more = msg_->flags () & msg_t::more? true: false; more = msg_->flags () & msg_t::more ? true : false;
if (!more) { if (!more) {
pipes [current]->flush (); pipes[current]->flush ();
if (++current >= active) if (++current >= active)
current = 0; current = 0;
@@ -157,9 +150,8 @@ bool zmq::lb_t::has_out ()
return true; return true;
while (active > 0) { while (active > 0) {
// Check whether a pipe has room for another message. // Check whether a pipe has room for another message.
if (pipes [current]->check_write ()) if (pipes[current]->check_write ())
return true; return true;
// Deactivate the pipe. // Deactivate the pipe.

View File

@@ -35,54 +35,50 @@
namespace zmq namespace zmq
{ {
// This class manages a set of outbound pipes. On send it load balances
// messages fairly among the pipes.
// This class manages a set of outbound pipes. On send it load balances class lb_t
// messages fairly among the pipes. {
public:
lb_t ();
~lb_t ();
class lb_t void attach (pipe_t *pipe_);
{ void activated (pipe_t *pipe_);
public: void pipe_terminated (pipe_t *pipe_);
lb_t (); int send (msg_t *msg_);
~lb_t ();
void attach (pipe_t *pipe_); // Sends a message and stores the pipe that was used in pipe_.
void activated (pipe_t *pipe_); // It is possible for this function to return success but keep pipe_
void pipe_terminated (pipe_t *pipe_); // unset if the rest of a multipart message to a terminated pipe is
// being dropped. For the first frame, this will never happen.
int sendpipe (msg_t *msg_, pipe_t **pipe_);
int send (msg_t *msg_); bool has_out ();
// Sends a message and stores the pipe that was used in pipe_. private:
// It is possible for this function to return success but keep pipe_ // List of outbound pipes.
// unset if the rest of a multipart message to a terminated pipe is typedef array_t<pipe_t, 2> pipes_t;
// being dropped. For the first frame, this will never happen. pipes_t pipes;
int sendpipe (msg_t *msg_, pipe_t **pipe_);
bool has_out (); // Number of active pipes. All the active pipes are located at the
// beginning of the pipes array.
pipes_t::size_type active;
private: // Points to the last pipe that the most recent message was sent to.
pipes_t::size_type current;
// List of outbound pipes. // True if last we are in the middle of a multipart message.
typedef array_t <pipe_t, 2> pipes_t; bool more;
pipes_t pipes;
// Number of active pipes. All the active pipes are located at the // True if we are dropping current message.
// beginning of the pipes array. bool dropping;
pipes_t::size_type active;
// Points to the last pipe that the most recent message was sent to.
pipes_t::size_type current;
// True if last we are in the middle of a multipart message.
bool more;
// True if we are dropping current message.
bool dropping;
lb_t (const lb_t&);
const lb_t &operator = (const lb_t&);
};
lb_t (const lb_t &);
const lb_t &operator= (const lb_t &);
};
} }
#endif #endif

View File

@@ -3,10 +3,11 @@
/* 0MQ Internal Use */ /* 0MQ Internal Use */
/******************************************************************************/ /******************************************************************************/
#define LIBZMQ_UNUSED(object) (void)object #define LIBZMQ_UNUSED(object) (void) object
#define LIBZMQ_DELETE(p_object) {\ #define LIBZMQ_DELETE(p_object) \
delete p_object; \ { \
p_object = 0; \ delete p_object; \
} p_object = 0; \
}
/******************************************************************************/ /******************************************************************************/

View File

@@ -42,51 +42,47 @@
namespace zmq namespace zmq
{ {
class mailbox_t : public i_mailbox
{
public:
mailbox_t ();
~mailbox_t ();
class mailbox_t : public i_mailbox fd_t get_fd () const;
{ void send (const command_t &cmd_);
public: int recv (command_t *cmd_, int timeout_);
mailbox_t (); bool valid () const;
~mailbox_t ();
fd_t get_fd () const;
void send (const command_t &cmd_);
int recv (command_t *cmd_, int timeout_);
bool valid () const;
#ifdef HAVE_FORK #ifdef HAVE_FORK
// close the file descriptors in the signaller. This is used in a forked // close the file descriptors in the signaller. This is used in a forked
// child process to close the file descriptors so that they do not interfere // child process to close the file descriptors so that they do not interfere
// with the context in the parent process. // with the context in the parent process.
void forked () { signaler.forked (); } void forked () { signaler.forked (); }
#endif #endif
private: private:
// The pipe to store actual commands.
typedef ypipe_t<command_t, command_pipe_granularity> cpipe_t;
cpipe_t cpipe;
// The pipe to store actual commands. // Signaler to pass signals from writer thread to reader thread.
typedef ypipe_t <command_t, command_pipe_granularity> cpipe_t; signaler_t signaler;
cpipe_t cpipe;
// Signaler to pass signals from writer thread to reader thread. // There's only one thread receiving from the mailbox, but there
signaler_t signaler; // is arbitrary number of threads sending. Given that ypipe requires
// synchronised access on both of its endpoints, we have to synchronise
// the sending side.
mutex_t sync;
// There's only one thread receiving from the mailbox, but there // True if the underlying pipe is active, ie. when we are allowed to
// is arbitrary number of threads sending. Given that ypipe requires // read commands from it.
// synchronised access on both of its endpoints, we have to synchronise bool active;
// the sending side.
mutex_t sync;
// True if the underlying pipe is active, ie. when we are allowed to
// read commands from it.
bool active;
// Disable copying of mailbox_t object.
mailbox_t (const mailbox_t&);
const mailbox_t &operator = (const mailbox_t&);
};
// Disable copying of mailbox_t object.
mailbox_t (const mailbox_t &);
const mailbox_t &operator= (const mailbox_t &);
};
} }
#endif #endif

View File

@@ -32,8 +32,7 @@
#include "clock.hpp" #include "clock.hpp"
#include "err.hpp" #include "err.hpp"
zmq::mailbox_safe_t::mailbox_safe_t (mutex_t* sync_) : zmq::mailbox_safe_t::mailbox_safe_t (mutex_t *sync_) : sync (sync_)
sync (sync_)
{ {
// Get the pipe into passive state. That way, if the users starts by // Get the pipe into passive state. That way, if the users starts by
// polling on the associated file descriptor it will get woken up when // polling on the associated file descriptor it will get woken up when
@@ -52,23 +51,23 @@ zmq::mailbox_safe_t::~mailbox_safe_t ()
sync->unlock (); sync->unlock ();
} }
void zmq::mailbox_safe_t::add_signaler (signaler_t* signaler) void zmq::mailbox_safe_t::add_signaler (signaler_t *signaler)
{ {
signalers.push_back(signaler); signalers.push_back (signaler);
} }
void zmq::mailbox_safe_t::remove_signaler (signaler_t* signaler) void zmq::mailbox_safe_t::remove_signaler (signaler_t *signaler)
{ {
std::vector<signaler_t*>::iterator it = signalers.begin(); std::vector<signaler_t *>::iterator it = signalers.begin ();
// TODO: make a copy of array and signal outside the lock // TODO: make a copy of array and signal outside the lock
for (; it != signalers.end(); ++it){ for (; it != signalers.end (); ++it) {
if (*it == signaler) if (*it == signaler)
break; break;
} }
if (it != signalers.end()) if (it != signalers.end ())
signalers.erase(it); signalers.erase (it);
} }
void zmq::mailbox_safe_t::clear_signalers () void zmq::mailbox_safe_t::clear_signalers ()
@@ -84,8 +83,9 @@ void zmq::mailbox_safe_t::send (const command_t &cmd_)
if (!ok) { if (!ok) {
cond_var.broadcast (); cond_var.broadcast ();
for (std::vector<signaler_t*>::iterator it = signalers.begin(); it != signalers.end(); ++it){ for (std::vector<signaler_t *>::iterator it = signalers.begin ();
(*it)->send(); it != signalers.end (); ++it) {
(*it)->send ();
} }
} }

View File

@@ -44,51 +44,47 @@
namespace zmq namespace zmq
{ {
class mailbox_safe_t : public i_mailbox
{
public:
mailbox_safe_t (mutex_t *sync_);
~mailbox_safe_t ();
class mailbox_safe_t : public i_mailbox void send (const command_t &cmd_);
{ int recv (command_t *cmd_, int timeout_);
public:
mailbox_safe_t (mutex_t* sync_); // Add signaler to mailbox which will be called when a message is ready
~mailbox_safe_t (); void add_signaler (signaler_t *signaler);
void remove_signaler (signaler_t *signaler);
void send (const command_t &cmd_); void clear_signalers ();
int recv (command_t *cmd_, int timeout_);
// Add signaler to mailbox which will be called when a message is ready
void add_signaler (signaler_t* signaler);
void remove_signaler (signaler_t* signaler);
void clear_signalers ();
#ifdef HAVE_FORK #ifdef HAVE_FORK
// close the file descriptors in the signaller. This is used in a forked // close the file descriptors in the signaller. This is used in a forked
// child process to close the file descriptors so that they do not interfere // child process to close the file descriptors so that they do not interfere
// with the context in the parent process. // with the context in the parent process.
void forked () void forked ()
{ {
// TODO: call fork on the condition variable // TODO: call fork on the condition variable
} }
#endif #endif
private: private:
// The pipe to store actual commands.
typedef ypipe_t<command_t, command_pipe_granularity> cpipe_t;
cpipe_t cpipe;
// The pipe to store actual commands. // Condition variable to pass signals from writer thread to reader thread.
typedef ypipe_t <command_t, command_pipe_granularity> cpipe_t; condition_variable_t cond_var;
cpipe_t cpipe;
// Condition variable to pass signals from writer thread to reader thread. // Synchronize access to the mailbox from receivers and senders
condition_variable_t cond_var; mutex_t *sync;
// Synchronize access to the mailbox from receivers and senders std::vector<zmq::signaler_t *> signalers;
mutex_t* sync;
std::vector <zmq::signaler_t* > signalers;
// Disable copying of mailbox_t object.
mailbox_safe_t (const mailbox_safe_t&);
const mailbox_safe_t &operator = (const mailbox_safe_t&);
};
// Disable copying of mailbox_t object.
mailbox_safe_t (const mailbox_safe_t &);
const mailbox_safe_t &operator= (const mailbox_safe_t &);
};
} }
#endif #endif

View File

@@ -37,8 +37,7 @@
#include "wire.hpp" #include "wire.hpp"
#include "session_base.hpp" #include "session_base.hpp"
zmq::mechanism_t::mechanism_t (const options_t &options_) : zmq::mechanism_t::mechanism_t (const options_t &options_) : options (options_)
options (options_)
{ {
} }
@@ -48,7 +47,7 @@ zmq::mechanism_t::~mechanism_t ()
void zmq::mechanism_t::set_peer_routing_id (const void *id_ptr, size_t id_size) void zmq::mechanism_t::set_peer_routing_id (const void *id_ptr, size_t id_size)
{ {
routing_id.set (static_cast <const unsigned char*> (id_ptr), id_size); routing_id.set (static_cast<const unsigned char *> (id_ptr), id_size);
} }
void zmq::mechanism_t::peer_routing_id (msg_t *msg_) void zmq::mechanism_t::peer_routing_id (msg_t *msg_)
@@ -61,7 +60,7 @@ void zmq::mechanism_t::peer_routing_id (msg_t *msg_)
void zmq::mechanism_t::set_user_id (const void *data_, size_t size_) void zmq::mechanism_t::set_user_id (const void *data_, size_t size_)
{ {
user_id.set (static_cast <const unsigned char*> (data_), size_); user_id.set (static_cast<const unsigned char *> (data_), size_);
zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE ( zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE (
ZMQ_MSG_PROPERTY_USER_ID, std::string ((char *) data_, size_)); ZMQ_MSG_PROPERTY_USER_ID, std::string ((char *) data_, size_));
} }
@@ -73,14 +72,12 @@ const zmq::blob_t &zmq::mechanism_t::get_user_id () const
const char *zmq::mechanism_t::socket_type_string (int socket_type) const const char *zmq::mechanism_t::socket_type_string (int socket_type) const
{ {
static const char *names [] = {"PAIR", "PUB", "SUB", "REQ", "REP", static const char *names[] = {
"DEALER", "ROUTER", "PULL", "PUSH", "PAIR", "PUB", "SUB", "REQ", "REP", "DEALER", "ROUTER",
"XPUB", "XSUB", "STREAM", "PULL", "PUSH", "XPUB", "XSUB", "STREAM", "SERVER", "CLIENT",
"SERVER", "CLIENT", "RADIO", "DISH", "GATHER", "SCATTER", "DGRAM"};
"RADIO", "DISH",
"GATHER", "SCATTER", "DGRAM"};
zmq_assert (socket_type >= 0 && socket_type <= 18); zmq_assert (socket_type >= 0 && socket_type <= 18);
return names [socket_type]; return names[socket_type];
} }
static size_t property_len (size_t name_len, size_t value_len) static size_t property_len (size_t name_len, size_t value_len)
@@ -105,11 +102,11 @@ size_t zmq::mechanism_t::add_property (unsigned char *ptr,
const size_t total_len = ::property_len (name_len, value_len); const size_t total_len = ::property_len (name_len, value_len);
zmq_assert (total_len <= ptr_capacity); zmq_assert (total_len <= ptr_capacity);
*ptr++ = static_cast <unsigned char> (name_len); *ptr++ = static_cast<unsigned char> (name_len);
memcpy (ptr, name, name_len); memcpy (ptr, name, name_len);
ptr += name_len; ptr += name_len;
zmq_assert (value_len <= 0x7FFFFFFF); zmq_assert (value_len <= 0x7FFFFFFF);
put_uint32 (ptr, static_cast <uint32_t> (value_len)); put_uint32 (ptr, static_cast<uint32_t> (value_len));
ptr += 4; ptr += 4;
memcpy (ptr, value, value_len); memcpy (ptr, value, value_len);
@@ -131,28 +128,26 @@ size_t zmq::mechanism_t::add_basic_properties (unsigned char *buf,
// Add socket type property // Add socket type property
const char *socket_type = socket_type_string (options.type); const char *socket_type = socket_type_string (options.type);
ptr += add_property (ptr, buf_capacity, ptr += add_property (ptr, buf_capacity, ZMTP_PROPERTY_SOCKET_TYPE,
ZMTP_PROPERTY_SOCKET_TYPE, socket_type, socket_type, strlen (socket_type));
strlen (socket_type));
// Add identity (aka routing id) property // Add identity (aka routing id) property
if (options.type == ZMQ_REQ || options.type == ZMQ_DEALER if (options.type == ZMQ_REQ || options.type == ZMQ_DEALER
|| options.type == ZMQ_ROUTER) || options.type == ZMQ_ROUTER)
ptr += add_property (ptr, buf_capacity - (ptr - buf), ptr +=
ZMTP_PROPERTY_IDENTITY, options.routing_id, add_property (ptr, buf_capacity - (ptr - buf), ZMTP_PROPERTY_IDENTITY,
options.routing_id_size); options.routing_id, options.routing_id_size);
return ptr - buf; return ptr - buf;
} }
size_t zmq::mechanism_t::basic_properties_len() const size_t zmq::mechanism_t::basic_properties_len () const
{ {
const char *socket_type = socket_type_string (options.type); const char *socket_type = socket_type_string (options.type);
return property_len (ZMTP_PROPERTY_SOCKET_TYPE, strlen (socket_type)) return property_len (ZMTP_PROPERTY_SOCKET_TYPE, strlen (socket_type))
+ ((options.type == ZMQ_REQ || options.type == ZMQ_DEALER + ((options.type == ZMQ_REQ || options.type == ZMQ_DEALER
|| options.type == ZMQ_ROUTER) || options.type == ZMQ_ROUTER)
? property_len (ZMTP_PROPERTY_IDENTITY, ? property_len (ZMTP_PROPERTY_IDENTITY, options.routing_id_size)
options.routing_id_size)
: 0); : 0);
} }
@@ -169,8 +164,8 @@ void zmq::mechanism_t::make_command_with_basic_properties (
memcpy (ptr, prefix, prefix_len); memcpy (ptr, prefix, prefix_len);
ptr += prefix_len; ptr += prefix_len;
add_basic_properties ( add_basic_properties (ptr, command_size
ptr, command_size - (ptr - (unsigned char *) msg_->data ())); - (ptr - (unsigned char *) msg_->data ()));
} }
int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_, int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_,
@@ -180,7 +175,7 @@ int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_,
size_t bytes_left = length_; size_t bytes_left = length_;
while (bytes_left > 1) { while (bytes_left > 1) {
const size_t name_length = static_cast <size_t> (*ptr_); const size_t name_length = static_cast<size_t> (*ptr_);
ptr_ += 1; ptr_ += 1;
bytes_left -= 1; bytes_left -= 1;
if (bytes_left < name_length) if (bytes_left < name_length)
@@ -192,7 +187,7 @@ int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_,
if (bytes_left < 4) if (bytes_left < 4)
break; break;
const size_t value_length = static_cast <size_t> (get_uint32 (ptr_)); const size_t value_length = static_cast<size_t> (get_uint32 (ptr_));
ptr_ += 4; ptr_ += 4;
bytes_left -= 4; bytes_left -= 4;
if (bytes_left < value_length) if (bytes_left < value_length)
@@ -204,25 +199,23 @@ int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_,
if (name == ZMTP_PROPERTY_IDENTITY && options.recv_routing_id) if (name == ZMTP_PROPERTY_IDENTITY && options.recv_routing_id)
set_peer_routing_id (value, value_length); set_peer_routing_id (value, value_length);
else else if (name == ZMTP_PROPERTY_SOCKET_TYPE) {
if (name == ZMTP_PROPERTY_SOCKET_TYPE) {
const std::string socket_type ((char *) value, value_length); const std::string socket_type ((char *) value, value_length);
if (!check_socket_type (socket_type)) { if (!check_socket_type (socket_type)) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
} } else {
else {
const int rc = property (name, value, value_length); const int rc = property (name, value, value_length);
if (rc == -1) if (rc == -1)
return -1; return -1;
} }
if (zap_flag) if (zap_flag)
zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE ( zap_properties.ZMQ_MAP_INSERT_OR_EMPLACE (
name, std::string ((char *) value, value_length)); name, std::string ((char *) value, value_length));
else else
zmtp_properties.ZMQ_MAP_INSERT_OR_EMPLACE ( zmtp_properties.ZMQ_MAP_INSERT_OR_EMPLACE (
name, std::string ((char *) value, value_length)); name, std::string ((char *) value, value_length));
} }
if (bytes_left > 0) { if (bytes_left > 0) {
errno = EPROTO; errno = EPROTO;
@@ -231,15 +224,16 @@ int zmq::mechanism_t::parse_metadata (const unsigned char *ptr_,
return 0; return 0;
} }
int zmq::mechanism_t::property (const std::string& /* name_ */, int zmq::mechanism_t::property (const std::string & /* name_ */,
const void * /* value_ */, size_t /* length_ */) const void * /* value_ */,
size_t /* length_ */)
{ {
// Default implementation does not check // Default implementation does not check
// property values and returns 0 to signal success. // property values and returns 0 to signal success.
return 0; return 0;
} }
bool zmq::mechanism_t::check_socket_type (const std::string& type_) const bool zmq::mechanism_t::check_socket_type (const std::string &type_) const
{ {
switch (options.type) { switch (options.type) {
case ZMQ_REQ: case ZMQ_REQ:

View File

@@ -37,116 +37,108 @@
namespace zmq namespace zmq
{ {
class msg_t;
class session_base_t;
class msg_t; // Abstract class representing security mechanism.
class session_base_t; // Different mechanism extends this class.
// Abstract class representing security mechanism. class mechanism_t
// Different mechanism extends this class. {
public:
class mechanism_t enum status_t
{ {
public: handshaking,
ready,
enum status_t { error
handshaking,
ready,
error
};
mechanism_t (const options_t &options_);
virtual ~mechanism_t ();
// Prepare next handshake command that is to be sent to the peer.
virtual int next_handshake_command (msg_t *msg_) = 0;
// Process the handshake command received from the peer.
virtual int process_handshake_command (msg_t *msg_) = 0;
virtual int encode (msg_t *) { return 0; }
virtual int decode (msg_t *) { return 0; }
// Notifies mechanism about availability of ZAP message.
virtual int zap_msg_available () { return 0; }
// Returns the status of this mechanism.
virtual status_t status () const = 0;
void set_peer_routing_id (const void *id_ptr, size_t id_size);
void peer_routing_id (msg_t *msg_);
void set_user_id (const void *user_id, size_t size);
const blob_t &get_user_id () const;
const metadata_t::dict_t& get_zmtp_properties () {
return zmtp_properties;
}
const metadata_t::dict_t& get_zap_properties () {
return zap_properties;
}
protected:
// Only used to identify the socket for the Socket-Type
// property in the wire protocol.
const char *socket_type_string (int socket_type) const;
static size_t add_property (unsigned char *ptr,
size_t ptr_capacity,
const char *name,
const void *value,
size_t value_len);
static size_t property_len (const char *name,
size_t value_len);
size_t add_basic_properties (unsigned char *ptr, size_t ptr_capacity) const;
size_t basic_properties_len () const;
void make_command_with_basic_properties (msg_t *msg_,
const char *prefix,
size_t prefix_len) const;
// Parses a metadata.
// Metadata consists of a list of properties consisting of
// name and value as size-specified strings.
// Returns 0 on success and -1 on error, in which case errno is set.
int parse_metadata (
const unsigned char *ptr_, size_t length, bool zap_flag = false);
// This is called by parse_property method whenever it
// parses a new property. The function should return 0
// on success and -1 on error, in which case it should
// set errno. Signaling error prevents parser from
// parsing remaining data.
// Derived classes are supposed to override this
// method to handle custom processing.
virtual int property (const std::string& name_,
const void *value_, size_t length_);
// Properties received from ZMTP peer.
metadata_t::dict_t zmtp_properties;
// Properties received from ZAP server.
metadata_t::dict_t zap_properties;
options_t options;
private:
blob_t routing_id;
blob_t user_id;
// Returns true iff socket associated with the mechanism
// is compatible with a given socket type 'type_'.
bool check_socket_type (const std::string& type_) const;
}; };
mechanism_t (const options_t &options_);
virtual ~mechanism_t ();
// Prepare next handshake command that is to be sent to the peer.
virtual int next_handshake_command (msg_t *msg_) = 0;
// Process the handshake command received from the peer.
virtual int process_handshake_command (msg_t *msg_) = 0;
virtual int encode (msg_t *) { return 0; }
virtual int decode (msg_t *) { return 0; }
// Notifies mechanism about availability of ZAP message.
virtual int zap_msg_available () { return 0; }
// Returns the status of this mechanism.
virtual status_t status () const = 0;
void set_peer_routing_id (const void *id_ptr, size_t id_size);
void peer_routing_id (msg_t *msg_);
void set_user_id (const void *user_id, size_t size);
const blob_t &get_user_id () const;
const metadata_t::dict_t &get_zmtp_properties () { return zmtp_properties; }
const metadata_t::dict_t &get_zap_properties () { return zap_properties; }
protected:
// Only used to identify the socket for the Socket-Type
// property in the wire protocol.
const char *socket_type_string (int socket_type) const;
static size_t add_property (unsigned char *ptr,
size_t ptr_capacity,
const char *name,
const void *value,
size_t value_len);
static size_t property_len (const char *name, size_t value_len);
size_t add_basic_properties (unsigned char *ptr, size_t ptr_capacity) const;
size_t basic_properties_len () const;
void make_command_with_basic_properties (msg_t *msg_,
const char *prefix,
size_t prefix_len) const;
// Parses a metadata.
// Metadata consists of a list of properties consisting of
// name and value as size-specified strings.
// Returns 0 on success and -1 on error, in which case errno is set.
int parse_metadata (const unsigned char *ptr_,
size_t length,
bool zap_flag = false);
// This is called by parse_property method whenever it
// parses a new property. The function should return 0
// on success and -1 on error, in which case it should
// set errno. Signaling error prevents parser from
// parsing remaining data.
// Derived classes are supposed to override this
// method to handle custom processing.
virtual int
property (const std::string &name_, const void *value_, size_t length_);
// Properties received from ZMTP peer.
metadata_t::dict_t zmtp_properties;
// Properties received from ZAP server.
metadata_t::dict_t zap_properties;
options_t options;
private:
blob_t routing_id;
blob_t user_id;
// Returns true iff socket associated with the mechanism
// is compatible with a given socket type 'type_'.
bool check_socket_type (const std::string &type_) const;
};
} }
#endif #endif

View File

@@ -37,7 +37,6 @@ zmq::mechanism_base_t::mechanism_base_t (session_base_t *const session_,
mechanism_t (options_), mechanism_t (options_),
session (session_) session (session_)
{ {
} }
int zmq::mechanism_base_t::check_basic_command_structure (msg_t *msg_) int zmq::mechanism_base_t::check_basic_command_structure (msg_t *msg_)
@@ -64,7 +63,7 @@ void zmq::mechanism_base_t::handle_error_reason (const char *error_reason,
} }
} }
bool zmq::mechanism_base_t::zap_required() const bool zmq::mechanism_base_t::zap_required () const
{ {
return !options.zap_domain.empty (); return !options.zap_domain.empty ();
} }

View File

@@ -44,9 +44,10 @@ class mechanism_base_t : public mechanism_t
int check_basic_command_structure (msg_t *msg_); int check_basic_command_structure (msg_t *msg_);
void handle_error_reason (const char *error_reason, size_t error_reason_len); void handle_error_reason (const char *error_reason,
size_t error_reason_len);
bool zap_required() const; bool zap_required () const;
}; };
} }

View File

@@ -30,24 +30,20 @@
#include "precompiled.hpp" #include "precompiled.hpp"
#include "metadata.hpp" #include "metadata.hpp"
zmq::metadata_t::metadata_t (const dict_t &dict) : zmq::metadata_t::metadata_t (const dict_t &dict) : ref_cnt (1), dict (dict)
ref_cnt (1),
dict (dict)
{ {
} }
const char *zmq::metadata_t::get (const std::string &property) const const char *zmq::metadata_t::get (const std::string &property) const
{ {
dict_t::const_iterator it = dict.find (property); dict_t::const_iterator it = dict.find (property);
if (it == dict.end()) if (it == dict.end ()) {
{
/** \todo remove this when support for the deprecated name "Identity" is dropped */ /** \todo remove this when support for the deprecated name "Identity" is dropped */
if (property == "Identity") if (property == "Identity")
return get (ZMQ_MSG_PROPERTY_ROUTING_ID); return get (ZMQ_MSG_PROPERTY_ROUTING_ID);
return NULL; return NULL;
} } else
else
return it->second.c_str (); return it->second.c_str ();
} }

View File

@@ -37,34 +37,33 @@
namespace zmq namespace zmq
{ {
class metadata_t class metadata_t
{ {
public: public:
typedef std::map <std::string, std::string> dict_t; typedef std::map<std::string, std::string> dict_t;
metadata_t (const dict_t &dict); metadata_t (const dict_t &dict);
// Returns pointer to property value or NULL if // Returns pointer to property value or NULL if
// property is not found. // property is not found.
const char *get (const std::string &property) const; const char *get (const std::string &property) const;
void add_ref (); void add_ref ();
// Drop reference. Returns true iff the reference // Drop reference. Returns true iff the reference
// counter drops to zero. // counter drops to zero.
bool drop_ref (); bool drop_ref ();
private: private:
metadata_t(const metadata_t&); metadata_t (const metadata_t &);
metadata_t & operator=(const metadata_t&); metadata_t &operator= (const metadata_t &);
// Reference counter. // Reference counter.
atomic_counter_t ref_cnt; atomic_counter_t ref_cnt;
// Dictionary holding metadata.
dict_t dict;
};
// Dictionary holding metadata.
dict_t dict;
};
} }
#endif #endif

View File

@@ -43,38 +43,31 @@
// Check whether the sizes of public representation of the message (zmq_msg_t) // Check whether the sizes of public representation of the message (zmq_msg_t)
// and private representation of the message (zmq::msg_t) match. // and private representation of the message (zmq::msg_t) match.
typedef char zmq_msg_size_check typedef char
[2 * ((sizeof (zmq::msg_t) == sizeof (zmq_msg_t)) != 0) - 1]; zmq_msg_size_check[2 * ((sizeof (zmq::msg_t) == sizeof (zmq_msg_t)) != 0)
- 1];
bool zmq::msg_t::check () const bool zmq::msg_t::check () const
{ {
return u.base.type >= type_min && u.base.type <= type_max; return u.base.type >= type_min && u.base.type <= type_max;
} }
int zmq::msg_t::init (void* data_, size_t size_, int zmq::msg_t::init (
msg_free_fn* ffn_, void* hint, void *data_, size_t size_, msg_free_fn *ffn_, void *hint, content_t *content_)
content_t* content_)
{ {
if (size_ < max_vsm_size) { if (size_ < max_vsm_size) {
int const rc = init_size(size_); int const rc = init_size (size_);
if (rc != -1) if (rc != -1) {
{ memcpy (data (), data_, size_);
memcpy(data(), data_, size_);
return 0; return 0;
} } else {
else
{
return -1; return -1;
} }
} } else if (content_) {
else if(content_) return init_external_storage (content_, data_, size_, ffn_, hint);
{ } else {
return init_external_storage(content_, data_, size_, ffn_, hint); return init_data (data_, size_, ffn_, hint);
}
else
{
return init_data(data_, size_, ffn_, hint);
} }
} }
@@ -98,8 +91,7 @@ int zmq::msg_t::init_size (size_t size_)
u.vsm.size = (unsigned char) size_; u.vsm.size = (unsigned char) size_;
u.vsm.group[0] = '\0'; u.vsm.group[0] = '\0';
u.vsm.routing_id = 0; u.vsm.routing_id = 0;
} } else {
else {
u.lmsg.metadata = NULL; u.lmsg.metadata = NULL;
u.lmsg.type = type_lmsg; u.lmsg.type = type_lmsg;
u.lmsg.flags = 0; u.lmsg.flags = 0;
@@ -107,7 +99,7 @@ int zmq::msg_t::init_size (size_t size_)
u.lmsg.routing_id = 0; u.lmsg.routing_id = 0;
u.lmsg.content = NULL; u.lmsg.content = NULL;
if (sizeof (content_t) + size_ > size_) if (sizeof (content_t) + size_ > size_)
u.lmsg.content = (content_t*) malloc (sizeof (content_t) + size_); u.lmsg.content = (content_t *) malloc (sizeof (content_t) + size_);
if (unlikely (!u.lmsg.content)) { if (unlikely (!u.lmsg.content)) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@@ -122,11 +114,14 @@ int zmq::msg_t::init_size (size_t size_)
return 0; return 0;
} }
int zmq::msg_t::init_external_storage(content_t* content_, void* data_, size_t size_, int zmq::msg_t::init_external_storage (content_t *content_,
msg_free_fn *ffn_, void* hint_) void *data_,
size_t size_,
msg_free_fn *ffn_,
void *hint_)
{ {
zmq_assert(NULL != data_); zmq_assert (NULL != data_);
zmq_assert(NULL != content_); zmq_assert (NULL != content_);
u.zclmsg.metadata = NULL; u.zclmsg.metadata = NULL;
u.zclmsg.type = type_zclmsg; u.zclmsg.type = type_zclmsg;
@@ -139,13 +134,15 @@ int zmq::msg_t::init_external_storage(content_t* content_, void* data_, size_t s
u.zclmsg.content->size = size_; u.zclmsg.content->size = size_;
u.zclmsg.content->ffn = ffn_; u.zclmsg.content->ffn = ffn_;
u.zclmsg.content->hint = hint_; u.zclmsg.content->hint = hint_;
new (&u.zclmsg.content->refcnt) zmq::atomic_counter_t(); new (&u.zclmsg.content->refcnt) zmq::atomic_counter_t ();
return 0; return 0;
} }
int zmq::msg_t::init_data (void *data_, size_t size_, int zmq::msg_t::init_data (void *data_,
msg_free_fn *ffn_, void *hint_) size_t size_,
msg_free_fn *ffn_,
void *hint_)
{ {
// If data is NULL and size is not 0, a segfault // If data is NULL and size is not 0, a segfault
// would occur once the data is accessed // would occur once the data is accessed
@@ -160,14 +157,13 @@ int zmq::msg_t::init_data (void *data_, size_t size_,
u.cmsg.size = size_; u.cmsg.size = size_;
u.cmsg.group[0] = '\0'; u.cmsg.group[0] = '\0';
u.cmsg.routing_id = 0; u.cmsg.routing_id = 0;
} } else {
else {
u.lmsg.metadata = NULL; u.lmsg.metadata = NULL;
u.lmsg.type = type_lmsg; u.lmsg.type = type_lmsg;
u.lmsg.flags = 0; u.lmsg.flags = 0;
u.lmsg.group[0] = '\0'; u.lmsg.group[0] = '\0';
u.lmsg.routing_id = 0; u.lmsg.routing_id = 0;
u.lmsg.content = (content_t*) malloc (sizeof (content_t)); u.lmsg.content = (content_t *) malloc (sizeof (content_t));
if (!u.lmsg.content) { if (!u.lmsg.content) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
@@ -180,7 +176,6 @@ int zmq::msg_t::init_data (void *data_, size_t size_,
new (&u.lmsg.content->refcnt) zmq::atomic_counter_t (); new (&u.lmsg.content->refcnt) zmq::atomic_counter_t ();
} }
return 0; return 0;
} }
int zmq::msg_t::init_delimiter () int zmq::msg_t::init_delimiter ()
@@ -222,44 +217,40 @@ int zmq::msg_t::close ()
} }
if (u.base.type == type_lmsg) { if (u.base.type == type_lmsg) {
// If the content is not shared, or if it is shared and the reference // If the content is not shared, or if it is shared and the reference
// count has dropped to zero, deallocate it. // count has dropped to zero, deallocate it.
if (!(u.lmsg.flags & msg_t::shared) || if (!(u.lmsg.flags & msg_t::shared)
!u.lmsg.content->refcnt.sub (1)) { || !u.lmsg.content->refcnt.sub (1)) {
// We used "placement new" operator to initialize the reference // We used "placement new" operator to initialize the reference
// counter so we call the destructor explicitly now. // counter so we call the destructor explicitly now.
u.lmsg.content->refcnt.~atomic_counter_t (); u.lmsg.content->refcnt.~atomic_counter_t ();
if (u.lmsg.content->ffn) if (u.lmsg.content->ffn)
u.lmsg.content->ffn (u.lmsg.content->data, u.lmsg.content->ffn (u.lmsg.content->data,
u.lmsg.content->hint); u.lmsg.content->hint);
free (u.lmsg.content); free (u.lmsg.content);
} }
} }
if (is_zcmsg()) if (is_zcmsg ()) {
{ zmq_assert (u.zclmsg.content->ffn);
zmq_assert(u.zclmsg.content->ffn);
// If the content is not shared, or if it is shared and the reference // If the content is not shared, or if it is shared and the reference
// count has dropped to zero, deallocate it. // count has dropped to zero, deallocate it.
if (!(u.zclmsg.flags & msg_t::shared) || if (!(u.zclmsg.flags & msg_t::shared)
!u.zclmsg.content->refcnt.sub (1)) { || !u.zclmsg.content->refcnt.sub (1)) {
// We used "placement new" operator to initialize the reference // We used "placement new" operator to initialize the reference
// counter so we call the destructor explicitly now. // counter so we call the destructor explicitly now.
u.zclmsg.content->refcnt.~atomic_counter_t (); u.zclmsg.content->refcnt.~atomic_counter_t ();
u.zclmsg.content->ffn (u.zclmsg.content->data, u.zclmsg.content->ffn (u.zclmsg.content->data,
u.zclmsg.content->hint); u.zclmsg.content->hint);
} }
} }
if (u.base.metadata != NULL) { if (u.base.metadata != NULL) {
if (u.base.metadata->drop_ref ()) { if (u.base.metadata->drop_ref ()) {
LIBZMQ_DELETE(u.base.metadata); LIBZMQ_DELETE (u.base.metadata);
} }
u.base.metadata = NULL; u.base.metadata = NULL;
} }
@@ -303,8 +294,7 @@ int zmq::msg_t::copy (msg_t &src_)
if (unlikely (rc < 0)) if (unlikely (rc < 0))
return rc; return rc;
if (src_.u.base.type == type_lmsg ) { if (src_.u.base.type == type_lmsg) {
// One reference is added to shared messages. Non-shared messages // One reference is added to shared messages. Non-shared messages
// are turned into shared messages and reference count is set to 2. // are turned into shared messages and reference count is set to 2.
if (src_.u.lmsg.flags & msg_t::shared) if (src_.u.lmsg.flags & msg_t::shared)
@@ -315,15 +305,14 @@ int zmq::msg_t::copy (msg_t &src_)
} }
} }
if (src_.is_zcmsg()) { if (src_.is_zcmsg ()) {
// One reference is added to shared messages. Non-shared messages // One reference is added to shared messages. Non-shared messages
// are turned into shared messages and reference count is set to 2. // are turned into shared messages and reference count is set to 2.
if (src_.u.zclmsg.flags & msg_t::shared) if (src_.u.zclmsg.flags & msg_t::shared)
src_.refcnt()->add (1); src_.refcnt ()->add (1);
else { else {
src_.u.zclmsg.flags |= msg_t::shared; src_.u.zclmsg.flags |= msg_t::shared;
src_.refcnt()->set (2); src_.refcnt ()->set (2);
} }
} }
if (src_.u.base.metadata != NULL) if (src_.u.base.metadata != NULL)
@@ -332,7 +321,6 @@ int zmq::msg_t::copy (msg_t &src_)
*this = src_; *this = src_;
return 0; return 0;
} }
void *zmq::msg_t::data () void *zmq::msg_t::data ()
@@ -341,17 +329,17 @@ void *zmq::msg_t::data ()
zmq_assert (check ()); zmq_assert (check ());
switch (u.base.type) { switch (u.base.type) {
case type_vsm: case type_vsm:
return u.vsm.data; return u.vsm.data;
case type_lmsg: case type_lmsg:
return u.lmsg.content->data; return u.lmsg.content->data;
case type_cmsg: case type_cmsg:
return u.cmsg.data; return u.cmsg.data;
case type_zclmsg: case type_zclmsg:
return u.zclmsg.content->data; return u.zclmsg.content->data;
default: default:
zmq_assert (false); zmq_assert (false);
return NULL; return NULL;
} }
} }
@@ -361,17 +349,17 @@ size_t zmq::msg_t::size () const
zmq_assert (check ()); zmq_assert (check ());
switch (u.base.type) { switch (u.base.type) {
case type_vsm: case type_vsm:
return u.vsm.size; return u.vsm.size;
case type_lmsg: case type_lmsg:
return u.lmsg.content->size; return u.lmsg.content->size;
case type_zclmsg: case type_zclmsg:
return u.zclmsg.content->size; return u.zclmsg.content->size;
case type_cmsg: case type_cmsg:
return u.cmsg.size; return u.cmsg.size;
default: default:
zmq_assert (false); zmq_assert (false);
return 0; return 0;
} }
} }
@@ -407,7 +395,7 @@ void zmq::msg_t::reset_metadata ()
{ {
if (u.base.metadata) { if (u.base.metadata) {
if (u.base.metadata->drop_ref ()) { if (u.base.metadata->drop_ref ()) {
LIBZMQ_DELETE(u.base.metadata); LIBZMQ_DELETE (u.base.metadata);
} }
u.base.metadata = NULL; u.base.metadata = NULL;
} }
@@ -438,17 +426,17 @@ bool zmq::msg_t::is_cmsg () const
return u.base.type == type_cmsg; return u.base.type == type_cmsg;
} }
bool zmq::msg_t::is_zcmsg() const bool zmq::msg_t::is_zcmsg () const
{ {
return u.base.type == type_zclmsg; return u.base.type == type_zclmsg;
} }
bool zmq::msg_t::is_join() const bool zmq::msg_t::is_join () const
{ {
return u.base.type == type_join; return u.base.type == type_join;
} }
bool zmq::msg_t::is_leave() const bool zmq::msg_t::is_leave () const
{ {
return u.base.type == type_leave; return u.base.type == type_leave;
} }
@@ -466,11 +454,11 @@ void zmq::msg_t::add_refs (int refs_)
// VSMs, CMSGS and delimiters can be copied straight away. The only // VSMs, CMSGS and delimiters can be copied straight away. The only
// message type that needs special care are long messages. // message type that needs special care are long messages.
if (u.base.type == type_lmsg || is_zcmsg() ) { if (u.base.type == type_lmsg || is_zcmsg ()) {
if (u.base.flags & msg_t::shared) if (u.base.flags & msg_t::shared)
refcnt()->add (refs_); refcnt ()->add (refs_);
else { else {
refcnt()->set (refs_ + 1); refcnt ()->set (refs_ + 1);
u.base.flags |= msg_t::shared; u.base.flags |= msg_t::shared;
} }
} }
@@ -488,13 +476,14 @@ bool zmq::msg_t::rm_refs (int refs_)
return true; return true;
// If there's only one reference close the message. // If there's only one reference close the message.
if ( (u.base.type != type_zclmsg && u.base.type != type_lmsg) || !(u.base.flags & msg_t::shared)) { if ((u.base.type != type_zclmsg && u.base.type != type_lmsg)
|| !(u.base.flags & msg_t::shared)) {
close (); close ();
return false; return false;
} }
// The only message type that needs special care are long and zcopy messages. // The only message type that needs special care are long and zcopy messages.
if (u.base.type == type_lmsg && !u.lmsg.content->refcnt.sub(refs_)) { if (u.base.type == type_lmsg && !u.lmsg.content->refcnt.sub (refs_)) {
// We used "placement new" operator to initialize the reference // We used "placement new" operator to initialize the reference
// counter so we call the destructor explicitly now. // counter so we call the destructor explicitly now.
u.lmsg.content->refcnt.~atomic_counter_t (); u.lmsg.content->refcnt.~atomic_counter_t ();
@@ -506,10 +495,11 @@ bool zmq::msg_t::rm_refs (int refs_)
return false; return false;
} }
if (is_zcmsg() && !u.zclmsg.content->refcnt.sub(refs_)) { if (is_zcmsg () && !u.zclmsg.content->refcnt.sub (refs_)) {
// storage for rfcnt is provided externally // storage for rfcnt is provided externally
if (u.zclmsg.content->ffn) { if (u.zclmsg.content->ffn) {
u.zclmsg.content->ffn(u.zclmsg.content->data, u.zclmsg.content->hint); u.zclmsg.content->ffn (u.zclmsg.content->data,
u.zclmsg.content->hint);
} }
return false; return false;
@@ -539,20 +529,19 @@ int zmq::msg_t::reset_routing_id ()
return 0; return 0;
} }
const char * zmq::msg_t::group () const char *zmq::msg_t::group ()
{ {
return u.base.group; return u.base.group;
} }
int zmq::msg_t::set_group (const char * group_) int zmq::msg_t::set_group (const char *group_)
{ {
return set_group (group_, strlen (group_)); return set_group (group_, strlen (group_));
} }
int zmq::msg_t::set_group (const char * group_, size_t length_) int zmq::msg_t::set_group (const char *group_, size_t length_)
{ {
if (length_> ZMQ_GROUP_MAX_LENGTH) if (length_ > ZMQ_GROUP_MAX_LENGTH) {
{
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
@@ -563,16 +552,15 @@ int zmq::msg_t::set_group (const char * group_, size_t length_)
return 0; return 0;
} }
zmq::atomic_counter_t *zmq::msg_t::refcnt() zmq::atomic_counter_t *zmq::msg_t::refcnt ()
{ {
switch(u.base.type) switch (u.base.type) {
{
case type_lmsg: case type_lmsg:
return &u.lmsg.content->refcnt; return &u.lmsg.content->refcnt;
case type_zclmsg: case type_zclmsg:
return &u.zclmsg.content->refcnt; return &u.zclmsg.content->refcnt;
default: default:
zmq_assert(false); zmq_assert (false);
return NULL; return NULL;
} }
} }

View File

@@ -42,227 +42,232 @@
// Signature for free function to deallocate the message content. // Signature for free function to deallocate the message content.
// Note that it has to be declared as "C" so that it is the same as // Note that it has to be declared as "C" so that it is the same as
// zmq_free_fn defined in zmq.h. // zmq_free_fn defined in zmq.h.
extern "C" extern "C" {
{ typedef void(msg_free_fn) (void *data, void *hint);
typedef void (msg_free_fn) (void *data, void *hint);
} }
namespace zmq namespace zmq
{ {
// Note that this structure needs to be explicitly constructed
// (init functions) and destructed (close function).
// Note that this structure needs to be explicitly constructed class msg_t
// (init functions) and destructed (close function). {
public:
class msg_t // Shared message buffer. Message data are either allocated in one
// continuous block along with this structure - thus avoiding one
// malloc/free pair or they are stored in user-supplied memory.
// In the latter case, ffn member stores pointer to the function to be
// used to deallocate the data. If the buffer is actually shared (there
// are at least 2 references to it) refcount member contains number of
// references.
struct content_t
{ {
public: void *data;
size_t size;
// Shared message buffer. Message data are either allocated in one msg_free_fn *ffn;
// continuous block along with this structure - thus avoiding one void *hint;
// malloc/free pair or they are stored in user-supplied memory. zmq::atomic_counter_t refcnt;
// In the latter case, ffn member stores pointer to the function to be
// used to deallocate the data. If the buffer is actually shared (there
// are at least 2 references to it) refcount member contains number of
// references.
struct content_t
{
void *data;
size_t size;
msg_free_fn *ffn;
void *hint;
zmq::atomic_counter_t refcnt;
};
// Message flags.
enum
{
more = 1, // Followed by more parts
command = 2, // Command frame (see ZMTP spec)
credential = 32,
routing_id = 64,
shared = 128
};
bool check () const;
int init();
int init (void* data, size_t size_,
msg_free_fn* ffn_, void* hint,
content_t* content_ = NULL);
int init_size (size_t size_);
int init_data (void *data_, size_t size_, msg_free_fn *ffn_,
void *hint_);
int init_external_storage(content_t* content_, void *data_, size_t size_,
msg_free_fn *ffn_, void *hint_);
int init_delimiter ();
int init_join ();
int init_leave ();
int close ();
int move (msg_t &src_);
int copy (msg_t &src_);
void *data ();
size_t size () const;
unsigned char flags () const;
void set_flags (unsigned char flags_);
void reset_flags (unsigned char flags_);
metadata_t *metadata () const;
void set_metadata (metadata_t *metadata_);
void reset_metadata ();
bool is_routing_id () const;
bool is_credential () const;
bool is_delimiter () const;
bool is_join () const;
bool is_leave () const;
bool is_vsm () const;
bool is_cmsg () const;
bool is_zcmsg() const;
uint32_t get_routing_id ();
int set_routing_id (uint32_t routing_id_);
int reset_routing_id ();
const char * group ();
int set_group (const char* group_);
int set_group (const char*, size_t length);
// After calling this function you can copy the message in POD-style
// refs_ times. No need to call copy.
void add_refs (int refs_);
// Removes references previously added by add_refs. If the number of
// references drops to 0, the message is closed and false is returned.
bool rm_refs (int refs_);
// Size in bytes of the largest message that is still copied around
// rather than being reference-counted.
enum { msg_t_size = 64 };
enum { max_vsm_size = msg_t_size - (sizeof (metadata_t *) +
3 +
16 +
sizeof (uint32_t))};
private:
zmq::atomic_counter_t* refcnt();
// Different message types.
enum type_t
{
type_min = 101,
// VSM messages store the content in the message itself
type_vsm = 101,
// LMSG messages store the content in malloc-ed memory
type_lmsg = 102,
// Delimiter messages are used in envelopes
type_delimiter = 103,
// CMSG messages point to constant data
type_cmsg = 104,
// zero-copy LMSG message for v2_decoder
type_zclmsg = 105,
// Join message for radio_dish
type_join = 106,
// Leave message for radio_dish
type_leave = 107,
type_max = 107
};
// Note that fields shared between different message types are not
// moved to the parent class (msg_t). This way we get tighter packing
// of the data. Shared fields can be accessed via 'base' member of
// the union.
union {
struct {
metadata_t *metadata;
unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
2 +
16 +
sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group [16];
uint32_t routing_id;
} base;
struct {
metadata_t *metadata;
unsigned char data [max_vsm_size];
unsigned char size;
unsigned char type;
unsigned char flags;
char group [16];
uint32_t routing_id;
} vsm;
struct {
metadata_t *metadata;
content_t *content;
unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
sizeof (content_t*) +
2 +
16 +
sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group [16];
uint32_t routing_id;
} lmsg;
struct {
metadata_t *metadata;
content_t *content;
unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
sizeof (content_t*) +
2 +
16 +
sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group [16];
uint32_t routing_id;
} zclmsg;
struct {
metadata_t *metadata;
void* data;
size_t size;
unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
sizeof (void*) +
sizeof (size_t) +
2 +
16 +
sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group [16];
uint32_t routing_id;
} cmsg;
struct {
metadata_t *metadata;
unsigned char unused [msg_t_size - (sizeof (metadata_t *) +
2 +
16 +
sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group [16];
uint32_t routing_id;
} delimiter;
} u;
}; };
inline int close_and_return (zmq::msg_t *msg, int echo) // Message flags.
enum
{ {
// Since we abort on close failure we preserve errno for success case. more = 1, // Followed by more parts
int err = errno; command = 2, // Command frame (see ZMTP spec)
const int rc = msg->close (); credential = 32,
errno_assert (rc == 0); routing_id = 64,
errno = err; shared = 128
return echo; };
}
inline int close_and_return (zmq::msg_t msg [], int count, int echo) bool check () const;
int init ();
int init (void *data,
size_t size_,
msg_free_fn *ffn_,
void *hint,
content_t *content_ = NULL);
int init_size (size_t size_);
int init_data (void *data_, size_t size_, msg_free_fn *ffn_, void *hint_);
int init_external_storage (content_t *content_,
void *data_,
size_t size_,
msg_free_fn *ffn_,
void *hint_);
int init_delimiter ();
int init_join ();
int init_leave ();
int close ();
int move (msg_t &src_);
int copy (msg_t &src_);
void *data ();
size_t size () const;
unsigned char flags () const;
void set_flags (unsigned char flags_);
void reset_flags (unsigned char flags_);
metadata_t *metadata () const;
void set_metadata (metadata_t *metadata_);
void reset_metadata ();
bool is_routing_id () const;
bool is_credential () const;
bool is_delimiter () const;
bool is_join () const;
bool is_leave () const;
bool is_vsm () const;
bool is_cmsg () const;
bool is_zcmsg () const;
uint32_t get_routing_id ();
int set_routing_id (uint32_t routing_id_);
int reset_routing_id ();
const char *group ();
int set_group (const char *group_);
int set_group (const char *, size_t length);
// After calling this function you can copy the message in POD-style
// refs_ times. No need to call copy.
void add_refs (int refs_);
// Removes references previously added by add_refs. If the number of
// references drops to 0, the message is closed and false is returned.
bool rm_refs (int refs_);
// Size in bytes of the largest message that is still copied around
// rather than being reference-counted.
enum
{ {
for (int i = 0; i < count; i++) msg_t_size = 64
close_and_return (&msg [i], 0); };
return echo; enum
} {
max_vsm_size =
msg_t_size - (sizeof (metadata_t *) + 3 + 16 + sizeof (uint32_t))
};
private:
zmq::atomic_counter_t *refcnt ();
// Different message types.
enum type_t
{
type_min = 101,
// VSM messages store the content in the message itself
type_vsm = 101,
// LMSG messages store the content in malloc-ed memory
type_lmsg = 102,
// Delimiter messages are used in envelopes
type_delimiter = 103,
// CMSG messages point to constant data
type_cmsg = 104,
// zero-copy LMSG message for v2_decoder
type_zclmsg = 105,
// Join message for radio_dish
type_join = 106,
// Leave message for radio_dish
type_leave = 107,
type_max = 107
};
// Note that fields shared between different message types are not
// moved to the parent class (msg_t). This way we get tighter packing
// of the data. Shared fields can be accessed via 'base' member of
// the union.
union
{
struct
{
metadata_t *metadata;
unsigned char
unused[msg_t_size
- (sizeof (metadata_t *) + 2 + 16 + sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group[16];
uint32_t routing_id;
} base;
struct
{
metadata_t *metadata;
unsigned char data[max_vsm_size];
unsigned char size;
unsigned char type;
unsigned char flags;
char group[16];
uint32_t routing_id;
} vsm;
struct
{
metadata_t *metadata;
content_t *content;
unsigned char unused[msg_t_size
- (sizeof (metadata_t *) + sizeof (content_t *)
+ 2 + 16 + sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group[16];
uint32_t routing_id;
} lmsg;
struct
{
metadata_t *metadata;
content_t *content;
unsigned char unused[msg_t_size
- (sizeof (metadata_t *) + sizeof (content_t *)
+ 2 + 16 + sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group[16];
uint32_t routing_id;
} zclmsg;
struct
{
metadata_t *metadata;
void *data;
size_t size;
unsigned char
unused[msg_t_size
- (sizeof (metadata_t *) + sizeof (void *)
+ sizeof (size_t) + 2 + 16 + sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group[16];
uint32_t routing_id;
} cmsg;
struct
{
metadata_t *metadata;
unsigned char
unused[msg_t_size
- (sizeof (metadata_t *) + 2 + 16 + sizeof (uint32_t))];
unsigned char type;
unsigned char flags;
char group[16];
uint32_t routing_id;
} delimiter;
} u;
};
inline int close_and_return (zmq::msg_t *msg, int echo)
{
// Since we abort on close failure we preserve errno for success case.
int err = errno;
const int rc = msg->close ();
errno_assert (rc == 0);
errno = err;
return echo;
}
inline int close_and_return (zmq::msg_t msg[], int count, int echo)
{
for (int i = 0; i < count; i++)
close_and_return (&msg[i], 0);
return echo;
}
} }
#endif #endif

View File

@@ -38,25 +38,20 @@
#include "macros.hpp" #include "macros.hpp"
#include "mtrie.hpp" #include "mtrie.hpp"
zmq::mtrie_t::mtrie_t () : zmq::mtrie_t::mtrie_t () : pipes (0), min (0), count (0), live_nodes (0)
pipes (0),
min (0),
count (0),
live_nodes (0)
{ {
} }
zmq::mtrie_t::~mtrie_t () zmq::mtrie_t::~mtrie_t ()
{ {
LIBZMQ_DELETE(pipes); LIBZMQ_DELETE (pipes);
if (count == 1) { if (count == 1) {
zmq_assert (next.node); zmq_assert (next.node);
LIBZMQ_DELETE(next.node); LIBZMQ_DELETE (next.node);
} } else if (count > 1) {
else if (count > 1) {
for (unsigned short i = 0; i != count; ++i) { for (unsigned short i = 0; i != count; ++i) {
LIBZMQ_DELETE(next.table[i]); LIBZMQ_DELETE (next.table[i]);
} }
free (next.table); free (next.table);
} }
@@ -67,8 +62,9 @@ bool zmq::mtrie_t::add (unsigned char *prefix_, size_t size_, pipe_t *pipe_)
return add_helper (prefix_, size_, pipe_); return add_helper (prefix_, size_, pipe_);
} }
bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_, bool zmq::mtrie_t::add_helper (unsigned char *prefix_,
pipe_t *pipe_) size_t size_,
pipe_t *pipe_)
{ {
// We are at the node corresponding to the prefix. We are done. // We are at the node corresponding to the prefix. We are done.
if (!size_) { if (!size_) {
@@ -83,49 +79,42 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_,
unsigned char c = *prefix_; unsigned char c = *prefix_;
if (c < min || c >= min + count) { if (c < min || c >= min + count) {
// The character is out of range of currently handled // The character is out of range of currently handled
// characters. We have to extend the table. // characters. We have to extend the table.
if (!count) { if (!count) {
min = c; min = c;
count = 1; count = 1;
next.node = NULL; next.node = NULL;
} } else if (count == 1) {
else
if (count == 1) {
unsigned char oldc = min; unsigned char oldc = min;
mtrie_t *oldp = next.node; mtrie_t *oldp = next.node;
count = (min < c ? c - min : min - c) + 1; count = (min < c ? c - min : min - c) + 1;
next.table = (mtrie_t**) next.table = (mtrie_t **) malloc (sizeof (mtrie_t *) * count);
malloc (sizeof (mtrie_t*) * count);
alloc_assert (next.table); alloc_assert (next.table);
for (unsigned short i = 0; i != count; ++i) for (unsigned short i = 0; i != count; ++i)
next.table [i] = 0; next.table[i] = 0;
min = std::min (min, c); min = std::min (min, c);
next.table [oldc - min] = oldp; next.table[oldc - min] = oldp;
} } else if (min < c) {
else
if (min < c) {
// The new character is above the current character range. // The new character is above the current character range.
unsigned short old_count = count; unsigned short old_count = count;
count = c - min + 1; count = c - min + 1;
next.table = (mtrie_t**) realloc (next.table, next.table =
sizeof (mtrie_t*) * count); (mtrie_t **) realloc (next.table, sizeof (mtrie_t *) * count);
alloc_assert (next.table); alloc_assert (next.table);
for (unsigned short i = old_count; i != count; i++) for (unsigned short i = old_count; i != count; i++)
next.table [i] = NULL; next.table[i] = NULL;
} } else {
else {
// The new character is below the current character range. // The new character is below the current character range.
unsigned short old_count = count; unsigned short old_count = count;
count = (min + old_count) - c; count = (min + old_count) - c;
next.table = (mtrie_t**) realloc (next.table, next.table =
sizeof (mtrie_t*) * count); (mtrie_t **) realloc (next.table, sizeof (mtrie_t *) * count);
alloc_assert (next.table); alloc_assert (next.table);
memmove (next.table + min - c, next.table, memmove (next.table + min - c, next.table,
old_count * sizeof (mtrie_t*)); old_count * sizeof (mtrie_t *));
for (unsigned short i = 0; i != min - c; i++) for (unsigned short i = 0; i != min - c; i++)
next.table [i] = NULL; next.table[i] = NULL;
min = c; min = c;
} }
} }
@@ -138,31 +127,38 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_,
++live_nodes; ++live_nodes;
} }
return next.node->add_helper (prefix_ + 1, size_ - 1, pipe_); return next.node->add_helper (prefix_ + 1, size_ - 1, pipe_);
} } else {
else { if (!next.table[c - min]) {
if (!next.table [c - min]) { next.table[c - min] = new (std::nothrow) mtrie_t;
next.table [c - min] = new (std::nothrow) mtrie_t; alloc_assert (next.table[c - min]);
alloc_assert (next.table [c - min]);
++live_nodes; ++live_nodes;
} }
return next.table [c - min]->add_helper (prefix_ + 1, size_ - 1, pipe_); return next.table[c - min]->add_helper (prefix_ + 1, size_ - 1, pipe_);
} }
} }
void zmq::mtrie_t::rm (pipe_t *pipe_, void zmq::mtrie_t::rm (pipe_t *pipe_,
void (*func_) (unsigned char *data_, size_t size_, void *arg_), void (*func_) (unsigned char *data_,
void *arg_, bool call_on_uniq_) size_t size_,
void *arg_),
void *arg_,
bool call_on_uniq_)
{ {
unsigned char *buff = NULL; unsigned char *buff = NULL;
rm_helper (pipe_, &buff, 0, 0, func_, arg_, call_on_uniq_); rm_helper (pipe_, &buff, 0, 0, func_, arg_, call_on_uniq_);
free (buff); free (buff);
} }
void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, void zmq::mtrie_t::rm_helper (pipe_t *pipe_,
size_t buffsize_, size_t maxbuffsize_, unsigned char **buff_,
void (*func_) (unsigned char *data_, size_t size_, void *arg_), size_t buffsize_,
void *arg_, bool call_on_uniq_) size_t maxbuffsize_,
void (*func_) (unsigned char *data_,
size_t size_,
void *arg_),
void *arg_,
bool call_on_uniq_)
{ {
// Remove the subscription from this node. // Remove the subscription from this node.
if (pipes && pipes->erase (pipe_)) { if (pipes && pipes->erase (pipe_)) {
@@ -171,14 +167,14 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
} }
if (pipes->empty ()) { if (pipes->empty ()) {
LIBZMQ_DELETE(pipes); LIBZMQ_DELETE (pipes);
} }
} }
// Adjust the buffer. // Adjust the buffer.
if (buffsize_ >= maxbuffsize_) { if (buffsize_ >= maxbuffsize_) {
maxbuffsize_ = buffsize_ + 256; maxbuffsize_ = buffsize_ + 256;
*buff_ = (unsigned char*) realloc (*buff_, maxbuffsize_); *buff_ = (unsigned char *) realloc (*buff_, maxbuffsize_);
alloc_assert (*buff_); alloc_assert (*buff_);
} }
@@ -188,14 +184,14 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
// If there's one subnode (optimisation). // If there's one subnode (optimisation).
if (count == 1) { if (count == 1) {
(*buff_) [buffsize_] = min; (*buff_)[buffsize_] = min;
buffsize_++; buffsize_++;
next.node->rm_helper (pipe_, buff_, buffsize_, maxbuffsize_, next.node->rm_helper (pipe_, buff_, buffsize_, maxbuffsize_, func_,
func_, arg_, call_on_uniq_); arg_, call_on_uniq_);
// Prune the node if it was made redundant by the removal // Prune the node if it was made redundant by the removal
if (next.node->is_redundant ()) { if (next.node->is_redundant ()) {
LIBZMQ_DELETE(next.node); LIBZMQ_DELETE (next.node);
count = 0; count = 0;
--live_nodes; --live_nodes;
zmq_assert (live_nodes == 0); zmq_assert (live_nodes == 0);
@@ -210,19 +206,18 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
// New max non-null character in the node table after the removal // New max non-null character in the node table after the removal
unsigned char new_max = min; unsigned char new_max = min;
for (unsigned short c = 0; c != count; c++) { for (unsigned short c = 0; c != count; c++) {
(*buff_) [buffsize_] = min + c; (*buff_)[buffsize_] = min + c;
if (next.table [c]) { if (next.table[c]) {
next.table [c]->rm_helper (pipe_, buff_, buffsize_ + 1, next.table[c]->rm_helper (pipe_, buff_, buffsize_ + 1, maxbuffsize_,
maxbuffsize_, func_, arg_, call_on_uniq_); func_, arg_, call_on_uniq_);
// Prune redundant nodes from the mtrie // Prune redundant nodes from the mtrie
if (next.table [c]->is_redundant ()) { if (next.table[c]->is_redundant ()) {
LIBZMQ_DELETE(next.table[c]); LIBZMQ_DELETE (next.table[c]);
zmq_assert (live_nodes > 0); zmq_assert (live_nodes > 0);
--live_nodes; --live_nodes;
} } else {
else {
// The node is not redundant, so it's a candidate for being // The node is not redundant, so it's a candidate for being
// the new min/max node. // the new min/max node.
// //
@@ -247,22 +242,19 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
count = 0; count = 0;
} }
// Compact the node table if possible // Compact the node table if possible
else else if (live_nodes == 1) {
if (live_nodes == 1) {
// If there's only one live node in the table we can // If there's only one live node in the table we can
// switch to using the more compact single-node // switch to using the more compact single-node
// representation // representation
zmq_assert (new_min == new_max); zmq_assert (new_min == new_max);
zmq_assert (new_min >= min && new_min < min + count); zmq_assert (new_min >= min && new_min < min + count);
mtrie_t *node = next.table [new_min - min]; mtrie_t *node = next.table[new_min - min];
zmq_assert (node); zmq_assert (node);
free (next.table); free (next.table);
next.node = node; next.node = node;
count = 1; count = 1;
min = new_min; min = new_min;
} } else if (new_min > min || new_max < min + count - 1) {
else
if (new_min > min || new_max < min + count - 1) {
zmq_assert (new_max - new_min + 1 > 1); zmq_assert (new_max - new_min + 1 > 1);
mtrie_t **old_table = next.table; mtrie_t **old_table = next.table;
@@ -272,11 +264,11 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
zmq_assert (new_max - new_min + 1 < count); zmq_assert (new_max - new_min + 1 < count);
count = new_max - new_min + 1; count = new_max - new_min + 1;
next.table = (mtrie_t**) malloc (sizeof (mtrie_t*) * count); next.table = (mtrie_t **) malloc (sizeof (mtrie_t *) * count);
alloc_assert (next.table); alloc_assert (next.table);
memmove (next.table, old_table + (new_min - min), memmove (next.table, old_table + (new_min - min),
sizeof (mtrie_t*) * count); sizeof (mtrie_t *) * count);
free (old_table); free (old_table);
min = new_min; min = new_min;
@@ -288,15 +280,16 @@ bool zmq::mtrie_t::rm (unsigned char *prefix_, size_t size_, pipe_t *pipe_)
return rm_helper (prefix_, size_, pipe_); return rm_helper (prefix_, size_, pipe_);
} }
bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_, bool zmq::mtrie_t::rm_helper (unsigned char *prefix_,
pipe_t *pipe_) size_t size_,
pipe_t *pipe_)
{ {
if (!size_) { if (!size_) {
if (pipes) { if (pipes) {
pipes_t::size_type erased = pipes->erase (pipe_); pipes_t::size_type erased = pipes->erase (pipe_);
zmq_assert (erased == 1); zmq_assert (erased == 1);
if (pipes->empty ()) { if (pipes->empty ()) {
LIBZMQ_DELETE(pipes); LIBZMQ_DELETE (pipes);
} }
} }
return !pipes; return !pipes;
@@ -306,8 +299,7 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
if (!count || c < min || c >= min + count) if (!count || c < min || c >= min + count)
return false; return false;
mtrie_t *next_node = mtrie_t *next_node = count == 1 ? next.node : next.table[c - min];
count == 1 ? next.node : next.table [c - min];
if (!next_node) if (!next_node)
return false; return false;
@@ -315,7 +307,7 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
bool ret = next_node->rm_helper (prefix_ + 1, size_ - 1, pipe_); bool ret = next_node->rm_helper (prefix_ + 1, size_ - 1, pipe_);
if (next_node->is_redundant ()) { if (next_node->is_redundant ()) {
LIBZMQ_DELETE(next_node); LIBZMQ_DELETE (next_node);
zmq_assert (count > 0); zmq_assert (count > 0);
if (count == 1) { if (count == 1) {
@@ -323,9 +315,8 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
count = 0; count = 0;
--live_nodes; --live_nodes;
zmq_assert (live_nodes == 0); zmq_assert (live_nodes == 0);
} } else {
else { next.table[c - min] = 0;
next.table [c - min] = 0;
zmq_assert (live_nodes > 1); zmq_assert (live_nodes > 1);
--live_nodes; --live_nodes;
@@ -336,47 +327,43 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
// representation // representation
unsigned short i; unsigned short i;
for (i = 0; i < count; ++i) for (i = 0; i < count; ++i)
if (next.table [i]) if (next.table[i])
break; break;
zmq_assert (i < count); zmq_assert (i < count);
min += i; min += i;
count = 1; count = 1;
mtrie_t *oldp = next.table [i]; mtrie_t *oldp = next.table[i];
free (next.table); free (next.table);
next.node = oldp; next.node = oldp;
} } else if (c == min) {
else
if (c == min) {
// We can compact the table "from the left" // We can compact the table "from the left"
unsigned short i; unsigned short i;
for (i = 1; i < count; ++i) for (i = 1; i < count; ++i)
if (next.table [i]) if (next.table[i])
break; break;
zmq_assert (i < count); zmq_assert (i < count);
min += i; min += i;
count -= i; count -= i;
mtrie_t **old_table = next.table; mtrie_t **old_table = next.table;
next.table = (mtrie_t**) malloc (sizeof (mtrie_t*) * count); next.table = (mtrie_t **) malloc (sizeof (mtrie_t *) * count);
alloc_assert (next.table); alloc_assert (next.table);
memmove (next.table, old_table + i, sizeof (mtrie_t*) * count); memmove (next.table, old_table + i, sizeof (mtrie_t *) * count);
free (old_table); free (old_table);
} } else if (c == min + count - 1) {
else
if (c == min + count - 1) {
// We can compact the table "from the right" // We can compact the table "from the right"
unsigned short i; unsigned short i;
for (i = 1; i < count; ++i) for (i = 1; i < count; ++i)
if (next.table [count - 1 - i]) if (next.table[count - 1 - i])
break; break;
zmq_assert (i < count); zmq_assert (i < count);
count -= i; count -= i;
mtrie_t **old_table = next.table; mtrie_t **old_table = next.table;
next.table = (mtrie_t**) malloc (sizeof (mtrie_t*) * count); next.table = (mtrie_t **) malloc (sizeof (mtrie_t *) * count);
alloc_assert (next.table); alloc_assert (next.table);
memmove (next.table, old_table, sizeof (mtrie_t*) * count); memmove (next.table, old_table, sizeof (mtrie_t *) * count);
free (old_table); free (old_table);
} }
} }
@@ -385,16 +372,17 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
return ret; return ret;
} }
void zmq::mtrie_t::match (unsigned char *data_, size_t size_, void zmq::mtrie_t::match (unsigned char *data_,
void (*func_) (pipe_t *pipe_, void *arg_), void *arg_) size_t size_,
void (*func_) (pipe_t *pipe_, void *arg_),
void *arg_)
{ {
mtrie_t *current = this; mtrie_t *current = this;
while (true) { while (true) {
// Signal the pipes attached to this node. // Signal the pipes attached to this node.
if (current->pipes) { if (current->pipes) {
for (pipes_t::iterator it = current->pipes->begin (); for (pipes_t::iterator it = current->pipes->begin ();
it != current->pipes->end (); ++it) it != current->pipes->end (); ++it)
func_ (*it, arg_); func_ (*it, arg_);
} }
@@ -408,7 +396,7 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_,
// If there's one subnode (optimisation). // If there's one subnode (optimisation).
if (current->count == 1) { if (current->count == 1) {
if (data_ [0] != current->min) if (data_[0] != current->min)
break; break;
current = current->next.node; current = current->next.node;
data_++; data_++;
@@ -417,12 +405,12 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_,
} }
// If there are multiple subnodes. // If there are multiple subnodes.
if (data_ [0] < current->min || data_ [0] >= if (data_[0] < current->min
current->min + current->count) || data_[0] >= current->min + current->count)
break; break;
if (!current->next.table [data_ [0] - current->min]) if (!current->next.table[data_[0] - current->min])
break; break;
current = current->next.table [data_ [0] - current->min]; current = current->next.table[data_[0] - current->min];
data_++; data_++;
size_--; size_--;
} }

View File

@@ -37,66 +37,67 @@
namespace zmq namespace zmq
{ {
class pipe_t;
class pipe_t; // Multi-trie. Each node in the trie is a set of pointers to pipes.
// Multi-trie. Each node in the trie is a set of pointers to pipes. class mtrie_t
{
public:
mtrie_t ();
~mtrie_t ();
class mtrie_t // Add key to the trie. Returns true if it's a new subscription
// rather than a duplicate.
bool add (unsigned char *prefix_, size_t size_, zmq::pipe_t *pipe_);
// Remove all subscriptions for a specific peer from the trie.
// The call_on_uniq_ flag controls if the callback is invoked
// when there are no subscriptions left on some topics or on
// every removal.
void rm (zmq::pipe_t *pipe_,
void (*func_) (unsigned char *data_, size_t size_, void *arg_),
void *arg_,
bool call_on_uniq_);
// Remove specific subscription from the trie. Return true is it was
// actually removed rather than de-duplicated.
bool rm (unsigned char *prefix_, size_t size_, zmq::pipe_t *pipe_);
// Signal all the matching pipes.
void match (unsigned char *data_,
size_t size_,
void (*func_) (zmq::pipe_t *pipe_, void *arg_),
void *arg_);
private:
bool add_helper (unsigned char *prefix_, size_t size_, zmq::pipe_t *pipe_);
void
rm_helper (zmq::pipe_t *pipe_,
unsigned char **buff_,
size_t buffsize_,
size_t maxbuffsize_,
void (*func_) (unsigned char *data_, size_t size_, void *arg_),
void *arg_,
bool call_on_uniq_);
bool rm_helper (unsigned char *prefix_, size_t size_, zmq::pipe_t *pipe_);
bool is_redundant () const;
typedef std::set<zmq::pipe_t *> pipes_t;
pipes_t *pipes;
unsigned char min;
unsigned short count;
unsigned short live_nodes;
union
{ {
public: class mtrie_t *node;
class mtrie_t **table;
mtrie_t (); } next;
~mtrie_t ();
// Add key to the trie. Returns true if it's a new subscription
// rather than a duplicate.
bool add (unsigned char *prefix_, size_t size_, zmq::pipe_t *pipe_);
// Remove all subscriptions for a specific peer from the trie.
// The call_on_uniq_ flag controls if the callback is invoked
// when there are no subscriptions left on some topics or on
// every removal.
void rm (zmq::pipe_t *pipe_,
void (*func_) (unsigned char *data_, size_t size_, void *arg_),
void *arg_, bool call_on_uniq_);
// Remove specific subscription from the trie. Return true is it was
// actually removed rather than de-duplicated.
bool rm (unsigned char *prefix_, size_t size_, zmq::pipe_t *pipe_);
// Signal all the matching pipes.
void match (unsigned char *data_, size_t size_,
void (*func_) (zmq::pipe_t *pipe_, void *arg_), void *arg_);
private:
bool add_helper (unsigned char *prefix_, size_t size_,
zmq::pipe_t *pipe_);
void rm_helper (zmq::pipe_t *pipe_, unsigned char **buff_,
size_t buffsize_, size_t maxbuffsize_,
void (*func_) (unsigned char *data_, size_t size_, void *arg_),
void *arg_, bool call_on_uniq_);
bool rm_helper (unsigned char *prefix_, size_t size_,
zmq::pipe_t *pipe_);
bool is_redundant () const;
typedef std::set <zmq::pipe_t*> pipes_t;
pipes_t *pipes;
unsigned char min;
unsigned short count;
unsigned short live_nodes;
union {
class mtrie_t *node;
class mtrie_t **table;
} next;
mtrie_t (const mtrie_t&);
const mtrie_t &operator = (const mtrie_t&);
};
mtrie_t (const mtrie_t &);
const mtrie_t &operator= (const mtrie_t &);
};
} }
#endif #endif

View File

@@ -40,49 +40,31 @@
namespace zmq namespace zmq
{ {
class mutex_t
{
public:
inline mutex_t () { InitializeCriticalSection (&cs); }
class mutex_t inline ~mutex_t () { DeleteCriticalSection (&cs); }
inline void lock () { EnterCriticalSection (&cs); }
inline bool try_lock ()
{ {
public: return (TryEnterCriticalSection (&cs)) ? true : false;
inline mutex_t () }
{
InitializeCriticalSection (&cs);
}
inline ~mutex_t () inline void unlock () { LeaveCriticalSection (&cs); }
{
DeleteCriticalSection (&cs);
}
inline void lock () inline CRITICAL_SECTION *get_cs () { return &cs; }
{
EnterCriticalSection (&cs);
}
inline bool try_lock () private:
{ CRITICAL_SECTION cs;
return (TryEnterCriticalSection (&cs)) ? true : false;
}
inline void unlock ()
{
LeaveCriticalSection (&cs);
}
inline CRITICAL_SECTION* get_cs()
{
return &cs;
}
private:
CRITICAL_SECTION cs;
// Disable copy construction and assignment.
mutex_t (const mutex_t&);
void operator = (const mutex_t&);
};
// Disable copy construction and assignment.
mutex_t (const mutex_t &);
void operator= (const mutex_t &);
};
} }
#else #else
@@ -91,68 +73,62 @@ namespace zmq
namespace zmq namespace zmq
{ {
class mutex_t
class mutex_t {
public:
inline mutex_t ()
{ {
public: int rc = pthread_mutexattr_init (&attr);
inline mutex_t () posix_assert (rc);
{
int rc = pthread_mutexattr_init(&attr);
posix_assert (rc);
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); rc = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
posix_assert (rc); posix_assert (rc);
rc = pthread_mutex_init (&mutex, &attr); rc = pthread_mutex_init (&mutex, &attr);
posix_assert (rc); posix_assert (rc);
} }
inline ~mutex_t () inline ~mutex_t ()
{ {
int rc = pthread_mutex_destroy (&mutex); int rc = pthread_mutex_destroy (&mutex);
posix_assert (rc); posix_assert (rc);
rc = pthread_mutexattr_destroy (&attr); rc = pthread_mutexattr_destroy (&attr);
posix_assert (rc); posix_assert (rc);
} }
inline void lock () inline void lock ()
{ {
int rc = pthread_mutex_lock (&mutex); int rc = pthread_mutex_lock (&mutex);
posix_assert (rc); posix_assert (rc);
} }
inline bool try_lock () inline bool try_lock ()
{ {
int rc = pthread_mutex_trylock (&mutex); int rc = pthread_mutex_trylock (&mutex);
if (rc == EBUSY) if (rc == EBUSY)
return false; return false;
posix_assert (rc); posix_assert (rc);
return true; return true;
} }
inline void unlock () inline void unlock ()
{ {
int rc = pthread_mutex_unlock (&mutex); int rc = pthread_mutex_unlock (&mutex);
posix_assert (rc); posix_assert (rc);
} }
inline pthread_mutex_t* get_mutex() inline pthread_mutex_t *get_mutex () { return &mutex; }
{
return &mutex;
}
private: private:
pthread_mutex_t mutex;
pthread_mutex_t mutex; pthread_mutexattr_t attr;
pthread_mutexattr_t attr;
// Disable copy construction and assignment.
mutex_t (const mutex_t&);
const mutex_t &operator = (const mutex_t&);
};
// Disable copy construction and assignment.
mutex_t (const mutex_t &);
const mutex_t &operator= (const mutex_t &);
};
} }
#endif #endif
@@ -160,54 +136,42 @@ namespace zmq
namespace zmq namespace zmq
{ {
struct scoped_lock_t struct scoped_lock_t
{
scoped_lock_t (mutex_t &mutex_) : mutex (mutex_) { mutex.lock (); }
~scoped_lock_t () { mutex.unlock (); }
private:
mutex_t &mutex;
// Disable copy construction and assignment.
scoped_lock_t (const scoped_lock_t &);
const scoped_lock_t &operator= (const scoped_lock_t &);
};
struct scoped_optional_lock_t
{
scoped_optional_lock_t (mutex_t *mutex_) : mutex (mutex_)
{ {
scoped_lock_t (mutex_t& mutex_) if (mutex != NULL)
: mutex (mutex_) mutex->lock ();
{ }
mutex.lock ();
}
~scoped_lock_t () ~scoped_optional_lock_t ()
{
mutex.unlock ();
}
private:
mutex_t& mutex;
// Disable copy construction and assignment.
scoped_lock_t (const scoped_lock_t&);
const scoped_lock_t &operator = (const scoped_lock_t&);
};
struct scoped_optional_lock_t
{ {
scoped_optional_lock_t (mutex_t* mutex_) if (mutex != NULL)
: mutex (mutex_) mutex->unlock ();
{ }
if(mutex != NULL)
mutex->lock ();
}
~scoped_optional_lock_t ()
{
if(mutex != NULL)
mutex->unlock ();
}
private:
mutex_t* mutex;
// Disable copy construction and assignment.
scoped_optional_lock_t (const scoped_lock_t&);
const scoped_optional_lock_t &operator = (const scoped_lock_t&);
};
private:
mutex_t *mutex;
// Disable copy construction and assignment.
scoped_optional_lock_t (const scoped_lock_t &);
const scoped_optional_lock_t &operator= (const scoped_lock_t &);
};
} }
#endif #endif

View File

@@ -9,88 +9,90 @@
#include "session_base.hpp" #include "session_base.hpp"
#include "v2_protocol.hpp" #include "v2_protocol.hpp"
zmq::norm_engine_t::norm_engine_t(io_thread_t* parent_, zmq::norm_engine_t::norm_engine_t (io_thread_t *parent_,
const options_t& options_) const options_t &options_) :
: io_object_t(parent_), zmq_session(NULL), options(options_), io_object_t (parent_),
norm_instance(NORM_INSTANCE_INVALID), norm_session(NORM_SESSION_INVALID), zmq_session (NULL),
is_sender(false), is_receiver(false), options (options_),
zmq_encoder(0), norm_tx_stream(NORM_OBJECT_INVALID), norm_instance (NORM_INSTANCE_INVALID),
tx_first_msg(true), tx_more_bit(false), norm_session (NORM_SESSION_INVALID),
zmq_output_ready(false), norm_tx_ready(false), is_sender (false),
tx_index(0), tx_len(0), is_receiver (false),
zmq_input_ready(false) zmq_encoder (0),
norm_tx_stream (NORM_OBJECT_INVALID),
tx_first_msg (true),
tx_more_bit (false),
zmq_output_ready (false),
norm_tx_ready (false),
tx_index (0),
tx_len (0),
zmq_input_ready (false)
{ {
int rc = tx_msg.init(); int rc = tx_msg.init ();
errno_assert(0 == rc); errno_assert (0 == rc);
} }
zmq::norm_engine_t::~norm_engine_t() zmq::norm_engine_t::~norm_engine_t ()
{ {
shutdown(); // in case it was not already called shutdown (); // in case it was not already called
} }
int zmq::norm_engine_t::init(const char* network_, bool send, bool recv) int zmq::norm_engine_t::init (const char *network_, bool send, bool recv)
{ {
// Parse the "network_" address int "iface", "addr", and "port" // Parse the "network_" address int "iface", "addr", and "port"
// norm endpoint format: [id,][<iface>;]<addr>:<port> // norm endpoint format: [id,][<iface>;]<addr>:<port>
// First, look for optional local NormNodeId // First, look for optional local NormNodeId
// (default NORM_NODE_ANY causes NORM to use host IP addr for NormNodeId) // (default NORM_NODE_ANY causes NORM to use host IP addr for NormNodeId)
NormNodeId localId = NORM_NODE_ANY; NormNodeId localId = NORM_NODE_ANY;
const char* ifacePtr = strchr(network_, ','); const char *ifacePtr = strchr (network_, ',');
if (NULL != ifacePtr) if (NULL != ifacePtr) {
{
size_t idLen = ifacePtr - network_; size_t idLen = ifacePtr - network_;
if (idLen > 31) idLen = 31; if (idLen > 31)
idLen = 31;
char idText[32]; char idText[32];
strncpy(idText, network_, idLen); strncpy (idText, network_, idLen);
idText[idLen] = '\0'; idText[idLen] = '\0';
localId = (NormNodeId)atoi(idText); localId = (NormNodeId) atoi (idText);
ifacePtr++; ifacePtr++;
} } else {
else
{
ifacePtr = network_; ifacePtr = network_;
} }
// Second, look for optional multicast ifaceName // Second, look for optional multicast ifaceName
char ifaceName[256]; char ifaceName[256];
const char* addrPtr = strchr(ifacePtr, ';'); const char *addrPtr = strchr (ifacePtr, ';');
if (NULL != addrPtr) if (NULL != addrPtr) {
{
size_t ifaceLen = addrPtr - ifacePtr; size_t ifaceLen = addrPtr - ifacePtr;
if (ifaceLen > 255) ifaceLen = 255; // return error instead? if (ifaceLen > 255)
strncpy(ifaceName, ifacePtr, ifaceLen); ifaceLen = 255; // return error instead?
strncpy (ifaceName, ifacePtr, ifaceLen);
ifaceName[ifaceLen] = '\0'; ifaceName[ifaceLen] = '\0';
ifacePtr = ifaceName; ifacePtr = ifaceName;
addrPtr++; addrPtr++;
} } else {
else
{
addrPtr = ifacePtr; addrPtr = ifacePtr;
ifacePtr = NULL; ifacePtr = NULL;
} }
// Finally, parse IP address and port number // Finally, parse IP address and port number
const char* portPtr = strrchr(addrPtr, ':'); const char *portPtr = strrchr (addrPtr, ':');
if (NULL == portPtr) if (NULL == portPtr) {
{
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
char addr[256]; char addr[256];
size_t addrLen = portPtr - addrPtr; size_t addrLen = portPtr - addrPtr;
if (addrLen > 255) addrLen = 255; if (addrLen > 255)
strncpy(addr, addrPtr, addrLen); addrLen = 255;
strncpy (addr, addrPtr, addrLen);
addr[addrLen] = '\0'; addr[addrLen] = '\0';
portPtr++; portPtr++;
unsigned short portNumber = atoi(portPtr); unsigned short portNumber = atoi (portPtr);
if (NORM_INSTANCE_INVALID == norm_instance) if (NORM_INSTANCE_INVALID == norm_instance) {
{ if (NORM_INSTANCE_INVALID == (norm_instance = NormCreateInstance ())) {
if (NORM_INSTANCE_INVALID == (norm_instance = NormCreateInstance()))
{
// errno set by whatever caused NormCreateInstance() to fail // errno set by whatever caused NormCreateInstance() to fail
return -1; return -1;
} }
@@ -103,46 +105,44 @@ int zmq::norm_engine_t::init(const char* network_, bool send, bool recv)
// c) Randomize and implement a NORM session layer // c) Randomize and implement a NORM session layer
// conflict detection/resolution protocol // conflict detection/resolution protocol
norm_session = NormCreateSession(norm_instance, addr, portNumber, localId); norm_session = NormCreateSession (norm_instance, addr, portNumber, localId);
if (NORM_SESSION_INVALID == norm_session) if (NORM_SESSION_INVALID == norm_session) {
{
int savedErrno = errno; int savedErrno = errno;
NormDestroyInstance(norm_instance); NormDestroyInstance (norm_instance);
norm_instance = NORM_INSTANCE_INVALID; norm_instance = NORM_INSTANCE_INVALID;
errno = savedErrno; errno = savedErrno;
return -1; return -1;
} }
// There's many other useful NORM options that could be applied here // There's many other useful NORM options that could be applied here
if (NormIsUnicastAddress(addr)) if (NormIsUnicastAddress (addr)) {
{ NormSetDefaultUnicastNack (norm_session, true);
NormSetDefaultUnicastNack(norm_session, true); } else {
}
else
{
// These only apply for multicast sessions // These only apply for multicast sessions
//NormSetTTL(norm_session, options.multicast_hops); // ZMQ default is 1 //NormSetTTL(norm_session, options.multicast_hops); // ZMQ default is 1
NormSetTTL(norm_session, 255); // since the ZMQ_MULTICAST_HOPS socket option isn't well-supported NormSetTTL (
NormSetRxPortReuse(norm_session, true); // port reuse doesn't work for non-connected unicast norm_session,
NormSetLoopback(norm_session, true); // needed when multicast users on same machine 255); // since the ZMQ_MULTICAST_HOPS socket option isn't well-supported
if (NULL != ifacePtr) NormSetRxPortReuse (
{ norm_session,
true); // port reuse doesn't work for non-connected unicast
NormSetLoopback (norm_session,
true); // needed when multicast users on same machine
if (NULL != ifacePtr) {
// Note a bad interface may not be caught until sender or receiver start // Note a bad interface may not be caught until sender or receiver start
// (Since sender/receiver is not yet started, this always succeeds here) // (Since sender/receiver is not yet started, this always succeeds here)
NormSetMulticastInterface(norm_session, ifacePtr); NormSetMulticastInterface (norm_session, ifacePtr);
} }
} }
if (recv) if (recv) {
{
// The alternative NORM_SYNC_CURRENT here would provide "instant" // The alternative NORM_SYNC_CURRENT here would provide "instant"
// receiver sync to the sender's _current_ message transmission. // receiver sync to the sender's _current_ message transmission.
// NORM_SYNC_STREAM tries to get everything the sender has cached/buffered // NORM_SYNC_STREAM tries to get everything the sender has cached/buffered
NormSetDefaultSyncPolicy(norm_session, NORM_SYNC_STREAM); NormSetDefaultSyncPolicy (norm_session, NORM_SYNC_STREAM);
if (!NormStartReceiver(norm_session, 2*1024*1024)) if (!NormStartReceiver (norm_session, 2 * 1024 * 1024)) {
{
// errno set by whatever failed // errno set by whatever failed
int savedErrno = errno; int savedErrno = errno;
NormDestroyInstance(norm_instance); // session gets closed, too NormDestroyInstance (norm_instance); // session gets closed, too
norm_session = NORM_SESSION_INVALID; norm_session = NORM_SESSION_INVALID;
norm_instance = NORM_INSTANCE_INVALID; norm_instance = NORM_INSTANCE_INVALID;
errno = savedErrno; errno = savedErrno;
@@ -151,29 +151,29 @@ int zmq::norm_engine_t::init(const char* network_, bool send, bool recv)
is_receiver = true; is_receiver = true;
} }
if (send) if (send) {
{
// Pick a random sender instance id (aka norm sender session id) // Pick a random sender instance id (aka norm sender session id)
NormSessionId instanceId = NormGetRandomSessionId(); NormSessionId instanceId = NormGetRandomSessionId ();
// TBD - provide "options" for some NORM sender parameters // TBD - provide "options" for some NORM sender parameters
if (!NormStartSender(norm_session, instanceId, 2*1024*1024, 1400, 16, 4)) if (!NormStartSender (norm_session, instanceId, 2 * 1024 * 1024, 1400,
{ 16, 4)) {
// errno set by whatever failed // errno set by whatever failed
int savedErrno = errno; int savedErrno = errno;
NormDestroyInstance(norm_instance); // session gets closed, too NormDestroyInstance (norm_instance); // session gets closed, too
norm_session = NORM_SESSION_INVALID; norm_session = NORM_SESSION_INVALID;
norm_instance = NORM_INSTANCE_INVALID; norm_instance = NORM_INSTANCE_INVALID;
errno = savedErrno; errno = savedErrno;
return -1; return -1;
} }
NormSetCongestionControl(norm_session, true); NormSetCongestionControl (norm_session, true);
norm_tx_ready = true; norm_tx_ready = true;
is_sender = true; is_sender = true;
if (NORM_OBJECT_INVALID == (norm_tx_stream = NormStreamOpen(norm_session, 2*1024*1024))) if (NORM_OBJECT_INVALID
{ == (norm_tx_stream =
NormStreamOpen (norm_session, 2 * 1024 * 1024))) {
// errno set by whatever failed // errno set by whatever failed
int savedErrno = errno; int savedErrno = errno;
NormDestroyInstance(norm_instance); // session gets closed, too NormDestroyInstance (norm_instance); // session gets closed, too
norm_session = NORM_SESSION_INVALID; norm_session = NORM_SESSION_INVALID;
norm_instance = NORM_INSTANCE_INVALID; norm_instance = NORM_INSTANCE_INVALID;
errno = savedErrno; errno = savedErrno;
@@ -185,100 +185,95 @@ int zmq::norm_engine_t::init(const char* network_, bool send, bool recv)
//NormSetDebugLevel(3); //NormSetDebugLevel(3);
//NormOpenDebugLog(norm_instance, "normLog.txt"); //NormOpenDebugLog(norm_instance, "normLog.txt");
return 0; // no error return 0; // no error
} // end zmq::norm_engine_t::init() } // end zmq::norm_engine_t::init()
void zmq::norm_engine_t::shutdown() void zmq::norm_engine_t::shutdown ()
{ {
// TBD - implement a more graceful shutdown option // TBD - implement a more graceful shutdown option
if (is_receiver) if (is_receiver) {
{ NormStopReceiver (norm_session);
NormStopReceiver(norm_session);
// delete any active NormRxStreamState // delete any active NormRxStreamState
rx_pending_list.Destroy(); rx_pending_list.Destroy ();
rx_ready_list.Destroy(); rx_ready_list.Destroy ();
msg_ready_list.Destroy(); msg_ready_list.Destroy ();
is_receiver = false; is_receiver = false;
} }
if (is_sender) if (is_sender) {
{ NormStopSender (norm_session);
NormStopSender(norm_session);
is_sender = false; is_sender = false;
} }
if (NORM_SESSION_INVALID != norm_session) if (NORM_SESSION_INVALID != norm_session) {
{ NormDestroySession (norm_session);
NormDestroySession(norm_session);
norm_session = NORM_SESSION_INVALID; norm_session = NORM_SESSION_INVALID;
} }
if (NORM_INSTANCE_INVALID != norm_instance) if (NORM_INSTANCE_INVALID != norm_instance) {
{ NormStopInstance (norm_instance);
NormStopInstance(norm_instance); NormDestroyInstance (norm_instance);
NormDestroyInstance(norm_instance);
norm_instance = NORM_INSTANCE_INVALID; norm_instance = NORM_INSTANCE_INVALID;
} }
} // end zmq::norm_engine_t::shutdown() } // end zmq::norm_engine_t::shutdown()
void zmq::norm_engine_t::plug (io_thread_t* io_thread_, session_base_t *session_) void zmq::norm_engine_t::plug (io_thread_t *io_thread_,
session_base_t *session_)
{ {
// TBD - we may assign the NORM engine to an io_thread in the future??? // TBD - we may assign the NORM engine to an io_thread in the future???
zmq_session = session_; zmq_session = session_;
if (is_sender) zmq_output_ready = true; if (is_sender)
if (is_receiver) zmq_input_ready = true; zmq_output_ready = true;
if (is_receiver)
zmq_input_ready = true;
fd_t normDescriptor = NormGetDescriptor(norm_instance); fd_t normDescriptor = NormGetDescriptor (norm_instance);
norm_descriptor_handle = add_fd(normDescriptor); norm_descriptor_handle = add_fd (normDescriptor);
// Set POLLIN for notification of pending NormEvents // Set POLLIN for notification of pending NormEvents
set_pollin(norm_descriptor_handle); set_pollin (norm_descriptor_handle);
if (is_sender) send_data(); if (is_sender)
send_data ();
} // end zmq::norm_engine_t::init() } // end zmq::norm_engine_t::init()
void zmq::norm_engine_t::unplug() void zmq::norm_engine_t::unplug ()
{ {
rm_fd(norm_descriptor_handle); rm_fd (norm_descriptor_handle);
zmq_session = NULL; zmq_session = NULL;
} // end zmq::norm_engine_t::unplug() } // end zmq::norm_engine_t::unplug()
void zmq::norm_engine_t::terminate() void zmq::norm_engine_t::terminate ()
{ {
unplug(); unplug ();
shutdown(); shutdown ();
delete this; delete this;
} }
void zmq::norm_engine_t::restart_output() void zmq::norm_engine_t::restart_output ()
{ {
// There's new message data available from the session // There's new message data available from the session
zmq_output_ready = true; zmq_output_ready = true;
if (norm_tx_ready) send_data(); if (norm_tx_ready)
send_data ();
} // end zmq::norm_engine_t::restart_output() } // end zmq::norm_engine_t::restart_output()
void zmq::norm_engine_t::send_data() void zmq::norm_engine_t::send_data ()
{ {
// Here we write as much as is available or we can // Here we write as much as is available or we can
while (zmq_output_ready && norm_tx_ready) while (zmq_output_ready && norm_tx_ready) {
{ if (0 == tx_len) {
if (0 == tx_len)
{
// Our tx_buffer needs data to send // Our tx_buffer needs data to send
// Get more data from encoder // Get more data from encoder
size_t space = BUFFER_SIZE; size_t space = BUFFER_SIZE;
unsigned char* bufPtr = (unsigned char*)tx_buffer; unsigned char *bufPtr = (unsigned char *) tx_buffer;
tx_len = zmq_encoder.encode(&bufPtr, space); tx_len = zmq_encoder.encode (&bufPtr, space);
if (0 == tx_len) if (0 == tx_len) {
{ if (tx_first_msg) {
if (tx_first_msg)
{
// We don't need to mark eom/flush until a message is sent // We don't need to mark eom/flush until a message is sent
tx_first_msg = false; tx_first_msg = false;
} } else {
else
{
// A prior message was completely written to stream, so // A prior message was completely written to stream, so
// mark end-of-message and possibly flush (to force packet transmission, // mark end-of-message and possibly flush (to force packet transmission,
// even if it's not a full segment so message gets delivered quickly) // even if it's not a full segment so message gets delivered quickly)
@@ -286,16 +281,15 @@ void zmq::norm_engine_t::send_data()
// Note NORM_FLUSH_ACTIVE makes NORM fairly chatty for low duty cycle messaging // Note NORM_FLUSH_ACTIVE makes NORM fairly chatty for low duty cycle messaging
// but makes sure content is delivered quickly. Positive acknowledgements // but makes sure content is delivered quickly. Positive acknowledgements
// with flush override would make NORM more succinct here // with flush override would make NORM more succinct here
NormStreamFlush(norm_tx_stream, true, NORM_FLUSH_ACTIVE); NormStreamFlush (norm_tx_stream, true, NORM_FLUSH_ACTIVE);
} }
// Need to pull and load a new message to send // Need to pull and load a new message to send
if (-1 == zmq_session->pull_msg(&tx_msg)) if (-1 == zmq_session->pull_msg (&tx_msg)) {
{
// We need to wait for "restart_output()" to be called by ZMQ // We need to wait for "restart_output()" to be called by ZMQ
zmq_output_ready = false; zmq_output_ready = false;
break; break;
} }
zmq_encoder.load_msg(&tx_msg); zmq_encoder.load_msg (&tx_msg);
// Should we write message size header for NORM to use? Or expect NORM // Should we write message size header for NORM to use? Or expect NORM
// receiver to decode ZMQ message framing format(s)? // receiver to decode ZMQ message framing format(s)?
// OK - we need to use a byte to denote when the ZMQ frame is the _first_ // OK - we need to use a byte to denote when the ZMQ frame is the _first_
@@ -304,71 +298,68 @@ void zmq::norm_engine_t::send_data()
// I.e.,If more_flag _was_ false previously, this is the first // I.e.,If more_flag _was_ false previously, this is the first
// frame of a ZMQ message. // frame of a ZMQ message.
if (tx_more_bit) if (tx_more_bit)
tx_buffer[0] = (char)0xff; // this is not first frame of message tx_buffer[0] =
(char) 0xff; // this is not first frame of message
else else
tx_buffer[0] = 0x00; // this is first frame of message tx_buffer[0] = 0x00; // this is first frame of message
tx_more_bit = (0 != (tx_msg.flags() & msg_t::more)); tx_more_bit = (0 != (tx_msg.flags () & msg_t::more));
// Go ahead an get a first chunk of the message // Go ahead an get a first chunk of the message
bufPtr++; bufPtr++;
space--; space--;
tx_len = 1 + zmq_encoder.encode(&bufPtr, space); tx_len = 1 + zmq_encoder.encode (&bufPtr, space);
tx_index = 0; tx_index = 0;
} }
} }
// Do we have data in our tx_buffer pending // Do we have data in our tx_buffer pending
if (tx_index < tx_len) if (tx_index < tx_len) {
{
// We have data in our tx_buffer to send, so write it to the stream // We have data in our tx_buffer to send, so write it to the stream
tx_index += NormStreamWrite(norm_tx_stream, tx_buffer + tx_index, tx_len - tx_index); tx_index += NormStreamWrite (norm_tx_stream, tx_buffer + tx_index,
if (tx_index < tx_len) tx_len - tx_index);
{ if (tx_index < tx_len) {
// NORM stream buffer full, wait for NORM_TX_QUEUE_VACANCY // NORM stream buffer full, wait for NORM_TX_QUEUE_VACANCY
norm_tx_ready = false; norm_tx_ready = false;
break; break;
} }
tx_len = 0; // all buffered data was written tx_len = 0; // all buffered data was written
} }
} // end while (zmq_output_ready && norm_tx_ready) } // end while (zmq_output_ready && norm_tx_ready)
} // end zmq::norm_engine_t::send_data() } // end zmq::norm_engine_t::send_data()
void zmq::norm_engine_t::in_event() void zmq::norm_engine_t::in_event ()
{ {
// This means a NormEvent is pending, so call NormGetNextEvent() and handle // This means a NormEvent is pending, so call NormGetNextEvent() and handle
NormEvent event; NormEvent event;
if (!NormGetNextEvent(norm_instance, &event)) if (!NormGetNextEvent (norm_instance, &event)) {
{
// NORM has died before we unplugged?! // NORM has died before we unplugged?!
zmq_assert(false); zmq_assert (false);
return; return;
} }
switch(event.type) switch (event.type) {
{
case NORM_TX_QUEUE_VACANCY: case NORM_TX_QUEUE_VACANCY:
case NORM_TX_QUEUE_EMPTY: case NORM_TX_QUEUE_EMPTY:
if (!norm_tx_ready) if (!norm_tx_ready) {
{
norm_tx_ready = true; norm_tx_ready = true;
send_data(); send_data ();
} }
break; break;
case NORM_RX_OBJECT_NEW: case NORM_RX_OBJECT_NEW:
//break; //break;
case NORM_RX_OBJECT_UPDATED: case NORM_RX_OBJECT_UPDATED:
recv_data(event.object); recv_data (event.object);
break; break;
case NORM_RX_OBJECT_ABORTED: case NORM_RX_OBJECT_ABORTED: {
{ NormRxStreamState *rxState =
NormRxStreamState* rxState = (NormRxStreamState*)NormObjectGetUserData(event.object); (NormRxStreamState *) NormObjectGetUserData (event.object);
if (NULL != rxState) if (NULL != rxState) {
{
// Remove the state from the list it's in // Remove the state from the list it's in
// This is now unnecessary since deletion takes care of list removal // This is now unnecessary since deletion takes care of list removal
// but in the interest of being clear ... // but in the interest of being clear ...
NormRxStreamState::List* list = rxState->AccessList(); NormRxStreamState::List *list = rxState->AccessList ();
if (NULL != list) list->Remove(*rxState); if (NULL != list)
list->Remove (*rxState);
} }
delete rxState; delete rxState;
break; break;
@@ -382,278 +373,262 @@ void zmq::norm_engine_t::in_event()
// user configurable timeout here to wait some amount of time // user configurable timeout here to wait some amount of time
// after this event to declare the remote sender truly dead // after this event to declare the remote sender truly dead
// and delete its state??? // and delete its state???
NormNodeDelete(event.sender); NormNodeDelete (event.sender);
break; break;
default: default:
// We ignore some NORM events // We ignore some NORM events
break; break;
} }
} // zmq::norm_engine_t::in_event() } // zmq::norm_engine_t::in_event()
void zmq::norm_engine_t::restart_input() void zmq::norm_engine_t::restart_input ()
{ {
// TBD - should we check/assert that zmq_input_ready was false??? // TBD - should we check/assert that zmq_input_ready was false???
zmq_input_ready = true; zmq_input_ready = true;
// Process any pending received messages // Process any pending received messages
if (!msg_ready_list.IsEmpty()) if (!msg_ready_list.IsEmpty ())
recv_data(NORM_OBJECT_INVALID); recv_data (NORM_OBJECT_INVALID);
} // end zmq::norm_engine_t::restart_input() } // end zmq::norm_engine_t::restart_input()
void zmq::norm_engine_t::recv_data(NormObjectHandle object) void zmq::norm_engine_t::recv_data (NormObjectHandle object)
{ {
if (NORM_OBJECT_INVALID != object) if (NORM_OBJECT_INVALID != object) {
{
// Call result of NORM_RX_OBJECT_UPDATED notification // Call result of NORM_RX_OBJECT_UPDATED notification
// This is a rx_ready indication for a new or existing rx stream // This is a rx_ready indication for a new or existing rx stream
// First, determine if this is a stream we already know // First, determine if this is a stream we already know
zmq_assert(NORM_OBJECT_STREAM == NormObjectGetType(object)); zmq_assert (NORM_OBJECT_STREAM == NormObjectGetType (object));
// Since there can be multiple senders (publishers), we keep // Since there can be multiple senders (publishers), we keep
// state for each separate rx stream. // state for each separate rx stream.
NormRxStreamState* rxState = (NormRxStreamState*)NormObjectGetUserData(object); NormRxStreamState *rxState =
if (NULL == rxState) (NormRxStreamState *) NormObjectGetUserData (object);
{ if (NULL == rxState) {
// This is a new stream, so create rxState with zmq decoder, etc // This is a new stream, so create rxState with zmq decoder, etc
rxState = new (std::nothrow) NormRxStreamState(object, options.maxmsgsize); rxState =
errno_assert(rxState); new (std::nothrow) NormRxStreamState (object, options.maxmsgsize);
errno_assert (rxState);
if (!rxState->Init()) if (!rxState->Init ()) {
{ errno_assert (false);
errno_assert(false);
delete rxState; delete rxState;
return; return;
} }
NormObjectSetUserData(object, rxState); NormObjectSetUserData (object, rxState);
} } else if (!rxState->IsRxReady ()) {
else if (!rxState->IsRxReady())
{
// Existing non-ready stream, so remove from pending // Existing non-ready stream, so remove from pending
// list to be promoted to rx_ready_list ... // list to be promoted to rx_ready_list ...
rx_pending_list.Remove(*rxState); rx_pending_list.Remove (*rxState);
} }
if (!rxState->IsRxReady()) if (!rxState->IsRxReady ()) {
{
// TBD - prepend up front for immediate service? // TBD - prepend up front for immediate service?
rxState->SetRxReady(true); rxState->SetRxReady (true);
rx_ready_list.Append(*rxState); rx_ready_list.Append (*rxState);
} }
} }
// This loop repeats until we've read all data available from "rx ready" inbound streams // This loop repeats until we've read all data available from "rx ready" inbound streams
// and pushed any accumulated messages we can up to the zmq session. // and pushed any accumulated messages we can up to the zmq session.
while (!rx_ready_list.IsEmpty() || (zmq_input_ready && !msg_ready_list.IsEmpty())) while (!rx_ready_list.IsEmpty ()
{ || (zmq_input_ready && !msg_ready_list.IsEmpty ())) {
// Iterate through our rx_ready streams, reading data into the decoder // Iterate through our rx_ready streams, reading data into the decoder
// (This services incoming "rx ready" streams in a round-robin fashion) // (This services incoming "rx ready" streams in a round-robin fashion)
NormRxStreamState::List::Iterator iterator(rx_ready_list); NormRxStreamState::List::Iterator iterator (rx_ready_list);
NormRxStreamState* rxState; NormRxStreamState *rxState;
while (NULL != (rxState = iterator.GetNextItem())) while (NULL != (rxState = iterator.GetNextItem ())) {
{ switch (rxState->Decode ()) {
switch(rxState->Decode()) case 1: // msg completed
{
case 1: // msg completed
// Complete message decoded, move this stream to msg_ready_list // Complete message decoded, move this stream to msg_ready_list
// to push the message up to the session below. Note the stream // to push the message up to the session below. Note the stream
// will be returned to the "rx_ready_list" after that's done // will be returned to the "rx_ready_list" after that's done
rx_ready_list.Remove(*rxState); rx_ready_list.Remove (*rxState);
msg_ready_list.Append(*rxState); msg_ready_list.Append (*rxState);
continue; continue;
case -1: // decoding error (shouldn't happen w/ NORM, but ...) case -1: // decoding error (shouldn't happen w/ NORM, but ...)
// We need to re-sync this stream (decoder buffer was reset) // We need to re-sync this stream (decoder buffer was reset)
rxState->SetSync(false); rxState->SetSync (false);
break; break;
default: // 0 - need more data default: // 0 - need more data
break; break;
} }
// Get more data from this stream // Get more data from this stream
NormObjectHandle stream = rxState->GetStreamHandle(); NormObjectHandle stream = rxState->GetStreamHandle ();
// First, make sure we're in sync ... // First, make sure we're in sync ...
while (!rxState->InSync()) while (!rxState->InSync ()) {
{
// seek NORM message start // seek NORM message start
if (!NormStreamSeekMsgStart(stream)) if (!NormStreamSeekMsgStart (stream)) {
{
// Need to wait for more data // Need to wait for more data
break; break;
} }
// read message 'flag' byte to see if this it's a 'final' frame // read message 'flag' byte to see if this it's a 'final' frame
char syncFlag; char syncFlag;
unsigned int numBytes = 1; unsigned int numBytes = 1;
if (!NormStreamRead(stream, &syncFlag, &numBytes)) if (!NormStreamRead (stream, &syncFlag, &numBytes)) {
{
// broken stream (shouldn't happen after seek msg start?) // broken stream (shouldn't happen after seek msg start?)
zmq_assert(false); zmq_assert (false);
continue; continue;
} }
if (0 == numBytes) if (0 == numBytes) {
{
// This probably shouldn't happen either since we found msg start // This probably shouldn't happen either since we found msg start
// Need to wait for more data // Need to wait for more data
break; break;
} }
if (0 == syncFlag) rxState->SetSync(true); if (0 == syncFlag)
rxState->SetSync (true);
// else keep seeking ... // else keep seeking ...
} // end while(!rxState->InSync()) } // end while(!rxState->InSync())
if (!rxState->InSync()) if (!rxState->InSync ()) {
{
// Need more data for this stream, so remove from "rx ready" // Need more data for this stream, so remove from "rx ready"
// list and iterate to next "rx ready" stream // list and iterate to next "rx ready" stream
rxState->SetRxReady(false); rxState->SetRxReady (false);
// Move from rx_ready_list to rx_pending_list // Move from rx_ready_list to rx_pending_list
rx_ready_list.Remove(*rxState); rx_ready_list.Remove (*rxState);
rx_pending_list.Append(*rxState); rx_pending_list.Append (*rxState);
continue; continue;
} }
// Now we're actually ready to read data from the NORM stream to the zmq_decoder // Now we're actually ready to read data from the NORM stream to the zmq_decoder
// the underlying zmq_decoder->get_buffer() call sets how much is needed. // the underlying zmq_decoder->get_buffer() call sets how much is needed.
unsigned int numBytes = rxState->GetBytesNeeded(); unsigned int numBytes = rxState->GetBytesNeeded ();
if (!NormStreamRead(stream, rxState->AccessBuffer(), &numBytes)) if (!NormStreamRead (stream, rxState->AccessBuffer (), &numBytes)) {
{
// broken NORM stream, so re-sync // broken NORM stream, so re-sync
rxState->Init(); // TBD - check result rxState->Init (); // TBD - check result
// This will retry syncing, and getting data from this stream // This will retry syncing, and getting data from this stream
// since we don't increment the "it" iterator // since we don't increment the "it" iterator
continue; continue;
} }
rxState->IncrementBufferCount(numBytes); rxState->IncrementBufferCount (numBytes);
if (0 == numBytes) if (0 == numBytes) {
{
// All the data available has been read // All the data available has been read
// Need to wait for NORM_RX_OBJECT_UPDATED for this stream // Need to wait for NORM_RX_OBJECT_UPDATED for this stream
rxState->SetRxReady(false); rxState->SetRxReady (false);
// Move from rx_ready_list to rx_pending_list // Move from rx_ready_list to rx_pending_list
rx_ready_list.Remove(*rxState); rx_ready_list.Remove (*rxState);
rx_pending_list.Append(*rxState); rx_pending_list.Append (*rxState);
} }
} // end while(NULL != (rxState = iterator.GetNextItem())) } // end while(NULL != (rxState = iterator.GetNextItem()))
if (zmq_input_ready) if (zmq_input_ready) {
{
// At this point, we've made a pass through the "rx_ready" stream list // At this point, we've made a pass through the "rx_ready" stream list
// Now make a pass through the "msg_pending" list (if the zmq session // Now make a pass through the "msg_pending" list (if the zmq session
// ready for more input). This may possibly return streams back to // ready for more input). This may possibly return streams back to
// the "rx ready" stream list after their pending message is handled // the "rx ready" stream list after their pending message is handled
NormRxStreamState::List::Iterator iterator(msg_ready_list); NormRxStreamState::List::Iterator iterator (msg_ready_list);
NormRxStreamState* rxState; NormRxStreamState *rxState;
while (NULL != (rxState = iterator.GetNextItem())) while (NULL != (rxState = iterator.GetNextItem ())) {
{ msg_t *msg = rxState->AccessMsg ();
msg_t* msg = rxState->AccessMsg(); int rc = zmq_session->push_msg (msg);
int rc = zmq_session->push_msg(msg); if (-1 == rc) {
if (-1 == rc) if (EAGAIN == errno) {
{
if (EAGAIN == errno)
{
// need to wait until session calls "restart_input()" // need to wait until session calls "restart_input()"
zmq_input_ready = false; zmq_input_ready = false;
break; break;
} } else {
else
{
// session rejected message? // session rejected message?
// TBD - handle this better // TBD - handle this better
zmq_assert(false); zmq_assert (false);
} }
} }
// else message was accepted. // else message was accepted.
msg_ready_list.Remove(*rxState); msg_ready_list.Remove (*rxState);
if (rxState->IsRxReady()) // Move back to "rx_ready" list to read more data if (
rx_ready_list.Append(*rxState); rxState
else // Move back to "rx_pending" list until NORM_RX_OBJECT_UPDATED ->IsRxReady ()) // Move back to "rx_ready" list to read more data
msg_ready_list.Append(*rxState); rx_ready_list.Append (*rxState);
} // end while(NULL != (rxState = iterator.GetNextItem())) else // Move back to "rx_pending" list until NORM_RX_OBJECT_UPDATED
} // end if (zmq_input_ready) msg_ready_list.Append (*rxState);
} // end while ((!rx_ready_list.empty() || (zmq_input_ready && !msg_ready_list.empty())) } // end while(NULL != (rxState = iterator.GetNextItem()))
} // end if (zmq_input_ready)
} // end while ((!rx_ready_list.empty() || (zmq_input_ready && !msg_ready_list.empty()))
// Alert zmq of the messages we have pushed up // Alert zmq of the messages we have pushed up
zmq_session->flush(); zmq_session->flush ();
} // end zmq::norm_engine_t::recv_data() } // end zmq::norm_engine_t::recv_data()
zmq::norm_engine_t::NormRxStreamState::NormRxStreamState(NormObjectHandle normStream, zmq::norm_engine_t::NormRxStreamState::NormRxStreamState (
int64_t maxMsgSize) NormObjectHandle normStream, int64_t maxMsgSize) :
: norm_stream(normStream), max_msg_size(maxMsgSize), norm_stream (normStream),
in_sync(false), rx_ready(false), zmq_decoder(NULL), skip_norm_sync(false), max_msg_size (maxMsgSize),
buffer_ptr(NULL), buffer_size(0), buffer_count(0), in_sync (false),
prev(NULL), next(NULL), list(NULL) rx_ready (false),
zmq_decoder (NULL),
skip_norm_sync (false),
buffer_ptr (NULL),
buffer_size (0),
buffer_count (0),
prev (NULL),
next (NULL),
list (NULL)
{ {
} }
zmq::norm_engine_t::NormRxStreamState::~NormRxStreamState() zmq::norm_engine_t::NormRxStreamState::~NormRxStreamState ()
{ {
if (NULL != zmq_decoder) if (NULL != zmq_decoder) {
{
delete zmq_decoder; delete zmq_decoder;
zmq_decoder = NULL; zmq_decoder = NULL;
} }
if (NULL != list) if (NULL != list) {
{ list->Remove (*this);
list->Remove(*this);
list = NULL; list = NULL;
} }
} }
bool zmq::norm_engine_t::NormRxStreamState::Init() bool zmq::norm_engine_t::NormRxStreamState::Init ()
{ {
in_sync = false; in_sync = false;
skip_norm_sync = false; skip_norm_sync = false;
if (NULL != zmq_decoder) delete zmq_decoder; if (NULL != zmq_decoder)
delete zmq_decoder;
// Note "in_batch_size" comes from config.h // Note "in_batch_size" comes from config.h
zmq_decoder = new (std::nothrow) v2_decoder_t (in_batch_size, max_msg_size); zmq_decoder = new (std::nothrow) v2_decoder_t (in_batch_size, max_msg_size);
alloc_assert (zmq_decoder); alloc_assert (zmq_decoder);
if (NULL != zmq_decoder) if (NULL != zmq_decoder) {
{
buffer_count = 0; buffer_count = 0;
buffer_size = 0; buffer_size = 0;
zmq_decoder->get_buffer(&buffer_ptr, &buffer_size); zmq_decoder->get_buffer (&buffer_ptr, &buffer_size);
return true; return true;
} } else {
else
{
return false; return false;
} }
} // end zmq::norm_engine_t::NormRxStreamState::Init() } // end zmq::norm_engine_t::NormRxStreamState::Init()
// This decodes any pending data sitting in our stream decoder buffer // This decodes any pending data sitting in our stream decoder buffer
// It returns 1 upon message completion, -1 on error, 1 on msg completion // It returns 1 upon message completion, -1 on error, 1 on msg completion
int zmq::norm_engine_t::NormRxStreamState::Decode() int zmq::norm_engine_t::NormRxStreamState::Decode ()
{ {
// If we have pending bytes to decode, process those first // If we have pending bytes to decode, process those first
while (buffer_count > 0) while (buffer_count > 0) {
{
// There's pending data for the decoder to decode // There's pending data for the decoder to decode
size_t processed = 0; size_t processed = 0;
// This a bit of a kludgy approach used to weed // This a bit of a kludgy approach used to weed
// out the NORM ZMQ message transport "syncFlag" byte // out the NORM ZMQ message transport "syncFlag" byte
// from the ZMQ message stream being decoded (but it works!) // from the ZMQ message stream being decoded (but it works!)
if (skip_norm_sync) if (skip_norm_sync) {
{
buffer_ptr++; buffer_ptr++;
buffer_count--; buffer_count--;
skip_norm_sync = false; skip_norm_sync = false;
} }
int rc = zmq_decoder->decode(buffer_ptr, buffer_count, processed); int rc = zmq_decoder->decode (buffer_ptr, buffer_count, processed);
buffer_ptr += processed; buffer_ptr += processed;
buffer_count -= processed; buffer_count -= processed;
switch (rc) switch (rc) {
{
case 1: case 1:
// msg completed // msg completed
if (0 == buffer_count) if (0 == buffer_count) {
{
buffer_size = 0; buffer_size = 0;
zmq_decoder->get_buffer(&buffer_ptr, &buffer_size); zmq_decoder->get_buffer (&buffer_ptr, &buffer_size);
} }
skip_norm_sync = true; skip_norm_sync = true;
return 1; return 1;
case -1: case -1:
// decoder error (reset decoder and state variables) // decoder error (reset decoder and state variables)
in_sync = false; in_sync = false;
skip_norm_sync = false; // will get consumed by norm sync check skip_norm_sync = false; // will get consumed by norm sync check
Init(); Init ();
break; break;
case 0: case 0:
@@ -664,33 +639,32 @@ int zmq::norm_engine_t::NormRxStreamState::Decode()
// Reset buffer pointer/count for next read // Reset buffer pointer/count for next read
buffer_count = 0; buffer_count = 0;
buffer_size = 0; buffer_size = 0;
zmq_decoder->get_buffer(&buffer_ptr, &buffer_size); zmq_decoder->get_buffer (&buffer_ptr, &buffer_size);
return 0; // need more data return 0; // need more data
} // end zmq::norm_engine_t::NormRxStreamState::Decode() } // end zmq::norm_engine_t::NormRxStreamState::Decode()
zmq::norm_engine_t::NormRxStreamState::List::List() zmq::norm_engine_t::NormRxStreamState::List::List () : head (NULL), tail (NULL)
: head(NULL), tail(NULL)
{ {
} }
zmq::norm_engine_t::NormRxStreamState::List::~List() zmq::norm_engine_t::NormRxStreamState::List::~List ()
{ {
Destroy(); Destroy ();
} }
void zmq::norm_engine_t::NormRxStreamState::List::Destroy() void zmq::norm_engine_t::NormRxStreamState::List::Destroy ()
{ {
NormRxStreamState* item = head; NormRxStreamState *item = head;
while (NULL != item) while (NULL != item) {
{ Remove (*item);
Remove(*item);
delete item; delete item;
item = head; item = head;
} }
} // end zmq::norm_engine_t::NormRxStreamState::List::Destroy() } // end zmq::norm_engine_t::NormRxStreamState::List::Destroy()
void zmq::norm_engine_t::NormRxStreamState::List::Append(NormRxStreamState& item) void zmq::norm_engine_t::NormRxStreamState::List::Append (
NormRxStreamState &item)
{ {
item.prev = tail; item.prev = tail;
if (NULL != tail) if (NULL != tail)
@@ -700,33 +674,37 @@ void zmq::norm_engine_t::NormRxStreamState::List::Append(NormRxStreamState& item
item.next = NULL; item.next = NULL;
tail = &item; tail = &item;
item.list = this; item.list = this;
} // end zmq::norm_engine_t::NormRxStreamState::List::Append() } // end zmq::norm_engine_t::NormRxStreamState::List::Append()
void zmq::norm_engine_t::NormRxStreamState::List::Remove(NormRxStreamState& item) void zmq::norm_engine_t::NormRxStreamState::List::Remove (
NormRxStreamState &item)
{ {
if (NULL != item.prev) if (NULL != item.prev)
item.prev->next = item.next; item.prev->next = item.next;
else else
head = item.next; head = item.next;
if (NULL != item.next) if (NULL != item.next)
item.next ->prev = item.prev; item.next->prev = item.prev;
else else
tail = item.prev; tail = item.prev;
item.prev = item.next = NULL; item.prev = item.next = NULL;
item.list = NULL; item.list = NULL;
} // end zmq::norm_engine_t::NormRxStreamState::List::Remove() } // end zmq::norm_engine_t::NormRxStreamState::List::Remove()
zmq::norm_engine_t::NormRxStreamState::List::Iterator::Iterator(const List& list) zmq::norm_engine_t::NormRxStreamState::List::Iterator::Iterator (
: next_item(list.head) const List &list) :
next_item (list.head)
{ {
} }
zmq::norm_engine_t::NormRxStreamState* zmq::norm_engine_t::NormRxStreamState::List::Iterator::GetNextItem() zmq::norm_engine_t::NormRxStreamState *
zmq::norm_engine_t::NormRxStreamState::List::Iterator::GetNextItem ()
{ {
NormRxStreamState* nextItem = next_item; NormRxStreamState *nextItem = next_item;
if (NULL != nextItem) next_item = nextItem->next; if (NULL != nextItem)
next_item = nextItem->next;
return nextItem; return nextItem;
} // end zmq::norm_engine_t::NormRxStreamState::List::Iterator::GetNextItem() } // end zmq::norm_engine_t::NormRxStreamState::List::Iterator::GetNextItem()
const char *zmq::norm_engine_t::get_endpoint () const const char *zmq::norm_engine_t::get_endpoint () const
{ {

View File

@@ -14,175 +14,170 @@
namespace zmq namespace zmq
{ {
class io_thread_t; class io_thread_t;
class session_base_t; class session_base_t;
class norm_engine_t : public io_object_t, public i_engine class norm_engine_t : public io_object_t, public i_engine
{
public:
norm_engine_t (zmq::io_thread_t *parent_, const options_t &options_);
~norm_engine_t ();
// create NORM instance, session, etc
int init (const char *network_, bool send, bool recv);
void shutdown ();
// i_engine interface implementation.
// Plug the engine to the session.
virtual void plug (zmq::io_thread_t *io_thread_,
class session_base_t *session_);
// Terminate and deallocate the engine. Note that 'detached'
// events are not fired on termination.
virtual void terminate ();
// This method is called by the session to signalise that more
// messages can be written to the pipe.
virtual void restart_input ();
// This method is called by the session to signalise that there
// are messages to send available.
virtual void restart_output ();
virtual void zap_msg_available (){};
virtual const char *get_endpoint () const;
// i_poll_events interface implementation.
// (we only need in_event() for NormEvent notification)
// (i.e., don't have any output events or timers (yet))
void in_event ();
private:
void unplug ();
void send_data ();
void recv_data (NormObjectHandle stream);
enum
{ {
public: BUFFER_SIZE = 2048
norm_engine_t (zmq::io_thread_t *parent_, const options_t &options_); };
~norm_engine_t ();
// create NORM instance, session, etc // Used to keep track of streams from multiple senders
int init(const char* network_, bool send, bool recv); class NormRxStreamState
void shutdown(); {
public:
NormRxStreamState (NormObjectHandle normStream, int64_t maxMsgSize);
~NormRxStreamState ();
// i_engine interface implementation. NormObjectHandle GetStreamHandle () const { return norm_stream; }
// Plug the engine to the session.
virtual void plug (zmq::io_thread_t *io_thread_,
class session_base_t *session_);
// Terminate and deallocate the engine. Note that 'detached' bool Init ();
// events are not fired on termination.
virtual void terminate ();
// This method is called by the session to signalise that more void SetRxReady (bool state) { rx_ready = state; }
// messages can be written to the pipe. bool IsRxReady () const { return rx_ready; }
virtual void restart_input ();
// This method is called by the session to signalise that there void SetSync (bool state) { in_sync = state; }
// are messages to send available. bool InSync () const { return in_sync; }
virtual void restart_output ();
virtual void zap_msg_available () {}; // These are used to feed data to decoder
// and its underlying "msg" buffer
char *AccessBuffer () { return (char *) (buffer_ptr + buffer_count); }
size_t GetBytesNeeded () const { return (buffer_size - buffer_count); }
void IncrementBufferCount (size_t count) { buffer_count += count; }
msg_t *AccessMsg () { return zmq_decoder->msg (); }
// This invokes the decoder "decode" method
// returning 0 if more data is needed,
// 1 if the message is complete, If an error
// occurs the 'sync' is dropped and the
// decoder re-initialized
int Decode ();
virtual const char *get_endpoint () const; class List
{
public:
List ();
~List ();
// i_poll_events interface implementation. void Append (NormRxStreamState &item);
// (we only need in_event() for NormEvent notification) void Remove (NormRxStreamState &item);
// (i.e., don't have any output events or timers (yet))
void in_event ();
private: bool IsEmpty () const { return (NULL == head); }
void unplug();
void send_data();
void recv_data(NormObjectHandle stream);
void Destroy ();
enum {BUFFER_SIZE = 2048}; class Iterator
// Used to keep track of streams from multiple senders
class NormRxStreamState
{ {
public: public:
NormRxStreamState(NormObjectHandle normStream, Iterator (const List &list);
int64_t maxMsgSize); NormRxStreamState *GetNextItem ();
~NormRxStreamState();
NormObjectHandle GetStreamHandle() const private:
{return norm_stream;} NormRxStreamState *next_item;
};
friend class Iterator;
bool Init(); private:
NormRxStreamState *head;
NormRxStreamState *tail;
void SetRxReady(bool state) }; // end class zmq::norm_engine_t::NormRxStreamState::List
{rx_ready = state;}
bool IsRxReady() const
{return rx_ready;}
void SetSync(bool state) friend class List;
{in_sync = state;}
bool InSync() const
{return in_sync;}
// These are used to feed data to decoder List *AccessList () { return list; }
// and its underlying "msg" buffer
char* AccessBuffer()
{return (char*)(buffer_ptr + buffer_count);}
size_t GetBytesNeeded() const
{return (buffer_size - buffer_count);}
void IncrementBufferCount(size_t count)
{buffer_count += count;}
msg_t* AccessMsg()
{return zmq_decoder->msg();}
// This invokes the decoder "decode" method
// returning 0 if more data is needed,
// 1 if the message is complete, If an error
// occurs the 'sync' is dropped and the
// decoder re-initialized
int Decode();
class List
{
public:
List();
~List();
void Append(NormRxStreamState& item);
void Remove(NormRxStreamState& item);
bool IsEmpty() const
{return (NULL == head);}
void Destroy();
class Iterator
{
public:
Iterator(const List& list);
NormRxStreamState* GetNextItem();
private:
NormRxStreamState* next_item;
};
friend class Iterator;
private:
NormRxStreamState* head;
NormRxStreamState* tail;
}; // end class zmq::norm_engine_t::NormRxStreamState::List
friend class List;
List* AccessList()
{return list;}
private: private:
NormObjectHandle norm_stream; NormObjectHandle norm_stream;
int64_t max_msg_size; int64_t max_msg_size;
bool in_sync; bool in_sync;
bool rx_ready; bool rx_ready;
v2_decoder_t* zmq_decoder; v2_decoder_t *zmq_decoder;
bool skip_norm_sync; bool skip_norm_sync;
unsigned char* buffer_ptr; unsigned char *buffer_ptr;
size_t buffer_size; size_t buffer_size;
size_t buffer_count; size_t buffer_count;
NormRxStreamState* prev; NormRxStreamState *prev;
NormRxStreamState* next; NormRxStreamState *next;
NormRxStreamState::List* list; NormRxStreamState::List *list;
}; // end class zmq::norm_engine_t::NormRxStreamState }; // end class zmq::norm_engine_t::NormRxStreamState
session_base_t* zmq_session; session_base_t *zmq_session;
options_t options; options_t options;
NormInstanceHandle norm_instance; NormInstanceHandle norm_instance;
handle_t norm_descriptor_handle; handle_t norm_descriptor_handle;
NormSessionHandle norm_session; NormSessionHandle norm_session;
bool is_sender; bool is_sender;
bool is_receiver; bool is_receiver;
// Sender state // Sender state
msg_t tx_msg; msg_t tx_msg;
v2_encoder_t zmq_encoder; // for tx messages (we use v2 for now) v2_encoder_t zmq_encoder; // for tx messages (we use v2 for now)
NormObjectHandle norm_tx_stream; NormObjectHandle norm_tx_stream;
bool tx_first_msg; bool tx_first_msg;
bool tx_more_bit; bool tx_more_bit;
bool zmq_output_ready; // zmq has msg(s) to send bool zmq_output_ready; // zmq has msg(s) to send
bool norm_tx_ready; // norm has tx queue vacancy bool norm_tx_ready; // norm has tx queue vacancy
// TBD - maybe don't need buffer if can access zmq message buffer directly? // TBD - maybe don't need buffer if can access zmq message buffer directly?
char tx_buffer[BUFFER_SIZE]; char tx_buffer[BUFFER_SIZE];
unsigned int tx_index; unsigned int tx_index;
unsigned int tx_len; unsigned int tx_len;
// Receiver state // Receiver state
// Lists of norm rx streams from remote senders // Lists of norm rx streams from remote senders
bool zmq_input_ready; // zmq ready to receive msg(s) bool zmq_input_ready; // zmq ready to receive msg(s)
NormRxStreamState::List rx_pending_list; // rx streams waiting for data reception NormRxStreamState::List
NormRxStreamState::List rx_ready_list; // rx streams ready for NormStreamRead() rx_pending_list; // rx streams waiting for data reception
NormRxStreamState::List msg_ready_list; // rx streams w/ msg ready for push to zmq NormRxStreamState::List
rx_ready_list; // rx streams ready for NormStreamRead()
NormRxStreamState::List
msg_ready_list; // rx streams w/ msg ready for push to zmq
}; // end class norm_engine_t }; // end class norm_engine_t
} }
#endif // ZMQ_HAVE_NORM #endif // ZMQ_HAVE_NORM

View File

@@ -64,17 +64,15 @@ int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_)
return -1; return -1;
} }
if (zap_required() && !zap_reply_received) { if (zap_required () && !zap_reply_received) {
if (zap_request_sent) { if (zap_request_sent) {
errno = EAGAIN; errno = EAGAIN;
return -1; return -1;
} }
int rc = session->zap_connect(); int rc = session->zap_connect ();
if (rc == -1) if (rc == -1) {
{ session->get_socket ()->event_handshake_failed_no_detail (
session->get_socket()->event_handshake_failed_no_detail ( session->get_endpoint (), EFAULT);
session->get_endpoint(),
EFAULT);
return -1; return -1;
} }
send_zap_request (); send_zap_request ();
@@ -120,26 +118,23 @@ int zmq::null_mechanism_t::process_handshake_command (msg_t *msg_)
{ {
if (ready_command_received || error_command_received) { if (ready_command_received || error_command_received) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
const unsigned char *cmd_data = const unsigned char *cmd_data =
static_cast <unsigned char *> (msg_->data ()); static_cast<unsigned char *> (msg_->data ());
const size_t data_size = msg_->size (); const size_t data_size = msg_->size ();
int rc = 0; int rc = 0;
if (data_size >= 6 && !memcmp (cmd_data, "\5READY", 6)) if (data_size >= 6 && !memcmp (cmd_data, "\5READY", 6))
rc = process_ready_command (cmd_data, data_size); rc = process_ready_command (cmd_data, data_size);
else else if (data_size >= 6 && !memcmp (cmd_data, "\5ERROR", 6))
if (data_size >= 6 && !memcmp (cmd_data, "\5ERROR", 6))
rc = process_error_command (cmd_data, data_size); rc = process_error_command (cmd_data, data_size);
else { else {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
ZMQ_PROTOCOL_ERROR_ZMTP_UNEXPECTED_COMMAND);
errno = EPROTO; errno = EPROTO;
rc = -1; rc = -1;
} }
@@ -153,15 +148,15 @@ int zmq::null_mechanism_t::process_handshake_command (msg_t *msg_)
return rc; return rc;
} }
int zmq::null_mechanism_t::process_ready_command ( int zmq::null_mechanism_t::process_ready_command (const unsigned char *cmd_data,
const unsigned char *cmd_data, size_t data_size) size_t data_size)
{ {
ready_command_received = true; ready_command_received = true;
return parse_metadata (cmd_data + 6, data_size - 6); return parse_metadata (cmd_data + 6, data_size - 6);
} }
int zmq::null_mechanism_t::process_error_command ( int zmq::null_mechanism_t::process_error_command (const unsigned char *cmd_data,
const unsigned char *cmd_data, size_t data_size) size_t data_size)
{ {
if (data_size < 7) { if (data_size < 7) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
@@ -171,7 +166,7 @@ int zmq::null_mechanism_t::process_error_command (
errno = EPROTO; errno = EPROTO;
return -1; return -1;
} }
const size_t error_reason_len = static_cast <size_t> (cmd_data [6]); const size_t error_reason_len = static_cast<size_t> (cmd_data[6]);
if (error_reason_len > data_size - 7) { if (error_reason_len > data_size - 7) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), session->get_endpoint (),
@@ -200,15 +195,13 @@ int zmq::null_mechanism_t::zap_msg_available ()
zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const
{ {
const bool command_sent = const bool command_sent = ready_command_sent || error_command_sent;
ready_command_sent || error_command_sent;
const bool command_received = const bool command_received =
ready_command_received || error_command_received; ready_command_received || error_command_received;
if (ready_command_sent && ready_command_received) if (ready_command_sent && ready_command_received)
return mechanism_t::ready; return mechanism_t::ready;
else else if (command_sent && command_received)
if (command_sent && command_received)
return error; return error;
else else
return handshaking; return handshaking;

View File

@@ -36,42 +36,36 @@
namespace zmq namespace zmq
{ {
class msg_t;
class session_base_t;
class msg_t; class null_mechanism_t : public zap_client_t
class session_base_t; {
public:
null_mechanism_t (session_base_t *session_,
const std::string &peer_address,
const options_t &options_);
virtual ~null_mechanism_t ();
class null_mechanism_t : public zap_client_t // mechanism implementation
{ virtual int next_handshake_command (msg_t *msg_);
public: virtual int process_handshake_command (msg_t *msg_);
virtual int zap_msg_available ();
virtual status_t status () const;
null_mechanism_t (session_base_t *session_, private:
const std::string &peer_address, bool ready_command_sent;
const options_t &options_); bool error_command_sent;
virtual ~null_mechanism_t (); bool ready_command_received;
bool error_command_received;
bool zap_request_sent;
bool zap_reply_received;
// mechanism implementation int process_ready_command (const unsigned char *cmd_data, size_t data_size);
virtual int next_handshake_command (msg_t *msg_); int process_error_command (const unsigned char *cmd_data, size_t data_size);
virtual int process_handshake_command (msg_t *msg_);
virtual int zap_msg_available ();
virtual status_t status () const;
private:
bool ready_command_sent;
bool error_command_sent;
bool ready_command_received;
bool error_command_received;
bool zap_request_sent;
bool zap_reply_received;
int process_ready_command (
const unsigned char *cmd_data, size_t data_size);
int process_error_command (
const unsigned char *cmd_data, size_t data_size);
void send_zap_request ();
};
void send_zap_request ();
};
} }
#endif #endif

Some files were not shown because too many files have changed in this diff Show More