value hx_zmq_construct_socket (value context_handle,value type) { val_is_kind(context_handle,k_zmq_context_handle); if (!val_is_int(type)) { val_throw(alloc_int(EINVAL)); return alloc_null(); } void *s = zmq_socket (val_data(context_handle),val_int(type)); int err = zmq_errno(); if (s == NULL) { val_throw (alloc_int(err)); return alloc_null(); } // See: http://nekovm.org/doc/ffi#abstracts_and_kinds value v = alloc_abstract(k_zmq_socket_handle,s); val_gc(v,finalize_socket); // finalize_socket is called when the abstract value is garbage collected return v; }
bool ztop::InitialService(std::vector<std::pair<std::string, std::string>> servicePair, ztop*pztop) { auto lambda_setsocket = [](std::pair<std::string, std::string>& sPair, ztop*pztop){ void*s = zmq_socket(pztop->ctx, ZMQ_PUSH); int rc = zmq_bind(s, sPair.second.c_str()); if (rc != -1) { pztop->pairs.insert(std::make_pair(sPair.first, s)); pztop->vItems.push_back({ s, 0, ZMQ_POLLIN, 0 }); /* pztop->jobSendSoketSets.insert(s);*/ std::cerr << sPair.first << " start success:" << sPair.second << std::endl; } else std::cerr << __FUNCTION__ << ":"<<"service not start,with err:=====>" << zmq_strerror(zmq_errno()); }; for_each(servicePair.begin(), servicePair.end(), [&](std::pair<std::string, std::string>& sPair){ lambda_setsocket(sPair, pztop); }); return true; }
int create_sub_socket(void* ctx) { std::stringstream conn; conn << "tcp://" << g_sip << ":" << g_port_sub; void * sub = zmq_socket(ctx, ZMQ_SUB); int rc = zmq_connect(sub, conn.str().c_str()); assert(rc == 0); if (rc != 0) { std::cerr << "Error Connect to " << conn.str() << " errno " << errno << std::endl; zmq_close(sub); exit(EXIT_FAILURE); } char temp[256] = { 0 }; sprintf(temp, "%s ", g_sn); printf("Subscribe id: %s\n", temp); rc = zmq_setsockopt(sub, ZMQ_SUBSCRIBE, temp, strlen(temp)); assert(rc == 0); rc = zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "ALL ", strlen("ALL ")); assert(rc == 0); g_sub_socket = sub; return 0; }
//******************************************************************************** // Function: redirect_count // // Description: Recieve Control Plane redirect count. //******************************************************************************** static int32_t redirect_count(void) { void *context = zmq_ctx_new (); void *requester = zmq_socket(context, ZMQ_REQ); int32_t timeo{}; zmq_setsockopt(requester, ZMQ_LINGER, (void*) &timeo, sizeof(timeo)); char buffer[64]; sprintf(buffer, "tcp://localhost:%d", TCPLANE_SERVICE); zmq_connect(requester, buffer); sprintf(buffer, "%d", redirectCount); zmq_send(requester, buffer, strlen(buffer), 0); size_t size = timed_read(requester, buffer, sizeof(buffer), READ_TIMEOUT); if(size) size = atoi(buffer); zmq_close(requester); zmq_ctx_destroy(context); return size; }
int main (void) { void *context = zmq_ctx_new (); void *responder = zmq_socket (context, ZMQ_REP); int i=1; char skeystr[] = "JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6"; uint8_t server_key [32]; zmq_z85_decode (server_key, skeystr); zmq_setsockopt(responder, ZMQ_CURVE_SERVER, &i, sizeof(i)); zmq_setsockopt(responder, ZMQ_CURVE_SECRETKEY, &server_key, sizeof(server_key)); int rc = zmq_bind (responder, "tcp://*:55555"); assert (rc == 0); while (1) { char buffer [10]; zmq_recv (responder, buffer, 10, 0); printf ("Received Hello\n"); sleep (1); zmq_send (responder, "World", 5, 0); } return 0; }
// .split worker task // While this example runs in a single process, that is just to make // it easier to start and stop the example. Each thread has its own // context and conceptually acts as a separate process. // This is the worker task, using a REQ socket to do load-balancing. // Because s_send and s_recv can't handle 0MQ binary identities, we // set a printable text identity to allow routing. static void *worker_task(void *args) { void *context = zmq_ctx_new(); void *worker = zmq_socket(context, ZMQ_REQ); #if (defined (WIN32)) s_set_id(worker, (intptr_t)args); zmq_connect(worker, "tcp://localhost:5673"); // backend #else s_set_id(worker); zmq_connect(worker, "ipc://backend.ipc"); #endif // Tell broker we're ready for work s_send(worker, "READY"); while (1) { // Read and save all frames until we get an empty frame // In this example there is only 1, but there could be more char *identity = s_recv(worker); char *empty = s_recv(worker); assert(*empty == 0); free(empty); // Get request, send reply char *request = s_recv(worker); printf("Worker: %s from %s\n", request, identity); free(request); s_sendmore(worker, identity); s_sendmore(worker, ""); s_send(worker, "OK"); free(identity); } zmq_close(worker); zmq_ctx_destroy(context); return NULL; }
int main (void) { setup_test_environment (); void *ctx = zmq_ctx_new (); assert (ctx); void *sock = zmq_socket (ctx, ZMQ_PUB); assert (sock); int rc = zmq_connect (sock, "tcp://localhost:1234"); assert (rc == 0); rc = zmq_connect (sock, "tcp://[::1]:1234"); assert (rc == 0); rc = zmq_connect (sock, "tcp://localhost:invalid"); assert (rc == -1); rc = zmq_connect (sock, "tcp://in val id:1234"); assert (rc == -1); rc = zmq_connect (sock, "tcp://"); assert (rc == -1); rc = zmq_connect (sock, "tcp://192.168.0.200:*"); assert (rc == -1); rc = zmq_connect (sock, "invalid://localhost:1234"); assert (rc == -1); assert (errno == EPROTONOSUPPORT); rc = zmq_close (sock); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); return 0; }
int main(int argc, char *argv[]) { if (argc < 3) return EXIT_FAILURE; int M = atoi(argv[1]); int N = atoi(argv[2]); printf("M: %d, N: %d\n", M, N); void *ctx = zmq_init(1); void *b = zmq_socket(ctx, ZMQ_PAIR); zmq_connect(b, "tcp://localhost:4444"); zmq_msg_t msg; int val[M]; long long start_time = sc_time(); int i; for (i=0; i<N; i++) { int *buf = (int *)malloc(M * sizeof(int)); memset(val, i, M * sizeof(int)); memcpy(buf, val, M * sizeof(int)); zmq_msg_init_data(&msg, buf, M * sizeof(int), _dealloc, NULL); zmq_send(b, &msg, 0); zmq_msg_close(&msg); zmq_msg_init(&msg); zmq_recv(b, &msg, 0); memcpy(val, (int *)zmq_msg_data(&msg), zmq_msg_size(&msg)); zmq_msg_close(&msg); } long long end_time = sc_time(); printf("zmq_a: Time elapsed: %f sec\n", sc_time_diff(start_time, end_time)); zmq_close(b); zmq_term(ctx); return EXIT_SUCCESS; }
bool SubscriberZMQ::_onInit(TUInt port) { m_context = zmq_ctx_new(); if (m_context == 0) { zmqlog("Error occurred during zmq_ctx_new()"); return false; } m_socket = zmq_socket(m_context, ZMQ_SUB); if (m_socket == 0) { zmqlog("Error occurred during zmq_socket()"); clear(); return false; } TChar address[100] = {'\0'}; sprintf(address, "tcp://localhost:%u", port); int result = zmq_connect(m_socket, address); if (result != 0) { zmqlog("Error occurred during zmq_init()"); clear(); return false; } const TString& subscribeId = getSubscribeId(); result = zmq_setsockopt (m_socket, ZMQ_SUBSCRIBE, subscribeId.c_str(), subscribeId.length() - 1); if (result != 0) { zmqlog("Error occurred during zmq_setsockopt"); clear(); return false; } return true; }
int main () { void *ctx = zmq_init (1); assert (ctx); void *dealer = zmq_socket (ctx, ZMQ_DEALER); assert (dealer); int rc = zmq_connect (dealer, "tcp://localhost:2211"); assert (rc == 0); while (1) { // Send signal to parent zmq_msg_t msg; zmq_msg_init (&msg); zmq_sendmsg (dealer, &msg, 0); zmq_msg_close (&msg); // Do some work sleep (1); } return 0; }
int main(void) { void *context = zmq_ctx_new(); //Socket to connect to Server void *requester = zmq_socket(context, ZMQ_REQ); zmq_connect(requester, "tcp://localhost:8888"); int request_nbr; for(request_nbr = 0; request_nbr != 10; request_nbr++) { s_send(requester, "Hello"); char* string = s_recv(requester); printf("Received reply %d [%s] \n", request_nbr, string); free(string); } zmq_close(requester); zmq_ctx_destroy(context); return 0; }
void main(){ fprintf(stdout,"##### Client-1 is running - C Broker Test #####\n"); void *context = zmq_ctx_new (); void *requester = zmq_socket (context, ZMQ_PUSH); zmq_connect (requester, "tcp://127.0.0.1:5559"); char *str_message="i am client-1"; int request_number; //sending 1000000 requests from client for (request_number = 1; request_number <= 1000000; request_number++) { zstr_send (requester, str_message); } zmq_close (requester); zmq_ctx_destroy (context); }
int main (int argc, char const *argv[]) { void* context = zmq_ctx_new(); //create a SUB socket void* subscriber = zmq_socket(context, ZMQ_SUB); const char* filter; if(argc > 1) { filter = argv[1]; } else { filter = "Company1|"; } printf("Collecting stock information from the server.\n"); int conn = zmq_connect(subscriber, "tcp://localhost:4040"); // must set a subscription for SUB socket conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter, strlen(filter)); int i = 0; for(i = 0; i < 10; i++) { zmq_msg_t reply; zmq_msg_init(&reply); // receive the message, previous message is deallocated zmq_msg_recv(&reply, subscriber, 0); int length = zmq_msg_size(&reply); char* value = malloc(length + 1); memcpy(value, zmq_msg_data(&reply), length); zmq_msg_close(&reply); printf("%s\n", value); free(value); } zmq_close(subscriber); zmq_ctx_destroy(context); return 0; }
JNIEXPORT void JNICALL Java_org_zeromq_ZMQ_00024Socket_construct (JNIEnv *env, jobject obj, jobject context, jint type) { void *s = get_socket (env, obj); if (s) return; void *c = fetch_context (env, context); if (c == NULL) { raise_exception (env, EINVAL); return; } s = zmq_socket (c, type); int err = zmq_errno(); if (s == NULL) { raise_exception (env, err); return; } put_socket(env, obj, s); }
int main(void) { // Prepare our context and socket void *context = zmq_ctx_new(); void *receiver = zmq_socket(context, ZMQ_PULL); zmq_bind(receiver, "tcp://*:5558"); // Wait for start of batch char *string = s_recv(receiver); free(string); printf("start of batch\n"); // Start our clock now int64_t start_time = s_clock(); // Process 100 confirmations int task_nbr; for(task_nbr=0; task_nbr<100; task_nbr++) { char *string = s_recv(receiver); free(string); if(task_nbr % 10 == 0) { printf(":"); } else { printf("."); } fflush(stdout); } // Calculate and report duration of batch printf("\nTotal elapsed time: %d msec\n", (int)(s_clock() - start_time)); zmq_close(receiver); zmq_ctx_destroy(context); return 0; }
int main(void) { void *ctx = zmq_ctx_new(); void *sock = zmq_socket(ctx, ZMQ_ROUTER); int rc = zmq_bind(sock, "tcp://127.0.0.1:5555"); assert(0 == rc); char buf[128]; int recvBytes = 0; while (1) { // recv id recvBytes = zmq_recv(sock, buf, sizeof(buf), 0); if (0 < recvBytes) { // send it back printf("recv id bytes=%d\n", recvBytes); zmq_send(sock, buf, recvBytes, ZMQ_SNDMORE); } // delimiter emtpy frame recvBytes = zmq_recv(sock, buf, sizeof(buf), 0); assert(0 == recvBytes); recvBytes = zmq_recv(sock, buf, sizeof(buf), 0); // actual message if (0 < recvBytes) { // send it back buf[recvBytes] = '\0'; printf("recv msg %s, bytes=%d\n", buf, recvBytes); zmq_send(sock, "", 0, ZMQ_SNDMORE); zmq_send(sock, buf, recvBytes, 0); } } zmq_close(sock); zmq_ctx_destroy(ctx); return 0; }
int main (void) { void *context = zmq_ctx_new (); void *socket = zmq_socket (context, ZMQ_REP); zmq_bind (socket, "tcp://*:5555"); s_catch_signals (); while (true) { // Blocking read will exit on a signal zmq_msg_t message; zmq_msg_init (&message); zmq_msg_recv (&message, socket, 0); if (s_interrupted) { printf ("W: interrupt received, killing server...\n"); break; } } zmq_close (socket); zmq_ctx_destroy (context); return 0; }
tbg_socket_t *tbg_open(char *server_uri) { int ret; tbg_socket_t *tsock = g_new(tbg_socket_t, 1); tsock->timeout = TBG_DEFAULT_TIMEOUT; tsock->zsocket = zmq_socket(zcontext, ZMQ_DEALER); if (tsock->zsocket == NULL) { g_free(tsock); return NULL; } ret = zmq_connect (tsock->zsocket, server_uri); if (ret != 0) { zmq_close(tsock->zsocket); g_free(tsock); return NULL; } return tsock; }
int register_table(void *ctx, const char *address){ void *skt = zmq_socket(ctx, ZMQ_REQ); assert(skt); assert(zmq_connect(skt, address) == 0); char *str = strdup(init_str); send_msg_data(skt, str, strlen(str)+1, free_fn, NULL); int64_t more; size_t msg_size, more_size = sizeof(int64_t); void *data; recieve_msg(skt, &msg_size, &more, &more_size, &data); if (msg_size != sizeof(uint8_t)){ return -1; } memcpy(&table_number, data, sizeof(uint8_t)); free(data); if (more) flushall_msg_parts(skt); assert(zmq_close(skt) == 0); return 0; }
// REQ socket monitor thread static void req_socket_monitor (void *ctx) { zmq_event_t event; std::string ep ; int rc; void *s = zmq_socket (ctx, ZMQ_PAIR); assert (s); rc = zmq_connect (s, "inproc://monitor.req"); assert (rc == 0); while (!read_msg(s, event, ep)) { assert (ep == addr); switch (event.event) { case ZMQ_EVENT_CONNECTED: assert (event.value > 0); req_socket_events |= ZMQ_EVENT_CONNECTED; req2_socket_events |= ZMQ_EVENT_CONNECTED; break; case ZMQ_EVENT_CONNECT_DELAYED: assert (event.value != 0); req_socket_events |= ZMQ_EVENT_CONNECT_DELAYED; break; case ZMQ_EVENT_CLOSE_FAILED: assert (event.value != 0); req_socket_events |= ZMQ_EVENT_CLOSE_FAILED; break; case ZMQ_EVENT_CLOSED: assert (event.value != 0); req_socket_events |= ZMQ_EVENT_CLOSED; break; case ZMQ_EVENT_DISCONNECTED: assert (event.value != 0); req_socket_events |= ZMQ_EVENT_DISCONNECTED; break; } } zmq_close (s); }
int main (void) { setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); void *sb = zmq_socket (ctx, ZMQ_REP); assert (sb); int rc = zmq_bind (sb, "inproc://a"); assert (rc == 0); rc = zmq_unbind (sb, "inproc://a"); assert (rc == 0); rc = zmq_close (sb); assert (rc == 0); rc = zmq_ctx_term (ctx); assert (rc == 0); return 0; }
int main(int argc, char *argv[]) { q_open = r_open = 0; if (argc != 2) { printf("usage: %s <message>\n", argv[0]); exit(-1); } ctx = zmq_init(1); if (ctx < 0) { printf("could not create context\n"); exit(-1); } s = zmq_socket(ctx, ZMQ_REQ); check((long)s,"could not initialize socket"); printf("connecting...\n"); check(zmq_connect(s, "tcp://127.0.0.1:5554"), "could not connect to server"); check(zmq_msg_init_data(&query, argv[1], strlen(argv[1]), NULL, NULL), "could not initialize message"); q_open = 1; printf("sending...\n"); check(zmq_send(s, &query, 0), "could not send message\n"); printf("waiting for result...\n"); check(zmq_msg_init(&result), "could not initialize message"); r_open = 1; check(zmq_recv(s, &result, 0), "could not receive reply"); printf("got response from server: %s\n", (char *)zmq_msg_data(&result)); cleanup(); }
void Executor::work() { void *socket = zmq_socket(vcontext, ZMQ_REP); zmq_msg_t zMsg; char *cstring = NULL; size_t msgSize = 0; zmq_connect(socket, "inproc://storageWorkers"); while(vstop==false) { zmq_msg_init(&zMsg); zmq_msg_recv(&zMsg, socket, 0); msgSize = zmq_msg_size(&zMsg); cstring = new char[msgSize+1]; memcpy(cstring, zmq_msg_data(&zMsg), msgSize); zmq_msg_close(&zMsg); cstring[msgSize] = '\0'; std::string str(cstring); delete cstring; Message* msg = vmsgFactory->parse(str); Action *action = vactionFactory->parse(*msg); delete msg; msg = action->execute(); delete action; msgSize = msg->toString().size()+1; zmq_msg_init_size(&zMsg, msgSize); memcpy(zmq_msg_data(&zMsg), msg->toString().c_str(), msgSize); delete msg; zmq_msg_send(&zMsg, socket, 0); zmq_msg_close(&zMsg); } zmq_close(socket); }
bool CZMQAbstractPublishNotifier::Initialize(void *pcontext) { assert(!psocket); // check if address is being used by other publish notifier std::multimap<std::string, CZMQAbstractPublishNotifier*>::iterator i = mapPublishNotifiers.find(address); if (i==mapPublishNotifiers.end()) { psocket = zmq_socket(pcontext, ZMQ_PUB); if (!psocket) { zmqError("Failed to create socket"); return false; } int rc = zmq_bind(psocket, address.c_str()); if (rc!=0) { zmqError("Failed to bind address"); zmq_close(psocket); return false; } // register this notifier for the address, so it can be reused for other publish notifier mapPublishNotifiers.insert(std::make_pair(address, this)); return true; } else { LogPrint(BCLog::ZMQ, "zmq: Reusing socket for address %s\n", address); psocket = i->second->psocket; mapPublishNotifiers.insert(std::make_pair(address, this)); return true; } }
// worker thread function static void* worker_routine (void *context) { // socket to talk to dispatcher void* receiver = zmq_socket(context, ZMQ_REP); if (!receiver) { dzlog_error("create socket to talk to dispatcher failed, err: %s", zmq_strerror(errno)); return NULL; } if (zmq_connect(receiver, "inproc://workers") != 0) { dzlog_error("worker thread connect worker_socket failed, err: %s", zmq_strerror(errno)); return NULL; } dzlog_info("worker thread run ..."); while (1) { zmq_msg_t request; zmq_msg_init(&request); zmq_recvmsg(receiver, &request, 0); printf("Recv Hello\n"); zmq_msg_close(&request); sleep(1); zmq_msg_t reply; zmq_msg_init_size(&reply, 5); memcpy(zmq_msg_data (&reply), "World", 5); zmq_sendmsg(receiver, &reply, 0); zmq_msg_close(&reply); } zmq_close (receiver); return NULL; }
static void subscriber_thread_main (void *pvoid) { const proxy_hwm_cfg_t *cfg = (proxy_hwm_cfg_t *) pvoid; const int idx = cfg->thread_idx; void *subsocket = zmq_socket (cfg->context, ZMQ_SUB); assert (subsocket); set_hwm (subsocket); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (subsocket, ZMQ_SUBSCRIBE, 0, 0)); TEST_ASSERT_SUCCESS_ERRNO ( zmq_connect (subsocket, cfg->backend_endpoint[idx])); // Receive message_count messages uint64_t rxsuccess = 0; bool success = true; while (success) { zmq_msg_t msg; int rc = zmq_msg_init (&msg); assert (rc == 0); rc = zmq_msg_recv (&msg, subsocket, 0); if (rc != -1) { TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg)); rxsuccess++; } if (rxsuccess == message_count) break; } // Cleanup zmq_close (subsocket); printf ("subscriber thread ended\n"); }
int main (void) { fprintf (stderr, "test_router_mandatory_tipc running...\n"); void *ctx = zmq_init (1); assert (ctx); // Creating the first socket. void *sa = zmq_socket (ctx, ZMQ_ROUTER); assert (sa); int rc = zmq_bind (sa, "tipc://{15560,0,0}"); assert (rc == 0); // Sending a message to an unknown peer with the default setting rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE); assert (rc == 7); rc = zmq_send (sa, "DATA", 4, 0); assert (rc == 4); int mandatory = 1; // Set mandatory routing on socket rc = zmq_setsockopt (sa, ZMQ_ROUTER_MANDATORY, &mandatory, sizeof (mandatory)); assert (rc == 0); // Send a message and check that it fails rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE | ZMQ_DONTWAIT); assert (rc == -1 && errno == EHOSTUNREACH); rc = zmq_close (sa); assert (rc == 0); rc = zmq_term (ctx); assert (rc == 0); return 0 ; }
int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL,"Chinese"); setlocale(LC_ALL,"chs"); void *m_context; void *m_subscriber; char m_subAddr[64]; m_context = zmq_init(1); m_subscriber = zmq_socket(m_context,ZMQ_SUB); char *puberIp = "127.0.0.1"; WORD port = 8585; memset(m_subAddr,0,sizeof(m_subAddr)); sprintf_s(m_subAddr,"tcp://%s:%d",puberIp,port); zmq_connect(m_subscriber,m_subAddr); char *option = "642"; int ret = zmq_setsockopt(m_subscriber,ZMQ_SUBSCRIBE,"642",strlen(option)); while (1) { BYTE buffer[1024] = {0}; DWORD bufLen = sizeof(buffer); DWORD gotLen = zmq_recv(m_subscriber,buffer,bufLen,0); printf("收到发布信息:%s\n",buffer); } zmq_close(m_subscriber); zmq_term(m_context); return 0; }
int main (int argc, char *argv []) { fprintf (stderr, "test_router_behavior running...\n"); void *ctx = zmq_init (1); assert (ctx); // Creating the first socket. void *sa = zmq_socket (ctx, ZMQ_ROUTER); assert (sa); int rc = zmq_bind (sa, "tcp://127.0.0.1:15560"); assert (rc == 0); // Sending a message to an unknown peer with the default behavior. rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE); assert (rc == 7); rc = zmq_send (sa, "DATA", 4, 0); assert (rc == 4); int behavior = 1; // Setting the socket behavior to a new mode. rc = zmq_setsockopt (sa, ZMQ_ROUTER_BEHAVIOR, &behavior, sizeof (behavior)); assert (rc == 0); // Sending a message to an unknown peer with verbose behavior. rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE | ZMQ_DONTWAIT); assert (rc == -1 && errno == EAGAIN); rc = zmq_close (sa); assert (rc == 0); rc = zmq_term (ctx); assert (rc == 0); return 0 ; }
void ini_zmq() { void * ctx = zmq_init(1); if (!ctx) { return; } void * s = zmq_socket(ctx, ZMQ_PUSH); if (!s) { return; } s32 rc = zmq_bind(s, UI_ZMQ_NAME); if (rc) { return; } g_zmq_socket = s; g_zmq_ui_ctx = ctx; }