本文整理汇总了C++中zsocket_connect函数的典型用法代码示例。如果您正苦于以下问题:C++ zsocket_connect函数的具体用法?C++ zsocket_connect怎么用?C++ zsocket_connect使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zsocket_connect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: worker_task
// Worker using REQ socket to do load-balancing
//
static void *
worker_task(void *args)
{
zctx_t *ctx = zctx_new();
void *worker = zsocket_new(ctx, ZMQ_REQ);
#if (defined (WIN32))
zsocket_connect(worker, "tcp://localhost:5673"); // backend
#else
zsocket_connect(worker, "ipc://backend.ipc");
#endif
// Tell broker we're ready for work
zframe_t *frame = zframe_new(WORKER_READY, strlen(WORKER_READY));
zframe_send(&frame, worker, 0);
// Process messages as they arrive
while (1) {
zmsg_t *msg = zmsg_recv(worker);
if (!msg)
break; // Interrupted
zframe_print(zmsg_last(msg), "Worker: ");
zframe_reset(zmsg_last(msg), "OK", 2);
zmsg_send(&msg, worker);
}
zctx_destroy(&ctx);
return NULL;
}
示例2: main
int
main (int argc, char *argv[])
{
if (argc != 3) {
exit (-1);
}
int numb_msgs = atoi (argv[2]);
zctx_t *ctx = zctx_new ();
void *dealer = zsocket_new (ctx, ZMQ_DEALER);
zsocket_set_linger (dealer, -1);
zsocket_connect (dealer, "%s:9000", argv[1]);
void *sub = zsocket_new (ctx, ZMQ_SUB);
zsocket_connect (sub, "%s:9002", argv[1]);
zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "all", 4);
int64_t time[2];
zmq_pollitem_t pollitem[1] = { {sub, 0, ZMQ_POLLIN}
};
zmq_poll (pollitem, 1, -1);
zmsg_t *signal = zmsg_recv (sub);
zmsg_destroy (&signal);
char blob[SIZE] = { 0 };
zmsg_t *msg = zmsg_new ();
zframe_t *frame = zframe_new (blob, SIZE);
zmsg_add (msg, frame);
time[0] = zclock_time ();
int i;
for (i = 0; i < numb_msgs; i++) {
zmsg_t *nmsg = zmsg_dup (msg);
zmsg_send (&nmsg, dealer);
}
time[1] = zclock_time ();
zmsg_destroy (&msg);
zmq_poll (pollitem, 1, -1);
msg = zmsg_recv (sub);
zmsg_destroy (&msg);
msg = zmsg_new ();
frame = zframe_new (time, sizeof (int64_t) * 2);
zmsg_add (msg, frame);
zmsg_send (&msg, dealer);
zctx_destroy (&ctx);
}
示例3: client_task
// Basic request-reply client using REQ socket
//
static void *
client_task(void *args)
{
zctx_t *ctx = zctx_new();
void *client = zsocket_new(ctx, ZMQ_REQ);
#if (defined (WIN32))
zsocket_connect(client, "tcp://localhost:5672"); // frontend
#else
zsocket_connect(client, "ipc://frontend.ipc");
#endif
// Send request, get reply
while (1) {
zstr_send(client, "HELLO");
char *reply = zstr_recv(client);
if (!reply) {
break;
}
printf("Client: %s\n", reply);
free(reply);
zclock_sleep(1);
}
zctx_destroy(&ctx);
return NULL;
}
示例4: zproxy_test
void
zproxy_test (bool verbose)
{
printf (" * zproxy: ");
// @selftest
zctx_t *ctx = zctx_new ();
void *frontend = zsocket_new (ctx, ZMQ_PULL);
int rc = zsocket_bind (frontend, "inproc://frontend");
assert (rc == 0);
void *backend = zsocket_new (ctx, ZMQ_PUSH);
rc = zsocket_bind (backend, "inproc://backend");
assert (rc == 0);
zproxy_t *proxy = zproxy_new (ctx, frontend, backend);
// Connect application sockets to proxy
void *faucet = zsocket_new (ctx, ZMQ_PUSH);
rc = zsocket_connect (faucet, "inproc://frontend");
assert (rc == 0);
void *sink = zsocket_new (ctx, ZMQ_PULL);
rc = zsocket_connect (sink, "inproc://backend");
assert (rc == 0);
// Send some messages and check they arrived
zstr_send (faucet, "Hello");
char *string = zstr_recv (sink);
assert (streq (string, "Hello"));
zstr_free (&string);
// Check pause/resume functionality
zproxy_pause (proxy);
zstr_send (faucet, "World");
zproxy_resume (proxy);
string = zstr_recv (sink);
assert (streq (string, "World"));
zstr_free (&string);
// Create capture socket, must be a PULL socket
void *capture = zsocket_new (ctx, ZMQ_PULL);
rc = zsocket_bind (capture, "inproc://capture");
assert (rc == 0);
// Switch on capturing, check that it works
zproxy_capture (proxy, "inproc://capture");
zstr_send (faucet, "Hello");
string = zstr_recv (sink);
assert (streq (string, "Hello"));
zstr_free (&string);
string = zstr_recv (capture);
assert (streq (string, "Hello"));
zstr_free (&string);
zproxy_destroy (&proxy);
zctx_destroy (&ctx);
// @end
printf ("OK\n");
}
示例5: collabclient_remakeSockets
static void
collabclient_remakeSockets( cloneclient_t *cc )
{
cc->snapshot = zsocket_new (cc->ctx, ZMQ_DEALER);
zsocket_connect (cc->snapshot,
collabclient_makeAddressString( cc->address,
cc->port + socket_offset_snapshot));
cc->subscriber = zsocket_new (cc->ctx, ZMQ_SUB);
zsocket_set_subscribe (cc->subscriber, "");
zsocket_connect (cc->subscriber,
collabclient_makeAddressString( cc->address,
cc->port + socket_offset_subscriber));
zsocket_set_subscribe (cc->subscriber, SUBTREE);
cc->publisher = zsocket_new (cc->ctx, ZMQ_PUSH);
zsocket_connect (cc->publisher,
collabclient_makeAddressString( cc->address,
cc->port + socket_offset_publisher));
int fd = 0;
size_t fdsz = sizeof(fd);
int rc = zmq_getsockopt( cc->subscriber, ZMQ_FD, &fd, &fdsz );
printf("subscriber rc:%d fd:%d\n", rc, fd );
GDrawAddReadFD( 0, fd, cc, zeromq_subscriber_fd_callback );
}
示例6: game_thread
int game_thread( void * _parms ) {
GameThreadParms * parms = (GameThreadParms*)_parms;
GameState gs;
SharedRenderState rs;
game_init( gs, rs );
gs.zmq_control_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR );
{
int ret = zsocket_connect( gs.zmq_control_socket, "inproc://control_game" );
assert( ret == 0 );
}
gs.zmq_render_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR );
zsocket_bind( gs.zmq_render_socket, "inproc://game_render" );
gs.zmq_input_req = zsocket_new( parms->zmq_context, ZMQ_REQ );
{
int ret = zsocket_connect( gs.zmq_input_req, "inproc://input" );
assert( ret == 0 );
}
unsigned int baseline = SDL_GetTicks();
int framenum = 0;
while ( true ) {
unsigned int now = SDL_GetTicks();
unsigned int target_frame = ( now - baseline ) / GAME_DELAY;
if ( framenum <= target_frame ) {
framenum++;
// NOTE: build the state of the world at t = framenum * GAME_DELAY,
// under normal conditions that's a time in the future
// (the exception to that is if we are catching up on ticking game frames)
game_tick( now, gs, rs );
// notify the render thread that a new game state is ready.
// on the next render frame, it will start interpolating between the previous state and this new one
zstr_sendf( gs.zmq_render_socket, "%d %f %f %f %f %f %f %f %f %f", baseline + framenum * GAME_DELAY, rs.position.x, rs.position.y, rs.orientation.w, rs.orientation.x, rs.orientation.y, rs.orientation.z, rs.smoothed_angular.x, rs.smoothed_angular.y, rs.smoothed_angular.z );
} else {
int ahead = framenum * GAME_DELAY - ( now - baseline );
assert( ahead > 0 );
printf( "game sleep %d ms\n", ahead );
SDL_Delay( ahead );
}
char * cmd = zstr_recv_nowait( gs.zmq_control_socket );
if ( cmd != NULL ) {
assert( strcmp( cmd, "stop" ) == 0 );
free( cmd );
break;
}
}
return 0;
}
示例7: socket_connect
int socket_connect(void *socket, const char *format, ...)
{
va_list args;
int err, n;
char buf[255], id[255];
//zsocket_set_router_mandatory(socket, 1);
va_start(args, format);
//** Set the ID
snprintf(buf, 255, format, args);
// snprintf(id, 255, "%s:%ld", buf, random());
snprintf(id, 255, "%ld", random_int(1, 1000));
zsocket_set_identity(socket, strdup(id));
err = zsocket_connect(socket, format, args);
n = errno;
va_end(args);
log_printf(0, "id=!%s! err=%d errno=%d\n", id, err, n);
assert(err == 0);
return(err);
}
示例8: main
int
main(int argc, const char **argv){
zctx_t *ctx = zctx_new();
void *sub = zsocket_new(ctx, ZMQ_SUB);
zsocket_set_subscribe(sub, "");
zsocket_connect(sub, "tcp://127.0.0.1:3000");
printf("connected to :3000\n");
struct timeval start;
gettimeofday(&start, NULL);
while (1) {
char *msg = zstr_recv(sub);
if (0 == n--) {
struct timeval end;
gettimeofday(&end, NULL);
int duration = end.tv_sec - start.tv_sec;
printf("\n");
printf(" pub/sub:\n");
printf(" %d msgs\n", ops);
printf(" %d ops/s\n", ops / duration);
printf(" %d s\n", duration);
printf("\n");
exit(0);
}
}
return 0;
}
示例9: worker_routine
static void*
worker_routine(void* arg)
{
zmsg_t* msg;
zframe_t* frame;
zctx_t* ctx = zctx_new();
void* worker = zsocket_new(ctx, ZMQ_REQ);
zsocket_connect(worker, "ipc://%s-localbe.ipc", self);
frame = zframe_new(WORKER_READY, 1);
zframe_send(&frame, worker, 0);
while (1) {
msg = zmsg_recv(worker);
if (!msg)
break;
zframe_print(zmsg_last(msg), "Worker: ");
zframe_reset(zmsg_last(msg), "OK", 2);
zmsg_send(&msg, worker);
}
zctx_destroy(&ctx);
return NULL;
}
示例10: zthread_fork
void *
zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args)
{
shim_t *shim = NULL;
// Create our end of the pipe
void *pipe = zctx__socket_pipe (ctx);
assert (pipe);
zsocket_bind (pipe, "inproc://zctx-pipe-%p", pipe);
// Prepare argument shim for child thread
shim = (shim_t *) zmalloc (sizeof (shim_t));
assert (shim);
shim->attached = thread_fn;
shim->args = args;
shim->ctx = zctx_shadow (ctx);
assert (shim->ctx);
shim->pipe = zctx__socket_pipe (shim->ctx);
assert (shim->pipe);
// Connect child pipe to our pipe
zsocket_connect (shim->pipe, "inproc://zctx-pipe-%p", pipe);
s_thread_start (shim);
return pipe;
}
示例11: s_agent_new
static agent_t *
s_agent_new (zctx_t *ctx, void *control)
{
agent_t *self = (agent_t *) zmalloc (sizeof (agent_t));
self->ctx = ctx;
self->control = control;
self->router = zsocket_new (ctx, ZMQ_ROUTER);
// Connect our data socket to caller's endpoint
self->data = zsocket_new (ctx, ZMQ_PAIR);
char *endpoint = zstr_recv (self->control);
int rc = zsocket_connect (self->data, "%s", endpoint);
assert (rc != -1);
free (endpoint);
// Create new client codec using cert from API
byte public_key [32];
byte secret_key [32];
rc = zmq_recv (self->control, public_key, 32, 0);
assert (rc == 32);
rc = zmq_recv (self->control, secret_key, 32, 0);
assert (rc == 32);
self->cert = zcert_new_from (public_key, secret_key);
self->metadata = zhash_new ();
zhash_autofree (self->metadata);
self->clients = zhash_new ();
self->max_clients = 100;
self->max_pending = 10;
self->client_ttl = 3600; // 60 minutes
self->pending_ttl = 60; // 60 seconds
return self;
}
示例12: client_task
static void *
client_task (void *args)
{
zctx_t *ctx = zctx_new ();
void *client = zsocket_new (ctx, ZMQ_DEALER);
// Set random identity to make tracing easier
char identity [10];
sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
zsocket_set_identity (client, identity);
zsocket_connect (client, "tcp://localhost:5570");
zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 } };
int request_nbr = 0;
while (true) {
// Tick once per second, pulling in arriving messages
int centitick;
for (centitick = 0; centitick < 100; centitick++) {
zmq_poll (items, 1, 10 * ZMQ_POLL_MSEC);
if (items [0].revents & ZMQ_POLLIN) {
zmsg_t *msg = zmsg_recv (client);
zframe_print (zmsg_last (msg), identity);
zmsg_destroy (&msg);
}
}
zstr_send (client, "request #%d");
}
zctx_destroy (&ctx);
return NULL;
}
示例13: test_zmq
static void test_zmq (flux_reactor_t *reactor)
{
zctx_t *zctx;
void *zs[2];
flux_watcher_t *r, *w;
ok ((zctx = zctx_new ()) != NULL,
"zmq: created zmq context");
zs[0] = zsocket_new (zctx, ZMQ_PAIR);
zs[1] = zsocket_new (zctx, ZMQ_PAIR);
ok (zs[0] && zs[1]
&& zsocket_bind (zs[0], "inproc://test_zmq") == 0
&& zsocket_connect (zs[1], "inproc://test_zmq") == 0,
"zmq: connected ZMQ_PAIR sockets over inproc");
r = flux_zmq_watcher_create (reactor, zs[0], FLUX_POLLIN, zmqreader, NULL);
w = flux_zmq_watcher_create (reactor, zs[1], FLUX_POLLOUT, zmqwriter, NULL);
ok (r != NULL && w != NULL,
"zmq: nonblocking reader and writer created");
flux_watcher_start (r);
flux_watcher_start (w);
ok (flux_reactor_run (reactor, 0) == 0,
"zmq: reactor ran to completion after %d messages", zmqwriter_msgcount);
flux_watcher_stop (r);
flux_watcher_stop (w);
flux_watcher_destroy (r);
flux_watcher_destroy (w);
zsocket_destroy (zctx, zs[0]);
zsocket_destroy (zctx, zs[1]);
zctx_destroy (&zctx);
}
示例14: main
int main (void)
{
zctx_t *ctx = zctx_new ();
zctx_set_linger (ctx, 1000);
void *pub = zsocket_new (ctx, ZMQ_XPUB);
zsocket_set_hwm (pub, 0);
zsocket_connect (pub, "tcp://127.0.0.1:9000");
// Wait for subscriber to subscribe
zframe_t *frame = zframe_recv (pub);
zframe_destroy (&frame);
// Send HELLOs for five seconds
size_t total = 0;
int64_t finish_at = zclock_time () + 5000;
while (zclock_time () < finish_at) {
// Send 100K and then check time again
int count = 0;
for (count = 0; count < 100000; count++)
zstr_send (pub, "HELLO");
total++;
}
printf ("%zd00000 messages sent\n", total);
zstr_send (pub, "WORLD");
zctx_destroy (&ctx);
return 0;
}
示例15: collabclient_closeLocalServer
void
collabclient_closeLocalServer( int port )
{
#ifdef BUILD_COLLAB
collabclient_sniffForLocalServer_t* cc = &collabclient_sniffForLocalServer_singleton;
zctx_t* ctx = obtainMainZMQContext();
int beacon_port = port;
if( !port )
port = collabclient_getDefaultBasePort();
printf("collabclient_closeLocalServer() port:%d\n", port);
void* socket = zsocket_new ( ctx, ZMQ_REQ );
zsocket_connect ( socket,
"tcp://localhost:%d", port+socket_offset_ping);
zstr_send( socket, "quit" );
cc->haveServer = 0;
if( beacon_port )
{
g_hash_table_remove_all( peers );
}
collabclient_sniffForLocalServer();
#endif
}