void * zctx__socket_new (zctx_t *self, int type) { // Initialize context now if necessary assert (self); zmutex_lock (self->mutex); if (!self->context) self->context = zmq_init (self->iothreads); zmutex_unlock (self->mutex); if (!self->context) return NULL; // Create and register socket void *zocket = zmq_socket (self->context, type); if (!zocket) return NULL; #if (ZMQ_VERSION_MAJOR == 2) // For ZeroMQ/2.x we use sndhwm for both send and receive zsocket_set_hwm (zocket, self->sndhwm); #else // For later versions we use separate SNDHWM and RCVHWM zsocket_set_sndhwm (zocket, self->sndhwm); zsocket_set_rcvhwm (zocket, self->rcvhwm); #endif zmutex_lock (self->mutex); if (zlist_push (self->sockets, zocket)) { zmutex_unlock (self->mutex); zmq_close (zocket); return NULL; } zmutex_unlock (self->mutex); return zocket; }
void zre_peer_connect (zre_peer_t *self, char *reply_to, char *endpoint) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsocket_new (self->ctx, ZMQ_DEALER); // Set our caller 'From' identity so that receiving node knows // who each message came from. zsocket_set_identity (self->mailbox, reply_to); // Set a high-water mark that allows for reasonable activity zsocket_set_sndhwm (self->mailbox, PEER_EXPIRED * 100); // Send messages immediately or return EAGAIN zsocket_set_sndtimeo (self->mailbox, 0); // Connect through to peer node zsocket_connect (self->mailbox, "tcp://%s", endpoint); self->endpoint = strdup (endpoint); self->connected = true; self->ready = false; }
/** * Connect the given socket to the given binding * @param socket * An existing socket * @param binding * A string binding * @return * If connection was successful */ bool BoomStick::ConnectToBinding(void* socket, const std::string& binding) { if (nullptr == socket) { return false; } zsocket_set_sndhwm(socket, mSendHWM); zsocket_set_rcvhwm(socket, mRecvHWM); return (zsocket_connect(socket, binding.c_str()) >= 0); }
static rsRetVal initZMQ(instanceData* pData) { DEFiRet; /* create the context if necessary. */ if (NULL == s_context) { zsys_handler_set(NULL); s_context = zctx_new(); if (s_workerThreads > 0) zctx_set_iothreads(s_context, s_workerThreads); } pData->socket = zsocket_new(s_context, pData->type); if (NULL == pData->socket) { errmsg.LogError(0, RS_RET_NO_ERRCODE, "omzmq3: zsocket_new failed for %s: %s", pData->description, zmq_strerror(errno)); ABORT_FINALIZE(RS_RET_NO_ERRCODE); } /* use czmq defaults for these, unless set to non-default values */ if(pData->identity) zsocket_set_identity(pData->socket, (char*)pData->identity); if(pData->sndBuf > -1) zsocket_set_sndbuf(pData->socket, pData->sndBuf); if(pData->rcvBuf > -1) zsocket_set_sndbuf(pData->socket, pData->rcvBuf); if(pData->linger > -1) zsocket_set_linger(pData->socket, pData->linger); if(pData->backlog > -1) zsocket_set_backlog(pData->socket, pData->backlog); if(pData->sndTimeout > -1) zsocket_set_sndtimeo(pData->socket, pData->sndTimeout); if(pData->rcvTimeout > -1) zsocket_set_rcvtimeo(pData->socket, pData->rcvTimeout); if(pData->maxMsgSize > -1) zsocket_set_maxmsgsize(pData->socket, pData->maxMsgSize); if(pData->rate > -1) zsocket_set_rate(pData->socket, pData->rate); if(pData->recoveryIVL > -1) zsocket_set_recovery_ivl(pData->socket, pData->recoveryIVL); if(pData->multicastHops > -1) zsocket_set_multicast_hops(pData->socket, pData->multicastHops); if(pData->reconnectIVL > -1) zsocket_set_reconnect_ivl(pData->socket, pData->reconnectIVL); if(pData->reconnectIVLMax > -1) zsocket_set_reconnect_ivl_max(pData->socket, pData->reconnectIVLMax); if(pData->ipv4Only > -1) zsocket_set_ipv4only(pData->socket, pData->ipv4Only); if(pData->affinity != 1) zsocket_set_affinity(pData->socket, pData->affinity); if(pData->rcvHWM > -1) zsocket_set_rcvhwm(pData->socket, pData->rcvHWM); if(pData->sndHWM > -1) zsocket_set_sndhwm(pData->socket, pData->sndHWM); /* bind or connect to it */ if (pData->action == ACTION_BIND) { /* bind asserts, so no need to test return val here which isn't the greatest api -- oh well */ if(-1 == zsocket_bind(pData->socket, (char*)pData->description)) { errmsg.LogError(0, RS_RET_NO_ERRCODE, "omzmq3: bind failed for %s: %s", pData->description, zmq_strerror(errno)); ABORT_FINALIZE(RS_RET_NO_ERRCODE); } DBGPRINTF("omzmq3: bind to %s successful\n",pData->description); } else { if(-1 == zsocket_connect(pData->socket, (char*)pData->description)) { errmsg.LogError(0, RS_RET_NO_ERRCODE, "omzmq3: connect failed for %s: %s", pData->description, zmq_strerror(errno)); ABORT_FINALIZE(RS_RET_NO_ERRCODE); } DBGPRINTF("omzmq3: connect to %s successful", pData->description); } finalize_it: RETiRet; }
void *new_socket(zctx_t *ctx, int stype) { void *s; s = zsocket_new(ctx, stype); assert(s); zsocket_set_linger(s, 0); zsocket_set_sndhwm(s, 100000); zsocket_set_rcvhwm(s, 100000); return(s); }
/** * Setup the location to receive messages. * @param location */ void Alien::PrepareToBeShot(const std::string& location) { //Subscribe to everything char dummy = '\0'; zsocket_set_subscribe(mBody, &dummy); zsocket_set_rcvhwm(mBody, 32 * 1024); zsocket_set_sndhwm(mBody, 32 * 1024); int rc = zsocket_connect(mBody, location.c_str()); if (rc == -1) { LOG(WARNING) << "location: " << location; LOG(WARNING) << "connect socket rc == " << rc; throw std::string("Failed to connect to socket"); } }
static rsRetVal initZMQ(instanceData* pData) { DEFiRet; /* create the context if necessary. */ if (NULL == s_context) { s_context = zctx_new(); if (s_workerThreads > 0) zctx_set_iothreads(s_context, s_workerThreads); } pData->socket = zsocket_new(s_context, pData->type); /* ALWAYS set the HWM as the zmq3 default is 1000 and we default to 0 (infinity) */ zsocket_set_rcvhwm(pData->socket, pData->rcvHWM); zsocket_set_sndhwm(pData->socket, pData->sndHWM); /* use czmq defaults for these, unless set to non-default values */ if(pData->identity) zsocket_set_identity(pData->socket, (char*)pData->identity); if(pData->sndBuf > -1) zsocket_set_sndbuf(pData->socket, pData->sndBuf); if(pData->rcvBuf > -1) zsocket_set_sndbuf(pData->socket, pData->rcvBuf); if(pData->linger > -1) zsocket_set_linger(pData->socket, pData->linger); if(pData->backlog > -1) zsocket_set_backlog(pData->socket, pData->backlog); if(pData->sndTimeout > -1) zsocket_set_sndtimeo(pData->socket, pData->sndTimeout); if(pData->rcvTimeout > -1) zsocket_set_rcvtimeo(pData->socket, pData->rcvTimeout); if(pData->maxMsgSize > -1) zsocket_set_maxmsgsize(pData->socket, pData->maxMsgSize); if(pData->rate > -1) zsocket_set_rate(pData->socket, pData->rate); if(pData->recoveryIVL > -1) zsocket_set_recovery_ivl(pData->socket, pData->recoveryIVL); if(pData->multicastHops > -1) zsocket_set_multicast_hops(pData->socket, pData->multicastHops); if(pData->reconnectIVL > -1) zsocket_set_reconnect_ivl(pData->socket, pData->reconnectIVL); if(pData->reconnectIVLMax > -1) zsocket_set_reconnect_ivl_max(pData->socket, pData->reconnectIVLMax); if(pData->ipv4Only > -1) zsocket_set_ipv4only(pData->socket, pData->ipv4Only); if(pData->affinity != 1) zsocket_set_affinity(pData->socket, pData->affinity); /* bind or connect to it */ if (pData->action == ACTION_BIND) { /* bind asserts, so no need to test return val here which isn't the greatest api -- oh well */ zsocket_bind(pData->socket, (char*)pData->description); } else { if(zsocket_connect(pData->socket, (char*)pData->description) == -1) { errmsg.LogError(0, RS_RET_SUSPENDED, "omzmq3: connect failed!"); ABORT_FINALIZE(RS_RET_SUSPENDED); } } finalize_it: RETiRet; }
int main (void) { zctx_t *ctx = zctx_new (); void *mailbox = zsocket_new (ctx, ZMQ_DEALER); zsocket_set_sndhwm (mailbox, 4); zsocket_set_sndtimeo (mailbox, 0); zsocket_connect (mailbox, "tcp://localhost:9876"); int count; for (count = 0; count < 10; count++) { printf ("Sending message %d\n", count); int rc = zstr_sendf (mailbox, "message %d", count); if (rc == -1) { printf ("%s\n", strerror (errno)); break; } } zctx_destroy (&ctx); return 0; }
void * zsys_socket (int type, const char *filename, size_t line_nbr) { // First time initialization; if the application is mixing // its own threading calls with zsock, this may fail if two // threads try to create sockets at the same time. In such // apps, they MUST create a socket in the main program before // starting any threads. If the app uses zactor for its threads // then we can guarantee this to always be safe. zsys_init (); ZMUTEX_LOCK (s_mutex); void *handle = zmq_socket (s_process_ctx, type); // Configure socket with process defaults zsocket_set_linger (handle, (int) s_linger); #if (ZMQ_VERSION_MAJOR == 2) // For ZeroMQ/2.x we use sndhwm for both send and receive zsocket_set_hwm (handle, s_sndhwm); #else // For later versions we use separate SNDHWM and RCVHWM zsocket_set_sndhwm (handle, (int) s_sndhwm); zsocket_set_rcvhwm (handle, (int) s_rcvhwm); # if defined (ZMQ_IPV6) zsocket_set_ipv6 (handle, s_ipv6); # else zsocket_set_ipv4only (handle, s_ipv6 ? 0 : 1); # endif #endif // Add socket to reference tracker so we can report leaks; this is // done only when the caller passes a filename/line_nbr if (filename) { s_sockref_t *sockref = (s_sockref_t *) malloc (sizeof (s_sockref_t)); sockref->handle = handle; sockref->type = type; sockref->filename = filename; sockref->line_nbr = line_nbr; zlist_append (s_sockref_list, sockref); } s_open_sockets++; ZMUTEX_UNLOCK (s_mutex); return handle; }
void *zeromq_create_socket (zctx_t *context, const char *endpoint, int type, const char *topic, bool connect, int linger, int hwm) { void *socket = NULL; assert(context); assert(endpoint); if (endpoint[0] == 0) { errorLog("Trying to open NULL output socket !"); return NULL; } socket = zsocket_new (context, type); if (hwm != -1) { zsocket_set_sndhwm (socket, hwm); zsocket_set_rcvhwm (socket, hwm); } if (linger != -1) { zsocket_set_linger (socket, linger); } else { // Set linger to 0 as default, safety measure zsocket_set_linger (socket, 0); } // Allow for exponential backoff: up to 5 minutes and 1s minimum zsocket_set_reconnect_ivl(socket, 1 * 1000); zsocket_set_reconnect_ivl_max(socket, 5 * 60 * 1000); if (type == ZMQ_SUB) zsocket_set_subscribe (socket, (char*)(topic == NULL ? "" : topic)); if (connect) { zsocket_connect (socket, "%s", endpoint); } else { zsocket_bind (socket, "%s", endpoint); } return socket; }
mq_socket_t *zero_create_simple_router_socket(mq_socket_context_t *ctx) { mq_socket_t *s; tbx_type_malloc_clear(s, mq_socket_t, 1); s->type = MQ_SIMPLE_ROUTER; s->arg = zsocket_new((zctx_t *)ctx->arg, ZMQ_ROUTER); zsocket_set_linger(s->arg, 0); zsocket_set_sndhwm(s->arg, 100000); zsocket_set_rcvhwm(s->arg, 100000); s->destroy = zero_native_destroy; s->bind = zero_native_bind; s->connect = zero_native_connect; s->disconnect = zero_native_disconnect; s->poll_handle = zero_native_poll_handle; s->monitor = zero_native_monitor; s->send = zero_native_send; s->recv = zero_simple_router_recv; return(s); }
void zsocket_set_hwm (void *self, int hwm) { zsocket_set_sndhwm (self, hwm); zsocket_set_rcvhwm (self, hwm); }
void zsocket_set_hwm (void *zocket, int hwm) { zsocket_set_sndhwm (zocket, hwm); zsocket_set_rcvhwm (zocket, hwm); }
int main(int argc, char const * const *argv) { int socket_count = argc>1 ? atoi(argv[1]) : 1; int message_count = argc>2 ? atoi(argv[2]) : 100000; int rc; zctx_t *context = zctx_new(); assert(context); zctx_set_sndhwm(context, 1); zctx_set_linger(context, 100); // zmq 2.2 only supports up to 512 sockets assert_x(ZMQ_VERSION <= 20200 && socket_count > 512, "zeromq < 3.2 only supports up to 512 sockets"); void **sockets = zmalloc(socket_count * sizeof(void*)); int j; for (j=0; j<socket_count; j++) { void *socket = zsocket_new(context, ZMQ_PUSH); if (NULL==socket) { printf("Error occurred during %dth call to zsocket_new: %s\n", j, zmq_strerror (errno)); exit(1); } // zsocket_set_sndtimeo(socket, 10); zsocket_set_sndhwm(socket, 10); zsocket_set_linger(socket, 50); zsocket_set_reconnect_ivl(socket, 100); // 100 ms zsocket_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s // printf("hwm %d\n", zsocket_hwm(socket)); // printf("sndbuf %d\n", zsocket_sndbuf(socket)); // printf("swap %d\n", zsocket_swap(socket)); // printf("linger %d\n", zsocket_linger(socket)); // printf("sndtimeo %d\n", zsocket_sndtimeo(socket)); // printf("reconnect_ivl %d\n", zsocket_reconnect_ivl(socket)); // printf("reconnect_ivl_max %d\n", zsocket_reconnect_ivl_max(socket)); rc = zsocket_connect(socket, "tcp://localhost:12345"); assert(rc==0); sockets[j] = socket; zclock_sleep(10); // sleep 10 ms } char data[MESSAGE_BODY_SIZE]; memset(data, 'a', MESSAGE_BODY_SIZE); char *exchanges[2] = {"zmq-device-1", "zmq-device-2"}; // char *keys[2] = {"1","2"}; int i = 0, queued = 0, rejected = 0; for (i=0; i<message_count; i++) { if (zsys_interrupted) break; zmsg_t *message = zmsg_new(); zmsg_addstr(message, exchanges[i%2]); zmsg_addstrf(message, "logjam.zmq.test.%d.%d", i%2, i); zmsg_addmem(message, data, MESSAGE_BODY_SIZE); // zmsg_dump(message); rc = zmsg_send_dont_wait(&message, sockets[i%socket_count]); assert(message == NULL); if (rc == 0) { queued++; } else { rejected++; } // usleep(20); usleep(1000); } printf("queued: %8d\n", queued); printf("rejected: %8d\n", rejected); for (i=0;i<socket_count;i++) { zsocket_destroy(context, sockets[i]); } free(sockets); zctx_destroy(&context); return 0; }
void PrimeWorker::Work(zctx_t *ctx, void *pipe) { printf("PrimeWorker started.\n"); mBackend = zsocket_new(ctx, ZMQ_DEALER); void* frontend = zsocket_new(ctx, ZMQ_DEALER); void* input = zsocket_new(ctx, ZMQ_SUB); mServer = zsocket_new(ctx, ZMQ_ROUTER); mSignals = zsocket_new(ctx, ZMQ_PUB); zsocket_set_sndhwm(mBackend, 1*1000*1000); int err = 0; err = zsocket_bind(mServer, "tcp://*:%d", mServerPort); if(!err) printf("zsocket_bind(mServer, tcp://*:*) failed.\n"); err = zsocket_bind(mSignals, "tcp://*:%d", mSignalPort); if(!err) printf("zsocket_bind(mSignals, tcp://*:*) failed.\n"); printf("PrimeWorker: mServerPort=%d mSignalPort=%d\n", mServerPort, mSignalPort); err = zsocket_connect(mBackend, "tcp://localhost:8888"); assert(!err); err = zsocket_connect(frontend, "tcp://localhost:7777"); assert(!err); err = zsocket_connect(input, "inproc://bitcoin"); assert(!err); const char one[2] = {1, 0}; zsocket_set_subscribe(input, one); zloop_t* wloop = zloop_new(); zmq_pollitem_t item_input = {input, 0, ZMQ_POLLIN, 0}; err = zloop_poller(wloop, &item_input, &PrimeWorker::InvokeInput, this); assert(!err); zmq_pollitem_t item_server = {mServer, 0, ZMQ_POLLIN, 0}; err = zloop_poller(wloop, &item_server, &PrimeWorker::InvokeRequest, this); assert(!err); zmq_pollitem_t item_frontend = {frontend, 0, ZMQ_POLLIN, 0}; err = zloop_poller(wloop, &item_frontend, &PrimeWorker::InvokeRequest, this); assert(!err); err = zloop_timer(wloop, 60000, 0, &PrimeWorker::InvokeTimerFunc, this); assert(err >= 0); zsocket_signal(pipe); zloop_start(wloop); zloop_destroy(&wloop); zsocket_destroy(ctx, mServer); zsocket_destroy(ctx, mSignals); zsocket_destroy(ctx, mBackend); zsocket_destroy(ctx, frontend); zsocket_destroy(ctx, input); zsocket_signal(pipe); printf("PrimeWorker exited.\n"); }
int zsockopt_test (bool verbose) { printf (" * zsockopt: "); // @selftest zctx_t *ctx = zctx_new (); assert (ctx); void *zocket; #if (ZMQ_VERSION_MAJOR == 2) # if defined (ZMQ_HWM) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_hwm (zocket, 1); assert (zsocket_hwm (zocket) == 1); zsocket_hwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SWAP) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_swap (zocket, 1); assert (zsocket_swap (zocket) == 1); zsocket_swap (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_AFFINITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_affinity (zocket, 1); assert (zsocket_affinity (zocket) == 1); zsocket_affinity (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IDENTITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_identity (zocket, "test"); char *identity = zsocket_identity (zocket); assert (identity); free (identity); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RATE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rate (zocket, 1); assert (zsocket_rate (zocket) == 1); zsocket_rate (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl (zocket, 1); assert (zsocket_recovery_ivl (zocket) == 1); zsocket_recovery_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL_MSEC) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl_msec (zocket, 1); assert (zsocket_recovery_ivl_msec (zocket) == 1); zsocket_recovery_ivl_msec (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MCAST_LOOP) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_mcast_loop (zocket, 1); assert (zsocket_mcast_loop (zocket) == 1); zsocket_mcast_loop (zocket); zsocket_destroy (ctx, zocket); # endif # if (ZMQ_VERSION_MINOR == 2) # if defined (ZMQ_RCVTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvtimeo (zocket, 1); assert (zsocket_rcvtimeo (zocket) == 1); zsocket_rcvtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # endif # if (ZMQ_VERSION_MINOR == 2) # if defined (ZMQ_SNDTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndtimeo (zocket, 1); assert (zsocket_sndtimeo (zocket) == 1); zsocket_sndtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # endif # if defined (ZMQ_SNDBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndbuf (zocket, 1); assert (zsocket_sndbuf (zocket) == 1); zsocket_sndbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvbuf (zocket, 1); assert (zsocket_rcvbuf (zocket) == 1); zsocket_rcvbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LINGER) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_linger (zocket, 1); assert (zsocket_linger (zocket) == 1); zsocket_linger (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl (zocket, 1); assert (zsocket_reconnect_ivl (zocket) == 1); zsocket_reconnect_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL_MAX) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl_max (zocket, 1); assert (zsocket_reconnect_ivl_max (zocket) == 1); zsocket_reconnect_ivl_max (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_BACKLOG) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_backlog (zocket, 1); assert (zsocket_backlog (zocket) == 1); zsocket_backlog (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_subscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_UNSUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_unsubscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_TYPE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_type (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVMORE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_rcvmore (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_FD) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_fd (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_EVENTS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_events (zocket); zsocket_destroy (ctx, zocket); # endif #endif #if (ZMQ_VERSION_MAJOR == 3) # if defined (ZMQ_TYPE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_type (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDHWM) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_sndhwm (zocket, 1); assert (zsocket_sndhwm (zocket) == 1); zsocket_sndhwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVHWM) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvhwm (zocket, 1); assert (zsocket_rcvhwm (zocket) == 1); zsocket_rcvhwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_AFFINITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_affinity (zocket, 1); assert (zsocket_affinity (zocket) == 1); zsocket_affinity (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_subscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_UNSUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_unsubscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IDENTITY) zocket = zsocket_new (ctx, ZMQ_DEALER); assert (zocket); zsocket_set_identity (zocket, "test"); char *identity = zsocket_identity (zocket); assert (identity); free (identity); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RATE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rate (zocket, 1); assert (zsocket_rate (zocket) == 1); zsocket_rate (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl (zocket, 1); assert (zsocket_recovery_ivl (zocket) == 1); zsocket_recovery_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDBUF) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_sndbuf (zocket, 1); assert (zsocket_sndbuf (zocket) == 1); zsocket_sndbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvbuf (zocket, 1); assert (zsocket_rcvbuf (zocket) == 1); zsocket_rcvbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LINGER) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_linger (zocket, 1); assert (zsocket_linger (zocket) == 1); zsocket_linger (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl (zocket, 1); assert (zsocket_reconnect_ivl (zocket) == 1); zsocket_reconnect_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL_MAX) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl_max (zocket, 1); assert (zsocket_reconnect_ivl_max (zocket) == 1); zsocket_reconnect_ivl_max (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_BACKLOG) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_backlog (zocket, 1); assert (zsocket_backlog (zocket) == 1); zsocket_backlog (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MAXMSGSIZE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_maxmsgsize (zocket, 1); assert (zsocket_maxmsgsize (zocket) == 1); zsocket_maxmsgsize (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MULTICAST_HOPS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_multicast_hops (zocket, 1); assert (zsocket_multicast_hops (zocket) == 1); zsocket_multicast_hops (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvtimeo (zocket, 1); assert (zsocket_rcvtimeo (zocket) == 1); zsocket_rcvtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndtimeo (zocket, 1); assert (zsocket_sndtimeo (zocket) == 1); zsocket_sndtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IPV4ONLY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_ipv4only (zocket, 1); assert (zsocket_ipv4only (zocket) == 1); zsocket_ipv4only (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_DELAY_ATTACH_ON_CONNECT) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_delay_attach_on_connect (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_ROUTER_MANDATORY) zocket = zsocket_new (ctx, ZMQ_ROUTER); assert (zocket); zsocket_set_router_mandatory (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_ROUTER_RAW) zocket = zsocket_new (ctx, ZMQ_ROUTER); assert (zocket); zsocket_set_router_raw (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_XPUB_VERBOSE) zocket = zsocket_new (ctx, ZMQ_XPUB); assert (zocket); zsocket_set_xpub_verbose (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVMORE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_rcvmore (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_FD) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_fd (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_EVENTS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_events (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LAST_ENDPOINT) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); char *last_endpoint = zsocket_last_endpoint (zocket); assert (last_endpoint); free (last_endpoint); zsocket_destroy (ctx, zocket); # endif zocket = zsocket_new (ctx, ZMQ_SUB); zsocket_set_hwm (zocket, 1); #endif zctx_destroy (&ctx); // @end printf ("OK\n"); return 0; }
static rsRetVal createSocket(socket_info* info, void** sock) { size_t ii; int rv; *sock = zsocket_new(s_context, info->type); if (!sock) { errmsg.LogError(0, RS_RET_INVALID_PARAMS, "zsocket_new failed: %s, for type %d", strerror(errno),info->type); /* DK: invalid params seems right here */ return RS_RET_INVALID_PARAMS; } /* Set options *before* the connect/bind. */ if (info->identity) zsocket_set_identity(*sock, info->identity); if (info->sndBuf > -1) zsocket_set_sndbuf(*sock, info->sndBuf); if (info->rcvBuf > -1) zsocket_set_rcvbuf(*sock, info->rcvBuf); if (info->linger > -1) zsocket_set_linger(*sock, info->linger); if (info->backlog > -1) zsocket_set_backlog(*sock, info->backlog); if (info->sndTimeout > -1) zsocket_set_sndtimeo(*sock, info->sndTimeout); if (info->rcvTimeout > -1) zsocket_set_rcvtimeo(*sock, info->rcvTimeout); if (info->maxMsgSize > -1) zsocket_set_maxmsgsize(*sock, info->maxMsgSize); if (info->rate > -1) zsocket_set_rate(*sock, info->rate); if (info->recoveryIVL > -1) zsocket_set_recovery_ivl(*sock, info->recoveryIVL); if (info->multicastHops > -1) zsocket_set_multicast_hops(*sock, info->multicastHops); if (info->reconnectIVL > -1) zsocket_set_reconnect_ivl(*sock, info->reconnectIVL); if (info->reconnectIVLMax > -1) zsocket_set_reconnect_ivl_max(*sock, info->reconnectIVLMax); if (info->ipv4Only > -1) zsocket_set_ipv4only(*sock, info->ipv4Only); if (info->affinity > -1) zsocket_set_affinity(*sock, info->affinity); /* since HWM have defaults, we always set them. No return codes to check, either.*/ zsocket_set_sndhwm(*sock, info->sndHWM); zsocket_set_rcvhwm(*sock, info->rcvHWM); /* Set subscriptions.*/ if (info->type == ZMQ_SUB) { for (ii = 0; ii < sizeof(info->subscriptions)/sizeof(char*); ++ii) zsocket_set_subscribe(*sock, info->subscriptions[ii]); } /* Do the bind/connect... */ if (info->action==ACTION_CONNECT) { rv = zsocket_connect(*sock, info->description); if (rv < 0) { errmsg.LogError(0, RS_RET_INVALID_PARAMS, "zmq_connect using %s failed: %s", info->description, strerror(errno)); return RS_RET_INVALID_PARAMS; } } else { rv = zsocket_bind(*sock, info->description); if (rv <= 0) { errmsg.LogError(0, RS_RET_INVALID_PARAMS, "zmq_bind using %s failed: %s", info->description, strerror(errno)); return RS_RET_INVALID_PARAMS; } } return RS_RET_OK; }
int main (int argc, char *argv []) { if (argc != 4) { printf ("usage: %s <bind-to> <message-size> " "<roundtrip-count>\n", argv[0]); return 1; } char* bind_to = argv [1]; long message_size = atoi (argv [2]); long roundtrip_count = atoi (argv [3]); char check_dropped_packets = 1; if(message_size < GetNumberOfDigits(roundtrip_count)) { printf("CAUTION: Message size too small to check for dropped packets\r\n"); check_dropped_packets = 0; } zctx_t *context = zctx_new (); void *publisher = zsocket_new (context, ZMQ_XPUB); zctx_set_linger(context, 1000); zsocket_set_sndhwm(publisher, 1000); int hwm = zsocket_sndhwm(publisher); printf("HMW=%d\r\n", hwm); #ifdef ZMQ_PUB_RELIABLE // set PUB_RELIABLE int pub_reliable = 1; int rc = zmq_setsockopt(publisher, ZMQ_PUB_RELIABLE, &pub_reliable, sizeof(pub_reliable)); if (rc != 0) { printf ("error in zmq_setsockopt (ZMQ_PUB_RELIABLE): %s\n", zmq_strerror (errno)); return -1; } #endif printf("Connecting to %s\r\n", bind_to); zsocket_bind (publisher, bind_to); //Wait for sub connection printf("Waiting for subscriber.\r\n"); zmsg_t* connection = zmsg_recv(publisher); zmsg_destroy(&connection); printf("Subscriber connected!\r\n"); int i = 0; while (i<roundtrip_count && !zctx_interrupted) { void* data = malloc(message_size); bzero(data, message_size); if(check_dropped_packets) { sprintf(data, "%d", i); } zmsg_t* msg = zmsg_new(); zmsg_addstr(msg, "TEST", 4); zmsg_addmem(msg, data, message_size); //zmsg_dump(msg); zmsg_send (&msg, publisher); i++; free(data); } zctx_destroy (&context); return 0; }