| | |
| | | #include "net_mod_socket_io.h" |
| | | |
| | | |
| | | std::map<std::string, rio_t *> NetModSocket::connectionMap; |
| | | std::map<std::string, int> NetModSocket::connectionMap; |
| | | |
| | | NetModSocket::NetModSocket() |
| | | { |
| | |
| | | |
| | | |
| | | NetModSocket::~NetModSocket() { |
| | | rio_t * rio; |
| | | int clientfd; |
| | | for (auto map_iter = connectionMap.begin(); map_iter != connectionMap.end(); map_iter++) { |
| | | rio = map_iter->second; |
| | | Close(rio->rio_fd); |
| | | if(rio != NULL) { |
| | | free(rio); |
| | | } |
| | | clientfd = map_iter->second; |
| | | Close(clientfd); |
| | | |
| | | } |
| | | } |
| | | |
| | | int NetModSocket::connect(const char *host, int port) { |
| | | std::map<std::string, int>::iterator mapIter; |
| | | int clientfd; |
| | | char mapKey[256]; |
| | | char portstr[32]; |
| | | |
| | | sprintf(mapKey, "%s:%d", host, port); |
| | | if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) { |
| | | clientfd = mapIter->second; |
| | | } else { |
| | | |
| | | sprintf(portstr, "%d", port); |
| | | clientfd = open_clientfd(host, portstr); |
| | | connectionMap.insert({mapKey, clientfd}); |
| | | |
| | | } |
| | | return clientfd; |
| | | } |
| | | |
| | | void NetModSocket::remove_connect(const char *host, int port) { |
| | | std::map<std::string, int>::iterator mapIter; |
| | | char mapKey[256]; |
| | | // char portstr[32]; |
| | | sprintf(mapKey, "%s:%d", host, port); |
| | | connectionMap.erase(mapKey); |
| | | } |
| | | |
| | | 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; |
| | | char portstr[32]; |
| | | net_node_t *node; |
| | | char mapKey[256]; |
| | | 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; |
| | | std::map<std::string, rio_t*>::iterator mapIter; |
| | | rio_t *rio; |
| | | |
| | | |
| | | int nsuc = 0; |
| | | net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t)); |
| | | |
| | | for (i = 0; i< arrlen; i++) { |
| | |
| | | shmModSocket.sendandrecv(send_buf, send_size, node->key, &recv_buf, &recv_size); |
| | | goto LABEL_ARR_PUSH; |
| | | } |
| | | sprintf(mapKey, "%s:%d", node->host, node->port); |
| | | if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) { |
| | | rio = mapIter->second; |
| | | } else { |
| | | rio = (rio_t *)malloc(sizeof(rio_t)); |
| | | sprintf(portstr, "%d", node->port); |
| | | clientfd = Open_clientfd(node-> host, portstr); |
| | | Rio_readinitb(rio, clientfd); |
| | | connectionMap.insert({mapKey, rio}); |
| | | |
| | | if( (clientfd = connect(node->host, node->port)) < 0 ) { |
| | | continue; |
| | | } |
| | | |
| | | request_head.mod = REQ_REP; |
| | | request_head.key = node->key; |
| | | request_head.content_length = send_size; |
| | | request_head.topic_length = 0; |
| | | if(rio_writen(rio->rio_fd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) { |
| | | 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"); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | |
| | | } |
| | | |
| | | if(rio_writen(rio->rio_fd, send_buf, send_size) != send_size ) { |
| | | if(rio_writen(clientfd, send_buf, send_size) != send_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send conent rio_writen"); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | |
| | | if ( rio_readnb(rio, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH) { |
| | | 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"); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | response_head = NetModSocket::decode_response_head(response_head_bs); |
| | |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send malloc"); |
| | | exit(1); |
| | | } |
| | | if ( (recv_size = rio_readnb(rio, recv_buf, response_head.content_length) ) != response_head.content_length) { |
| | | if ( (recv_size = rio_readn(clientfd, recv_buf, response_head.content_length) ) != response_head.content_length) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send rio_readnb"); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | LABEL_ARR_PUSH: |
| | | if(node->host != NULL) { |
| | | strcpy( ret_arr[i].host, node->host); |
| | | strcpy( ret_arr[nsuc].host, node->host); |
| | | } else { |
| | | strcpy( ret_arr[i].host, "local"); |
| | | strcpy( ret_arr[nsuc].host, "local"); |
| | | } |
| | | |
| | | ret_arr[i].port = node->port; |
| | | ret_arr[i].key = node->key; |
| | | ret_arr[i].content = recv_buf; |
| | | ret_arr[i].content_length = recv_size; |
| | | ret_arr[nsuc].port = node->port; |
| | | ret_arr[nsuc].key = node->key; |
| | | ret_arr[nsuc].content = recv_buf; |
| | | ret_arr[nsuc].content_length = recv_size; |
| | | |
| | | nsuc++; |
| | | } |
| | | |
| | | *recv_arr = ret_arr; |
| | | if(recv_arr_size != NULL) { |
| | | *recv_arr_size = i; |
| | | *recv_arr_size = nsuc; |
| | | } |
| | | |
| | | return i; |
| | | return nsuc; |
| | | |
| | | } |
| | | |
| | |
| | | |
| | | int NetModSocket::pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) { |
| | | int i, n, clientfd; |
| | | char portstr[32]; |
| | | net_node_t *node; |
| | | char mapKey[256]; |
| | | 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; |
| | | std::map<std::string, rio_t*>::iterator mapIter; |
| | | rio_t *rio; |
| | | std::map<std::string, int>::iterator mapIter; |
| | | int nsuc = 0; |
| | | for (i = 0; i< arrlen; i++) { |
| | | |
| | | node = &node_arr[i]; |
| | | if(node->host == NULL) { |
| | | // 本地发送 |
| | | shmModSocket.pub(topic, topic_size, content, content_size, node->key); |
| | | |
| | | |
| | | } else { |
| | | sprintf(mapKey, "%s:%d", node->host, node->port); |
| | | if( ( mapIter = connectionMap.find(mapKey)) != connectionMap.end()) { |
| | | rio = mapIter->second; |
| | | } else { |
| | | rio = (rio_t *)malloc(sizeof(rio_t)); |
| | | sprintf(portstr, "%d", node->port); |
| | | clientfd = Open_clientfd(node-> host, portstr); |
| | | Rio_readinitb(rio, clientfd); |
| | | connectionMap.insert({mapKey, rio}); |
| | | if( (clientfd = connect(node->host, node->port)) < 0 ) { |
| | | continue; |
| | | } |
| | | |
| | | request_head.mod = BUS; |
| | | request_head.key = node->key; |
| | | request_head.content_length = content_size; |
| | | request_head.topic_length = strlen(topic) + 1; |
| | | if(rio_writen(rio->rio_fd, NetModSocket::encode_request_head(request_head), NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) { |
| | | 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"); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | |
| | | } |
| | | |
| | | if(rio_writen(rio->rio_fd, content, content_size) != content_size ) { |
| | | if(rio_writen(clientfd, content, content_size) != content_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent "); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | if(rio_writen(rio->rio_fd, topic, request_head.topic_length) != request_head.topic_length ) { |
| | | if(rio_writen(clientfd, topic, request_head.topic_length) != request_head.topic_length ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::pub rio_writen conent "); |
| | | exit(1); |
| | | remove_connect(node->host, node->port); |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | nsuc++; |
| | | } |
| | | return i; |
| | | return nsuc; |
| | | } |
| | | |
| | | void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) { |