| | |
| | | #include "net_mod_socket.h" |
| | | #include "socket_io.h" |
| | | #include "net_mod_socket_io.h" |
| | | #include "net_conn_pool.h" |
| | | #include <sys/types.h> /* See NOTES */ |
| | | #include <sys/socket.h> |
| | | #include <pthread.h> |
| | | |
| | | static Logger *logger = LoggerFactory::getLogger(); |
| | | |
| | | static pthread_once_t once = PTHREAD_ONCE_INIT; |
| | | static pthread_key_t poolKey; |
| | | |
| | | |
| | | |
| | | NetModSocket::NetModSocket() |
| | | { |
| | | init_req_rep_req_resp_pool(); |
| | | |
| | | if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR) err_msg(errno, "signal"); |
| | | if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
| | | logger->error(errno, "NetModSocket::NetModSocket signal"); |
| | | } |
| | | |
| | | |
| | | NetModSocket::~NetModSocket() { |
| | | int clientfd; |
| | | for (auto map_iter = req_resp_pool.connectionMap.begin(); map_iter != req_resp_pool.connectionMap.end(); map_iter++) { |
| | | clientfd = map_iter->second; |
| | | Close(clientfd); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::bind(int port) { |
| | | return shmModSocket.bind(port); |
| | | int NetModSocket::bind(int key) { |
| | | return shmModSocket.bind(key); |
| | | } |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::force_bind( int port) { |
| | | return shmModSocket.force_bind(port); |
| | | int NetModSocket::force_bind( int key) { |
| | | return shmModSocket.force_bind(key); |
| | | } |
| | | |
| | | 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) { |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, -1); |
| | | } |
| | | int NetModSocket::sendandrecv_timout(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); |
| | | 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 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) { |
| | |
| | | |
| | | } |
| | | |
| | | 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) { |
| | | |
| | | int i, n, recv_size, connfd; |
| | | /* Free thread-specific data buffer */ |
| | | void NetModSocket::_destroyConnPool_(void *_pool) |
| | | { |
| | | |
| | | NetConnPool *mpool = (NetConnPool *)_pool; |
| | | delete mpool; |
| | | logger->debug("destory connPool"); |
| | | } |
| | | |
| | | /* One-time key creation function */ |
| | | void NetModSocket::_createConnPoolKey_(void) |
| | | { |
| | | int ret; |
| | | |
| | | /* Allocate a unique thread-specific data key and save the address |
| | | of the destructor for thread-specific data buffers */ |
| | | |
| | | ret = pthread_key_create(&poolKey, _destroyConnPool_); |
| | | if (ret != 0) { |
| | | logger->error(ret, "pthread_key_create"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | 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 msec ) { |
| | | |
| | | int i, n, recv_size, connfd; |
| | | net_node_t *node; |
| | | void *recv_buf; |
| | | |
| | | 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)); |
| | | |
| | | int ret; |
| | | NetConnPool *mpool; |
| | | |
| | | /* Make first caller allocate key for thread-specific data */ |
| | | ret = pthread_once(&once, _createConnPoolKey_); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_once"); |
| | | exit(1); |
| | | } |
| | | |
| | | mpool = (NetConnPool *)pthread_getspecific(poolKey); |
| | | if (mpool == NULL) |
| | | { |
| | | /* If first call from this thread, allocate buffer for thread, and save its location */ |
| | | logger->debug("Create connPool"); |
| | | mpool = new NetConnPool(); |
| | | if (mpool == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ malloc"); |
| | | exit(1); |
| | | } |
| | | |
| | | |
| | | |
| | | ret = pthread_setspecific(poolKey, mpool); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_setspecific"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | //init_req_rep_req_resp_pool(); |
| | | |
| | | |
| | | for (i = 0; i< arrlen; i++) { |
| | | |
| | |
| | | continue; |
| | | } |
| | | |
| | | if( (connfd = connect(node)) < 0 ) { |
| | | if( (connfd = mpool->getConn(node->host, node->port)) < 0 ) { |
| | | continue; |
| | | } |
| | | |
| | |
| | | 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(connfd); |
| | | // req_resp_pool.conns[i].fd = -1; |
| | | mpool->closeConn( connfd); |
| | | } else { |
| | | n_req++; |
| | | } |
| | | |
| | | } |
| | | |
| | | // printf(" req_resp_pool.maxi = %d\n", req_resp_pool.maxi); |
| | | // 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( (req_resp_pool.nready = poll(req_resp_pool.conns, req_resp_pool.maxi + 1, timeout) ) <= 0) { |
| | | if( (mpool->nready = poll(mpool->conns, mpool->maxi + 1, msec) ) <= 0) { |
| | | // wirite_set 和 read_set 在指定时间内都没准备好 |
| | | break; |
| | | } |
| | | // 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.conns[i].fd) > 0 ) { |
| | | // 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 the descriptor is ready, echo a text line from it */ |
| | | if (req_resp_pool.conns[i].revents & POLLIN ) |
| | | if (mpool->conns[i].revents & POLLIN ) |
| | | { |
| | | req_resp_pool.nready--; |
| | | mpool->nready--; |
| | | // printf("POLLIN %d\n", connfd); |
| | | if( (n = read_response(connfd, ret_arr+n_recv_suc)) == 0) { |
| | | |
| | | // 成功收到返回消息,清空读入位 |
| | | req_resp_pool.conns[i].fd = -1; |
| | | n_recv_suc++; |
| | | // 成功收到返回消息,清空读入位 |
| | | mpool->conns[i].fd = -1; |
| | | |
| | | } else if(n == -1) { |
| | | req_resp_pool.conns[i].fd = -1; |
| | | close_connect(connfd); |
| | | } else { |
| | | req_resp_pool.conns[i].fd = -1; |
| | | |
| | | } |
| | | else if(n == -1) { |
| | | // 网络错误 |
| | | mpool->closeConn( connfd); |
| | | // mpool->conns[i].fd = -1; |
| | | } else { |
| | | // 对方key是关闭的 |
| | | mpool->conns[i].fd = -1; |
| | | } |
| | | |
| | | n_resp++; |
| | | // printf("read response %d\n", n); |
| | | |
| | | } |
| | | |
| | | if (req_resp_pool.conns[i].revents & POLLOUT ) { |
| | | if (mpool->conns[i].revents & POLLOUT ) { |
| | | // printf("poll POLLOUT %d\n", connfd); |
| | | } |
| | | |
| | | if (req_resp_pool.conns[i].revents & (POLLRDHUP | POLLHUP | POLLERR) ) |
| | | if (mpool->conns[i].revents & (POLLRDHUP | POLLHUP | POLLERR) ) |
| | | { |
| | | // printf("poll POLLERR %d\n", connfd); |
| | | req_resp_pool.nready--; |
| | | close_connect(connfd); |
| | | req_resp_pool.conns[i].fd = -1; |
| | | mpool->nready--; |
| | | mpool->closeConn( connfd); |
| | | // mpool->conns[i].fd = -1; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (i = 0; i <= req_resp_pool.maxi; i++) { |
| | | if ( (connfd = req_resp_pool.conns[i].fd) > 0 ) { |
| | | //超时后,关闭超时连接 |
| | | for (i = 0; i <= mpool->maxi; i++) { |
| | | if ( (connfd = mpool->conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | | close_connect(connfd); |
| | | req_resp_pool.conns[i].fd = -1; |
| | | mpool->closeConn( connfd); |
| | | // mpool->conns[i].fd = -1; |
| | | } |
| | | } |
| | | |
| | | req_resp_pool.maxi = -1; |
| | | mpool->maxi = -1; |
| | | |
| | | *recv_arr = ret_arr; |
| | | if(recv_arr_size != NULL) { |
| | |
| | | } |
| | | 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; |
| | | NetConnPool *mpool; |
| | | |
| | | /* Make first caller allocate key for thread-specific data */ |
| | | ret = pthread_once(&once, _createConnPoolKey_); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_once"); |
| | | exit(1); |
| | | } |
| | | |
| | | mpool = (NetConnPool *)pthread_getspecific(poolKey); |
| | | if (mpool == NULL) |
| | | { |
| | | /* If first call from this thread, allocte buffer for thread, and save its location */ |
| | | mpool = new NetConnPool(); |
| | | if (mpool == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ malloc"); |
| | | exit(1); |
| | | } |
| | | |
| | | ret = pthread_setspecific(poolKey, mpool); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_setspecific"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | |
| | | 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 = mpool->getConn(node->host, node->port)) < 0 ) { |
| | | continue; |
| | | } |
| | | request_head.mod = BUS; |
| | | memcpy(request_head.host, node->host, sizeof(request_head.host)); |
| | | 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); |
| | | mpool->closeConn( 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) { |
| | | // 网络连接错误 |
| | | mpool->closeConn( connfd); |
| | | } else { |
| | | // 对方的key是关闭的 |
| | | mpool->conns[i].fd = -1; |
| | | } |
| | | 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; |
| | | mpool->closeConn( connfd); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //超时后,关闭超时连接 |
| | | for (i = 0; i <= mpool->maxi; i++) { |
| | | if ( (connfd = mpool->conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | | mpool->closeConn( 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @key 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::sendto(const void *buf, const int size, const int port){ |
| | | return shmModSocket.sendto(buf, size,port); |
| | | int NetModSocket::sendto(const void *buf, const int size, const int key){ |
| | | return shmModSocket.sendto(buf, size, key); |
| | | } |
| | | |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::sendto_timeout(const void *buf, const int size, const int port, int sec, int nsec){ |
| | | int NetModSocket::sendto_timeout(const void *buf, const int size, const int key, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.sendto_timeout(buf, size, port, &timeout); |
| | | return shmModSocket.sendto_timeout(buf, size, key, &timeout); |
| | | } |
| | | |
| | | // 发送信息立刻返回。 |
| | | int NetModSocket::sendto_nowait(const void *buf, const int size, const int port){ |
| | | return shmModSocket.sendto_nowait(buf, size,port); |
| | | int NetModSocket::sendto_nowait(const void *buf, const int size, const int key){ |
| | | return shmModSocket.sendto_nowait(buf, size, key); |
| | | } |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @key 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::recvfrom(void **buf, int *size, int *port) { |
| | | return shmModSocket.recvfrom(buf, size, port); |
| | | int NetModSocket::recvfrom(void **buf, int *size, int *key) { |
| | | return shmModSocket.recvfrom(buf, size, key); |
| | | } |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::recvfrom_timeout(void **buf, int *size, int *port, int sec, int nsec){ |
| | | int NetModSocket::recvfrom_timeout(void **buf, int *size, int *key, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.recvfrom_timeout(buf, size, port, &timeout); |
| | | return shmModSocket.recvfrom_timeout(buf, size, key, &timeout); |
| | | } |
| | | |
| | | int NetModSocket::recvfrom_nowait(void **buf, int *size, int *port){ |
| | | return shmModSocket.recvfrom_nowait(buf, size, port); |
| | | int NetModSocket::recvfrom_nowait(void **buf, int *size, int *key){ |
| | | return shmModSocket.recvfrom_nowait(buf, size, key); |
| | | } |
| | | |
| | | /** |
| | | * 发送请求信息并等待接收应答 |
| | | * @port 发送给谁 |
| | | * @key 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::sendandrecv( const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size){ |
| | | return shmModSocket.sendandrecv(send_buf, send_size, port, recv_buf, recv_size); |
| | | int NetModSocket::sendandrecv( const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size){ |
| | | return shmModSocket.sendandrecv(send_buf, send_size, key, recv_buf, recv_size); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::sendandrecv_timeout( const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size, int sec, int nsec){ |
| | | int NetModSocket::sendandrecv_timeout( const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.sendandrecv_timeout(send_buf, send_size, port, recv_buf, recv_size, &timeout); |
| | | return shmModSocket.sendandrecv_timeout(send_buf, send_size, key, recv_buf, recv_size, &timeout); |
| | | } |
| | | int NetModSocket::sendandrecv_nowait( const void *send_buf, const int send_size, const int port, void **recv_buf, int *recv_size) { |
| | | return shmModSocket.sendandrecv_nowait(send_buf, send_size, port, recv_buf, recv_size); |
| | | int NetModSocket::sendandrecv_nowait( const void *send_buf, const int send_size, const int key, void **recv_buf, int *recv_size) { |
| | | return shmModSocket.sendandrecv_nowait(send_buf, send_size, key, recv_buf, recv_size); |
| | | } |
| | | |
| | | |
| | |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | * @key 总线端口 |
| | | */ |
| | | int NetModSocket::sub( void *topic, int size, int port){ |
| | | return shmModSocket.sub((char *)topic, size, port); |
| | | int NetModSocket::sub( void *topic, int size, int key){ |
| | | return shmModSocket.sub((char *)topic, size, key); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::sub_timeout( void *topic, int size, int port, int sec, int nsec){ |
| | | int NetModSocket::sub_timeout( void *topic, int size, int key, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.sub_timeout((char *)topic, size, port, &timeout); |
| | | return shmModSocket.sub_timeout((char *)topic, size, key, &timeout); |
| | | } |
| | | int NetModSocket::sub_nowait( void *topic, int size, int port){ |
| | | return shmModSocket.sub_nowait((char *)topic, size, port); |
| | | int NetModSocket::sub_nowait( void *topic, int size, int key){ |
| | | return shmModSocket.sub_nowait((char *)topic, size, key); |
| | | } |
| | | |
| | | |
| | |
| | | * 取消订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | * @key 总线端口 |
| | | */ |
| | | int NetModSocket::desub( void *topic, int size, int port){ |
| | | return shmModSocket.desub((char *)topic, size, port); |
| | | int NetModSocket::desub( void *topic, int size, int key){ |
| | | return shmModSocket.desub((char *)topic, size, key); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::desub_timeout( void *topic, int size, int port, int sec, int nsec){ |
| | | int NetModSocket::desub_timeout( void *topic, int size, int key, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.desub_timeout((char *)topic, size, port, &timeout); |
| | | return shmModSocket.desub_timeout((char *)topic, size, key, &timeout); |
| | | } |
| | | int NetModSocket::desub_nowait( void *topic, int size, int port){ |
| | | return shmModSocket.desub_nowait((char *)topic, size, port); |
| | | int NetModSocket::desub_nowait( void *topic, int size, int key){ |
| | | return shmModSocket.desub_nowait((char *)topic, size, key); |
| | | } |
| | | |
| | | |
| | |
| | | * 发布主题 |
| | | * @topic 主题 |
| | | * @content 主题内容 |
| | | * @port 总线端口 |
| | | * @key 总线端口 |
| | | */ |
| | | int NetModSocket::pub( char *topic, int topic_size, void *content, int content_size, int port){ |
| | | return shmModSocket.pub(topic, topic_size, content, content_size, port); |
| | | int NetModSocket::pub( char *topic, int topic_size, void *content, int content_size, int key){ |
| | | return shmModSocket.pub(topic, topic_size, content, content_size, key); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::pub_timeout( char *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec){ |
| | | int NetModSocket::pub_timeout( char *topic, int topic_size, void *content, int content_size, int key, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.pub_timeout(topic, topic_size, content, content_size, port, &timeout); |
| | | return shmModSocket.pub_timeout(topic, topic_size, content, content_size, key, &timeout); |
| | | } |
| | | int NetModSocket::pub_nowait( char *topic, int topic_size, void *content, int content_size, int port){ |
| | | return shmModSocket.pub_nowait(topic, topic_size, content, content_size, port); |
| | | int NetModSocket::pub_nowait( char *topic, int topic_size, void *content, int content_size, int key){ |
| | | return shmModSocket.pub_nowait(topic, topic_size, content, content_size, key); |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | //====================================================================================== |
| | | |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | |
| | | 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 < OPEN_MAX; i++) { |
| | | req_resp_pool.conns[i].fd = -1; |
| | | req_resp_pool.conns[i].events = 0; |
| | | } |
| | | |
| | | } |
| | | |
| | | int NetModSocket::connect( net_node_t *node) { |
| | | std::map<std::string, int>::iterator mapIter; |
| | | int connfd; |
| | | int i; |
| | | char mapKey[256]; |
| | | char portstr[32]; |
| | | |
| | | sprintf(mapKey, "%s:%d", node->host, node->port); |
| | | mapIter = req_resp_pool.connectionMap.find(mapKey); |
| | | if( mapIter != req_resp_pool.connectionMap.end()) { |
| | | connfd = mapIter->second; |
| | | // printf("hit: %s\n", mapKey); |
| | | } else { |
| | | // printf("mis: %s\n", mapKey); |
| | | sprintf(portstr, "%d", node->port); |
| | | // printf("open before: %s\n", mapKey); |
| | | connfd = open_clientfd(node->host, portstr); |
| | | // printf("open after: %s\n", mapKey); |
| | | if(connfd < 0) { |
| | | LoggerFactory::getLogger()->error(errno, "connect %s:%d ", node->host, node->port); |
| | | return -1; |
| | | } |
| | | req_resp_pool.connectionMap.insert({mapKey, connfd}); |
| | | } |
| | | |
| | | |
| | | for (i = 0; i < OPEN_MAX; i++) { /* Find an available slot */ |
| | | if (req_resp_pool.conns[i].fd < 0) |
| | | { |
| | | /* Add connected descriptor to the req_resp_pool */ |
| | | req_resp_pool.conns[i].fd = connfd; |
| | | |
| | | req_resp_pool.conns[i].events = POLLIN; |
| | | /* Add the descriptor to descriptor set */ |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (i > req_resp_pool.maxi) |
| | | req_resp_pool.maxi = i; |
| | | |
| | | if (i == OPEN_MAX) { |
| | | /* Couldn't find an empty slot */ |
| | | LoggerFactory::getLogger()->error(errno, "add_client error: Too many clients"); |
| | | return -1; |
| | | } |
| | | |
| | | |
| | | return connfd; |
| | | } |
| | | |
| | | void NetModSocket::close_connect(int connfd) { |
| | | int i; |
| | | char mapKey[256]; |
| | | std::map<std::string, int>::iterator map_iter; |
| | | if(close(connfd) != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::close_connect close"); |
| | | } |
| | | |
| | | |
| | | for (i = 0; i <= req_resp_pool.maxi; i++) { |
| | | if(req_resp_pool.conns[i].fd == connfd) { |
| | | req_resp_pool.conns[i].fd = -1; |
| | | } |
| | | } |
| | | |
| | | for ( map_iter = req_resp_pool.connectionMap.begin(); map_iter != req_resp_pool.connectionMap.end(); ) { |
| | | if(connfd == map_iter->second) { |
| | | // std::cout << "map_iter->first==" << map_iter->first << std::endl; |
| | | map_iter = req_resp_pool.connectionMap.erase(map_iter); |
| | | } else { |
| | | ++map_iter; |
| | | } |
| | | } |
| | | |
| | | LoggerFactory::getLogger()->debug( "closed %d\n", connfd); |
| | | |
| | | } |
| | | /** |
| | | uint32_t mod; |
| | | char host[NI_MAXHOST]; |
| | |
| | | |
| | | 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; |
| | | } |
| | |
| | | |
| | | return head; |
| | | } |
| | | |