| | |
| | | #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> |
| | |
| | | |
| | | NetModSocket::NetModSocket() |
| | | { |
| | | int s; |
| | | if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
| | | logger->error(errno, "NetModSocket::NetModSocket signal"); |
| | | |
| | | gpool = new NetConnPool(); |
| | | |
| | | pthread_mutexattr_t mtxAttr; |
| | | s = pthread_mutexattr_init(&mtxAttr); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutexattr_init"); |
| | | s = pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutexattr_settype"); |
| | | s = pthread_mutex_init(&sendMutex, &mtxAttr); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutex_init"); |
| | | |
| | | s = pthread_mutexattr_destroy(&mtxAttr); |
| | | if (s != 0) |
| | | err_exit(s, "pthread_mutexattr_destroy"); |
| | | |
| | | } |
| | | |
| | | |
| | | NetModSocket::~NetModSocket() { |
| | | |
| | | int s; |
| | | delete gpool; |
| | | s = pthread_mutex_destroy(&sendMutex); |
| | | if(s != 0) { |
| | | err_exit(s, "shm_close_socket"); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | 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); |
| | | return _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 msec) { |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, msec); |
| | | return _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) { |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, 0); |
| | | return _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, 0); |
| | | |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | 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 ) { |
| | | NetConnPool* NetModSocket::_get_threadlocal_pool() { |
| | | |
| | | int i, n, recv_size, connfd; |
| | | net_node_t *node; |
| | | void *recv_buf = NULL; |
| | | struct timespec timeout; |
| | | int ret; |
| | | int n_req = 0, n_recv_suc = 0, n_resp =0; |
| | | |
| | | net_mod_request_head_t request_head = {}; |
| | | |
| | | net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t)); |
| | | |
| | | |
| | | NetConnPool *mpool; |
| | | |
| | | /* Make first caller allocate key for thread-specific data */ |
| | | ret = pthread_once(&once, _createConnPoolKey_); |
| | | if (ret != 0) { |
| | |
| | | exit(1); |
| | | } |
| | | |
| | | |
| | | |
| | | ret = pthread_setspecific(poolKey, mpool); |
| | | if (ret != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::_sendandrecv_ pthread_setspecific"); |
| | | exit(1); |
| | | } |
| | | } |
| | | return mpool; |
| | | |
| | | } |
| | | |
| | | NetConnPool* NetModSocket::_get_pool() { |
| | | return _get_threadlocal_pool(); |
| | | } |
| | | |
| | | |
| | | 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 = NULL; |
| | | struct timespec timeout; |
| | | int ret; |
| | | int n_req = 0, n_recv_suc = 0, n_resp =0; |
| | | |
| | | net_mod_request_head_t request_head = {}; |
| | | |
| | | net_mod_recv_msg_t *ret_arr = (net_mod_recv_msg_t *)calloc(arrlen, sizeof(net_mod_recv_msg_t)); |
| | | |
| | | |
| | | NetConnPool *mpool = _get_pool(); |
| | | |
| | | for (i = 0; i< arrlen; i++) { |
| | | |
| | |
| | | ret = shmModSocket.sendandrecv(send_buf, send_size, node->key, &recv_buf, &recv_size); |
| | | } |
| | | if( ret == 0) { |
| | | strcpy( ret_arr[n_recv_suc].host,""); |
| | | strcpy( ret_arr[n_recv_suc].host, ""); |
| | | 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++; |
| | | } else { |
| | | if(ret > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("NetModSocket:: %d _sendandrecv_ to key %d failed, %s", get_key(), node->key, bus_strerror(ret)); |
| | | |
| | | } else { |
| | | logger->error(ret, "NetModSocket:: %d _sendandrecv_ to key %d failed", get_key(), node->key); |
| | | } |
| | | } |
| | | |
| | | |
| | | continue; |
| | | } |
| | |
| | | mpool->conns[i].fd = -1; |
| | | |
| | | } |
| | | else if(n == -1) { |
| | | else if(n == EBUS_NET) { |
| | | // 网络错误 |
| | | mpool->closeConn( connfd); |
| | | // mpool->conns[i].fd = -1; |
| | | } else { |
| | | // 对方key是关闭的 |
| | | // 代理服务没有转发成功 |
| | | mpool->conns[i].fd = -1; |
| | | } |
| | | |
| | |
| | | // printf("poll POLLERR %d\n", connfd); |
| | | mpool->nready--; |
| | | mpool->closeConn( connfd); |
| | | // mpool->conns[i].fd = -1; |
| | | } |
| | | } |
| | | } |
| | |
| | | if ( (connfd = mpool->conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | | mpool->closeConn( connfd); |
| | | // mpool->conns[i].fd = -1; |
| | | } |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | | NetConnPool *mpool = _get_pool(); |
| | | |
| | | // 本地发送 |
| | | if(node_arr == NULL || arrlen == 0) { |
| | | if(msec == 0) { |
| | | ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, node->key); |
| | | ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, SHM_BUS_KEY); |
| | | } else if(msec > 0) { |
| | | timeout.tv_sec = msec / 1000; |
| | | timeout.tv_nsec = (msec - timeout.tv_sec * 1000) * 10e6; |
| | | ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, node->key, &timeout); |
| | | ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, SHM_BUS_KEY, &timeout); |
| | | } else { |
| | | ret = shmModSocket.pub(topic, topic_size, content, content_size, node->key); |
| | | ret = shmModSocket.pub(topic, topic_size, content, content_size, SHM_BUS_KEY); |
| | | } |
| | | if(ret == 0 ) { |
| | | n_pub_suc++; |
| | | } |
| | | return n_pub_suc; |
| | | } |
| | | |
| | | for (i = 0; i < arrlen; i++) { |
| | |
| | | if(node->host == NULL) { |
| | | // 本地发送 |
| | | if(msec == 0) { |
| | | ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, node->key); |
| | | ret = shmModSocket.pub_nowait(topic, topic_size, content, content_size, SHM_BUS_KEY); |
| | | } else if(msec > 0) { |
| | | timeout.tv_sec = msec / 1000; |
| | | timeout.tv_nsec = (msec - timeout.tv_sec * 1000) * 10e6; |
| | | ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, node->key, &timeout); |
| | | ret = shmModSocket.pub_timeout(topic, topic_size, content, content_size, SHM_BUS_KEY, &timeout); |
| | | } else { |
| | | ret = shmModSocket.pub(topic, topic_size, content, content_size, node->key); |
| | | ret = shmModSocket.pub(topic, topic_size, content, content_size, SHM_BUS_KEY); |
| | | } |
| | | |
| | | if(ret == 0 ) { |
| | |
| | | } |
| | | request_head.mod = BUS; |
| | | memcpy(request_head.host, node->host, sizeof(request_head.host)); |
| | | request_head.key = node->key; |
| | | request_head.key = SHM_BUS_KEY; |
| | | request_head.content_length = content_size; |
| | | request_head.topic_length = strlen(topic) + 1; |
| | | request_head.timeout = msec; |
| | |
| | | mpool->conns[i].fd = -1; |
| | | n_pub_suc++; |
| | | } |
| | | else if(ret == -1) { |
| | | else if(ret == EBUS_NET) { |
| | | // 网络连接错误 |
| | | mpool->closeConn( connfd); |
| | | } else { |
| | | // 对方的key是关闭的 |
| | | // 代理服务没有转发成功 |
| | | mpool->conns[i].fd = -1; |
| | | } |
| | | n_resp++; |
| | |
| | | return n_pub_suc; |
| | | } |
| | | |
| | | |
| | | 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; |
| | | |
| | | |
| | | 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)); |
| | | |
| | | 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); |
| | | goto LABEL_ARR_PUSH; |
| | | } |
| | | |
| | | 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) { |
| | | if((buf = (char *)realloc(buf, buf_size)) == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv_safe realloc buf"); |
| | | } else { |
| | | 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)); |
| | | 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::sendandrecv_safe rio_writen buf"); |
| | | |
| | | 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::sendandrecv_safe rio_readnb response_head_bs"); |
| | | |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | response_head = NetModSocket::decode_response_head(response_head_bs); |
| | | if(response_head.code != 0) { |
| | | continue; |
| | | } |
| | | |
| | | 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(clientfd, recv_buf, response_head.content_length) ) != response_head.content_length) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::sendandrecv_safe rio_readnb recv_buf"); |
| | | |
| | | close(clientfd); |
| | | continue; |
| | | } |
| | | |
| | | LABEL_ARR_PUSH: |
| | | 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; |
| | | |
| | | nsuc++; |
| | | } |
| | | |
| | | *recv_arr = ret_arr; |
| | | if(recv_arr_size != NULL) { |
| | | *recv_arr_size = nsuc; |
| | | } |
| | | |
| | | free(buf); |
| | | return nsuc; |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 发送信息 |
| | | * @key 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::sendto(const void *buf, const int size, const int key){ |
| | | return shmModSocket.sendto(buf, size, key); |
| | | int rv = shmModSocket.sendto(buf, size, key); |
| | | if(rv == 0) { |
| | | logger->debug("NetModSocket::sendto: %d sendto %d success.\n", get_key(), key); |
| | | return 0; |
| | | } |
| | | |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("NetModSocket::sendto: %d sendto %d failed %s", get_key(), key, bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "NetModSocket::sendto : %d sendto %d failed", get_key(), key); |
| | | } |
| | | return rv; |
| | | } |
| | | |
| | | // 发送信息超时返回。 @sec 秒 , @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, key, &timeout); |
| | | int rv = shmModSocket.sendto_timeout(buf, size, key, &timeout); |
| | | if(rv == 0) { |
| | | logger->debug("NetModSocket::sendto_timeout: %d sendto %d success.\n", get_key(), key); |
| | | return 0; |
| | | } |
| | | |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("NetModSocket::sendto_timeout : %d sendto %d failed %s", get_key(), key, bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "NetModSocket::sendto_timeout: %d sendto %d failed", get_key(), key); |
| | | } |
| | | return rv; |
| | | } |
| | | |
| | | // 发送信息立刻返回。 |
| | | int NetModSocket::sendto_nowait(const void *buf, const int size, const int key){ |
| | | return shmModSocket.sendto_nowait(buf, size, key); |
| | | int rv = shmModSocket.sendto_nowait(buf, size, key); |
| | | if(rv == 0) { |
| | | logger->debug("NetModSocket::sendto_nowait: %d sendto %d success.\n", get_key(), key); |
| | | return 0; |
| | | } |
| | | |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("NetModSocket::sendto_nowait %d sendto %d failed %s", get_key(), key, bus_strerror(rv)); |
| | | |
| | | } else { |
| | | logger->error(rv, "NetModSocket::sendto_nowait %d sendto %d failed", get_key(), key); |
| | | } |
| | | return rv; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::recvfrom(void **buf, int *size, int *key) { |
| | | return shmModSocket.recvfrom(buf, size, key); |
| | | int rv = shmModSocket.recvfrom(buf, size, key); |
| | | |
| | | if(rv == 0) { |
| | | logger->debug("NetModSocket::recvfrom: %d recvfrom %d success.\n", get_key(), *key); |
| | | return 0; |
| | | } |
| | | |
| | | if(rv > EBUS_BASE) { |
| | | logger->debug("NetModSocket::recvfrom: socket %d recvfrom failed %s", get_key(), bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "NetModSocket::recvfrom: socket %d recvfrom failed", get_key()); |
| | | } |
| | | return rv; |
| | | } |
| | | |
| | | // 接受信息超时返回。 @sec 秒 , @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, key, &timeout); |
| | | int rv = shmModSocket.recvfrom_timeout(buf, size, key, &timeout); |
| | | if(rv == 0) { |
| | | logger->debug("NetModSocket::recvfrom: %d recvfrom %d success.\n", get_key(), *key); |
| | | return 0; |
| | | } |
| | | |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("NetModSocket::recvfrom_timeout: %d recvfrom failed %s", get_key(), bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "NetModSocket::recvfrom_timeout: %d recvfrom failed", get_key()); |
| | | } |
| | | return rv; |
| | | } |
| | | |
| | | int NetModSocket::recvfrom_nowait(void **buf, int *size, int *key){ |
| | | return shmModSocket.recvfrom_nowait(buf, size, key); |
| | | int rv = shmModSocket.recvfrom_nowait(buf, size, key); |
| | | if(rv == 0) { |
| | | logger->debug("NetModSocket::recvfrom: %d recvfrom %d success.\n", get_key(), *key); |
| | | return 0; |
| | | } |
| | | |
| | | if(rv > EBUS_BASE) { |
| | | // bus_errno = EBUS_TIMEOUT; |
| | | logger->debug("NetModSocket::recvfrom_nowait: %d recvfrom failed %s", get_key(), bus_strerror(rv)); |
| | | } else { |
| | | logger->error(rv, "NetModSocket::recvfrom_nowait: %d recvfrom failed", get_key()); |
| | | } |
| | | return rv; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * @return 0 成功, 1 对方没有对应的key, -1 网络错误 |
| | | * @return 0 成功, EBUS_NET 网络错误, 其他值 代理服务没有转发成功。 |
| | | * |
| | | */ |
| | | int NetModSocket::read_response(int connfd, net_mod_recv_msg_t *recv_msg) { |
| | |
| | | // printf(">>>> read_response %s\n", response_head.host); |
| | | if(response_head.code != 0) { |
| | | // 代理服务没能成功发送给对应的key |
| | | return 1; |
| | | return response_head.code; |
| | | } |
| | | |
| | | recv_buf = malloc(response_head.content_length); |
| | |
| | | } |
| | | if ( (recv_size = rio_readn(connfd, recv_buf, response_head.content_length) ) != response_head.content_length) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::read_response rio_readnb recv_buf"); |
| | | |
| | | return -1; |
| | | //网络错误 |
| | | return EBUS_NET; |
| | | } |
| | | |
| | | strcpy( recv_msg->host, response_head.host); |