static void handle_connection (int connfd) { ossmix_commad_packet_t pack; struct timeval tmout; send_response (OSSMIX_CMD_HALOO, OSSMIX_P1_MAGIC, 0, 0, 0, 0, 0); tmout.tv_sec = 1; tmout.tv_usec = 0; while (1) { int ndevs; fd_set readfds, exfds; FD_ZERO (&readfds); FD_ZERO (&exfds); FD_SET (connfd, &readfds); FD_SET (connfd, &exfds); if ((ndevs = select (connfd + 1, &readfds, NULL, &exfds, &tmout)) == -1) { perror ("select"); exit (-1); } if (ndevs == 0) { if (polling_started) { poll_devices (); tmout.tv_sec = 0; tmout.tv_usec = 100000; } else { tmout.tv_sec = 1; tmout.tv_usec = 0; } } if (FD_ISSET (connfd, &readfds) || FD_ISSET (connfd, &exfds)) { if (read (connfd, &pack, sizeof (pack)) == sizeof (pack)) { serve_command (&pack); } else return; } } }
/// Calls poll_devices and update_connected_devices if poll_interval and reconnect_interval has elapsed, respectively. void DeviceTypeManager::poll() { std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); // See if it's time to poll controllers for data std::chrono::duration<double, std::milli> update_diff = now - m_last_poll_time; if (update_diff.count() >= poll_interval) { poll_devices(); m_last_poll_time = now; } // See if it's time to try update the list of connected devices std::chrono::duration<double, std::milli> reconnect_diff = now - m_last_reconnect_time; if (reconnect_diff.count() >= reconnect_interval) { if (update_connected_devices()) { m_last_reconnect_time = now; } } }