From 350b7fe82c54117f423fc1bc174f6f65ba855fc4 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期五, 16 十月 2020 09:56:53 +0800 Subject: [PATCH] update --- src/socket/net_mod_socket.c | 219 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 181 insertions(+), 38 deletions(-) diff --git a/src/socket/net_mod_socket.c b/src/socket/net_mod_socket.c index bdfa85d..aebd15b 100644 --- a/src/socket/net_mod_socket.c +++ b/src/socket/net_mod_socket.c @@ -1,9 +1,11 @@ #include "net_mod_socket.h" #include "socket_io.h" #include "net_mod_socket_io.h" +#include <sys/types.h> /* See NOTES */ +#include <sys/socket.h> -std::map<std::string, rio_t *> NetModSocket::connectionMap; +std::map<std::string, int> NetModSocket::connectionMap; NetModSocket::NetModSocket() { @@ -11,19 +13,55 @@ } +NetModSocket::~NetModSocket() { + int clientfd; + for (auto map_iter = connectionMap.begin(); map_iter != connectionMap.end(); map_iter++) { + 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 ** resp_arr, int *resp_arr_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; - net_mod_request_head_t request_head; + 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 optval; + + 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++) { @@ -34,63 +72,141 @@ 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; - if( (n = rio_writen(rio->rio_fd, &request_head, sizeof(request_head))) != sizeof(request_head)) { - err_exit(errno, "NetModSocket::send head rio_writen"); + 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; } - if( (n = rio_writen(rio->rio_fd, send_buf, send_size)) != send_size ) { - err_exit(errno, "NetModSocket::send conent rio_writen"); + if(rio_writen(clientfd, send_buf, send_size) != send_size ) { + LoggerFactory::getLogger()->error(errno, "NetModSocket::send conent rio_writen"); + remove_connect(node->host, node->port); + close(clientfd); + continue; + } + // optval = 0; + // setsockopt(clientfd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval)); + + 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 ((n = rio_readnb(rio, &response_head, sizeof(response_head))) != sizeof(response_head)) { - err_exit(errno, "NetModSocket::send rio_readnb"); - } + response_head = NetModSocket::decode_response_head(response_head_bs); recv_buf = malloc(response_head.content_length); if(recv_buf == NULL) { - err_exit(errno, "NetModSocket::send malloc"); + 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) { - err_exit(errno, "NetModSocket::send rio_readnb"); + 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: - strcpy( ret_arr[i].host, node->host); - 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; - } - *resp_arr = ret_arr; - *resp_arr_size = i; + if(node->host != NULL) { + strcpy( ret_arr[nsuc].host, node->host); + } else { + strcpy( ret_arr[nsuc].host, "local"); + } + + 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; - return i; + nsuc++; + } + + *recv_arr = ret_arr; + if(recv_arr_size != NULL) { + *recv_arr_size = nsuc; + } + + 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; + 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; + 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 { + 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(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; + + } + + if(rio_writen(clientfd, content, content_size) != content_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++; + } + return nsuc; +} void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) { for(int i =0; i< size; i++) { free(arr[i].content); } - + free(arr); } // ssize_t recv(void *buf, size_t len) { @@ -99,7 +215,34 @@ // } -NetModSocket::~NetModSocket() { - +void * NetModSocket::encode_request_head(net_mod_request_head_t & request) { + char * head = (char *)malloc(NET_MODE_REQUEST_HEAD_LENGTH); + PUT(head, htonl(request.mod)); + PUT(head + 4, htonl(request.key)); + PUT(head + 8, htonl(request.content_length)); + PUT(head + 12, htonl(request.topic_length)); + return head; } +net_mod_request_head_t NetModSocket::decode_request_head(void *_headbs) { + char *headbs = (char *)_headbs; + net_mod_request_head_t head; + head.mod = ntohl(GET(headbs)); + head.key = ntohl(GET(headbs + 4)); + head.content_length = ntohl(GET(headbs + 8)); + head.topic_length = ntohl(GET(headbs + 12)); + return head; +} + +void * NetModSocket::encode_response_head(net_mod_response_head_t & response) { + char * head = (char *)malloc(NET_MODE_RESPONSE_HEAD_LENGTH); + PUT(head, htonl(response.content_length)); + return head; +} + +net_mod_response_head_t NetModSocket::decode_response_head(void *_headbs) { + char *headbs = (char *)_headbs; + net_mod_response_head_t head; + head.content_length = ntohl(GET(headbs)); + return head; +} -- Gitblit v1.8.0