本文整理汇总了C++中zmq_bind函数的典型用法代码示例。如果您正苦于以下问题:C++ zmq_bind函数的具体用法?C++ zmq_bind怎么用?C++ zmq_bind使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zmq_bind函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main (int argc, char *argv[])
{
zmq::context_t context(1);
// Socket facing clients
zmq::socket_t frontend (context, ZMQ_ROUTER);
frontend.bind("tcp://*:5559");
// Socket facing services
zmq::socket_t backend (context, ZMQ_DEALER);
zmq_bind (static_cast<void *>(backend), "tcp://*:5560");
// Start built-in device
zmq_device (
ZMQ_QUEUE,
static_cast<void *>(frontend),
static_cast<void *>(backend)
);
return 0;
}
示例2: agent_control_message
static void
agent_control_message (agent_t *self)
{
zmsg_t *msg = zmsg_recv (self->pipe);
char *method = zmsg_popstr (msg);
if (streq (method, "BIND")) {
char *endpoint = zmsg_popstr (msg);
assert (endpoint);
int rc = zmq_bind (self->router, endpoint);
assert (rc == 0);
free (endpoint);
}
else
if (streq (method, "STOP")) {
zstr_send (self->pipe, "OK");
self->stopped = true;
}
free (method);
zmsg_destroy (&msg);
}
示例3: test_basic
void test_basic ()
{
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *router = test_context_socket (ZMQ_ROUTER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tcp://127.0.0.1:*"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
// Send a message to an unknown peer with the default setting
// This will not report any error
send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
send_string_expect_success (router, "DATA", 0);
// Send a message to an unknown peer with mandatory routing
// This will fail
int mandatory = 1;
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
&mandatory, sizeof (mandatory)));
int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
// Create dealer called "X" and connect it to our router
void *dealer = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
// Get message from dealer to know when connection is ready
send_string_expect_success (dealer, "Hello", 0);
recv_string_expect_success (router, "X", 0);
// Send a message to connected dealer now
// It should work
send_string_expect_success (router, "X", ZMQ_SNDMORE);
send_string_expect_success (router, "Hello", 0);
test_context_socket_close (router);
test_context_socket_close (dealer);
}
示例4: test_blocking
int test_blocking (int send_hwm_, int msg_cnt_)
{
// Set up bind socket
void *pub_socket = test_context_socket (ZMQ_PUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub_socket, "inproc://a"));
// Set up connect socket
void *sub_socket = test_context_socket (ZMQ_SUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub_socket, "inproc://a"));
//set a hwm on publisher
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (pub_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
int wait = 1;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (pub_socket, ZMQ_XPUB_NODROP, &wait, sizeof (wait)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, 0, 0));
// Send until we block
int send_count = 0;
int recv_count = 0;
while (send_count < msg_cnt_) {
const int rc = zmq_send (pub_socket, NULL, 0, ZMQ_DONTWAIT);
if (rc == 0) {
++send_count;
} else if (-1 == rc) {
TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
recv_count += receive (sub_socket);
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
}
}
recv_count += receive (sub_socket);
// Clean up
test_context_socket_close (sub_socket);
test_context_socket_close (pub_socket);
return recv_count;
}
示例5: main
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to send messages on
void *sender = zmq_socket (context, ZMQ_PUSH);
zmq_bind (sender, "tcp://*:5557");
// Socket to send start of batch message on
void *sink = zmq_socket (context, ZMQ_PUSH);
zmq_connect (sink, "tcp://localhost:5558");
printf ("Press Enter when the workers are ready: ");
getchar ();
printf ("Sending tasks to workers…\n");
// The first message is "0" and signals start of batch
s_send (sink, "0");
// Initialize random number generator
srandom ((unsigned) time (NULL));
// Send 100 tasks
int task_nbr;
int total_msec = 0; // Total expected cost in msecs
for (task_nbr = 0; task_nbr < 100; task_nbr++) {
int workload;
// Random workload from 1 to 100msecs
workload = randof (100) + 1;
total_msec += workload;
char string [10];
sprintf (string, "%d", workload);
s_send (sender, string);
}
printf ("Total expected cost: %d msec\n", total_msec);
zmq_close (sink);
zmq_close (sender);
zmq_ctx_destroy (context);
return 0;
}
示例6: main
int main(int argc, const char * argv[]) {
char time_buf[TIMESIZE], message[MSGSIZE];
void *ctx = NULL, *publisher = NULL;
int retval = 0, nbytes = 0, lucky_number = 0;
time_t current_time;
ctx = zmq_ctx_new();
publisher = zmq_socket(ctx, ZMQ_PUB);
retval = zmq_bind(publisher, "tcp://*:9000");
if (retval != 0) {
fprintf(stderr, "Unable to bind zmq socket on port 9000\n");
exit(1);
}
srandom(42);
while(1) {
current_time = time(NULL);
ctime_r(¤t_time, time_buf);
// Remove the newline added by ctime_r
time_buf[24] = '\0';
lucky_number = (int) (rand() % 1000000L);
sprintf(message, "%s %06d", time_buf, lucky_number);
nbytes = zmq_send(publisher, message, MSGSIZE, 0);
if (nbytes != MSGSIZE) {
fprintf(stderr, "ERROR: Unable to send bytes to subscriber\n");
}
sleep(1);
}
zmq_close(publisher);
zmq_ctx_destroy(ctx);
return 0;
}
示例7: main
int main ()
{
void *ctx = zmq_init (1);
assert (ctx);
void *pub = zmq_socket (ctx, ZMQ_PUB);
assert (pub);
int rc = zmq_bind (pub, "tcp://*:2211");
assert (rc == 0);
void *sub = zmq_socket (ctx, ZMQ_SUB);
assert (sub);
uint64_t size = 1;
rc = zmq_setsockopt (sub, ZMQ_HWM, &size, sizeof (size));
assert (rc == 0);
int64_t swap = 10000000;
rc = zmq_setsockopt (sub, ZMQ_SWAP, &swap, sizeof (swap));
assert (rc == 0);
rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
rc = zmq_connect (sub, "tcp://127.0.0.1:2211");
assert (rc == 0);
int count;
for (count = 0; count < 100000; count++) {
zmq_msg_t msg;
zmq_msg_init_size (&msg, 0);
zmq_send (pub, &msg, 0);
zmq_msg_close (&msg);
}
int linger = 0;
zmq_setsockopt (pub, ZMQ_LINGER, &linger, sizeof (int));
zmq_setsockopt (sub, ZMQ_LINGER, &linger, sizeof (int));
zmq_close (pub);
zmq_close (sub);
zmq_term (ctx);
return 0;
}
示例8: test_round_robin_out
void test_round_robin_out (void *ctx)
{
void *req = zmq_socket (ctx, ZMQ_REQ);
assert (req);
int rc = zmq_bind (req, bind_address);
assert (rc == 0);
const size_t services = 5;
void *rep [services];
for (size_t peer = 0; peer < services; peer++) {
rep [peer] = zmq_socket (ctx, ZMQ_REP);
assert (rep [peer]);
int timeout = 250;
rc = zmq_setsockopt (rep [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_connect (rep [peer], connect_address);
assert (rc == 0);
}
// We have to give the connects time to finish otherwise the requests
// will not properly round-robin. We could alternatively connect the
// REQ sockets to the REP sockets.
msleep (SETTLE_TIME);
// Send our peer-replies, and expect every REP it used once in order
for (size_t peer = 0; peer < services; peer++) {
s_send_seq (req, "ABC", SEQ_END);
s_recv_seq (rep [peer], "ABC", SEQ_END);
s_send_seq (rep [peer], "DEF", SEQ_END);
s_recv_seq (req, "DEF", SEQ_END);
}
close_zero_linger (req);
for (size_t peer = 0; peer < services; peer++)
close_zero_linger (rep [peer]);
// Wait for disconnects.
msleep (SETTLE_TIME);
}
示例9: main
int
main(int argc, char *argv[])
{
void *context = NULL;
void *sender = NULL;
FILE *fp = NULL;
char line[1024];
if (argc <= 1) {
printf("provide a file\n");
return ERROR;
}
if (access(argv[1], F_OK) == -1 ) {
printf("%s file does not exist\n", argv[1]);
return ERROR;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("unable to open %s\n", argv[1]);
return ERROR;
}
context = zmq_ctx_new();
sender = zmq_socket(context, ZMQ_PUSH);
set_socket_timeout(sender);
zmq_bind(sender, VENTBIND);
while(fgets(line, sizeof(line), fp)) {
if (line[0] == '#')
continue;
if (s_send(sender, line) < 0)
perror("s_send");
}
fclose(fp);
zmq_close(sender);
zmq_ctx_destroy(context);
return OK;
}
示例10: zmq_socket
static void *step2 (void *context)
{
// Bind inproc socket before starting step1
void *receiver = zmq_socket (context, ZMQ_PAIR);
zmq_bind (receiver, "inproc://step2");
pthread_t thread;
pthread_create (&thread, NULL, step1, context);
// Wait for signal and pass it on
char *string = s_recv (receiver);
free (string);
zmq_close (receiver);
// Connect to step3 and tel it we're ready
void *xmitter = zmq_socket (context, ZMQ_PAIR);
zmq_connect (xmitter, "inproc://step3");
s_send (xmitter, "READY");
zmq_close (xmitter);
return NULL;
}
示例11: zmq_socket
void Van::bind() {
receiver_ = zmq_socket(context_, ZMQ_ROUTER);
CHECK(receiver_ != nullptr)
<< "create receiver socket failed : " << zmq_strerror(errno);
std::string addr = "tcp://*:";
if (FLAGS_bind_to) {
LOG(INFO) << "Use port from command line";
addr += std::to_string(FLAGS_bind_to);
} else {
LOG(INFO) << "Use port from node specification";
CHECK(myNode_.has_port()) << myNode_.ShortDebugString();
addr += std::to_string(myNode_.port());
}
if (FLAGS_local) {
addr = "ipc:///tmp/" + myNode_.id();
}
CHECK(zmq_bind(receiver_, addr.c_str()) == 0)
<< "bind to " << addr << " failed:" << zmq_strerror(errno);
VLOG(1) << "Bind to " << addr;
}
示例12: test_ev_zmq
/* send 100 messages over PAIR sockets
* sender in one event handler, receiver in another
*/
void test_ev_zmq (void)
{
struct ev_loop *loop;
void *zctx;
void *zin, *zout;
int i;
ev_zmq win, wout;
ok ((loop = ev_loop_new (EVFLAG_AUTO)) != NULL,
"ev_loop_new works");
ok ((zctx = zmq_init (1)) != NULL,
"initialized zmq context");
ok ((zout = zmq_socket (zctx, ZMQ_PAIR)) != NULL
&& zmq_bind (zout, "inproc://eventloop_test") == 0,
"PAIR socket bind ok");
ok ((zin = zmq_socket (zctx, ZMQ_PAIR)) != NULL
&& zmq_connect (zin, "inproc://eventloop_test") == 0,
"PAIR socket connect ok");
i = 0;
ev_zmq_init (&win, zsock_rx_cb, zin, EV_READ);
win.data = &i;
ev_zmq_init (&wout, zsock_tx_cb, zout, EV_WRITE);
ev_zmq_start (loop, &win);
ev_zmq_start (loop, &wout);
ok (ev_run (loop, 0) == 0,
"both watchers removed themselves and ev_run exited");
ev_zmq_stop (loop, &win);
ev_zmq_stop (loop, &wout);
cmp_ok (i, "==", 100,
"ev_zmq handler ran 100 times");
ev_loop_destroy (loop);
zmq_close (zin);
zmq_close (zout);
zmq_ctx_destroy (zctx);
}
示例13: main
int main()
{
int rc;
void *ctx, *s;
zmq_msg_t query, resultset;
const char *query_string, *resultset_string = "ok";
ctx = zmq_init(1);
assert(ctx);
s = zmq_socket(ctx, ZMQ_REP);
assert(s);
rc = zmq_bind(s, "tcp://127.0.0.1:5555");
assert(rc == 0);
while (1)
{
rc = zmq_msg_init(&query);
assert(rc == 0);
rc = zmq_recv(s, &query, 0);
assert(rc == 0);
query_string = (const char *)zmq_msg_data(&query);
printf("recv: %s\n", query_string);
zmq_msg_close(&query);
rc = zmq_msg_init_size(&resultset, strlen(resultset_string)+1);
assert(rc == 0);
memcpy(zmq_msg_data(&resultset), resultset_string, strlen(resultset_string)+1);
rc = zmq_send(s, &resultset, 0);
assert(rc == 0);
zmq_msg_close(&resultset);
}
return 0;
}
示例14: stop_monitor
int zmq::socket_base_t::monitor (const char *addr_, int events_)
{
if (unlikely (ctx_terminated)) {
errno = ETERM;
return -1;
}
// Support deregistering monitoring endpoints as well
if (addr_ == NULL) {
stop_monitor ();
return 0;
}
// Parse addr_ string.
std::string protocol;
std::string address;
if (parse_uri (addr_, protocol, address) || check_protocol (protocol))
return -1;
// Event notification only supported over inproc://
if (protocol != "inproc") {
errno = EPROTONOSUPPORT;
return -1;
}
// Register events to monitor
monitor_events = events_;
monitor_socket = zmq_socket (get_ctx (), ZMQ_PAIR);
if (monitor_socket == NULL)
return -1;
// Never block context termination on pending event messages
int linger = 0;
int rc = zmq_setsockopt (monitor_socket, ZMQ_LINGER, &linger, sizeof (linger));
if (rc == -1)
stop_monitor (false);
// Spawn the monitor socket endpoint
rc = zmq_bind (monitor_socket, addr_);
if (rc == -1)
stop_monitor (false);
return rc;
}
示例15: Log_init
int Log_init(bstring access_log, bstring log_spec)
{
int rc = 0;
LogConfig *config = NULL;
if(LOG_SOCKET == NULL)
{
check(ZMQ_CTX, "No ZMQ context, cannot start access log.");
if(Setting_get_int("disable.access_logging", 0))
{
log_info("Access log is disabled according to disable.access_logging.");
}
else
{
config = LogConfig_create(access_log, log_spec);
check(config, "Failed to configure access logging.");
LOG_SOCKET = zmq_socket(ZMQ_CTX, ZMQ_PUB);
check(LOG_SOCKET != NULL, "Failed to create access log socket");
#ifdef ZMQ_LINGER
int opt = 0;
rc = zmq_setsockopt(LOG_SOCKET, ZMQ_LINGER, &opt, sizeof(opt));
check(rc == 0, "Could not set the linger option.");
#endif
rc = zmq_bind(LOG_SOCKET, bdata(log_spec));
check(rc == 0, "Failed to bind access_log zeromq socket.");
pthread_create(&LOG_THREAD, NULL, Log_internal_thread, config);
}
}
return 0;
error:
LogConfig_destroy(config);
return -1;
}