本文整理汇总了C++中zsock_new函数的典型用法代码示例。如果您正苦于以下问题:C++ zsock_new函数的具体用法?C++ zsock_new怎么用?C++ zsock_new使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zsock_new函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: s_can_connect
// Checks whether client can connect to server
static bool
s_can_connect (zsock_t **server, zsock_t **client)
{
int port_nbr = zsock_bind (*server, "tcp://127.0.0.1:*");
assert (port_nbr > 0);
int rc = zsock_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);
assert (poller);
bool success = (zpoller_wait (poller, 400) == *client);
zpoller_destroy (&poller);
zsock_destroy (client);
zsock_destroy (server);
*server = zsock_new (ZMQ_PUSH);
assert (*server);
*client = zsock_new (ZMQ_PULL);
assert (*client);
return success;
}
示例2: GlobalServer_init
bool
GlobalServer_init (
GlobalServer *self,
GlobalServerStartupInfo *info
) {
memcpy (&self->info, info, sizeof (self->info));
// ==========================
// Allocate ZMQ objects
// ==========================
if (!(self->cliConnection = zsock_new (ZMQ_RAW_ROUTER))) {
error ("Cannot allocate a new CLI zsock.");
return false;
}
if (!(self->zonesConnection = zsock_new (ZMQ_REQ))) {
error ("Cannot allocate a new zones zsock.");
return false;
}
// Connect to Redis Server
if (!(self->redis = Redis_new (&self->info.redisInfo))) {
error ("Cannot initialize Redis connection.");
return false;
}
if (!(Redis_connect (self->redis))) {
error ("Cannot connect to Redis.");
return false;
}
return true;
}
示例3: zactor_new
zactor_t *
zactor_new (zactor_fn *actor, void *args)
{
zactor_t *self = (zactor_t *) zmalloc (sizeof (zactor_t));
if (!self)
return NULL;
self->tag = ZACTOR_TAG;
shim_t *shim = (shim_t *) zmalloc (sizeof (shim_t));
if (!shim)
return NULL;
// Create front-to-back pipe pair
self->pipe = zsock_new (ZMQ_PAIR);
assert (self->pipe);
char endpoint [32];
while (true) {
sprintf (endpoint, "inproc://zactor-%04x-%04x\n",
randof (0x10000), randof (0x10000));
if (zsock_bind (self->pipe, "%s", endpoint) == 0)
break;
}
shim->pipe = zsock_new (ZMQ_PAIR);
assert (shim->pipe);
int rc = zsock_connect (shim->pipe, "%s", endpoint);
assert (rc != -1);
shim->handler = actor;
shim->args = args;
#if defined (__UNIX__)
pthread_t thread;
pthread_create (&thread, NULL, s_thread_shim, shim);
pthread_detach (thread);
#elif defined (__WINDOWS__)
HANDLE handle = (HANDLE) _beginthreadex (
NULL, // Handle is private to this process
0, // Use a default stack size for new thread
&s_thread_shim, // Start real thread function via this shim
shim, // Which gets arguments shim
CREATE_SUSPENDED, // Set thread priority before starting it
NULL); // We don't use the thread ID
assert (handle);
// Set child thread priority to same as current
int priority = GetThreadPriority (GetCurrentThread ());
SetThreadPriority (handle, priority);
// Start thread & release resources
ResumeThread (handle);
CloseHandle (handle);
#endif
// Mandatory handshake for new actor so that constructor returns only
// when actor has also initialized. This eliminates timing issues at
// application start up.
zsock_wait (self->pipe);
return self;
}
示例4: zmailer_msg_test
void
zmailer_msg_test (bool verbose)
{
printf (" * zmailer_msg:");
if (verbose)
printf ("\n");
// @selftest
// Simple create/destroy test
zmailer_msg_t *self = zmailer_msg_new ();
assert (self);
zmailer_msg_destroy (&self);
// Create pair of sockets we can send through
// We must bind before connect if we wish to remain compatible with ZeroMQ < v4
zsock_t *output = zsock_new (ZMQ_DEALER);
assert (output);
int rc = zsock_bind (output, "inproc://selftest-zmailer_msg");
assert (rc == 0);
zsock_t *input = zsock_new (ZMQ_ROUTER);
assert (input);
rc = zsock_connect (input, "inproc://selftest-zmailer_msg");
assert (rc == 0);
// Encode/send/decode and verify each message type
int instance;
self = zmailer_msg_new ();
zmailer_msg_set_id (self, ZMAILER_MSG_MAIL);
zmailer_msg_set_from (self, "Life is short but Now lasts for ever");
zmailer_msg_set_to (self, "Life is short but Now lasts for ever");
zmailer_msg_set_subject (self, " test subject ");
zmailer_msg_set_request (self, " this is the text to be sent ");
// Send twice
zmailer_msg_send (self, output);
zmailer_msg_send (self, output);
for (instance = 0; instance < 2; instance++) {
zmailer_msg_recv (self, input);
assert (zmailer_msg_routing_id (self));
assert (streq (zmailer_msg_from (self), "Life is short but Now lasts for ever"));
assert (streq (zmailer_msg_to (self), "Life is short but Now lasts for ever"));
assert (streq (zmailer_msg_subject (self), " test subject "));
assert (streq (zmailer_msg_request (self), " this is the text to be sent "));
}
zmailer_msg_destroy (&self);
zsock_destroy (&input);
zsock_destroy (&output);
// @end
printf ("OK\n");
}
示例5: main
int main(int argc, char const * const *argv)
{
int rc;
zsys_set_sndhwm(1);
zsys_set_linger(100);
void *pusher = zsock_new(ZMQ_PUSH);
assert(pusher);
zsock_set_sndhwm(pusher, 1000);
zsock_set_linger(pusher, 500);
rc = zsock_connect(pusher, "tcp://localhost:12345");
assert(rc==0);
void *puller = zsock_new(ZMQ_PULL);
assert(puller);
zsock_set_rcvhwm(puller, 1000);
zsock_set_linger(puller, 500);
rc = zsock_bind(puller, "tcp://*:12345");
if (rc != 12345){
printf("bind failed: %s\n", zmq_strerror(errno));
}
assert(rc == 12345);
void *publisher = zsock_new(ZMQ_PUB);
assert(publisher);
zsock_set_sndhwm(publisher, 1000);
zsock_set_linger(publisher, 500);
rc = zsock_bind(publisher, "tcp://*:12346");
assert(rc==12346);
// set up event loop
zloop_t *loop = zloop_new();
assert(loop);
zloop_set_verbose(loop, 0);
// push data every 10 ms
rc = zloop_timer(loop, 1, 0, timer_event, pusher);
assert(rc != -1);
zmq_pollitem_t item;
item.socket = puller;
item.events = ZMQ_POLLIN;
rc = zloop_poller(loop, &item, forward, publisher);
assert(rc == 0);
rc = zloop_start(loop);
printf("zloop return: %d", rc);
zloop_destroy(&loop);
assert(loop == NULL);
return 0;
}
示例6: zyre_node_test
void
zyre_node_test (bool verbose)
{
printf (" * zyre_node: ");
zsock_t *pipe = zsock_new (ZMQ_PAIR);
zsock_t *outbox = zsock_new (ZMQ_PAIR);
zyre_node_t *node = zyre_node_new (pipe, outbox);
zyre_node_destroy (&node);
zsock_destroy (&pipe);
// Node takes ownership of outbox and destroys it
printf ("OK\n");
}
示例7: zmonitor_test
void
zmonitor_test (bool verbose)
{
printf (" * zmonitor: ");
if (verbose)
printf ("\n");
#if defined (ZMQ_EVENT_ALL)
// @selftest
zsock_t *client = zsock_new (ZMQ_DEALER);
assert (client);
zactor_t *clientmon = zactor_new (zmonitor, client);
assert (clientmon);
if (verbose)
zstr_sendx (clientmon, "VERBOSE", NULL);
zstr_sendx (clientmon, "LISTEN", "LISTENING", "ACCEPTED", NULL);
zstr_sendx (clientmon, "START", NULL);
zsock_wait (clientmon);
zsock_t *server = zsock_new (ZMQ_DEALER);
assert (server);
zactor_t *servermon = zactor_new (zmonitor, server);
assert (servermon);
if (verbose)
zstr_sendx (servermon, "VERBOSE", NULL);
zstr_sendx (servermon, "LISTEN", "CONNECTED", "DISCONNECTED", NULL);
zstr_sendx (servermon, "START", NULL);
zsock_wait (servermon);
// Allow a brief time for the message to get there...
zmq_poll (NULL, 0, 200);
// Check client is now listening
int port_nbr = zsock_bind (client, "tcp://127.0.0.1:*");
assert (port_nbr != -1);
s_assert_event (clientmon, "LISTENING");
// Check server connected to client
zsock_connect (server, "tcp://127.0.0.1:%d", port_nbr);
s_assert_event (servermon, "CONNECTED");
// Check client accepted connection
s_assert_event (clientmon, "ACCEPTED");
zactor_destroy (&clientmon);
zactor_destroy (&servermon);
zsock_destroy (&client);
zsock_destroy (&server);
#endif
// @end
printf ("OK\n");
}
示例8: zloop_test
void
zloop_test (bool verbose)
{
printf (" * zloop: ");
int rc = 0;
// @selftest
// Create two PAIR sockets and connect over inproc
zsock_t *output = zsock_new (ZMQ_PAIR);
assert (output);
zsock_bind (output, "inproc://zloop.test");
zsock_t *input = zsock_new (ZMQ_PAIR);
assert (input);
zsock_connect (input, "inproc://zloop.test");
zloop_t *loop = zloop_new ();
assert (loop);
zloop_set_verbose (loop, verbose);
// Create a timer that will be cancelled
int timer_id = zloop_timer (loop, 1000, 1, s_timer_event, NULL);
zloop_timer (loop, 5, 1, s_cancel_timer_event, &timer_id);
// After 20 msecs, send a ping message to output3
zloop_timer (loop, 20, 1, s_timer_event, output);
// Set up some tickets that will never expire
zloop_set_ticket_delay (loop, 10000);
void *ticket1 = zloop_ticket (loop, s_timer_event, NULL);
void *ticket2 = zloop_ticket (loop, s_timer_event, NULL);
void *ticket3 = zloop_ticket (loop, s_timer_event, NULL);
// When we get the ping message, end the reactor
rc = zloop_reader (loop, input, s_socket_event, NULL);
assert (rc == 0);
zloop_reader_set_tolerant (loop, input);
zloop_start (loop);
zloop_ticket_delete (loop, ticket1);
zloop_ticket_delete (loop, ticket2);
zloop_ticket_delete (loop, ticket3);
zloop_destroy (&loop);
assert (loop == NULL);
zsock_destroy (&input);
zsock_destroy (&output);
// @end
printf ("OK\n");
}
示例9: main
int main (void)
{
printf("Starting dealer...\n");
// Socket to talk to clients
zsock_t *server = zsock_new (ZMQ_DEALER);
int rc = zsock_bind (server, "tcp://*:5556");
checkRetCode(rc);
printf("Hiding from others...\n");
char* msg = "LucTeo (blue shirt + red)";
int caught = 0;
// while ( 1 )
{
rc = zstr_send (server, msg);
checkRetCode(rc);
if ( !caught )
{
printf("I'm caught. :(\n");
msg = "Too late, secret was secret but it's taken";
caught = 1;
}
}
// zsock_destroy (&server);
return 0;
}
示例10: server_connect
static void
server_connect (server_t *self, const char *endpoint)
{
zsock_t *remote = zsock_new (ZMQ_DEALER);
assert (remote); // No recovery if exhausted
// Never block on sending; we use an infinite HWM and buffer as many
// messages as needed in outgoing pipes. Note that the maximum number
// is the overall tuple set size.
zsock_set_sndhwm (remote, 0);
if (zsock_connect (remote, "%s", endpoint)) {
zsys_error ("bad zgossip endpoint '%s'", endpoint);
zsock_destroy (&remote);
return;
}
// Send HELLO and then PUBLISH for each tuple we have
zgossip_msg_send_hello (remote);
tuple_t *tuple = (tuple_t *) zhash_first (self->tuples);
while (tuple) {
int rc = zgossip_msg_send_publish (remote, tuple->key, tuple->value, 0);
assert (rc == 0);
tuple = (tuple_t *) zhash_next (self->tuples);
}
// Now monitor this remote for incoming messages
engine_handle_socket (self, remote, remote_handler);
zlist_append (self->remotes, remote);
}
示例11: s_self_new
static self_t *
s_self_new (zsock_t *pipe, zcertstore_t *certstore)
{
self_t *self = (self_t *) zmalloc (sizeof (self_t));
assert (self);
if (certstore) {
self->certstore = certstore;
self->allow_any = false;
}
self->pipe = pipe;
self->whitelist = zhashx_new ();
assert (self->whitelist);
self->blacklist = zhashx_new ();
// Create ZAP handler and get ready for requests
assert (self->blacklist);
self->handler = zsock_new (ZMQ_REP);
assert (self->handler);
int rc = zsock_bind (self->handler, ZAP_ENDPOINT);
assert (rc == 0);
self->poller = zpoller_new (self->pipe, self->handler, NULL);
assert (self->poller);
return self;
}
示例12: mdp_broker_test
void
mdp_broker_test (bool verbose)
{
printf (" * mdp_broker: ");
if (verbose)
printf ("\n");
// @selftest
zactor_t *server = zactor_new (mdp_broker, "server");
if (verbose)
zstr_send (server, "VERBOSE");
zstr_sendx (server, "BIND", "ipc://@/mdp_broker", NULL);
zsock_t *client = zsock_new (ZMQ_DEALER);
assert (client);
zsock_set_rcvtimeo (client, 2000);
zsock_connect (client, "ipc://@/mdp_broker");
// TODO: fill this out
mdp_msg_t *request = mdp_msg_new ();
mdp_msg_destroy (&request);
zsock_destroy (&client);
zactor_destroy (&server);
// @end
printf ("OK\n");
}
示例13: server_connect
static void
server_connect (server_t *self, const char *endpoint)
{
zsock_t *remote = zsock_new (ZMQ_DEALER);
assert (remote); // No recovery if exhausted
// Never block on sending; we use an infinite HWM and buffer as many
// messages as needed in outgoing pipes. Note that the maximum number
// is the overall tuple set size.
zsock_set_unbounded (remote);
if (zsock_connect (remote, "%s", endpoint)) {
zsys_warning ("bad zgossip endpoint '%s'", endpoint);
zsock_destroy (&remote);
return;
}
// Send HELLO and then PUBLISH for each tuple we have
zgossip_msg_t *gossip = zgossip_msg_new ();
zgossip_msg_set_id (gossip, ZGOSSIP_MSG_HELLO);
zgossip_msg_send (gossip, remote);
tuple_t *tuple = (tuple_t *) zhashx_first (self->tuples);
while (tuple) {
zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH);
zgossip_msg_set_key (gossip, tuple->key);
zgossip_msg_set_value (gossip, tuple->value);
zgossip_msg_send (gossip, remote);
tuple = (tuple_t *) zhashx_next (self->tuples);
}
// Now monitor this remote for incoming messages
zgossip_msg_destroy (&gossip);
engine_handle_socket (self, remote, remote_handler);
zlistx_add_end (self->remotes, remote);
}
示例14: s_create_socket
static zsock_t *
s_create_socket (char *type_name, char *endpoints)
{
// 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", type_name
};
// We always match type at least at end of table
int index;
for (index = 0; strneq (type_name, type_names [index]); index++) ;
if (index > ZMQ_XSUB) {
zsys_error ("zproxy: invalid socket type '%s'", type_name);
return NULL;
}
zsock_t *sock = zsock_new (index);
if (sock) {
if (zsock_attach (sock, endpoints, true)) {
zsys_error ("zproxy: invalid endpoints '%s'", endpoints);
zsock_destroy (&sock);
}
}
return sock;
}
示例15: zyre_new
zyre_t *
zyre_new (const char *name)
{
zyre_t *self = (zyre_t *) zmalloc (sizeof (zyre_t));
assert (self);
// Create front-to-back pipe pair for data traffic
self->inbox = zsock_new (ZMQ_PAIR);
assert (self->inbox);
char endpoint [32];
while (true) {
sprintf (endpoint, "inproc://zyre-%04x-%04x\n",
randof (0x10000), randof (0x10000));
if (zsock_bind (self->inbox, "%s", endpoint) == 0)
break;
}
// Create other half of traffic pipe
zsock_t *outbox = zsock_new_pair (endpoint);
assert (outbox);
// Start node engine and wait for it to be ready
self->actor = zactor_new (zyre_node_actor, outbox);
assert (self->actor);
// Send name, if any, to node ending
if (name)
zstr_sendx (self->actor, "SET NAME", name, NULL);
return self;
}