| | |
| | | int NetModServerSocket::process_client(int connfd) { |
| | | net_mod_request_head_t request_head; |
| | | net_mod_response_head_t response_head; |
| | | int ret; |
| | | char request_head_bs[NET_MODE_REQUEST_HEAD_LENGTH]; |
| | | void *recv_buf; |
| | | // char tmp[8196]; |
| | | int recv_size, response_buf_size; |
| | | struct timespec timeout; |
| | | |
| | | if (rio_readn(connfd, request_head_bs, NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) |
| | | { |
| | |
| | | } |
| | | |
| | | request_head = NetModSocket::decode_request_head(request_head_bs); |
| | | // printf("server received request from host = %s:%d\n", request_head.host, request_head.port); |
| | | printf("server received request from host = %s:%d, key = %d, timeout=%d,\n", |
| | | request_head.host, request_head.port , request_head.key, request_head.timeout); |
| | | |
| | | if(request_head.content_length > max_buf) { |
| | | |
| | |
| | | return -1; |
| | | } |
| | | |
| | | |
| | | if(request_head.mod == REQ_REP) { |
| | | |
| | | memcpy(response_head.host, request_head.host, NI_MAXHOST); |
| | | response_head.port = request_head.port; |
| | | response_head.key = request_head.key; |
| | | if(shmModSocket.sendandrecv_unsafe(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size) != 0) { |
| | | |
| | | |
| | | if(request_head.timeout > 0) { |
| | | timeout.tv_sec = request_head.timeout / 1000; |
| | | timeout.tv_nsec = (request_head.timeout - timeout.tv_sec * 1000) * 10e6; |
| | | |
| | | // printf(" timeout.tv_sec = %d, timeout.tv_nsec=%ld\n", timeout.tv_sec, timeout.tv_nsec ); |
| | | |
| | | ret = shmModSocket.sendandrecv_unsafe_timeout(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size, &timeout); |
| | | } |
| | | else if(request_head.timeout == 0) { |
| | | ret = shmModSocket.sendandrecv_unsafe_nowait(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size); |
| | | } |
| | | else if(request_head.timeout == -1) { |
| | | ret = shmModSocket.sendandrecv_unsafe(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size); |
| | | } |
| | | |
| | | if( ret != 0) { |
| | | // 没有对应的key |
| | | response_head.code = 1; |
| | | response_head.code = ret; |
| | | response_head.content_length = 0; |
| | | if( rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH ) |
| | | return -1; |
| | |
| | | |
| | | |
| | | } else if(request_head.mod == BUS) { |
| | | // TODO: pub response |
| | | if(request_head.topic_length > max_topic_buf) { |
| | | if( (topic_buf = realloc(topic_buf, request_head.topic_length)) == NULL ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModServerSocket::process_client realloc topic_buf"); |
| | |
| | | return -1; |
| | | } |
| | | LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); |
| | | shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); |
| | | memcpy(response_head.host, request_head.host, NI_MAXHOST); |
| | | response_head.port = request_head.port; |
| | | response_head.key = request_head.key; |
| | | |
| | | if(request_head.timeout > 0) { |
| | | timeout.tv_sec = request_head.timeout / 1000; |
| | | timeout.tv_nsec = (request_head.timeout - timeout.tv_sec * 1000) * 10e6; |
| | | ret = shmModSocket.pub_timeout((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key, &timeout); |
| | | } |
| | | else if(request_head.timeout == 0) { |
| | | ret = shmModSocket.pub_nowait((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); |
| | | } |
| | | else if(request_head.timeout == -1) { |
| | | ret = shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); |
| | | } |
| | | |
| | | response_head.code = ret; |
| | | response_head.content_length = 0; |
| | | if( rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH ) |
| | | return -1; |
| | | |
| | | |
| | | } |
| | | |
| | | return 0; |
| | |
| | | _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 timeout) { |
| | | _sendandrecv_(node_arr, arrlen, send_buf,send_size, recv_arr, recv_arr_size, timeout); |
| | | 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) { |
| | |
| | | // printf("open after: %s\n", mapKey); |
| | | if(connfd < 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::connect %s:%d ", node->host, node->port); |
| | | exit(1); |
| | | return -1; |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | LoggerFactory::getLogger()->debug( "closed %d\n", connfd); |
| | | // LoggerFactory::getLogger()->debug( "NetModSocket::close_connect %d\n", connfd); |
| | | |
| | | } |
| | | |
| | | 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) { |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int msec ) { |
| | | |
| | | int i, n, recv_size, connfd; |
| | | net_node_t *node; |
| | |
| | | |
| | | 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)); |
| | |
| | | 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(mpool, connfd); |
| | | // mpool.conns[i].fd = -1; |
| | | } else { |
| | | n_req++; |
| | | } |
| | |
| | | // 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( (mpool.nready = poll(mpool.conns, mpool.maxi + 1, timeout) ) <= 0) { |
| | | if( (mpool.nready = poll(mpool.conns, mpool.maxi + 1, msec) ) <= 0) { |
| | | // wirite_set 和 read_set 在指定时间内都没准备好 |
| | | break; |
| | | } |
| | |
| | | mpool.nready--; |
| | | // printf("POLLIN %d\n", connfd); |
| | | if( (n = read_response(connfd, ret_arr+n_recv_suc)) == 0) { |
| | | |
| | | // 成功收到返回消息,清空读入位 |
| | | mpool.conns[i].fd = -1; |
| | | n_recv_suc++; |
| | | |
| | | } else if(n == -1) { |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | } else { |
| | | mpool.conns[i].fd = -1; |
| | | |
| | | } |
| | | // else if(n == -1) { |
| | | // // 网络错误 |
| | | // } else { |
| | | // // 对方key是关闭的 |
| | | // } |
| | | |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | n_resp++; |
| | | // printf("read response %d\n", n); |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | //超时后,关闭超时连接 |
| | | for (i = 0; i <= mpool.maxi; i++) { |
| | | if ( (connfd = mpool.conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | |
| | | } |
| | | 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; |
| | | |
| | | pool mpool; |
| | | init_conn_pool(mpool); |
| | | |
| | | |
| | | 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 = connect(mpool, node)) < 0 ) { |
| | | continue; |
| | | } |
| | | request_head.mod = BUS; |
| | | 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); |
| | | close_connect(mpool, 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) { |
| | | // // 网络连接错误 |
| | | // } else { |
| | | // // 对方的key是关闭的 |
| | | // } |
| | | mpool.conns[i].fd = -1; |
| | | close_connect(mpool, connfd); |
| | | 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; |
| | | close_connect(mpool, connfd); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //超时后,关闭超时连接 |
| | | for (i = 0; i <= mpool.maxi; i++) { |
| | | if ( (connfd = mpool.conns[i].fd) > 0 ) { |
| | | // 关闭并清除写入或读取失败的连接 |
| | | close_connect(mpool, 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; |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | //====================================================================================== |
| | | |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | |
| | | |
| | | 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; |
| | | } |
| | |
| | | #define GET(p) (*(uint32_t *)(p)) |
| | | #define PUT(p, val) (*(uint32_t *)(p) = (val)) |
| | | |
| | | |
| | | #define ERROR_NET_MOD_SOCKET_ADDR_IN_USE 1 |
| | | |
| | | |
| | | |
| | | #define GET_INT32(p) (*(int32_t *)(p)) |
| | | #define PUT_INT32(p, val) (*(int32_t *)(p) = (val)) |
| | | |
| | | class NetModServerSocket; |
| | | |
| | |
| | | int key; |
| | | }; |
| | | |
| | | #define NET_MODE_REQUEST_HEAD_LENGTH (NI_MAXHOST + 5 * sizeof(uint32_t)) |
| | | #define NET_MODE_REQUEST_HEAD_LENGTH (NI_MAXHOST + 6 * sizeof(uint32_t)) |
| | | |
| | | |
| | | // 请求头 |
| | | struct net_mod_request_head_t { |
| | | uint32_t mod; |
| | | char host[NI_MAXHOST]; |
| | | uint32_t port; |
| | | uint32_t key; |
| | | uint32_t content_length; |
| | | uint32_t topic_length; |
| | | uint32_t content_length; // 请求内容 |
| | | uint32_t topic_length; // 请求主题 |
| | | int32_t timeout; // -1 block, 0 nowait , > 0 timeout |
| | | }; |
| | | |
| | | #define NET_MODE_RESPONSE_HEAD_LENGTH (NI_MAXHOST + 4 * sizeof(uint32_t)) |
| | | |
| | | |
| | | // 应答头 |
| | | struct net_mod_response_head_t { |
| | | // socket_mod_t mod; |
| | | char host[NI_MAXHOST]; |
| | | uint32_t port; |
| | | uint32_t key; |
| | | uint32_t code; |
| | | uint32_t code; //返回值, 0 为成功 |
| | | uint32_t content_length; |
| | | }; |
| | | |
| | |
| | | int connect(pool& mpool, net_node_t* node); |
| | | void close_connect(pool& mpool , int connfd); |
| | | int read_response(int clientfd, net_mod_recv_msg_t *recv_msg); |
| | | int write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size); |
| | | int write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size, void *topic_buf, int topic_size); |
| | | |
| | | int _sendandrecv_(net_node_t *node_arr, int node_arr_len, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size, int timeout); |
| | | |
| | | int _pub_(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size, int timeout) ; |
| | | |
| | | |
| | | public: |
| | |
| | | */ |
| | | int start_bus(); |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @topic 主题,@topic_size 该主题的长度 |
| | | * @content 内容,@content_size 内容长度 |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | int pub(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size) ; |
| | | |
| | | int pub_nowait(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size); |
| | | /** |
| | | * @msec 毫秒 (千分之一秒) |
| | | */ |
| | | int pub_timeout(net_node_t *node_arr, int arrlen, char *topic, int topic_size, void *content, int content_size, int msec); |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @topic 主题,@topic_size 该主题的长度 |
| | | * @content 内容,@content_size 内容长度 |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | int pub(net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取soket key |
| | |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->pub(node_arr, node_arr_len, topic, topic_size, content, content_size); |
| | | } |
| | | int net_mod_socket_pub_timeout(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout){ |
| | | return 0; |
| | | int net_mod_socket_pub_timeout(void *_socket, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int msec){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->pub_timeout(node_arr, node_arr_len, topic, topic_size, content, content_size, msec); |
| | | } |
| | | int net_mod_socket_pub_nowait(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size){ |
| | | return 0; |
| | | int net_mod_socket_pub_nowait(void *_socket, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->pub_nowait(node_arr, node_arr_len, topic, topic_size, content, content_size); |
| | | } |
| | | |
| | | |
| | |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | int net_mod_socket_pub(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | int net_mod_socket_pub_timeout(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int timeout); |
| | | /* |
| | | * @msec 毫秒(千分之一秒) |
| | | */ |
| | | int net_mod_socket_pub_timeout(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size, int msec); |
| | | int net_mod_socket_pub_nowait(void *_sockt, net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | |
| | | /** |
| | |
| | | PLATFORM=$(shell $(ROOT)/systype.sh) |
| | | include $(ROOT)/Make.defines.$(PLATFORM) |
| | | |
| | | RPATH += -Wl,-rpath=$(ROOT)/lib:$(DEST)/lib |
| | | #RPATH += -Wl,-rpath=$(ROOT)/lib:$(DEST)/lib |
| | | # 开源工具包路径 |
| | | LDDIR += -L$(DEST)/lib |
| | | |
| | |
| | | |
| | | if (fgets(content, MAXLINE, stdin) != NULL) { |
| | | // 收到消息的节点即使没有对应的信息, 也要回复一个表示无的消息,否则会一直等待 |
| | | n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, content, |
| | | strlen(content), &recv_arr, &recv_arr_size, 5000); |
| | | n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, |
| | | strlen(content), &recv_arr, &recv_arr_size); |
| | | printf("send %d nodes\n", n); |
| | | for(i=0; i<recv_arr_size; i++) { |
| | | printf("host:%s, port: %d, key:%d, content: %s\n", |