int ping_pong(struct vsp_node * node) { char ping[64]; int len; asciiMessage *aM; struct pollfd pfd; int rc; ping[0] = '\0'; len = sprintf(ping, "%d 2 client ping\n", node->queueID); setNonBlocking(node->fd); rc = sendControlMessage(node->fd, ping, len, node->tunnel); clearNonBlocking(node->fd); if( rc < 0 ) { dc_debug(DC_ERROR, "Ping failed (control line down)."); return 0; } pfd.fd = node->fd; pfd.events = POLLIN; rc = poll(&pfd, 1, 1000*10); /* 10 seconds */ if((rc == 1) && (pfd.revents & POLLIN )) { dcap_set_alarm(10); aM = getControlMessage(HAVETO, node); dcap_set_alarm(0); if( (aM != NULL ) && (aM->type == ASCII_PING) ) { free(aM->msg); free(aM); return 1; } } dc_debug(DC_ERROR, "Ping failed."); return 0; }
// nonblocking connect is described in detail in chapter 15.4 of stevens unix network programming bool IPv4Socket::connect_nb(const std::string &host, uint16_t port) { LOG_DEBUG("IPv4Socket::connect_nb "<< host.c_str() << ":" << port); connectionInProgress_ = true; struct sockaddr *saddr = 0; struct sockaddr_in inaddr; setup_sockaddr_v4_(inaddr, host, port); socklen_t len = sizeof(sockaddr_in); saddr = (struct sockaddr *) &inaddr; setNonBlocking(); int res = rs_connect(sock_, saddr, len); if (res == 0) { connectionInProgress_ = false; clearNonBlocking(); return true; } const int err = getErrorNumber(); #ifdef WIN32 if (err == WSAEWOULDBLOCK) return true; if (err != WSAEINPROGRESS && err != WSAEINTR) { #else if (err != EINPROGRESS && err != EINTR) { #endif std::stringstream ss; ss << host << ":" << port; std::string str = ss.str(); throw ConnectionRefusedError("Failure to connect to", str.c_str(), err); }; return false; } }