struct rpc *rpc_create(const char *host, uint16_t port) { int ret; char str_ip[INET_ADDRSTRLEN]; struct skt_connection *sc; struct rpc *r = CALLOC(1, struct rpc); if (!r) { loge("malloc failed!\n"); return NULL; } sc = skt_tcp_connect(host, port); if (sc == NULL) { loge("connect failed!\n"); return NULL; } r->fd = sc->fd; if (-1 == skt_set_noblk(sc->fd, 0)) { loge("block skt_recv failed!\n"); } ret = rpc_recv(r, r->packet.header.uuid_src, MAX_UUID_LEN); if (ret != MAX_UUID_LEN) { loge("rpc_recv failed: ret = %d\n", ret); } // if (-1 == skt_set_noblk(sc->fd, 1)) { // loge("no-block skt_recv failed!\n"); // } skt_addr_ntop(str_ip, sc->local.ip); logi("local addr = %s:%d, uuid_src = %s\n", str_ip, sc->local.port, r->packet.header.uuid_src); skt_addr_ntop(str_ip, sc->remote.ip); //logd("remote ip = %s, port = %d\n", str_ip, sc->remote.port); r->evbase = gevent_base_create(); return r; }
static void* do_rpc_thread(void *data) { LOG_ENTRY; struct rpc *rpc = data; if (!rpc || !rpc->handler) { RPC_PUTS("bad rpc"); LOG_EXIT; return NULL; } rpc->active = 1; rpc_cond_signal(rpc); fd_set fds; while (rpc->active) { RPC_DEBUG("+loop"); FD_ZERO(&fds); FD_SET(rpc->fd, &fds); FD_SET(rpc->pipefd[READ_END], &fds); select(max(rpc->fd, rpc->pipefd[READ_END]) + 1, &fds, NULL, NULL, NULL); pthread_mutex_lock(&rpc->fd_mutex); if (FD_ISSET(rpc->fd, &fds)) { RPC_DEBUG("receiving RPC message"); if (rpc_recv(rpc) < 0) { RPC_ERROR("receive error"); rpc->active = 0; goto handled; } } if (FD_ISSET(rpc->pipefd[READ_END], &fds)) { RPC_DEBUG("calling remote party"); if (rpc_send(rpc) < 0) { RPC_ERROR("call error"); rpc->active = 0; goto handled; } } handled: pthread_mutex_unlock(&rpc->fd_mutex); RPC_DEBUG("-loop"); } rpc_cond_signal(rpc); LOG_EXIT; return NULL; }