// Checks whether client can connect to server static bool s_can_connect (zctx_t *ctx, void **server, void **client) { int port_nbr = zsocket_bind (*server, "tcp://127.0.0.1:*"); assert (port_nbr > 0); int rc = zsocket_connect (*client, "tcp://127.0.0.1:%d", port_nbr); assert (rc == 0); // Give the connection time to fail if that's the plan zclock_sleep (200); // By default PUSH sockets block if there's no peer zsock_set_sndtimeo (*server, 200); zstr_send (*server, "Hello, World"); zpoller_t *poller = zpoller_new (*client, NULL); bool success = (zpoller_wait (poller, 400) == *client); zpoller_destroy (&poller); zsocket_destroy (ctx, *client); zsocket_destroy (ctx, *server); *server = zsocket_new (ctx, ZMQ_PUSH); assert (*server); *client = zsocket_new (ctx, ZMQ_PULL); assert (*client); return success; }
static zsock_t* subscriber_push_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_PUSH); assert(socket); zsock_set_sndtimeo(socket, 10); int rc = zsock_bind(socket, "inproc://subscriber"); assert(rc == 0); return socket; }
// Thread shim for UNIX calls the actual thread and cleans up afterwards. static void * s_thread_shim (void *args) { assert (args); shim_t *shim = (shim_t *) args; shim->handler (shim->pipe, shim->args); // Do not block, if the other end of the pipe is already deleted zsock_set_sndtimeo (shim->pipe, 0); zsock_signal (shim->pipe, 0); zsock_destroy (&shim->pipe); free (shim); return NULL; }
// Thread shim for Windows that wraps a POSIX-style thread handler // and does the _endthreadex for us automatically. static unsigned __stdcall s_thread_shim (void *args) { assert (args); shim_t *shim = (shim_t *) args; shim->handler (shim->pipe, shim->args); // Do not block, if the other end of the pipe is already deleted zsock_set_sndtimeo (shim->pipe, 0); zsock_signal (shim->pipe, 0); zsock_destroy (&shim->pipe); free (shim); _endthreadex (0); // Terminates thread return 0; }
void zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsock_new (ZMQ_DEALER); if (!self->mailbox) return; // Null when we're shutting down // Set our own identity on the socket so that receiving node // knows who each message came from. Note that we cannot use // the UUID directly as the identity since it may contain a // zero byte at the start, which libzmq does not like for // historical and arguably bogus reasons that it nonetheless // enforces. byte routing_id [ZUUID_LEN + 1] = { 1 }; memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN); int rc = zmq_setsockopt (zsock_resolve (self->mailbox), ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1); assert (rc == 0); // Set a high-water mark that allows for reasonable activity zsock_set_sndhwm (self->mailbox, PEER_EXPIRED * 100); // Send messages immediately or return EAGAIN zsock_set_sndtimeo (self->mailbox, 0); // Connect through to peer node rc = zsock_connect (self->mailbox, "%s", endpoint); if (rc != 0) { zsys_error ("(%s) cannot connect to endpoint=%s", self->origin, endpoint); // Don't really have any error handling yet; if connect // fails, there's something wrong with connect endpoint? assert (false); } assert (rc == 0); if (self->verbose) zsys_info ("(%s) connect to peer: endpoint=%s", self->origin, endpoint); self->endpoint = strdup (endpoint); self->connected = true; self->ready = false; }
void zactor_destroy (zactor_t **self_p) { assert (self_p); if (*self_p) { zactor_t *self = *self_p; assert (zactor_is (self)); // Signal the actor to end and wait for the thread exit code // If the pipe isn't connected any longer, assume child thread // has already quit due to other reasons and don't collect the // exit signal. zsock_set_sndtimeo (self->pipe, 0); if (zstr_send (self->pipe, "$TERM") == 0) zsock_wait (self->pipe); zsock_destroy (&self->pipe); self->tag = 0xDeadBeef; free (self); *self_p = NULL; } }
int zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint, uint64_t expired_timeout) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsock_new (ZMQ_DEALER); if (!self->mailbox) return -1; // Null when we're shutting down // Set our own identity on the socket so that receiving node // knows who each message came from. Note that we cannot use // the UUID directly as the identity since it may contain a // zero byte at the start, which libzmq does not like for // historical and arguably bogus reasons that it nonetheless // enforces. byte routing_id [ZUUID_LEN + 1] = { 1 }; memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN); int rc = zmq_setsockopt (zsock_resolve (self->mailbox), ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1); assert (rc == 0); // Set a high-water mark that allows for reasonable activity zsock_set_sndhwm (self->mailbox, expired_timeout * 100); // Send messages immediately or return EAGAIN zsock_set_sndtimeo (self->mailbox, 0); // If the peer is a link-local IPv6 address but the interface is not set, // use ZSYS_INTERFACE_ADDRESS if provided zrex_t *rex = zrex_new (NULL); char endpoint_iface [NI_MAXHOST] = {0}; if (zsys_ipv6 () && zsys_interface () && strlen(zsys_interface ()) && !streq (zsys_interface (), "*") && zrex_eq (rex, endpoint, "^tcp://(fe80[^%]+)(:\\d+)$")) { const char *hostname, *port; zrex_fetch (rex, &hostname, &port, NULL); strcat (endpoint_iface, "tcp://"); strcat (endpoint_iface, hostname); strcat (endpoint_iface, "%"); strcat (endpoint_iface, zsys_interface ()); strcat (endpoint_iface, port); } else strcat (endpoint_iface, endpoint); zrex_destroy (&rex); // Connect through to peer node rc = zsock_connect (self->mailbox, "%s", endpoint_iface); if (rc != 0) { zsys_debug ("(%s) cannot connect to endpoint=%s", self->origin, endpoint_iface); zsock_destroy (&self->mailbox); return -1; } if (self->verbose) zsys_info ("(%s) connect to peer: endpoint=%s", self->origin, endpoint_iface); self->endpoint = strdup (endpoint_iface); self->connected = true; self->ready = false; return 0; }
/// // Set socket option `sndtimeo`. void QZsock::setSndtimeo (int sndtimeo) { zsock_set_sndtimeo (self, sndtimeo); }
JNIEXPORT void JNICALL Java_org_zeromq_czmq_Zsock__1_1setSndtimeo (JNIEnv *env, jclass c, jlong self, jint sndtimeo) { zsock_set_sndtimeo ((zsock_t *) (intptr_t) self, (int) sndtimeo); }