int zsock_connect (zsock_t *self, const char *format, ...) { assert (self); assert (zsock_is (self)); // Expand format to get full endpoint va_list argptr; va_start (argptr, format); char *endpoint = zsys_vprintf (format, argptr); va_end (argptr); int rc = zmq_connect (self->handle, endpoint); #if (ZMQ_VERSION < ZMQ_MAKE_VERSION (4,0,0)) int retries = 4; while (rc == -1 && zmq_errno () == ECONNREFUSED && retries) { // This bruteforces a synchronization between connecting and // binding threads on ZeroMQ v3.2 and earlier, where the bind // MUST happen before the connect on inproc transports. zclock_sleep (250); rc = zmq_connect (self->handle, endpoint); retries--; } #endif free (endpoint); return rc; }
void * zsock_resolve (void *self) { assert (self); if (zsock_is (self)) return ((zsock_t *) self)->handle; else if (zactor_is (self)) return zactor_resolve (self); else return self; }
void zsock_destroy_ (zsock_t **self_p, const char *filename, size_t line_nbr) { assert (self_p); if (*self_p) { zsock_t *self = *self_p; assert (zsock_is (self)); self->tag = 0xDeadBeef; int rc = zsys_close (self->handle, filename, line_nbr); assert (rc == 0); free (self->endpoint); free (self); *self_p = NULL; } }
const char * zsock_type_str (zsock_t *self) { assert (self); assert (zsock_is (self)); // This array matches ZMQ_XXX type definitions assert (ZMQ_PAIR == 0); char *type_names [] = { "PAIR", "PUB", "SUB", "REQ", "REP", "DEALER", "ROUTER", "PULL", "PUSH", "XPUB", "XSUB", "STREAM" }; return type_names [self->type]; }
int zsock_disconnect (zsock_t *self, const char *format, ...) { assert (self); assert (zsock_is (self)); #if (ZMQ_VERSION >= ZMQ_MAKE_VERSION (3,2,0)) // Expand format to get full endpoint va_list argptr; va_start (argptr, format); char *endpoint = zsys_vprintf (format, argptr); va_end (argptr); int rc = zmq_disconnect (self->handle, endpoint); free (endpoint); return rc; #else return -1; #endif }
/// // Probe the supplied object, and report if it looks like a zsock_t. // Takes a polymorphic socket reference. bool QZsock::is (void *self) { bool rv = zsock_is (self); return rv; }
JNIEXPORT jboolean JNICALL Java_org_zeromq_czmq_Zsock__1_1is (JNIEnv *env, jclass c, jlong self) { jboolean is_ = (jboolean) zsock_is ((void *) (intptr_t) self); return is_; }
int zsock_bind (zsock_t *self, const char *format, ...) { assert (self); assert (zsock_is (self)); // Expand format to get full endpoint va_list argptr; va_start (argptr, format); char *endpoint = zsys_vprintf (format, argptr); va_end (argptr); int rc; // If tcp:// endpoint, parse to get or make port number zrex_t *rex = zrex_new (NULL); if (zrex_eq (rex, endpoint, "^tcp://.*:(\\d+)$")) { assert (zrex_hits (rex) == 2); if (zmq_bind (self->handle, endpoint) == 0) rc = atoi (zrex_hit (rex, 1)); else rc = -1; } else if (zrex_eq (rex, endpoint, "^(tcp://.*):([*!])(\\[(\\d+)?-(\\d+)?\\])?$")) { assert (zrex_hits (rex) == 6); const char *hostname, *opcode, *group, *first_str, *last_str; zrex_fetch (rex, &hostname, &opcode, &group, &first_str, &last_str, NULL); int first = *first_str? atoi (first_str): DYNAMIC_FIRST; int last = *last_str? atoi (last_str): DYNAMIC_LAST; // This is how many times we'll try before giving up int attempts = last - first + 1; // If operator is '*', take first available port. // If operator is '!', take a random leap into our port space; we'll // still scan sequentially to make sure we find a free slot rapidly. int port = first; if (streq (opcode, "!")) port += randof (attempts); rc = -1; // Assume we don't succeed while (rc == -1 && attempts--) { free (endpoint); endpoint = zsys_sprintf ("%s:%d", hostname, port); if (zmq_bind (self->handle, endpoint) == 0) rc = port; if (++port > last) port = first; } } else rc = zmq_bind (self->handle, endpoint); // Store successful endpoint for later reference if (rc >= 0) { free (self->endpoint); self->endpoint = endpoint; } else free (endpoint); zrex_destroy (&rex); return rc; }
Z K1(zsockis){PC(x); R kb(zsock_is(VSK(x)));}
/// // Probe the supplied object, and report if it looks like a zsock_t. // Takes a polymorphic socket reference. bool QmlZsockAttached::is (void *self) { return zsock_is (self); };