| | |
| | | } |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::bind(int port) { |
| | | return shmModSocket.bind(port); |
| | | } |
| | | |
| | | 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; |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::force_bind( int port) { |
| | | return shmModSocket.force_bind(port); |
| | | } |
| | | |
| | | 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); |
| | | |
| | | } |
| | | |
| | | |
| | | 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_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); |
| | | |
| | | } |
| | | |
| | | 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; |
| | | net_node_t *node; |
| | | void *recv_buf; |
| | | int timeout = 5 * 1000; |
| | | |
| | | net_mod_request_head_t request_head = {}; |
| | | |
| | | int n_req = 0, n_recv_suc = 0, n_resp; |
| | |
| | | |
| | | } |
| | | |
| | | int NetModSocket::write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size) { |
| | | |
| | | int buf_size; |
| | | char *buf; |
| | | int max_buf_size; |
| | | if((buf = (char *)malloc(MAXBUF)) == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::NetModSocket malloc"); |
| | | exit(1); |
| | | } else { |
| | | max_buf_size = MAXBUF; |
| | | } |
| | | |
| | | 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::write_request realloc buf"); |
| | | exit(1); |
| | | } 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, send_buf, send_size); |
| | | |
| | | if(rio_writen(clientfd, buf, buf_size) != buf_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::write_request rio_writen"); |
| | | free(buf); |
| | | return -1; |
| | | } |
| | | free(buf); |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * @return 0 成功, 1 对方没有对应的key, -1 网络错误 |
| | | * |
| | | */ |
| | | int NetModSocket::read_response(int connfd, net_mod_recv_msg_t *recv_msg) { |
| | | int recv_size; |
| | | void *recv_buf; |
| | | char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH]; |
| | | |
| | | net_mod_response_head_t response_head; |
| | | if ( rio_readn(connfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::read_response rio_readnb response_head"); |
| | | |
| | | return -1; |
| | | } |
| | | |
| | | response_head = NetModSocket::decode_response_head(response_head_bs); |
| | | // printf(">>>> read_response %s\n", response_head.host); |
| | | if(response_head.code != 0) { |
| | | // 对方没有对应的key |
| | | return 1; |
| | | } |
| | | |
| | | recv_buf = malloc(response_head.content_length); |
| | | if(recv_buf == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send malloc recv_buf"); |
| | | exit(1); |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | strcpy( recv_msg->host, response_head.host); |
| | | recv_msg->port = response_head.port; |
| | | recv_msg->key = response_head.key; |
| | | recv_msg->content = recv_buf; |
| | | recv_msg->content_length = recv_size; |
| | | return 0; |
| | | } |
| | | |
| | | 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) { |
| | |
| | | return nsuc; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::sendto(const void *buf, const int size, const int port){ |
| | | return shmModSocket.sendto(buf, size,port); |
| | | } |
| | | |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::sendto_timeout(const void *buf, const int size, const int port, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.sendto_timeout(buf, size, port, &timeout); |
| | | } |
| | | |
| | | // 发送信息立刻返回。 |
| | | int NetModSocket::sendto_nowait(const void *buf, const int size, const int port){ |
| | | return shmModSocket.sendto_nowait(buf, size,port); |
| | | } |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::recvfrom(void **buf, int *size, int *port) { |
| | | return shmModSocket.recvfrom(buf, size, port); |
| | | } |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::recvfrom_timeout(void **buf, int *size, int *port, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.recvfrom_timeout(buf, size, port, &timeout); |
| | | } |
| | | |
| | | int NetModSocket::recvfrom_nowait(void **buf, int *size, int *port){ |
| | | return shmModSocket.recvfrom_nowait(buf, size, port); |
| | | } |
| | | |
| | | /** |
| | | * 发送请求信息并等待接收应答 |
| | | * @port 发送给谁 |
| | | * @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); |
| | | } |
| | | // 超时返回。 @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){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.sendandrecv_timeout(send_buf, send_size, port, 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); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int NetModSocket::start_bus() { |
| | | return shmModSocket.start_bus(); |
| | | } |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int NetModSocket::sub( void *topic, int size, int port){ |
| | | return shmModSocket.sub((char *)topic, size, port); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::sub_timeout( void *topic, int size, int port, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.sub_timeout((char *)topic, size, port, &timeout); |
| | | } |
| | | int NetModSocket::sub_nowait( void *topic, int size, int port){ |
| | | return shmModSocket.sub_nowait((char *)topic, size, port); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int NetModSocket::desub( void *topic, int size, int port){ |
| | | return shmModSocket.desub((char *)topic, size, port); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::desub_timeout( void *topic, int size, int port, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.desub_timeout((char *)topic, size, port, &timeout); |
| | | } |
| | | int NetModSocket::desub_nowait( void *topic, int size, int port){ |
| | | return shmModSocket.desub_nowait((char *)topic, size, port); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 发布主题 |
| | | * @topic 主题 |
| | | * @content 主题内容 |
| | | * @port 总线端口 |
| | | */ |
| | | 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); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int NetModSocket::pub_timeout( char *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec){ |
| | | struct timespec timeout = {sec, nsec}; |
| | | return shmModSocket.pub_timeout(topic, topic_size, content, content_size, port, &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); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取soket端口号 |
| | | */ |
| | | int NetModSocket::get_port() { |
| | | return shmModSocket.get_port(); |
| | | } |
| | | |
| | | |
| | | void NetModSocket::free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size) { |
| | | |
| | | for(int i =0; i< size; i++) { |
| | |
| | | free(arr); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | //====================================================================================== |
| | | |
| | | |
| | | |
| | | int NetModSocket::write_request(int clientfd, net_mod_request_head_t &request_head, void *send_buf, int send_size) { |
| | | |
| | | int buf_size; |
| | | char *buf; |
| | | int max_buf_size; |
| | | if((buf = (char *)malloc(MAXBUF)) == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::NetModSocket malloc"); |
| | | exit(1); |
| | | } else { |
| | | max_buf_size = MAXBUF; |
| | | } |
| | | |
| | | 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::write_request realloc buf"); |
| | | exit(1); |
| | | } 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, send_buf, send_size); |
| | | |
| | | if(rio_writen(clientfd, buf, buf_size) != buf_size ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::write_request rio_writen"); |
| | | free(buf); |
| | | return -1; |
| | | } |
| | | free(buf); |
| | | return 0; |
| | | } |
| | | |
| | | /** |
| | | * @return 0 成功, 1 对方没有对应的key, -1 网络错误 |
| | | * |
| | | */ |
| | | int NetModSocket::read_response(int connfd, net_mod_recv_msg_t *recv_msg) { |
| | | int recv_size; |
| | | void *recv_buf; |
| | | char response_head_bs[NET_MODE_RESPONSE_HEAD_LENGTH]; |
| | | |
| | | net_mod_response_head_t response_head; |
| | | if ( rio_readn(connfd, response_head_bs, NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::read_response rio_readnb response_head"); |
| | | |
| | | return -1; |
| | | } |
| | | |
| | | response_head = NetModSocket::decode_response_head(response_head_bs); |
| | | // printf(">>>> read_response %s\n", response_head.host); |
| | | if(response_head.code != 0) { |
| | | // 对方没有对应的key |
| | | return 1; |
| | | } |
| | | |
| | | recv_buf = malloc(response_head.content_length); |
| | | if(recv_buf == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModSocket::send malloc recv_buf"); |
| | | exit(1); |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | strcpy( recv_msg->host, response_head.host); |
| | | recv_msg->port = response_head.port; |
| | | recv_msg->key = response_head.key; |
| | | recv_msg->content = recv_buf; |
| | | recv_msg->content_length = recv_size; |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | 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]; |
| | |
| | | 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 _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); |
| | | |
| | | public: |
| | | |
| | | NetModSocket(); |
| | | ~NetModSocket(); |
| | | |
| | | |
| | | /** |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int bind( int port); |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int force_bind( int port); |
| | | |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息会一直等待 |
| | | * 向node_arr 中的所有网络节点发送请求消息,节点的返回信息汇总并存储在recv_arr中 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @send_buf 发送的消息,@send_size 该消息体的长度 |
| | | * @recv_arr 返回的应答消息组,@recv_arr_size 该数组长度 |
| | | * @return 成功发送的节点的个数 |
| | | * 优点:无阻塞,性能好 |
| | | * 缺点:不是线程安全的 |
| | | * 优点:1某个节点的故障不会阻塞其他节点。2性能好 |
| | | * 缺点:不是线程安全的, 即不能有两个以上的线程同时使用这个对象的方法 |
| | | */ |
| | | 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 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) ; |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息等待timeout的时间后返回 |
| | | * @timeout 等待时间,单位是千分之一秒 |
| | | */ |
| | | int 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); |
| | | |
| | | /** |
| | | * 不等待立即返回 |
| | | * @timeout 等待时间,单位是千分之一秒 |
| | | */ |
| | | int 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 |
| | |
| | | int sendandrecv_safe(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); |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |
| | | * @arr 消息组 |
| | | * @size 消息组的长度 |
| | | */ |
| | | static void free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size); |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int sendto( const void *buf, const int size, const int port); |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sendto_timeout( const void *buf, const int size, const int port, int sec, int nsec); |
| | | // 发送信息立刻返回。 |
| | | int sendto_nowait( const void *buf, const int size, const int port); |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int recvfrom( void **buf, int *size, int *port); |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int recvfrom_timeout( void **buf, int *size, int *port, int sec, int nsec); |
| | | int recvfrom_nowait( void **buf, int *size, int *port); |
| | | |
| | | /** |
| | | * 本地发送请求信息并等待接收应答 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int sendandrecv( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) ; |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sendandrecv_timeout( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size, int sec, int nsec) ; |
| | | int sendandrecv_nowait( const void *send_buf, const int send_size, const int send_port, void **recv_buf, int *recv_size) ; |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int start_bus(); |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int sub( void *topic, int size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int sub_timeout( void *topic, int size, int port, int sec, int nsec); |
| | | int sub_nowait( void *topic, int size, int port); |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题,主题为空时取消全部订阅 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int desub( void *topic, int size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int desub_timeout( void *topic, int size, int port, int sec, int nsec); |
| | | int desub_nowait( void *topic, int size, int port); |
| | | |
| | | /** |
| | | * 发布主题 |
| | | * @topic 主题 |
| | | * @content 主题内容 |
| | | * @port 总线端口 |
| | | */ |
| | | int pub( char *topic, int topic_size, void *content, int content_size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int pub_timeout( char *topic, int topic_size, void *content, int content_size, int port, int sec, int nsec); |
| | | int pub_nowait( char *topic, int topic_size, void *content, int content_size, int port); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | int pub(net_node_t *node_arr, int node_arr_len, char *topic, int topic_size, void *content, int content_size); |
| | | |
| | | /** |
| | | * 获取soket端口号 |
| | | */ |
| | | int get_port() ; |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |
| | | * @arr 消息组 |
| | | * @size 消息组的长度 |
| | | */ |
| | | static void free_recv_msg_arr(net_mod_recv_msg_t * arr, size_t size); |
| | | |
| | | |
| | | }; |
| | |
| | | #include "socket_io.h" |
| | | #include <stdint.h> |
| | | |
| | | ssize_t rio_readpkgb(rio_t *rp, char *usrbuf, size_t maxlen) |
| | | ssize_t rio_read_until_sep(rio_t *rp, char *usrbuf, size_t maxlen, const char *sep) |
| | | { |
| | | int n, rc; |
| | | char c; |
| | | char *bufp = usrbuf; |
| | | int pkg_sep_i = 0; |
| | | int pkg_sep_len = strlen(PKG_SEP); |
| | | const char * pkg_sep = PKG_SEP; |
| | | int sep_i = 0; |
| | | int sep_len = strlen(sep); |
| | | |
| | | for (n = 0; n < maxlen; n++) |
| | | { |
| | |
| | | |
| | | *bufp++ = c; |
| | | |
| | | if(c == *(pkg_sep + pkg_sep_i)) { |
| | | pkg_sep_i++; |
| | | if(pkg_sep_i == pkg_sep_len) { |
| | | if(c == *(sep + sep_i)) { |
| | | sep_i++; |
| | | if(sep_i == sep_len) { |
| | | |
| | | break; |
| | | } |
| | | } else { |
| | | |
| | | pkg_sep_i = 0; |
| | | sep_i = 0; |
| | | } |
| | | |
| | | } |
| | |
| | | return -1; /* Error */ |
| | | } |
| | | |
| | | if(pkg_sep_i == pkg_sep_len) { |
| | | *(bufp - pkg_sep_len) = 0; |
| | | return n - pkg_sep_len; |
| | | if(sep_i == sep_len) { |
| | | *(bufp - sep_len) = 0; |
| | | return n - sep_len; |
| | | } else { |
| | | return -1; |
| | | } |
| | |
| | | |
| | | #define PKG_SEP "\r\n\r\n" |
| | | |
| | | ssize_t rio_readpkgb(rio_t *rp, char *usrbuf, size_t maxlen); |
| | | ssize_t rio_read_until_sep(rio_t *rp, char *usrbuf, size_t maxlen, char * sep); |
| | | int is_little_endian(); |
| | | void swap_bytes(void *pv, size_t n); |
| | | uint16_t swap_uint16( uint16_t val ) ; |
| | |
| | | #include "net_mod_socket_wrapper.h" |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 创建 |
| | | */ |
| | |
| | | /** |
| | | * 关闭 |
| | | */ |
| | | void net_mod_socket_close(void *_sockt) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_sockt; |
| | | void net_mod_socket_close(void *_socket) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | delete sockt->sockt; |
| | | free(sockt); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_bind(void * _socket, int port){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->bind(port); |
| | | } |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_force_bind(void * _socket, int port) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->force_bind(port); |
| | | } |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_sendto(void *_socket, const void *buf, const int size, const int port) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sendto(buf, size, port); |
| | | } |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sendto_timeout(buf, size, port, sec, nsec); |
| | | } |
| | | // 发送信息立刻返回。 |
| | | int net_mod_socket_sendto_nowait(void *_socket, const void *buf, const int size, const int port){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sendto_nowait(buf, size, port); |
| | | } |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_recvfrom(void *_socket, void **buf, int *size, int *port){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->recvfrom(buf, size, port); |
| | | } |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->recvfrom_timeout(buf, size, port, sec, nsec); |
| | | } |
| | | int net_mod_socket_recvfrom_nowait(void *_socket, void **buf, int *size, int *port){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->recvfrom_nowait(buf, size, port); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息会一直等待 |
| | | * 向node_arr 中的所有网络节点发送请求消息,节点的返回信息汇总并存储在recv_arr中 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @send_buf 发送的消息,@send_size 该消息体的长度 |
| | | * @recv_arr 返回的应答消息组,@recv_arr_size 该数组长度 |
| | | * @return 成功发送的节点的个数 |
| | | * 优点:1某个节点的故障不会阻塞其他节点。2性能好 |
| | | * 缺点:不是线程安全的, 即不能有两个以上的线程同时使用这个对象的方法 |
| | | */ |
| | | int net_mod_socket_sendandrecv(void *_sockt, 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) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_sockt; |
| | | return sockt->sockt->sendandrecv(node_arr, node_arr_len, send_buf, send_size, recv_arr, recv_arr_size); |
| | | int net_mod_socket_sendandrecv(void *_socket, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sendandrecv(node_arr, arrlen, send_buf, send_size, recv_arr, recv_arr_size); |
| | | } |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息等待timeout的时间后返回 |
| | | * @timeout 等待时间,单位是千分之一秒 |
| | | */ |
| | | int net_mod_socket_sendandrecv_timout(void *_socket, 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){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sendandrecv_timout(node_arr, arrlen, send_buf, send_size, recv_arr, recv_arr_size, timeout); |
| | | } |
| | | |
| | | int net_mod_socket_sendandrecv_nowait(void *_socket, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sendandrecv_nowait(node_arr, arrlen, send_buf, send_size, recv_arr, recv_arr_size); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @topic 主题,@topic_size 该主题的长度 |
| | | * @content 内容,@content_size 内容长度 |
| | | * @return 成功发布的节点的个数 |
| | | */ |
| | | int net_mod_socket_pub(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(node_arr, node_arr_len, topic, topic_size, content, content_size); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_start_bus(void * _socket) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->start_bus(); |
| | | } |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int net_mod_socket_sub(void * _socket, void *topic, int size, int port) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sub((char *)topic, size, port); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sub_timeout((char *)topic, size, port, sec, nsec); |
| | | } |
| | | int net_mod_socket_sub_nowait(void * _socket, void *topic, int size, int port){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->sub_nowait((char *)topic, size, port); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题,主题为空时取消全部订阅 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int net_mod_socket_desub(void * _socket, void *topic, int size, int port) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->desub((char *)topic, size, port); |
| | | } |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->desub_timeout((char *)topic, size, port, sec, nsec); |
| | | } |
| | | int net_mod_socket_desub_nowait(void * _socket, void *topic, int size, int port){ |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->desub_nowait((char *)topic, size, port); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取soket端口号 |
| | | */ |
| | | int net_mod_socket_get_port(void * _socket) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_socket; |
| | | return sockt->sockt->get_port(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 释放存储接收信息的buf |
| | | */ |
| | | void net_mod_socket_free(void *buf) { |
| | | free(buf); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |
| | |
| | | void net_mod_socket_free_recv_msg_arr(net_mod_recv_msg_t * arr, int len) { |
| | | |
| | | return NetModSocket::free_recv_msg_arr(arr, len); |
| | | } |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | | * @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) { |
| | | net_mod_socket_t *sockt = (net_mod_socket_t *)_sockt; |
| | | return sockt->sockt->pub(node_arr, node_arr_len, topic, topic_size, content, content_size); |
| | | } |
| | |
| | | */ |
| | | void net_mod_socket_close(void *_sockt); |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发送请求消息,节点的返回信息汇总并存储在recv_arr中 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @send_buf 发送的消息,@send_size 该消息体的长度 |
| | | * @recv_arr 返回的应答消息组,@recv_arr_size 该数组长度 |
| | | * @return 成功发送的节点的个数 |
| | | * 该方法不是线程安全的,不能有多个线程同时使用一个socket |
| | | */ |
| | | int net_mod_socket_sendandrecv(void *_sockt, 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); |
| | | |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |
| | | * @arr 消息组 |
| | | * @size 消息组的长度 |
| | | * 绑定端口到socket, 如果不绑定则系统自动分配一个 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_bind(void * _socket, int port); |
| | | |
| | | /** |
| | | * 强制绑定端口到socket, 适用于程序非正常关闭的情况下,重启程序绑定原来还没释放的key |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_force_bind(void * _socket, int port); |
| | | /** |
| | | * 发送信息 |
| | | * @port 发送给谁 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | void net_mod_socket_free_recv_msg_arr(net_mod_recv_msg_t * arr, int size); |
| | | int net_mod_socket_sendto(void *_socket, const void *buf, const int size, const int port); |
| | | // 发送信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_sendto_timeout(void *_socket, const void *buf, const int size, const int port, int sec, int nsec); |
| | | // 发送信息立刻返回。 |
| | | int net_mod_socket_sendto_nowait(void *_socket, const void *buf, const int size, const int port); |
| | | |
| | | /** |
| | | * 接收信息 |
| | | * @port 从谁哪里收到的信息 |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_recvfrom(void *_socket, void **buf, int *size, int *port); |
| | | // 接受信息超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_recvfrom_timeout(void *_socket, void **buf, int *size, int *port, int sec, int nsec); |
| | | int net_mod_socket_recvfrom_nowait(void *_socket, void **buf, int *size, int *port); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息会一直等待 |
| | | * 向node_arr 中的所有网络节点发送请求消息,节点的返回信息汇总并存储在recv_arr中 |
| | | * @node_arr 网络节点组, @node_arr_len该数组长度 |
| | | * @send_buf 发送的消息,@send_size 该消息体的长度 |
| | | * @recv_arr 返回的应答消息组,@recv_arr_size 该数组长度 |
| | | * @return 成功发送的节点的个数 |
| | | * 优点:1某个节点的故障不会阻塞其他节点。2性能好 |
| | | * 缺点:不是线程安全的, 即不能有两个以上的线程同时使用这个对象的方法 |
| | | */ |
| | | int net_mod_socket_sendandrecv(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ; |
| | | /** |
| | | * 如果建立连接的节点没有接受到消息等待timeout的时间后返回 |
| | | * @timeout 等待时间,单位是千分之一秒 |
| | | */ |
| | | int net_mod_socket_sendandrecv_timout(void *_sockt, 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); |
| | | |
| | | /** |
| | | * 不等待立即返回 |
| | | */ |
| | | int net_mod_socket_sendandrecv_nowait(void *_sockt, net_node_t *node_arr, int arrlen, void *send_buf, int send_size, |
| | | net_mod_recv_msg_t ** recv_arr, int *recv_arr_size) ; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 向node_arr 中的所有网络节点发布消息 |
| | |
| | | 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); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 启动bus |
| | | * |
| | | * @return 0 成功, 其他值 失败的错误码 |
| | | */ |
| | | int net_mod_socket_start_bus(void * _socket); |
| | | |
| | | /** |
| | | * 订阅指定主题 |
| | | * @topic 主题 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int net_mod_socket_sub(void * _socket, void *topic, int size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_sub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec); |
| | | int net_mod_socket_sub_nowait(void * _socket, void *topic, int size, int port); |
| | | |
| | | |
| | | /** |
| | | * 取消订阅指定主题 |
| | | * @topic 主题,主题为空时取消全部订阅 |
| | | * @size 主题长度 |
| | | * @port 总线端口 |
| | | */ |
| | | int net_mod_socket_desub(void * _socket, void *topic, int size, int port); |
| | | // 超时返回。 @sec 秒 , @nsec 纳秒 |
| | | int net_mod_socket_desub_timeout(void * _socket, void *topic, int size, int port, int sec, int nsec); |
| | | int net_mod_socket_desub_nowait(void * _socket, void *topic, int size, int port); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取soket端口号 |
| | | */ |
| | | int net_mod_socket_get_port(void * _socket) ; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 销毁sendandrecv方法返回的消息组 |
| | | * @arr 消息组 |
| | | * @size 消息组的长度 |
| | | */ |
| | | void net_mod_socket_free_recv_msg_arr(net_mod_recv_msg_t * arr, int size); |
| | | |
| | | |
| | | /** |
| | | * 释放存储接收信息的buf |
| | | */ |
| | | void net_mod_socket_free(void *buf) ; |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |