static DWORD WINAPI #else static void* #endif einode_thread(void* num) { int n = (int)num; ei_cnode ec; char myname[100], destname[100]; int r, fd; sprintf(myname, "ei%d", n); sprintf(destname, "mth%d", n); printf("thread %d (%s %s) connecting\n", n, myname, destname); r = ei_connect_init(&ec, myname, cookie, 0); fd = ei_connect(&ec, (char*)desthost); if (r == 0 && fd >= 0) { ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_string(&x, myname); ei_reg_send(&ec, fd, destname, x.buff, x.index); ei_x_free(&x); //SleepEx(100); shutdown(fd, SD_SEND); closesocket(fd); } else { printf("coudn't connect fd %d r %d\n", fd, r); // DebugBreak(); } printf("done thread %d\n", n); return 0; }
void Client::sendMessage(QByteArray procName, QVariant value) { ei_x_buff x; ei_x_new_with_version(&x); encode(value, &x); ei_reg_send(m_ec, m_fd, procName.data(), x.buff, x.index); ei_x_free(&x); }
void Client::sendPid(QByteArray procName) { ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_pid(&x, &(m_ec->self)); ei_reg_send(m_ec, m_fd, procName.data(), x.buff, x.index); ei_x_free(&x); }
void Client::sendAtom(QByteArray procName, QByteArray atom) { ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_atom(&x, atom.data()); ei_reg_send(m_ec, m_fd, procName.data(), x.buff, x.index); ei_x_free(&x); }
/* * Send an Erlang message to a registered process * at the Erlang node, connected with a socket. */ int erl_reg_send(int fd, char *server_name, ETERM *msg) { ei_x_buff x; int r; ei_x_new_with_version(&x); if (ei_x_encode_term(&x, msg) < 0) { erl_errno = EINVAL; r = 0; } else { r = ei_reg_send(&erl_if_ec, fd, server_name, x.buff, x.index); } ei_x_free(&x); return r == 0; }
/* tests de tipos simples */ int test_performance_atom(char *atom, int times) { ei_x_buff output; ei_x_buff input; erlang_msg msg; int version; int ei_res; int index; char decoded_atom[MAXATOMLEN]; // Inicializa buffers ei_x_new_with_version(&output); ei_x_new(&input); // Codifica ei_x_encode_tuple_header(&output, 2); ei_x_encode_pid(&output, &local_pid); ei_x_encode_atom(&output, atom); for (int i = 0; i<times; i++) { if (ei_reg_send(&ec, connection_fd, REMOTE_SERVER_NAME, output.buff, output.index) < 0) { return 1; } do { ei_res = ei_xreceive_msg(connection_fd, &msg, &input); } while(ei_res == ERL_TICK); if (ei_res == ERL_ERROR) { return -1; } index = 0; if (ei_decode_version(input.buff, &index, &version) < 0) { std::cout << "failed decoding version \n"; return 1; } if (ei_decode_atom(input.buff, &index, decoded_atom) < 0) { std::cout << "failed decoding atom \n"; return 1; } } return 0; }
static void cmd_ei_reg_send(char* buf, int len) { int index = 0; long fd; char reg_name[MAXATOMLEN]; erlang_pid pid; ei_x_buff x; if (ei_decode_long(buf, &index, &fd) < 0) fail("expected long (fd)"); if (ei_decode_atom(buf, &index, reg_name) < 0) fail("expected atom (reg name)"); if (ei_x_new_with_version(&x) < 0) fail("ei_x_new_with_version"); if (ei_x_append_buf(&x, &buf[index], len - index) < 0) fail("append"); send_errno_result(ei_reg_send(&ec, fd, reg_name, x.buff, x.index)); ei_x_free(&x); }
int ei_sendto(ei_cnode * ec, int fd, struct erlang_process *process, ei_x_buff * buf) { int ret; if (process->type == ERLANG_PID) { ret = ei_send(fd, &process->pid, buf->buff, buf->index); #ifdef EI_DEBUG ei_x_print_msg(buf, &process->pid, 1); #endif } else if (process->type == ERLANG_REG_PROCESS) { ret = ei_reg_send(ec, fd, process->reg_name, buf->buff, buf->index); #ifdef EI_DEBUG ei_x_print_reg_msg(buf, process->reg_name, 1); #endif } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid process type!\n"); /* wuh-oh */ ret = -1; } return ret; }
/* function to spawn a process on a remote node */ int ei_spawn(struct ei_cnode_s *ec, int sockfd, erlang_ref * ref, char *module, char *function, int argc, char **argv) { int i; ei_x_buff buf; ei_x_new_with_version(&buf); ei_x_encode_tuple_header(&buf, 3); ei_x_encode_atom(&buf, "$gen_call"); ei_x_encode_tuple_header(&buf, 2); ei_x_encode_pid(&buf, ei_self(ec)); ei_init_ref(ec, ref); ei_x_encode_ref(&buf, ref); ei_x_encode_tuple_header(&buf, 5); ei_x_encode_atom(&buf, "spawn"); ei_x_encode_atom(&buf, module); ei_x_encode_atom(&buf, function); /* argument list */ if (argc < 0) { ei_x_encode_list_header(&buf, argc); for (i = 0; i < argc && argv[i]; i++) { ei_x_encode_atom(&buf, argv[i]); } } ei_x_encode_empty_list(&buf); /*if (i != argc - 1) { */ /* horked argument list */ /*} */ ei_x_encode_pid(&buf, ei_self(ec)); /* should really be a valid group leader */ #ifdef EI_DEBUG ei_x_print_reg_msg(&buf, "net_kernel", 1); #endif return ei_reg_send(ec, sockfd, "net_kernel", buf.buff, buf.index); }
int main(void) #endif { ErlConnect conp; Erl_IpAddr thisipaddr = (Erl_IpAddr)0; FILE *fp = (FILE *)0; char* charp = "foo"; double *doublep = NULL; double doublex = 0.0; ei_cnode xec; ei_reg *ei_regp = NULL; ei_term eterm; ei_x_buff eix; erlang_big *bigp = NULL; erlang_fun efun; erlang_msg *msgp = NULL; erlang_msg emsg; erlang_pid *pidp = NULL; erlang_pid epid; erlang_port eport; erlang_ref eref; erlang_trace etrace; int *intp = NULL; int intx = 0; long *longp = NULL; long longx = 0; short creation = 0; struct ei_reg_stat *ei_reg_statp = NULL; struct ei_reg_tabstat *ei_reg_tabstatp = NULL; struct hostent *hostp = NULL; unsigned char * ucharp = (unsigned char *)"foo"; unsigned long *ulongp = NULL; unsigned long ulongx = 0; void *voidp = NULL; #ifndef VXWORKS EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL; EI_LONGLONG longlongx = 0; EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL; EI_ULONGLONG ulonglongx = 0; #endif enum erlang_char_encoding enc; intx = erl_errno; ei_connect_init(&xec, charp, charp, creation); ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation); ei_connect(&xec, charp); ei_xconnect (&xec, thisipaddr, charp); ei_receive(intx, ucharp, intx); ei_receive_msg(intx, &emsg, &eix); ei_xreceive_msg(intx, &emsg, &eix); ei_send(intx, &epid, charp, intx); ei_reg_send(&xec, intx, charp, charp, intx); ei_rpc(&xec, intx, charp, charp, charp, intx, &eix); ei_rpc_to(&xec, intx, charp, charp, charp, intx); ei_rpc_from(&xec, intx, intx, &emsg, &eix); ei_publish(&xec, intx); ei_accept(&xec, intx, &conp); ei_unpublish(&xec); ei_thisnodename(&xec); ei_thishostname(&xec); ei_thisalivename(&xec); ei_self(&xec); ei_gethostbyname(charp); ei_gethostbyaddr(charp, intx, intx); ei_gethostbyname_r(charp, hostp, charp, intx, intp); ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); ei_encode_version(charp, intp); ei_x_encode_version(&eix); ei_encode_long(charp, intp, longx); ei_x_encode_long(&eix, longx); ei_encode_ulong(charp, intp, ulongx); ei_x_encode_ulong(&eix, ulongx); ei_encode_double(charp, intp, doublex); ei_x_encode_double(&eix, doublex); ei_encode_boolean(charp, intp, intx); ei_x_encode_boolean(&eix, intx); ei_encode_char(charp, intp, 'a'); ei_x_encode_char(&eix, 'a'); ei_encode_string(charp, intp, charp); ei_encode_string_len(charp, intp, charp, intx); ei_x_encode_string(&eix, charp); ei_x_encode_string_len(&eix, charp, intx); ei_encode_atom(charp, intp, charp); ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_atom_len(charp, intp, charp, intx); ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1); ei_x_encode_atom(&eix, charp); ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_x_encode_atom_len(&eix, charp, intx); ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_binary(charp, intp, (void *)0, longx); ei_x_encode_binary(&eix, (void*)0, intx); ei_encode_pid(charp, intp, &epid); ei_x_encode_pid(&eix, &epid); ei_encode_fun(charp, intp, &efun); ei_x_encode_fun(&eix, &efun); ei_encode_port(charp, intp, &eport); ei_x_encode_port(&eix, &eport); ei_encode_ref(charp, intp, &eref); ei_x_encode_ref(&eix, &eref); ei_encode_trace(charp, intp, &etrace); ei_x_encode_trace(&eix, &etrace); ei_encode_tuple_header(charp, intp, intx); ei_x_encode_tuple_header(&eix, longx); ei_encode_list_header(charp, intp, intx); ei_x_encode_list_header(&eix, longx); /* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */ ei_x_encode_empty_list(&eix); ei_get_type(charp, intp, intp, intp); ei_get_type_internal(charp, intp, intp, intp); ei_decode_version(charp, intp, intp); ei_decode_long(charp, intp, longp); ei_decode_ulong(charp, intp, ulongp); ei_decode_double(charp, intp, doublep); ei_decode_boolean(charp, intp, intp); ei_decode_char(charp, intp, charp); ei_decode_string(charp, intp, charp); ei_decode_atom(charp, intp, charp); ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc); ei_decode_binary(charp, intp, (void *)0, longp); ei_decode_fun(charp, intp, &efun); free_fun(&efun); ei_decode_pid(charp, intp, &epid); ei_decode_port(charp, intp, &eport); ei_decode_ref(charp, intp, &eref); ei_decode_trace(charp, intp, &etrace); ei_decode_tuple_header(charp, intp, intp); ei_decode_list_header(charp, intp, intp); ei_decode_ei_term(charp, intp, &eterm); ei_print_term(fp, charp, intp); ei_s_print_term(&charp, charp, intp); ei_x_format(&eix, charp); ei_x_format_wo_ver(&eix, charp); ei_x_new(&eix); ei_x_new_with_version(&eix); ei_x_free(&eix); ei_x_append(&eix, &eix); ei_x_append_buf(&eix, charp, intx); ei_skip_term(charp, intp); ei_reg_open(intx); ei_reg_resize(ei_regp, intx); ei_reg_close(ei_regp); ei_reg_setival(ei_regp, charp, longx); ei_reg_setfval(ei_regp, charp, doublex); ei_reg_setsval(ei_regp, charp, charp); ei_reg_setpval(ei_regp, charp, voidp, intx); ei_reg_setval(ei_regp, charp, intx); ei_reg_getival(ei_regp, charp); ei_reg_getfval(ei_regp, charp); ei_reg_getsval(ei_regp, charp); ei_reg_getpval(ei_regp, charp, intp); ei_reg_getval(ei_regp, charp, intx); ei_reg_markdirty(ei_regp, charp); ei_reg_delete(ei_regp, charp); ei_reg_stat(ei_regp, charp, ei_reg_statp); ei_reg_tabstat(ei_regp, ei_reg_tabstatp); ei_reg_dump(intx, ei_regp, charp, intx); ei_reg_restore(intx, ei_regp, charp); ei_reg_purge(ei_regp); #if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP) { mpz_t obj; ei_decode_bignum(charp, intp, obj); ei_encode_bignum(charp, intp, obj); ei_x_encode_bignum(&eix, obj); } #endif /* HAVE_GMP_H && HAVE_LIBGMP */ #ifndef VXWORKS ei_decode_longlong(charp, intp, longlongp); ei_decode_ulonglong(charp, intp, ulonglongp); ei_encode_longlong(charp, intp, longlongx); ei_encode_ulonglong(charp, intp, ulonglongx); ei_x_encode_longlong(&eix, longlongx); ei_x_encode_ulonglong(&eix, ulonglongx); #endif #ifdef USE_EI_UNDOCUMENTED ei_decode_intlist(charp, intp, longp, intp); ei_receive_encoded(intx, &charp, intp, msgp, intp); ei_send_encoded(intx, pidp, charp, intx); ei_send_reg_encoded(intx, pidp, charp, charp, intx); ei_decode_big(charp, intp, bigp); ei_big_comp(bigp, bigp); ei_big_to_double(bigp, doublep); ei_small_to_big(intx, bigp); ei_alloc_big(intx); ei_free_big(bigp); #endif /* USE_EI_UNDOCUMENTED */ return BUFSIZ + EAGAIN + EHOSTUNREACH + EIO + EI_BIN + EI_DELET + EI_DIRTY + EI_FLT + EI_FORCE + EI_INT + EI_NOPURGE + EI_STR + EMSGSIZE + ENOMEM + ERL_ERROR + ERL_EXIT + ERL_LINK + ERL_MSG + ERL_NO_TIMEOUT + ERL_REG_SEND + ERL_SEND + ERL_TICK + ERL_TIMEOUT + ERL_UNLINK + ETIMEDOUT + MAXATOMLEN; }
int worker_reg_send_impl(ei_cnode *ec, int s,int wpid) { str server = STR_NULL; ei_x_buff emsg; struct msghdr msgh; struct iovec cnt[6]; int rc; memset((void*)&emsg,0,sizeof(emsg)); memset((void*)&msgh,0,sizeof(msgh)); /* server name length */ cnt[0].iov_base = &server.len; cnt[0].iov_len = sizeof(int); /* Erlang args size */ cnt[1].iov_base = &emsg.buffsz; cnt[1].iov_len = sizeof(int); /* get data size */ msgh.msg_iov = cnt; msgh.msg_iovlen = 2; while ((rc = recvmsg(s, &msgh, MSG_PEEK)) == -1 && errno == EAGAIN) ; if (rc == -1){ LM_ERR("recvmsg failed (socket=%d): %s\n",s,strerror(errno)); return -1; } /* allocate space */ server.s = (char*)pkg_malloc(server.len+1); if (!server.s) { LM_ERR("not enough memory\n"); goto err; } emsg.buff = (char*)malloc(emsg.buffsz); if (!emsg.buff) { LM_ERR("malloc: not enough memory\n"); goto err; } /* buffers */ cnt[2].iov_base = server.s; cnt[2].iov_len = server.len; cnt[3].iov_base = emsg.buff; cnt[3].iov_len = emsg.buffsz; /* get whole data */ msgh.msg_iovlen = 4; while ((rc = recvmsg(s, &msgh, MSG_WAITALL)) == -1 && errno == EAGAIN) ; if (rc == -1){ LM_ERR("recvmsg failed (socket=%d): %s\n",s,strerror(errno)); goto err; } /* fix str */ server.s[server.len] = 0; if(!enode) { LM_NOTICE("there is no connected Erlang node\n"); goto err; } LM_DBG(">> {%.*s,'%s'} ! emsg\n",STR_FMT(&server),enode->conn.nodename); EI_X_BUFF_PRINT(&emsg); /* do ERL_REG_SEND */ if ((rc = ei_reg_send(ec,enode->sockfd,server.s,emsg.buff,emsg.buffsz)) == ERL_ERROR) { if (erl_errno) { LM_ERR("ei_rpc failed on node=<%s> socket=<%d>: %s\n",enode->conn.nodename,enode->sockfd,strerror(erl_errno)); } else if (errno) { LM_ERR("ei_rpc failed on node=<%s> socket=<%d>: %s\n",enode->conn.nodename,enode->sockfd,strerror(errno)); } else { LM_ERR("ei_rpc failed on node=<%s> socket=<%d>, Unknown error.\n",ec->thisalivename,enode->sockfd); } } pkg_free(server.s); free(emsg.buff); return 0; err: pkg_free(server.s); free(emsg.buff); return -1; }
void gn_send(ei_x_buff *xbuf) { extern ei_cnode ec; if ( ei_reg_send(&ec, fd, rem_regname, xbuf->buff, xbuf->index) ) g_critical("bad send"); }
int _impl_reg_send(const str *server, const ei_x_buff *msg) { struct msghdr msgh; struct iovec cnt[6]; int pid_no = my_pid(); eapi_t api = API_REG_SEND; int buffsz; int rc; int i=0,version; memset(&msgh, 0, sizeof(msgh)); memset(&cnt, 0, sizeof(cnt)); if (ei_decode_version(msg->buff,&i,&version)) { LM_ERR("msg must be encoded with version\n"); return -1; } if (!csockfd) { if (!enode) { LM_NOTICE("there is no connected Erlang node\n"); return -1; } if (rex_call_in_progress) { LM_ERR("RPC in progress detected\n"); return -1; } /* do ERL_REG_SEND in event route */ if ((rc = ei_reg_send(&enode->ec,enode->sockfd,server->s,msg->buff,msg->buffsz)) == ERL_ERROR) { if (erl_errno) { LM_ERR("ei_reg_send failed on node=<%s> socket=<%d>: %s\n",enode->conn.nodename,enode->sockfd,strerror(erl_errno)); } else if (errno) { LM_ERR("ei_reg_send failed on node=<%s> socket=<%d>: %s\n",enode->conn.nodename,enode->sockfd,strerror(errno)); } else { LM_ERR("ei_reg_send failed on node=<%s> socket=<%d>, Unknown error.\n",enode->ec.thisalivename,enode->sockfd); } } /* reset response */ enode->response.index = 0; return 0; } /* Kamailio PID */ cnt[0].iov_base = (void*)&pid_no; cnt[0].iov_len = sizeof(pid_no); /* method */ cnt[1].iov_base = (void*)&api; cnt[1].iov_len = sizeof(api); /* put size of following data */ cnt[2].iov_base = (void*)&server->len; cnt[2].iov_len = sizeof(int); buffsz = msg->index; /* occupied size */ cnt[3].iov_base = (void*)&buffsz; cnt[3].iov_len = sizeof(buffsz); /* module name */ cnt[4].iov_base = (void*)server->s; cnt[4].iov_len = server->len; /* Erlang arguments content */ cnt[5].iov_base = (void*)msg->buff; cnt[5].iov_len = buffsz; /* occupied size */ msgh.msg_iov = cnt; msgh.msg_iovlen = 6; while ((rc = sendmsg(csockfd, &msgh, 0)) == -1 && errno == EAGAIN) ; if (rc == -1) { LM_ERR("sendmsg failed: %s\n",strerror(errno)); return -1; } /* no reply */ return 0; }