| | |
| | | #include <sys/socket.h> |
| | | |
| | | |
| | | std::map<std::string, int> NetModSocket::connectionMap; |
| | | |
| | | |
| | | NetModSocket::NetModSocket() |
| | | { |
| | | |
| | | init_req_rep_req_resp_pool(); |
| | | } |
| | | |
| | | |
| | | NetModSocket::~NetModSocket() { |
| | | int clientfd; |
| | | for (auto map_iter = connectionMap.begin(); map_iter != connectionMap.end(); map_iter++) { |
| | | for (auto map_iter = req_resp_pool.connectionMap.begin(); map_iter != req_resp_pool.connectionMap.end(); map_iter++) { |
| | | clientfd = map_iter->second; |
| | | Close(clientfd); |
| | | |
| | | } |
| | | } |
| | | |
| | | int NetModSocket::connect(const char *host, int port) { |
| | | |
| | | void NetModSocket::init_req_rep_req_resp_pool() |
| | | { |
| | | /* Initially, there are no connected descriptors */ |
| | | int i; |
| | | req_resp_pool.maxi = -1; //line:conc:echoservers:beginempty |
| | | for (i = 0; i < FD_SETSIZE; i++) |
| | | req_resp_pool.connfd[i] = -1; //line:conc:echoservers:endempty |
| | | |
| | | /* Initially, listenfd is only member of select read set */ |
| | | FD_ZERO(&req_resp_pool.read_set); |
| | | FD_ZERO(&req_resp_pool.write_set); |
| | | FD_ZERO(&req_resp_pool.except_set); |
| | | } |
| | | |
| | | int NetModSocket::connect( net_node_t *node) { |
| | | std::map<std::string, int>::iterator mapIter; |
| | | int clientfd; |
| | | int connfd; |
| | | int i; |
| | | char mapKey[256]; |
| | | char portstr[32]; |
| | | |
| | | sprintf(mapKey, "%s:%d", host, port); |
| | | if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) { |
| | | clientfd = mapIter->second; |
| | | sprintf(mapKey, "%s:%d", node->host, node->port); |
| | | if( ( mapIter = req_resp_pool.connectionMap.find(mapKey)) != req_resp_pool.connectionMap.end()) { |
| | | connfd = mapIter->second; |
| | | |
| | | } else { |
| | | |
| | | sprintf(portstr, "%d", port); |
| | | clientfd = open_clientfd(host, portstr); |
| | | connectionMap.insert({mapKey, clientfd}); |
| | | |
| | | sprintf(portstr, "%d", node->port); |
| | | connfd = open_clientfd(node->host, portstr); |
| | | if(connfd < 0) { |
| | | return -1; |
| | | } |
| | | req_resp_pool.connectionMap.insert({mapKey, connfd}); |
| | | } |
| | | return clientfd; |
| | | |
| | | |
| | | for (i = 0; i < FD_SETSIZE; i++) { /* Find an available slot */ |
| | | if (req_resp_pool.connfd[i] < 0) |
| | | { |
| | | /* Add connected descriptor to the req_resp_pool */ |
| | | req_resp_pool.connfd[i] = connfd; |
| | | req_resp_pool.connfdNodeMap.insert({connfd, node}); |
| | | // Rio_readinitb(&req_resp_pool.clientrio[i], connfd); //line:conc:echoservers:endaddclient |
| | | |
| | | /* Add the descriptor to descriptor set */ |
| | | FD_SET(connfd, &req_resp_pool.read_set); //line:conc:echoservers:addconnfd |
| | | FD_SET(connfd, &req_resp_pool.write_set); |
| | | FD_SET(connfd, &req_resp_pool.except_set); |
| | | /* Update max descriptor and req_resp_pool highwater mark */ |
| | | if (connfd > req_resp_pool.maxfd) |
| | | req_resp_pool.maxfd = connfd; |
| | | if (i > req_resp_pool.maxi) |
| | | req_resp_pool.maxi = i; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (i == FD_SETSIZE) { |
| | | /* Couldn't find an empty slot */ |
| | | LoggerFactory::getLogger()->error(errno, "add_client error: Too many clients"); |
| | | return -1; |
| | | |
| | | } |
| | | |
| | | return connfd; |
| | | } |
| | | |
| | | void NetModSocket::remove_connect(const char *host, int port) { |
| | | std::map<std::string, int>::iterator mapIter; |
| | | void NetModSocket::close_connect(int connfd) { |
| | | |
| | | net_node_t *node = req_resp_pool.connfdNodeMap.find(connfd)->second; |
| | | |
| | | // std::map<std::string, int>::iterator mapIter; |
| | | Close(connfd); //line:conc:echoservers:closeconnfd |
| | | FD_CLR(connfd, &req_resp_pool.read_set); |
| | | FD_CLR(connfd, &req_resp_pool.write_set); |
| | | FD_CLR(connfd, &req_resp_pool.except_set); |
| | | |
| | | char mapKey[256]; |
| | | // char portstr[32]; |
| | | sprintf(mapKey, "%s:%d", host, port); |
| | | connectionMap.erase(mapKey); |
| | | sprintf(mapKey, "%s:%d", node->host, node->port); |
| | | req_resp_pool.connectionMap.erase(mapKey); |
| | | LoggerFactory::getLogger()->debug("close_connect"); |
| | | } |
| | | |
| | | |
| | | 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 i, n, clientfd; |
| | | int i, recv_size, connfd; |
| | | net_node_t *node; |
| | | void *recv_buf; |
| | | |
| | | |
| | | int n_conn_suc = 0, n_recv_suc = 0; |
| | | |
| | | net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t)); |
| | | |
| | | init_req_rep_req_resp_pool(); |
| | | |
| | | for (i = 0; i< arrlen; i++) { |
| | | |
| | | node = &node_arr[i]; |
| | | if(node->host == NULL) { |
| | | // 本地发送 |
| | | shmModSocket.sendandrecv(send_buf, send_size, node->key, &recv_buf, &recv_size); |
| | | strcpy( ret_arr[n_recv_suc].host,"localshm"); |
| | | ret_arr[n_recv_suc].port = 0; |
| | | ret_arr[n_recv_suc].key = node->key; |
| | | ret_arr[n_recv_suc].content = recv_buf; |
| | | ret_arr[n_recv_suc].content_length = recv_size; |
| | | n_recv_suc++; |
| | | } |
| | | |
| | | if( (connfd = connect(node)) < 0 ) { |
| | | continue; |
| | | } |
| | | |
| | | |
| | | // if(write_request(connfd, node->key, send_buf, send_size) != 0) { |
| | | // close_connect(connfd); |
| | | // } |
| | | |
| | | n_conn_suc++; |
| | | // optval = 0; |
| | | // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval)); |
| | | } |
| | | |
| | | printf("n_conn_suc =%d\n", n_conn_suc); |
| | | |
| | | while(n_recv_suc < n_conn_suc) |
| | | { |
| | | /* Wait for listening/connected descriptor(s) to become ready */ |
| | | req_resp_pool.ready_read_set = req_resp_pool.read_set; |
| | | req_resp_pool.ready_write_set = req_resp_pool.write_set; |
| | | req_resp_pool.ready_except_set = req_resp_pool.except_set; |
| | | req_resp_pool.nready = select(req_resp_pool.maxfd + 1, &req_resp_pool.ready_read_set, &req_resp_pool.ready_write_set, &req_resp_pool.ready_except_set, NULL); |
| | | printf("req_resp_pool.nready =%d\n", req_resp_pool.nready); |
| | | for (i = 0; (i <= req_resp_pool.maxi) && (req_resp_pool.nready > 0); i++) { |
| | | if ( (connfd = req_resp_pool.connfd[i]) > 0 ) { |
| | | /* If the descriptor is ready, echo a text line from it */ |
| | | node = req_resp_pool.connfdNodeMap.find(connfd)->second; |
| | | if ( FD_ISSET(connfd, &req_resp_pool.ready_read_set)) |
| | | { |
| | | req_resp_pool.nready--; |
| | | if(read_response(connfd, ret_arr+n_recv_suc) == 0) { |
| | | n_recv_suc++; |
| | | } else { |
| | | close_connect(connfd); |
| | | } |
| | | |
| | | } |
| | | if (FD_ISSET(connfd, &req_resp_pool.ready_write_set)) |
| | | { |
| | | req_resp_pool.nready--; |
| | | printf("write %d\n", connfd); |
| | | if(write_request(connfd, node->key, send_buf, send_size) != 0) { |
| | | close_connect(connfd); |
| | | } else{ |
| | | // 一次写入完成后清空写入位 |
| | | FD_CLR(connfd, &req_resp_pool.write_set); |
| | | } |
| | | |
| | | } |
| | | if (FD_ISSET(connfd, &req_resp_pool.ready_except_set)) |
| | | { |
| | | req_resp_pool.nready--; |
| | | close_connect(connfd); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | *recv_arr = ret_arr; |
| | | if(recv_arr_size != NULL) { |
| | | *recv_arr_size = n_recv_suc; |
| | | } |
| | | return n_recv_suc; |
| | | |
| | | } |
| | | |
| | | int NetModSocket::write_request(int clientfd, int key, void *send_buf, int send_size) { |
| | | net_mod_request_head_t request_head = {}; |
| | | static char *buf; |
| | | static int buf_size, max_buf_size; |
| | | |
| | | |
| | | if(buf == NULL) { |
| | | buf = (char *)malloc(MAXBUF); |
| | | max_buf_size = MAXBUF; |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv malloc"); |
| | | |
| | | } |
| | | |
| | | buf_size = send_size + NET_MODE_REQUEST_HEAD_LENGTH; |
| | | if(max_buf_size < buf_size) { |
| | | buf = (char *)realloc(buf, buf_size); |
| | | max_buf_size = buf_size; |
| | | } |
| | | |
| | | request_head.mod = REQ_REP; |
| | | request_head.key = key; |
| | | request_head.content_length = send_size; |
| | | request_head.topic_length = 0; |
| | | |
| | | // optval = 1; |
| | | // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval)); |
| | | memcpy(buf, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH); |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, send_buf, send_size); |
| | | |
| | | |
| | | if(rio_writen(clientfd, buf, buf_size) != buf_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send conent rio_writen"); |
| | | |
| | | return -1; |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | int NetModSocket::read_response(int connfd, net_mod_recv_msg_t *recv_msg) { |
| | | int recv_size; |
| | | void *recv_buf; |
| | | char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH]; |
| | | |
| | | net_mod_response_head_t response_head; |
| | | net_node_t *node = req_resp_pool.connfdNodeMap.find(connfd)->second; |
| | | if ( rio_readn(connfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send rio_readnb"); |
| | | |
| | | return -1; |
| | | } |
| | | |
| | | response_head = NetModSocket::decode_response_head(response_head_bs); |
| | | |
| | | recv_buf = malloc(response_head.content_length); |
| | | if(recv_buf == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send malloc"); |
| | | exit(1); |
| | | } |
| | | if ( (recv_size = rio_readn(connfd, recv_buf, response_head.content_length) ) != response_head.content_length) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send rio_readnb"); |
| | | |
| | | return -1; |
| | | } |
| | | |
| | | |
| | | strcpy( recv_msg->host, node->host); |
| | | recv_msg->port = node->port; |
| | | recv_msg->key = node->key; |
| | | recv_msg->content = recv_buf; |
| | | recv_msg->content_length = recv_size; |
| | | return 0; |
| | | } |
| | | |
| | | int NetModSocket::sendandrecv_safe(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 i, clientfd; |
| | | net_node_t *node; |
| | | void *recv_buf; |
| | | int recv_size; |
| | | char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH]; |
| | | net_mod_request_head_t request_head = {}; |
| | | net_mod_response_head_t response_head; |
| | | int optval; |
| | | |
| | | |
| | | char portstr[32]; |
| | | char *buf = NULL; |
| | | int buf_size, max_buf_size; |
| | | |
| | | if(buf == NULL) { |
| | | buf = (char *)malloc(MAXBUF); |
| | | max_buf_size = MAXBUF; |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv malloc"); |
| | | } |
| | | |
| | | int nsuc = 0; |
| | | net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t)); |
| | | |
| | |
| | | goto LABEL_ARR_PUSH; |
| | | } |
| | | |
| | | if( (clientfd = connect(node->host, node->port)) < 0 ) { |
| | | sprintf(portstr, "%d", node->port); |
| | | clientfd = open_clientfd(node->host, portstr); |
| | | if(clientfd < 0) { |
| | | continue; |
| | | } |
| | | |
| | | buf_size = send_size + NET_MODE_REQUEST_HEAD_LENGTH; |
| | | if(max_buf_size < buf_size) { |
| | | buf = (char *)realloc(buf, buf_size); |
| | | max_buf_size = buf_size; |
| | | } |
| | | |
| | | |
| | | request_head.mod = REQ_REP; |
| | | request_head.key = node->key; |
| | | request_head.content_length = send_size; |
| | | request_head.topic_length = 0; |
| | | |
| | | // optval = 1; |
| | | // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval)); |
| | | if(rio_writen(clientfd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send head rio_writen"); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | memcpy(buf, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH); |
| | | memcpy(buf + NET_MODE_REQUEST_HEAD_LENGTH, send_buf, send_size); |
| | | |
| | | } |
| | | |
| | | if(rio_writen(clientfd, send_buf, send_size) != send_size ) { |
| | | if(rio_writen(clientfd, buf, buf_size) != buf_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send conent rio_writen"); |
| | | remove_connect(node->host, node->port); |
| | | |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | |
| | | |
| | | if ( rio_readn(clientfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send rio_readnb"); |
| | | remove_connect(node->host, node->port); |
| | | |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | |
| | | } |
| | | if ( (recv_size = rio_readn(clientfd, recv_buf, response_head.content_length) ) != response_head.content_length) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send rio_readnb"); |
| | | remove_connect(node->host, node->port); |
| | | |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | LABEL_ARR_PUSH: |
| | | if(node->host != NULL) { |
| | | strcpy( ret_arr[nsuc].host, node->host); |
| | | strcpy(ret_arr[nsuc].host, node->host); |
| | | } else { |
| | | strcpy( ret_arr[nsuc].host, "local"); |
| | | strcpy(ret_arr[nsuc].host, "local"); |
| | | } |
| | | |
| | | ret_arr[nsuc].port = node->port; |
| | |
| | | if(recv_arr_size != NULL) { |
| | | *recv_arr_size = nsuc; |
| | | } |
| | | |
| | | |
| | | free(buf); |
| | | return nsuc; |
| | | |
| | | } |
| | |
| | | // 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, n, clientfd; |
| | | int i, clientfd; |
| | | net_node_t *node; |
| | | void *recv_buf; |
| | | int recv_size; |
| | | char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH]; |
| | | |
| | | char *buf; |
| | | int max_buf_size, buf_size; |
| | | |
| | | net_mod_request_head_t request_head; |
| | | net_mod_response_head_t response_head; |
| | | std::map<std::string, int>::iterator mapIter; |
| | | |
| | | char portstr[32]; |
| | | |
| | | buf = (char *)malloc(MAXBUF); |
| | | max_buf_size = MAXBUF; |
| | | if(buf == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv malloc"); |
| | | exit(1); |
| | | } |
| | | int nsuc = 0; |
| | | for (i = 0; i< arrlen; i++) { |
| | | |
| | |
| | | shmModSocket.pub(topic, topic_size, content, content_size, node->key); |
| | | |
| | | } else { |
| | | if( (clientfd = connect(node->host, node->port)) < 0 ) { |
| | | sprintf(portstr, "%d", node->port); |
| | | clientfd = open_clientfd(node->host, portstr); |
| | | if(clientfd < 0) { |
| | | continue; |
| | | } |
| | | |
| | |
| | | request_head.key = node->key; |
| | | request_head.content_length = content_size; |
| | | request_head.topic_length = strlen(topic) + 1; |
| | | if(rio_writen(clientfd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::pub head rio_writen"); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | |
| | | buf_size = NET_MODE_REQUEST_HEAD_LENGTH + content_size + request_head.topic_length; |
| | | |
| | | if(max_buf_size < buf_size) { |
| | | buf = (char *)realloc(buf, buf_size); |
| | | max_buf_size = buf_size; |
| | | } |
| | | |
| | | if(rio_writen(clientfd, content, content_size) != content_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 "); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | if(rio_writen(clientfd, topic, request_head.topic_length) != request_head.topic_length ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent "); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | |
| | | |
| | | nsuc++; |
| | | } |
| | | |
| | | free(buf); |
| | | return nsuc; |
| | | } |
| | | |
| | | |
| | | void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) { |
| | | |
| | | for(int i =0; i< size; i++) { |