void lamb_show_client(int sock, int id) { /* printf("id | account | company | queue | concurrent\n"); printf("--------------------------------------------\n"); printf("%d a10001 2 25461 1000/s\n", id++); printf("%d a10002 2 2461 2000/s\n", id++); printf("%d a10003 2 5461 5000/s\n", id++); printf("%d a10004 2 15461 1000/s\n", id++); printf("%d a10005 2 461 500/s\n", id++); */ int rc; char *cmd = "show client 12"; char res[4096]; rc = nn_send(sock, cmd, strlen(cmd), 0); if (rc < 0) { fprintf(stderr, "nn_send: %s (ignoring)\n", nn_strerror(nn_errno())); return; } memset(res, 0, sizeof(res)); rc = nn_recv (sock, res, sizeof(res), NN_DONTWAIT); if (rc < 0) { fprintf(stderr, "nn_recv: %s\n", nn_strerror(nn_errno())); return; } printf("\%s", res); return; }
int device (const char *url1, const char *url2) { int s1, s2, rv; s1 = nn_socket (AF_SP_RAW, NN_REQ); if (s1 < 0) { fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ())); return (-1); } if (nn_bind (s1, url1) < 0) { fprintf (stderr, "nn_bind1(%s): %s\n", url1, nn_strerror (nn_errno ())); return (-1); } s2 = nn_socket (AF_SP_RAW, NN_REP); if (s2 < 0) { fprintf (stderr, "nn_socket: %s\n", nn_strerror(nn_errno ())); return (-1); } if (nn_bind (s2, url2) < 0) { fprintf (stderr, "nn_bind2(%s): %s\n", url2, nn_strerror (nn_errno ())); return (-1); } if (nn_device (s1, s2) != 0) { fprintf (stderr, "nn_device: %s\n", nn_strerror (nn_errno ())); return (-1); } return (0); }
int main(int argc, char *argv[]) { #ifdef USE_NETWORK ArgMapping mapping; long role = 0; std::string host = "127.0.0.1"; mapping.arg("m", gM, "m"); mapping.arg("p", gP, "p"); mapping.arg("r", gR, "r"); mapping.arg("L", gL, "L"); mapping.arg("R", role, "role, 0:server, 1:client"); mapping.arg("H", host, "host"); mapping.arg("C", gC, "cipher to send"); mapping.parse(argc, argv); if (role == 0) { int sock = nn_socket(AF_SP, NN_REP); if (nn_bind(sock, "tcp://*:12345") < 0) { printf("%s\n", nn_strerror(errno)); return -1; } printf("SID %d\n", sock); act_server(sock); } else if (role == 1){ int sock = nn_socket(AF_SP, NN_REQ); std::string h = "tcp://" + host + ":12345"; if (nn_connect(sock, h.c_str()) < 0) { printf("%s\nn", nn_strerror(errno)); return -1; } printf("SID %d\n", sock); act_client(sock); } #endif return 0; }
// send data to the grapher app void graph_thread(void* user) { Graph* g = static_cast<Graph*>(user); int sock = nn_socket(AF_SP, NN_PUSH); if(sock == -1) { printf("error: cannot create client socket.\n"); return; } int rc = nn_connect(sock, g->address.c_str()); if(rc < 0) { printf("error: cannot connect, %s\n", nn_strerror(errno)); return; } std::vector<GraphPacket*> todo; while(true) { // get the work! uv_mutex_lock(&g->mutex); { while(g->work.size() == 0) { uv_cond_wait(&g->cv, &g->mutex); } std::copy(g->work.begin(), g->work.end(), std::back_inserter(todo)); g->work.clear(); } uv_mutex_unlock(&g->mutex); // perform work bool must_stop = false; for(std::vector<GraphPacket*>::iterator it = todo.begin(); it != todo.end(); ++it) { GraphPacket* p = *it; if(p->data[0] == PKT_TYPE_STOP) { must_stop = true; break; } else { int rc = nn_send(sock, (char*)&p->data.front(), p->data.size(), 0); if(rc < 0) { printf("error: cannot send to grapher: %s\n", nn_strerror(rc)); } } delete p; p = NULL; } // for todo.clear(); if(must_stop) { break; } } printf("@todo -> cleanup socket.\n"); }
/* The client runs just once, and then returns. */ int client (const char *url, const char *username) { int fd; int rc; char *greeting; char *msg; fd = nn_socket (AF_SP, NN_REQ); if (fd < 0) { fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ())); return (-1); } if (nn_connect (fd, url) < 0) { fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ())); nn_close (fd); return (-1); } usleep(1000); if (nn_send (fd, username, strlen (username), 0) < 0) { fprintf (stderr, "nn_send: %s\n", nn_strerror (nn_errno ())); nn_close (fd); return (-1); } /* Here we ask the library to allocate response buffer for us (NN_MSG). */ rc = nn_recv (fd, &msg, NN_MSG, 0); if (rc < 0) { fprintf (stderr, "nn_recv: %s\n", nn_strerror (nn_errno ())); nn_close (fd); return (-1); } nn_close (fd); /* Response is not ASCIIZ terminated. */ greeting = calloc (rc + 1, 1); if (greeting == NULL) { fprintf (stderr, "calloc: %s\n", strerror (errno)); return (-1); } memcpy(greeting, msg, rc); nn_freemsg (msg); printf ("%s\n", greeting); free (greeting); return (0); }
/* we use nano sockets to set up a pipeline like: * * spool-reader * PUSH * | <--- "ingress" socket * PULL * json-encoder * PUSH * | <--- "egress" socket * PULL * kaf-sender */ int setup_nano(void) { int rc = -1; /* set up the ingress and egress sockets */ if ( (CF.ingress_socket_push = nn_socket(AF_SP, NN_PUSH)) < 0) goto done; if ( (CF.ingress_socket_pull = nn_socket(AF_SP, NN_PULL)) < 0) goto done; if ( (CF.egress_socket_push = nn_socket(AF_SP, NN_PUSH)) < 0) goto done; if ( (CF.egress_socket_pull = nn_socket(AF_SP, NN_PULL)) < 0) goto done; pid_t pid = getpid(); snprintf(CF.ingress_socket_nanopath,sizeof(CF.ingress_socket_nanopath), "ipc:///tmp/%d.ingress.ipc", pid); /* if you change this, change below too! */ snprintf(CF.egress_socket_nanopath, sizeof(CF.egress_socket_nanopath), "ipc:///tmp/%d.egress.ipc", pid); /* if you change this, change below too! */ /* the path without the ipc:// prefix is kept for unlinking */ CF.ingress_socket_filepath = &CF.ingress_socket_nanopath[6]; CF.egress_socket_filepath = &CF.egress_socket_nanopath[6]; if (nn_bind(CF.ingress_socket_push, CF.ingress_socket_nanopath) < 0) goto done; if (nn_connect(CF.ingress_socket_pull, CF.ingress_socket_nanopath) < 0) goto done; if (nn_bind(CF.egress_socket_push, CF.egress_socket_nanopath) < 0) goto done; if (nn_connect(CF.egress_socket_pull, CF.egress_socket_nanopath) < 0) goto done; rc = 0; done: if (rc < 0) fprintf(stderr,"nano: %s\n", nn_strerror(errno)); return rc; }
void *spr_worker(void *data) { int rc=-1, nc; void *sp=NULL, *set; size_t len; char *buf; set = kv_set_new(); sp = kv_spoolreader_new(CF.dir); if (!sp) goto done; while (kv_spool_read(sp,set,1) > 0) { if (set_to_buf(set,&buf,&len) < 0) goto done; assert(len < MAX_BUF); nc = nn_send(CF.ingress_socket_push, buf, len, 0); free(buf); if (nc < 0) { fprintf(stderr,"nn_send: %s\n", nn_strerror(errno)); goto done; } ts_add(CF.spr_msgs_ts, CF.now, NULL); if (CF.shutdown) break; } done: CF.shutdown = 1; kv_set_free(set); if (sp) kv_spoolreader_free(sp); return NULL; }
bool pnet_server_start(pnet_socket *socket, const char *address) { if (unlikely(!address)) { plog_error("pnet_server_start() нет строки адреса"); return false; } if (unlikely(!pnet_internal_create_socket(socket, NN_REP))) { return false; } socket->endpoint_id = nn_bind(socket->socket_fd, address); if (unlikely(socket->endpoint_id < 0)) { plog_error("pnet_server_start() не удалось bind() | %s (%d)", nn_strerror(nn_errno()), nn_errno()); return false; } if (unlikely(!pnet_internal_get_socketfd(socket))) { return false; } socket->started = true; return true; }
bool pnet_internal_get_socketfd(pnet_socket *socket) { size_t sd_size = sizeof(int); if (unlikely(nn_getsockopt(socket->socket_fd, NN_SOL_SOCKET, NN_RCVFD, &socket->recv_fd, &sd_size))) { plog_error("pnet_server_start() не удалось получить NN_RCVFD | %s (%d)", nn_strerror(nn_errno()), nn_errno()); return false; } if (unlikely(nn_getsockopt(socket->socket_fd, NN_SOL_SOCKET, NN_SNDFD, &socket->send_fd, &sd_size))) { plog_error("pnet_server_start() не удалось получить NN_SNDFD | %s (%d)", nn_strerror(nn_errno()), nn_errno()); return false; } return true; }
void act_client(std::string &addr, size_t sze) { int sock = nn_socket(AF_SP, NN_REQ); std::string host = "tcp://" + addr + ":12345"; printf("host: %s\n", host.c_str()); if (nn_connect(sock, host.c_str()) < 0) { printf("%s\nn", nn_strerror(errno)); return; } std::vector<size_t> lens; make_package(sze, lens); std::vector<void *> data; for (auto len : lens) data.push_back((void *)new char[len]); MDL::Timer timer; nn_send(sock, NULL, 0, 0); nn_recv(sock, NULL, 0, 0); timer.start(); MDL::net::send_all(sock, data, lens); timer.end(); printf("send %f\n", timer.second()); nn_close(sock); for (auto p : data) { char *pp = (char *)p; delete []pp; } }
flux_cli_t * flux_cli_init(const char * broker_url, int timeout, int verbose) { flux_cli_t * client; if(verbose) printf("Client starting on %s...\n", broker_url); client = malloc(sizeof(flux_cli_t)); if(!client) return NULL; memset(client, 0, sizeof(flux_cli_t)); client->verbose = verbose; client->timeout = timeout; client->n_servers = 0; client->servers = NULL; // Bind to broker SURVEYOR socket client->broker_sock = nn_socket(AF_SP, NN_REQ); assert(client->broker_sock >= 0); assert(nn_setsockopt(client->broker_sock, NN_SOL_SOCKET, NN_RCVTIMEO, &client->timeout, sizeof(int)) >= 0); assert(nn_setsockopt(client->broker_sock, NN_SOL_SOCKET, NN_SNDTIMEO, &client->timeout, sizeof(int)) >= 0); if(nn_connect(client->broker_sock, broker_url) < 0) { printf("Unable to init flux client: %s\n", nn_strerror(errno)); return NULL; } return client; }
int ftw_nanomsg_error(LStrHandle error_message) { const char *human_readable_error; int current_error; current_error = errno; /* The FTW library will explicitly handle a few errors, since "socket" could be a less misleading term than "file descriptors". */ switch (current_error) { case EBADF: human_readable_error = "Invalid socket"; break; case EMFILE: human_readable_error = "Too many open sockets"; break; default: human_readable_error = nn_strerror(current_error); break; } ftw_support_CStr_to_LStrHandle(&error_message, human_readable_error, 1024); return current_error; }
/* encode a tpl (kv frame) as json. */ void *enc_worker(void *thread_id) { char buf[MAX_BUF], *key, *val; json_t *o = json_object(); int rc=-1, len, nc; tpl_node *tn; while (CF.shutdown == 0) { len = nn_recv(CF.ingress_socket_pull, buf, MAX_BUF, 0); if (len < 0) { fprintf(stderr,"nn_recv: %s\n", nn_strerror(errno)); goto done; } /* decode, then re-encode as json */ json_object_clear(o); tn = tpl_map("A(ss)",&key,&val); assert(tn); if (tpl_load(tn,TPL_MEM,buf,len) < 0) goto done; while(tpl_unpack(tn,1) > 0) { json_t *jval = json_string(val); json_object_set_new(o, key, jval); free(key); key=NULL; free(val); val=NULL; } tpl_free(tn); /* dump the json object, then newline-terminate it. */ if (CF.verbose>1) json_dumpf(o, stderr, JSON_INDENT(1)); char *dump = json_dumps(o, JSON_INDENT(0)); size_t dump_len = strlen(dump); /* give the buffer to nano, from here it goes to kaf thread */ nc = nn_send(CF.egress_socket_push, dump, dump_len, 0); free(dump); if (nc < 0) { fprintf(stderr,"nn_send: %s\n", nn_strerror(errno)); goto done; } } rc = 0; done: CF.shutdown = 1; json_decref(o); return NULL; }
void send_pk(int socket, const FHEPubKey &pk) { std::stringstream sstream; sstream << pk; auto str = sstream.str(); if (nn_send(socket, str.c_str(), str.size(), 0) < 0) { printf("send pk error %s\n", nn_strerror(errno)); exit(-1); } }
static void apply_socket_config(int sock, struct cfg_a_nanomsg_addr *addr) { int rc; for(; addr; addr = addr->next) { if(addr->val.tag == CFG_NANOMSG_ADDR_BIND) { rc = nn_bind(sock, addr->val.val); if(rc < 0) { fprintf(stderr, "Error in nn_bind: %s", nn_strerror(errno)); exit(2); } } else if(addr->val.tag == CFG_NANOMSG_ADDR_CONNECT) { rc = nn_connect(sock, addr->val.val); if(rc < 0) { fprintf(stderr, "Error in nn_connect: %s", nn_strerror(errno)); exit(2); } } } }
static void nn_assert_errno (int flag, char *description) { int err; if (!flag) { err = errno; fprintf (stderr, "%s: %s\n", description, nn_strerror (err)); exit (3); } }
void receive_pk(int socket, FHEPubKey &pk) { char *buf; int read; if ((read = nn_recv(socket, &buf, NN_MSG, 0)) < 0) { printf("receive pk error %s\n", nn_strerror(errno)); exit(-1); } std::stringstream sstream; sstream.str(buf); sstream >> pk; nn_freemsg(buf); }
int lamb_sock_connect(int *sock, char *host, int port) { int fd; int err; char url[128]; fd = nn_socket(AF_SP, NN_REQ); if (fd < 0) { fprintf(stderr, "socket: %s\n", nn_strerror(nn_errno())); return -1; } sprintf(url, "tcp://%s:%d", host, port); err = nn_connect(fd, url); if (err < 0) { fprintf(stderr, "socket: %s\n", nn_strerror(nn_errno())); return -1; } *sock = fd; return 0; }
static int32_t init_pluginsocks(struct plugin_info *plugin,int32_t permanentflag,uint64_t instanceid,uint64_t daemonid,int32_t timeout) { //#ifdef _WIN32 // sprintf(plugin->connectaddr,"tcp://127.0.0.1:7774"); //#endif if ( (plugin->pushsock= nn_socket(AF_SP,NN_PUSH)) < 0 ) { printf("error creating plugin->pushsock %s\n",nn_strerror(nn_errno())); return(-1); } else if ( nn_settimeouts2(plugin->pushsock,10,1) < 0 ) { printf("error setting plugin->pushsock timeouts %s\n",nn_strerror(nn_errno())); return(-1); } else if ( nn_connect(plugin->pushsock,plugin->connectaddr) < 0 ) { printf("error connecting plugin->pushsock.%d to %s %s\n",plugin->pushsock,plugin->connectaddr,nn_strerror(nn_errno())); return(-1); } if ( (plugin->pullsock= nn_socket(AF_SP,NN_BUS)) < 0 ) { printf("error creating plugin->pullsock %s\n",nn_strerror(nn_errno())); return(-1); } else if ( nn_settimeouts2(plugin->pullsock,10,1) < 0 ) { printf("error setting plugin->pullsock timeouts %s\n",nn_strerror(nn_errno())); return(-1); } else if ( nn_bind(plugin->pullsock,plugin->bindaddr) < 0 ) { printf("error connecting plugin->pullsock.%d to %s %s\n",plugin->pullsock,plugin->bindaddr,nn_strerror(nn_errno())); return(-1); } printf("%s bind.(%s) %d and connected.(%s) %d\n",plugin->name,plugin->bindaddr,plugin->pullsock,plugin->connectaddr,plugin->pushsock); return(0); }
JNIEXPORT jstring JNICALL Java_org_nanomsg_NanoLibrary_nn_1strerror(JNIEnv* env, jobject obj, jint errnum) { const char* cerr = 0; jstring jerr = 0; cerr = nn_strerror(errnum); if (cerr == 0) cerr = ""; jerr = (*env)->NewStringUTF(env, cerr); NANO_ASSERT(jerr); return jerr; }
void act_server(size_t sze) { int sock = nn_socket(AF_SP, NN_REP); std::string host = "tcp://*:12345"; if (nn_bind(sock, host.c_str()) < 0) { printf("Error: %s\n", nn_strerror(errno)); return; } nn_recv(sock, NULL, 0, 0); nn_send(sock, NULL, 0, 0); std::vector<size_t> lens; make_package(sze, lens); MDL::Timer timer; timer.start(); MDL::net::receive_all(sock, lens); timer.end(); printf("receive %f\n", timer.second()); nn_close(sock); }
/* The server runs forever. */ int server(const char *url) { int fd; /* Create the socket. */ fd = nn_socket (AF_SP, NN_REP); if (fd < 0) { fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ())); return (-1); } /* Connect to the URL. This will connect to the address and listen synchronously; new clients will be accepted asynchronously without further action from the calling program. */ if (nn_connect (fd, url) < 0) { fprintf (stderr, "nn_connect: %s\n", nn_strerror (nn_errno ())); nn_close (fd); return (-1); } /* Now we can just process results. Note that there is no explicit accept required. We just receive a request, and reply to it. Its important to note that we must not issue two receives in a row without replying first, or the following receive(s) will cancel any unreplied requests. */ for (;;) { char username[128]; char greeting[128]; time_t secs; struct tm *now; char *daytime; int rc; char *fmt; rc = nn_recv (fd, username, sizeof (username), 0); if (rc < 0) { /* Any error here is unexpected. */ fprintf (stderr, "nn_recv: %s\n", nn_strerror (nn_errno ())); break; } secs = time (NULL); now = localtime (&secs); if (now->tm_hour < 12) { daytime = "morning"; } else if (now->tm_hour < 17) { daytime = "afternoon"; } else if (now->tm_hour < 20) { daytime = "evening"; } else { daytime = "night"; } /* Ensure ASCIIZ terminated string. */ if (rc < sizeof (username)) { username[rc] = '\0'; } else { username[sizeof (username) - 1] = '\0'; } fmt = "Good %s, %s (from %d)."; /* Technically this might be overly pessimistic about size. */ if ((strlen (username) + strlen (daytime) + strlen (fmt)) >= sizeof (greeting)) { fmt = "I'm sorry, your name is too long. But good %s anyway."; } /* snprintf would be safer, but the above check protects us. */ sprintf (greeting, fmt, daytime, username, (int)getpid()); rc = nn_send (fd, greeting, strlen (greeting), 0); if (rc < 0) { /* There are several legitimate reasons this can fail. We note them for debugging purposes, but then ignore otherwise. If the socket is closed or failing, we will notice in recv above, and exit then. */ fprintf (stderr, "nn_send: %s (ignoring)\n", nn_strerror (nn_errno ())); } } nn_close (fd); return (-1); }
int flux_cli_id_list(flux_cli_t * client, flux_id_t ** ids) { assert(ids); assert(*ids == NULL); int id_size = nn_send(client->broker_sock, "ID", 2, 0); if(id_size != 2) { if(client->verbose) printf("Unable to list ids: unable to send to broker: %s\n", nn_strerror(errno)); return -1; } char * resp = NULL; int resp_size = nn_recv(client->broker_sock, &resp, NN_MSG, 0); if(resp_size < 0) { if(client->verbose) printf("Unable to list ids: invalid broker response: %s\n", nn_strerror(errno)); return -1; } if(servers_string) free(servers_string); servers_string = malloc(resp_size); assert(servers_string); memcpy(servers_string, resp, resp_size); nn_freemsg(resp); // Split lines & tokenize. Each newline is a server // Tokenize string in form "URL\tIDS\n"; replace '\t' with '\0' // XXX: not quite thread safe int n = 0; for(int i = 0; i < resp_size; i++) { if(servers_string[i] == '\n') n++; } struct flux_server * servers = NULL; if(n > 0) { if(servers_string[resp_size-1] != '\n') { if(client->verbose) printf("Warning while listing ids: invalid broker response.\n"); return -1; } servers = malloc(n * sizeof(struct flux_server)); assert(servers); char * sptr = servers_string; for(int i = 0; i < n; i++) { servers[i].rep_url = sptr; int j = 0; while(sptr[j] != '\t' && sptr[j] != '\n') j++; if(sptr[j] != '\t') { if(client->verbose) printf("Warning while listing ids: invalid broker response.\n"); if(client->verbose) printf("Msg: %s; %s\n", servers_string, sptr); free(servers); return -1; } sptr += j; *(sptr++) = '\0'; servers[i].ids = (flux_id_t *) sptr; for(j = 0; sptr[j] != '\n'; j++); servers[i].n_ids = j / sizeof(flux_id_t); //assert(servers[i].n_ids * sizeof(flux_id_t) == (size_t) j); sptr += j; *(sptr++) = '\0'; servers[i].sock = nn_socket(AF_SP, NN_REQ); assert(servers[i].sock >= 0); assert(nn_setsockopt(servers[i].sock, NN_SOL_SOCKET, NN_RCVTIMEO, &client->timeout, sizeof(int)) >= 0); assert(nn_setsockopt(servers[i].sock, NN_SOL_SOCKET, NN_SNDTIMEO, &client->timeout, sizeof(int)) >= 0); if(nn_connect(servers[i].sock, servers[i].rep_url) >= 0) { if(client->verbose) printf("Connected to server: %s\n", servers[i].rep_url); } else { if(client->verbose) printf("Unable to connect to server %s: %s\n", servers[i].rep_url, nn_strerror(errno)); assert(nn_close(servers[i].sock) == 0); n--; i--; } } } for(size_t i = 0; i < client->n_servers; i++) { assert(nn_close(client->servers[i].sock) == 0); } client->n_servers = n; if(client->servers) free(client->servers); client->servers = servers; // Count the total number of ids size_t n_ids = 0; for(size_t i = 0; i < client->n_servers; i++) { n_ids += client->servers[i].n_ids; } // Allocate a buffer to id array *ids = malloc(n_ids * sizeof(flux_id_t)); assert(*ids); // Concatenate ids char * iptr = (char *) *ids; for(size_t i = 0; i < client->n_servers; i++) { memcpy(iptr, client->servers[i].ids, client->servers[i].n_ids * sizeof(flux_id_t)); iptr += sizeof(flux_id_t); } return n_ids; }
const char *zmq_strerror (int errnum) { return nn_strerror (errnum); }
static int32_t nn_local_broadcast2(int32_t sock,uint64_t instanceid,int32_t flags,uint8_t *retstr,int32_t len) { int32_t i,sendlen,errs = 0; if ( sock >= 0 ) { for (i=0; i<10; i++) if ( (nn_socket_status2(sock,1) & NN_POLLOUT) != 0 ) break; if ( (sendlen= nn_send(sock,(char *)retstr,len,0)) <= 0 ) errs++, printf("sending to socket.%d sendlen.%d len.%d (%s) [%s]\n",sock,sendlen,len,nn_strerror(nn_errno()),retstr); //else if ( Debuglevel > 2 ) // printf("nn_local_broadcast SENT.(%s) len.%d sendlen.%d vs strlen.%ld instanceid.%llu -> sock.%d\n",retstr,len,sendlen,strlen((char *)retstr),(long long)instanceid,sock); } return(errs); }
static void nn_global_submit_errors (int i, struct nn_sock *s, char *name, int value) { /* TODO(tailhook) dynamically allocate buffer */ char buf[4096]; char *curbuf; int buf_left; char timebuf[20]; time_t numtime; struct tm strtime; int len; struct nn_list_item *it; struct nn_ep *ep; if (self.statistics_socket >= 0) { /* TODO(tailhook) add HAVE_GMTIME_R ifdef */ time(&numtime); #ifdef NN_HAVE_GMTIME_R gmtime_r (&numtime, &strtime); #else #error #endif strftime (timebuf, 20, "%Y-%m-%dT%H:%M:%S", &strtime); if(*s->socket_name) { len = sprintf (buf, "ESTP:%s:%s:socket.%s:%s: %sZ 10 %d\n", self.hostname, self.appname, s->socket_name, name, timebuf, value); } else { len = sprintf (buf, "ESTP:%s:%s:socket.%d:%s: %sZ 10 %d\n", self.hostname, self.appname, i, name, timebuf, value); } buf_left = sizeof(buf) - len; curbuf = buf + len; for (it = nn_list_begin (&s->eps); it != nn_list_end (&s->eps); it = nn_list_next (&s->eps, it)) { ep = nn_cont (it, struct nn_ep, item); if (ep->last_errno) { #ifdef NN_HAVE_WINDOWS len = _snprintf_s (curbuf, buf_left, _TRUNCATE, " nanomsg: Endpoint %d [%s] error: %s\n", ep->eid, nn_ep_getaddr (ep), nn_strerror (ep->last_errno)); #else len = snprintf (curbuf, buf_left, " nanomsg: Endpoint %d [%s] error: %s\n", ep->eid, nn_ep_getaddr (ep), nn_strerror (ep->last_errno)); #endif if (buf_left < len) break; curbuf += len; buf_left -= len; } } (void) nn_send (self.statistics_socket, buf, sizeof(buf) - buf_left, NN_DONTWAIT); }
static int nn_usock_recv_raw(struct nn_usock *self, void *buf, size_t *len) { int usebuf = 0; size_t sz; size_t length; ssize_t nbytes; struct iovec iov; struct msghdr hdr; unsigned char ctrl [256]; length = *len; /* If batch buffer doesn't exist, allocate it. The point of delayed deallocation to allow non-receiving sockets, such as TCP listening sockets, to do without the batch buffer. */ if (nn_slow (!self->in.batch)) { self->in.batch = nn_alloc (NN_USOCK_BATCH_SIZE, "AIO batch buffer"); alloc_assert (self->in.batch); } /* Try to satisfy the recv request by data from the batch buffer. */ sz = self->in.batch_len - self->in.batch_pos; if (sz) { if (sz > length) sz = length; memcpy (buf, self->in.batch + self->in.batch_pos, sz); self->in.batch_pos += sz; buf = ((char*) buf) + sz; length -= sz; if (!length) return 0; } #ifdef NN_USE_MYMSG usebuf = (length >= NN_USOCK_BATCH_SIZE); #else usebuf = (length >= NN_USOCK_BATCH_SIZE); #endif // If recv request is greater than the batch buffer, get the data directly into the place. Otherwise, read data to the batch buffer if ( usebuf != 0 ) { iov.iov_base = buf; iov.iov_len = length; } else { iov.iov_base = self->in.batch; iov.iov_len = NN_USOCK_BATCH_SIZE; } memset(&hdr,0,sizeof(hdr)); hdr.msg_iov = &iov; hdr.msg_iovlen = 1; #if defined NN_HAVE_MSG_CONTROL hdr.msg_control = ctrl; hdr.msg_controllen = sizeof(ctrl); #else *((int*) ctrl) = -1; hdr.msg_accrights = ctrl; hdr.msg_accrightslen = sizeof(int); #endif #if NN_USE_MYMSG nbytes = myrecvmsg(self->s,&hdr,0,(int32_t)iov.iov_len); //printf("got nbytes.%d from recvmsg errno.%d %s\n",(int32_t)nbytes,errno,nn_strerror(errno)); #else nbytes = recvmsg (self->s, &hdr, 0); #endif if ( nn_slow(nbytes <= 0) ) { if ( nn_slow(nbytes == 0) ) return -ECONNRESET; if ( nn_fast(errno == EAGAIN || errno == EWOULDBLOCK) ) // Zero bytes received nbytes = 0; else { printf("recvraw errno.%d %s\n",errno,nn_strerror(errno)); // If the peer closes the connection, return ECONNRESET errno_assert(errno == ECONNRESET || errno == ENOTCONN || errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH #if NN_USE_MYMSG // || errno == EADDRINUSE || errno == EINPROGRESS #endif ); return -ECONNRESET; } } else if ( hdr.msg_controllen > 0 ) nn_process_cmsg(self,&hdr); //PNACL_message("nbytes.%d length.%d *len %d\n",(int)nbytes,(int)length,(int)*len); // If the data were received directly into the place we can return straight away if ( usebuf != 0 ) { length -= nbytes; *len -= length; return 0; } // New data were read to the batch buffer. Copy the requested amount of it to the user-supplied buffer self->in.batch_len = nbytes; self->in.batch_pos = 0; if (nbytes) { sz = nbytes > (ssize_t)length ? length : (size_t)nbytes; memcpy (buf, self->in.batch, sz); length -= sz; self->in.batch_pos += sz; } *len -= length; return 0; }
void* run_test(void *threadid) { long tid = (long)threadid; for (int cycle = 0; cycle < CYCLES; ++cycle) { GHashTable* sockets = g_hash_table_new(g_direct_hash, g_direct_equal); int epfd = epoll_create(MAX_CONNECTIONS); if (epfd < 1) { printf("can't create epoll\n"); if (sockets) { g_hash_table_destroy(sockets); sockets = NULL; } return NULL; } struct epoll_event ev, ev_read, events[MAX_CONNECTIONS]; memset(&ev, 0x0, sizeof(struct epoll_event)); memset(&ev_read, 0x0, sizeof(struct epoll_event)); memset(events, 0x0, sizeof(struct epoll_event) * MAX_CONNECTIONS); ev.events = EPOLLONESHOT | EPOLLOUT | EPOLLRDHUP; ev_read.events = EPOLLIN | EPOLLRDHUP; //unsigned int sends = 0; int sdIN[MAX_CONNECTIONS] = { 0 }; int sdOUT[MAX_CONNECTIONS] = { 0 }; int requester[MAX_CONNECTIONS] = { 0 }; for (int i = 0; i < MAX_CONNECTIONS; i++) { requester[i] = nn_socket(AF_SP, NN_REQ); if(requester[i] < 0) { flockfile(stdout); printf("nn_socket() %s (%d)\n", nn_strerror(nn_errno()), nn_errno()); funlockfile(stdout); exit(1); } int val = 1; nn_setsockopt(requester[i], NN_TCP, NN_TCP_NODELAY, &val, sizeof(val)); int ret = nn_connect(requester[i], "tcp://10.2.142.102:12345"); //int ret = nn_connect(requester[i], "tcp://127.0.0.1:12345"); if (ret < 0) { flockfile(stdout); printf("nn_connect() %s (%d)\n", nn_strerror(nn_errno()), nn_errno()); funlockfile(stdout); } size_t sd_size = sizeof(int); nn_getsockopt(requester[i], NN_SOL_SOCKET, NN_SNDFD, &sdOUT[i], &sd_size); nn_getsockopt(requester[i], NN_SOL_SOCKET, NN_RCVFD, &sdIN[i], &sd_size); //printf("fd = %d | %d\n", sdOUT[i], sdIN[i]); if (requester[i] >= 0) { g_hash_table_insert(sockets, GINT_TO_POINTER(i), GINT_TO_POINTER(0)); ev.data.fd = sdOUT[i]; if (epoll_ctl(epfd, EPOLL_CTL_ADD, sdOUT[i], &ev)) { printf("can't epoll_ctl\n"); if (sockets) { g_hash_table_destroy(sockets); sockets = NULL; } nn_close(requester[i]); return NULL; } ev_read.data.fd = sdIN[i]; if (epoll_ctl(epfd, EPOLL_CTL_ADD, sdIN[i], &ev_read)) { printf("can't epoll_ctl\n"); if (sockets) { g_hash_table_destroy(sockets); sockets = NULL; } nn_close(requester[i]); return NULL; } } } while (1) { int rv = epoll_wait(epfd, events, MAX_CONNECTIONS, 5000); if (rv == 0) { break; } else if (rv < 0) { flockfile(stdout); printf("#%ld | can't epoll_wait: %s (%d)\n", tid, strerror(errno), errno); funlockfile(stdout); break; } int rv_ch = 0; for (int epoll_event = 0; epoll_event < MAX_CONNECTIONS ; epoll_event++) { if (rv_ch++ == rv) { break; } if (events[epoll_event].events & EPOLLIN) { int num = -1; for (int i = 0; i < MAX_CONNECTIONS; i++) { if (events[epoll_event].data.fd == sdIN[i]) { num = i; break; } } if (num == -1) { //nn_term(); flockfile(stdout); printf("IN | Can`t find socket\n"); funlockfile(stdout); exit(1); } char buffer[BUFFER_READ]; if (nn_recv(requester[num], buffer, BUFFER_READ, NN_DONTWAIT) < 0) { if (nn_errno() == EAGAIN || nn_errno() == EFSM) { continue; } flockfile(stdout); printf("#%ld | can't recv on socket #%d %s (%d)\n", tid, requester[num], nn_strerror(nn_errno()), nn_errno()); funlockfile(stdout); exit(1); } else { int a = GPOINTER_TO_INT(g_hash_table_lookup(sockets, GINT_TO_POINTER(num))); if (a < MESSAGES) { ev.data.fd = sdOUT[num]; if (epoll_ctl(epfd, EPOLL_CTL_MOD, sdOUT[num], &ev)) { printf("can't epoll_ctl\n"); if (sockets) { g_hash_table_destroy(sockets); sockets = NULL; } nn_close(requester[num]); return NULL; } } ++reads_counter; //printf("recv %d\n", events[epoll_event].data.fd); } } if (events[epoll_event].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { ++discon_counter; flockfile(stdout); printf("#%ld | Debug: Close conn: %d - 0x%04x\n", tid, events[epoll_event].data.fd, events[epoll_event].events); funlockfile(stdout); if (epoll_ctl(epfd, EPOLL_CTL_DEL, events[epoll_event].data.fd, NULL)) { flockfile(stdout); printf("#%ld | Couldn't epoll_ctl2, fd=%d, error: %s (%d)\n", tid, events[epoll_event].data.fd, strerror(errno), errno); funlockfile(stdout); } //shutdown(events[epoll_event].data.fd, SHUT_RDWR); continue; } if (events[epoll_event].events & EPOLLOUT) { int num = -1; for (int i = 0; i < MAX_CONNECTIONS; i++) { if (events[epoll_event].data.fd == sdOUT[i]) { num = i; break; } } if (num == -1) { //nn_term(); flockfile(stdout); printf("OUT | Can`t find socket\n"); funlockfile(stdout); exit(1); } msgpack_sbuffer sbuf; msgpack_sbuffer_init(&sbuf); msgpack_packer pck; msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write); msgpack_pack_raw(&pck, 5); msgpack_pack_raw_body(&pck, "Hello", 10); int size = sbuf.size; char *buf = malloc(sbuf.size); memcpy(buf, sbuf.data, sbuf.size); msgpack_sbuffer_destroy(&sbuf); if (nn_send(requester[num], buf, size, NN_DONTWAIT) < 0) { if (nn_errno() == EAGAIN) { continue; } ++discon_counter; flockfile(stdout); printf("#%ld | can't send on socket #%d %s (%d)\n", tid, num, nn_strerror(nn_errno()), nn_errno()); funlockfile(stdout); if (epoll_ctl(epfd, EPOLL_CTL_DEL, events[epoll_event].data.fd, NULL)) { flockfile(stdout); printf("#%ld | Couldn't epoll_ctl4, fd=%d, error: %s (%d)\n", tid, events[epoll_event].data.fd, strerror(errno), errno); funlockfile(stdout); exit(1); } nn_close(requester[num]); free(buf); break; } else { //printf("send %d\n", events[epoll_event].data.fd); int a = GPOINTER_TO_INT(g_hash_table_lookup(sockets, GINT_TO_POINTER(num))); g_hash_table_insert(sockets, GINT_TO_POINTER(num), GINT_TO_POINTER(++a)); update(1); ++sends_counter; free(buf); } } } fflush(stdout); } for (int i = 0; i < MAX_CONNECTIONS; i++) { if (GPOINTER_TO_INT(g_hash_table_lookup(sockets, GINT_TO_POINTER(i))) == 0) { ++sockets_wo_send_counter; } nn_close(requester[i]); } if (sockets) { g_hash_table_foreach(sockets, find_min_max, NULL); g_hash_table_destroy(sockets); } } return NULL; }
static void proxy_manage_event (proxy_t *p, uint32_t events) { struct sockaddr_in6 from, to; socklen_t slen; int rc, opt; (void) events; ASSERT(!(p->flags & FLAG_LISTED)); ASSERT(!(events & EPOLLOUT)); ASSERT(!(events & (EPOLLHUP|EPOLLERR))); if (!p->events) return; tunnel_t *t = tunnel_reserve(p); retry: slen = sizeof(from); t->front.sock = accept4(p->sock_front, SA(&from), &slen, SOCK_NONBLOCK|SOCK_CLOEXEC); if (t->front.sock < 0) { if (errno == EINTR) goto retry; ASSERT (errno == EAGAIN); tunnel_release(t); return proxy_register(p); } // The proxy front socket is maybe still active. Then instead of // systematically sending the proxy in ACTIVE, check if the limit // has been reached. It it is, re-monitor for only errors. if (p->pipes.max == ++(p->pipes.count)) proxy_pause(p); else proxy_resume(p); // Poll a backend void *buf = NULL; rc = nn_recv(p->nn_feed, &buf, NN_MSG, NN_DONTWAIT); if (rc < 0) { // TODO better manage the backend's starvation (e.g. retry) return tunnel_abort(t, "backend starvation: (%d) %s", nn_errno(), nn_strerror(nn_errno())); } else if (rc > 128) { nn_freemsg(buf); return tunnel_abort(t, "invalid backend: %s", "URL too big"); } else { char *sto = alloca(rc + 1); memcpy(sto, buf, rc); sto[rc] = 0; rc = sockaddr_init(SA(&to), sto); nn_freemsg(buf); if (!rc) return tunnel_abort(t, "invalid backend: %s", "bad URL"); char sfrom[64]; sockaddr_dump(SA(&from), sfrom, sizeof(sfrom)); ACCESS("%s -> %s", sfrom, sto); } // Connect to the polled backend t->back.sock = socket(SAFAM(&to), SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); if (t->back.sock < 0) return tunnel_abort(t, "socket() error: (%d) %s", errno, strerror(errno)); slen = sizeof(struct sockaddr_in6); rc = connect(t->back.sock, SA(&to), slen); if (0 > rc && errno != EINPROGRESS) return tunnel_abort(t, "connect() error: (%d) %s", errno, strerror(errno)); // Tweak the socket options opt = PIPE_SIZE/2; setsockopt(t->front.sock, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); setsockopt(t->back.sock, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); opt = PIPE_SIZE; setsockopt(t->front.sock, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)); setsockopt(t->back.sock, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)); errno = 0; tunnel_register(t); }
int32_t crypto777_peer(struct crypto777_node *nn,struct crypto777_node *peer) { struct crypto777_peer *ptr; int32_t err=0,err2=0,timeout = 1;//,insock; struct crypto777_transport *transport = &nn->transport; if ( nn->numpeers < MAXPEERS ) { ptr = &transport->peers[transport->numpeers++]; printf("%p peer.(%s).%d\n",peer->ip_port,peer->ip_port,peer->transport.port); strcpy(ptr->type,peer->transport.type), strcpy(ptr->ip_port,peer->ip_port), ptr->port = peer->transport.port; //if ( nn_connect(transport->bus,peer->ip_port) < 0 ) // printf("error %d nn_connect.%d bus (%s) | %s\n",err,transport->bus,peer->ip_port,nn_strerror(nn_errno())), getchar(); if ( (ptr->subsock= nn_socket(AF_SP,NN_SUB)) < 0 || (err= nn_setsockopt(ptr->subsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0)) < 0 || (err2= nn_connect(ptr->subsock,ptr->ip_port)) < 0 ) { printf("error %d too many subs.%d nodeid.%d peerid.%d err.%s err.%d err2.%d ip_port.(%s)\n",ptr->subsock,nn->numpeers,nn->nodeid,peer->nodeid,nn_strerror(nn_errno()),err,err2,ptr->ip_port); if ( ptr->subsock >= 0 ) nn_shutdown(ptr->subsock,0); return(-1); } if ( nn_setsockopt(ptr->subsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)) < 0 ) printf("error setting recv timeout to %d\n",timeout); /*if ( (ptr->pairsock= nn_socket(AF_SP,NN_PAIR)) < 0 ) printf("error %d nn_socket err.%s\n",ptr->pairsock,nn_strerror(nn_errno())); else if ( (err= nn_bind(ptr->pairsock,transport->ip_port)) < 0 ) printf("error %d nn_bind.%d (%s) | %s\n",err,ptr->pairsock,transport->ip_port,nn_strerror(nn_errno())); else if ( nn_connect(ptr->pairsock,peer->ip_port) < 0 ) printf("error %d nn_connect.%d pairsock (%s) | %s\n",err,ptr->pairsock,peer->ip_port,nn_strerror(nn_errno())); if ( nn_setsockopt(ptr->pairsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)) < 0 ) printf("error setting recv timeout to %d\n",timeout); if ( (insock= nn_socket(AF_SP,NN_PAIR)) < 0 ) printf("error %d nn_socket err.%s\n",insock,nn_strerror(nn_errno())); else if ( (err= nn_bind(insock,peer->ip_port)) < 0 ) printf("error %d nn_bind.%d (%s) | %s\n",err,insock,peer->ip_port,nn_strerror(nn_errno())); else if ( nn_connect(insock,nn->ip_port) < 0 ) printf("error %d nn_connect.%d insock (%s) | %s\n",err,insock,nn->ip_port,nn_strerror(nn_errno())); else peer->transport.insocks[peer->transport.numpairs++] = insock;*/ set_bloombits(&nn->blooms[0],peer->nxt64bits); nn->peers[nn->numpeers++] = peer; return(nn->numpeers); } else return(-1); }