Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
623 B
4
Indexable
Never
void *selector::run()
{
	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 1000;
	fd_set rfds;
	int fd_max;

	fd_max = *max_element(fds.begin(), fds.end());

	while (g->running) {
		FD_ZERO(&rfds);
		for (auto s: fds) {
			FD_SET(s, &rfds);
		}

		/* Mandatory to re-set timeval values each time, or 100% cpu */
		tv.tv_sec = 0;
		tv.tv_usec = 1000;

		if (select(fd_max + 1, &rfds, NULL, NULL, &tv) > 0) {
			for (auto s: fds) {
				if (FD_ISSET(s, &rfds)) {
					ssize_t in = recv(s, rx_buff, 2048, 0);
					if (in > 0)
						on_frame_in(in);
				}
			}
		}
	}

	dbg << __func__ << "(): exiting normally\n";

	return 0;
}