| | |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, -1); |
| | | } |
| | | int NetModSocket::sendandrecv_timeout(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int timeout) { |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, timeout); |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int msec) { |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, msec); |
| | | } |
| | | int NetModSocket::sendandrecv_nowait(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) { |
| | |
| | | // printf("open after: %s\n", mapKey); |
| | | if(connfd < 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::connect %s:%d ", node->host, node->port); |
| | | exit(1); |
| | | return -1; |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | LoggerFactory::getLogger()->debug( "closed %d\n", connfd); |
| | | // LoggerFactory::getLogger()->debug( "NetModSocket::close_connect %d\n", connfd); |
| | | |
| | | } |
| | | |
| | | int NetModSocket::_sendandrecv_(net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int timeout = 5 * 1000) { |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int msec ) { |
| | | |
| | | int i, n, recv_size, connfd; |
| | | net_node_t *node; |
| | |
| | | |
| | | net_mod_request_head_t request_head = {}; |
| | | |
| | | int n_req = 0, n_recv_suc = 0, n_resp; |
| | | int n_req = 0, n_recv_suc = 0, n_resp =0; |
| | | |
| | | |
| | | net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t)); |
| | |
| | | request_head.port = node->port; |
| | | request_head.key = node->key; |
| | | request_head.content_length = send_size; |
| | | |
| | | request_head.timeout = msec; |
| | | |
| | | // printf("write_request %s:%d\n", request_head.host, request_head.port); |
| | | if(write_request(connfd, request_head, send_buf, send_size) != 0) { |
| | | if(write_request(connfd, request_head, send_buf, send_size, NULL, 0) != 0) { |
| | | LoggerFactory::getLogger()->error("write_request failture %s:%d\n", node->host, node->port); |
| | | close_connect(mpool, connfd); |
| | | // mpool.conns[i].fd = -1; |
| | | } else { |
| | | n_req++; |
| | | } |
| | |
| | | // printf(" mpool.maxi = %d\n", mpool.maxi); |
| | | // printf(" n_req = %d\n", n_req); |
| | | |
| | | // int tmp = 0; |
| | | while(n_resp < n_req) |
| | | { |
| | | // printf(" while %d\n", tmp++); |
| | | /* Wait for listening/connected descriptor(s) to become ready */ |
| | | if( (mpool.nready = poll(mpool.conns, mpool.maxi + 1, timeout) ) <= 0) { |
| | | if( (mpool.nready = poll(mpool.conns, mpool.maxi + 1, msec) ) <= 0) { |
| | | // wirite_set 和 read_set 在指定时间内都没准备好 |
| | | break; |
| | | } |
| | |
| | | mpool.nready--; |
| | | // printf("POLLIN %d\n", connfd); |
| | | if( (n = read_response(connfd, ret_arr+n_recv_suc)) == 0) { |
| | | |
| | | // 成功收到返回消息,清空读入位 |
| | | mpool.conns[i].fd = -1; |
| | | n_recv_suc++; |
| | | |
| | | } else if(n == -1) { |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | } else { |
| | | mpool.conns[i].fd = -1; |
| | | |
| | | } |
| | | // else if(n == -1) { |
| | | // // 网络错误 |
| | | // } else { |
| | | // // 对方key是关闭的 |
| | | // } |
| | | |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | n_resp++; |
| | | // printf("read response %d\n", n); |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | //超时后,关闭超时连接 |
| | | for (i = 0; i <= mpool.maxi; i++) { |
| | | if ( (connfd = mpool.conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | |
| | | } |
| | | return n_recv_suc; |
| | | |
| | | } |
| | | |
| | | int NetModSocket::pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) { |
| | | return _pub_(node_arr, arrlen, topic, topic_size, content, content_size, -1); |
| | | } |
| | | |
| | | int NetModSocket::pub_nowait(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) { |
| | | return _pub_(node_arr, arrlen, topic, topic_size, content, content_size, 0); |
| | | } |
| | | |
| | | int NetModSocket::pub_timeout(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size, int msec ) { |
| | | return _pub_(node_arr, arrlen, topic, topic_size, content, content_size, msec); |
| | | } |
| | | |
| | | |
| | | // int pub(char *topic, int topic_size, void *content, int content_size, int port); |
| | | |
| | | int NetModSocket::_pub_(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, |
| | | int content_size, int timeout) { |
| | | int i, connfd; |
| | | net_node_t *node; |
| | | |
| | | net_mod_request_head_t request_head; |
| | | net_mod_recv_msg_t recv_msg; |
| | | char portstr[32]; |
| | | int n_req = 0, n_pub_suc = 0, n_resp = 0; |
| | | int ret; |
| | | |
| | | pool mpool; |
| | | init_conn_pool(mpool); |
| | | |
| | | |
| | | for (i = 0; i < arrlen; i++) { |
| | | |
| | | node = &node_arr[i]; |
| | | if(node->host == NULL) { |
| | | // 本地发送 |
| | | if(shmModSocket.pub(topic, topic_size, content, content_size, node->key) == 0 ) { |
| | | n_pub_suc++; |
| | | } |
| | | |
| | | } else { |
| | | sprintf(portstr, "%d", node->port); |
| | | if( (connfd = connect(mpool, node)) < 0 ) { |
| | | continue; |
| | | } |
| | | request_head.mod = BUS; |
| | | request_head.key = node->key; |
| | | request_head.content_length = content_size; |
| | | request_head.topic_length = strlen(topic) + 1; |
| | | request_head.timeout = timeout; |
| | | |
| | | if(write_request(connfd, request_head, content, content_size, topic, request_head.topic_length) != 0) { |
| | | LoggerFactory::getLogger()->error(" NetModSocket::_pub_ write_request failture %s:%d\n", node->host, node->port); |
| | | close_connect(mpool, connfd); |
| | | } else { |
| | | n_req++; |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | while(n_resp < n_req) |
| | | { |
| | | /* Wait for listening/connected descriptor(s) to become ready */ |
| | | if( (mpool.nready = poll(mpool.conns, mpool.maxi + 1, timeout) ) <= 0) { |
| | | // wirite_set 和 read_set 在指定时间内都没准备好 |
| | | break; |
| | | } |
| | | // printf("mpool.nready =%d\n", mpool.nready); |
| | | for (i = 0; (i <= mpool.maxi) && (mpool.nready > 0); i++) { |
| | | if ( (connfd = mpool.conns[i].fd) > 0 ) { |
| | | if (mpool.conns[i].revents & POLLIN ) |
| | | { |
| | | mpool.nready--; |
| | | // printf("POLLIN %d\n", connfd); |
| | | if( (ret = read_response(connfd, &recv_msg)) == 0) { |
| | | |
| | | // 成功收到返回消息,清空读入位 |
| | | mpool.conns[i].fd = -1; |
| | | n_pub_suc++; |
| | | } |
| | | // else if(ret == -1) { |
| | | // // 网络连接错误 |
| | | // } else { |
| | | // // 对方的key是关闭的 |
| | | // } |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | n_resp++; |
| | | // printf("read response %d\n", n); |
| | | |
| | | } |
| | | |
| | | if (mpool.conns[i].revents & POLLOUT ) { |
| | | // printf("poll POLLOUT %d\n", connfd); |
| | | } |
| | | |
| | | if (mpool.conns[i].revents & (POLLRDHUP | POLLHUP | POLLERR) ) |
| | | { |
| | | // printf("poll POLLERR %d\n", connfd); |
| | | mpool.nready--; |
| | | |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //超时后,关闭超时连接 |
| | | for (i = 0; i <= mpool.maxi; i++) { |
| | | if ( (connfd = mpool.conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | | close_connect(mpool, connfd); |
| | | mpool.conns[i].fd = -1; |
| | | } |
| | | } |
| | | |
| | | mpool.maxi = -1; |
| | | |
| | | return n_pub_suc; |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | // int pub(char *topic, int topic_size, void *content, int content_size, int port); |
| | | |
| | | int NetModSocket::pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) { |
| | | int i, clientfd; |
| | | net_node_t *node; |
| | | |
| | | char *buf; |
| | | int max_buf_size, buf_size; |
| | | |
| | | net_mod_request_head_t request_head; |
| | | char portstr[32]; |
| | | int nsuc = 0; |
| | | |
| | | if((buf = (char *)malloc(MAXBUF)) == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv malloc"); |
| | | exit(1); |
| | | } else { |
| | | max_buf_size = MAXBUF; |
| | | } |
| | | |
| | | for (i = 0; i< arrlen; i++) { |
| | | |
| | | node = &node_arr[i]; |
| | | if(node->host == NULL) { |
| | | // 本地发送 |
| | | if(shmModSocket.pub(topic, topic_size, content, content_size, node->key) == 0 ) { |
| | | nsuc++; |
| | | } |
| | | |
| | | } else { |
| | | sprintf(portstr, "%d", node->port); |
| | | clientfd = open_clientfd(node->host, portstr); |
| | | if(clientfd < 0) { |
| | | continue; |
| | | } |
| | | |
| | | request_head.mod = BUS; |
| | | request_head.key = node->key; |
| | | request_head.content_length = content_size; |
| | | request_head.topic_length = strlen(topic) + 1; |
| | | |
| | | buf_size = NET_MODE_REQUEST_HEAD_LENGTH + content_size + request_head.topic_length; |
| | | |
| | | if(max_buf_size < buf_size) { |
| | | if( ( buf = (char *)realloc(buf, buf_size)) == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::pub realloc buf "); |
| | | } else { |
| | | max_buf_size = buf_size; |
| | | } |
| | | |
| | | } |
| | | |
| | | memcpy(buf, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH); |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, content, content_size); |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH + content_size, topic, request_head.topic_length); |
| | | |
| | | if(rio_writen(clientfd, buf, buf_size) != buf_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent "); |
| | | } else { |
| | | nsuc++; |
| | | } |
| | | close(clientfd); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | free(buf); |
| | | return nsuc; |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | //====================================================================================== |
| | | |
| | | |
| | | |
| | | int NetModSocket::write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size) { |
| | | int NetModSocket::write_request(int clientfd, net_mod_request_head_t &request_head, |
| | | void *content_buf, int content_size, void *topic_buf, int topic_size) { |
| | | |
| | | int buf_size; |
| | | char *buf; |
| | |
| | | max_buf_size = MAXBUF; |
| | | } |
| | | |
| | | buf_size = send_size + NET_MODE_REQUEST_HEAD_LENGTH; |
| | | buf_size = NET_MODE_REQUEST_HEAD_LENGTH + content_size + topic_size ; |
| | | if(max_buf_size < buf_size) { |
| | | |
| | | if((buf = (char *)realloc(buf, buf_size)) == NULL) { |
| | |
| | | } |
| | | |
| | | memcpy(buf, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH); |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, send_buf, send_size); |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, content_buf, content_size); |
| | | if(topic_size != 0 ) |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH + content_size, topic_buf, topic_size); |
| | | |
| | | if(rio_writen(clientfd, buf, buf_size) != buf_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::write_request rio_writen"); |
| | |
| | | response_head = NetModSocket::decode_response_head(response_head_bs); |
| | | // printf(">>>> read_response %s\n", response_head.host); |
| | | if(response_head.code != 0) { |
| | | // 对方没有对应的key |
| | | // 代理服务没能成功发送给对应的key |
| | | return 1; |
| | | } |
| | | |
| | |
| | | |
| | | tmp_ptr += 4; |
| | | PUT(tmp_ptr, htonl(head.topic_length)); |
| | | |
| | | tmp_ptr += 4; |
| | | PUT_INT32(tmp_ptr, htonl(head.timeout)); |
| | | |
| | | |
| | | return headbs; |
| | |
| | | |
| | | tmp_ptr += 4; |
| | | head.topic_length = ntohl(GET(tmp_ptr)); |
| | | |
| | | tmp_ptr += 4; |
| | | head.timeout = ntohl(GET_INT32(tmp_ptr)); |
| | | |
| | | return head; |
| | | } |