-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
53 lines (46 loc) · 1.35 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @file utils.cpp
* @author ottojo
* @date 5/25/21
* Description here TODO
*/
#include "utils.hpp"
#include <fmt/format.h>
FileDescriptorSets waitForFDs(const FileDescriptorSets &fds, int timeoutSec, int timeoutUsec) {
while (true) {
fd_set readSet, writeSet, exceptSet;
FD_ZERO(&readSet);
FD_ZERO(&writeSet);
FD_ZERO(&exceptSet);
for (const auto &fd: fds.read) {
FD_SET(fd, &readSet);
}
for (const auto &fd: fds.write) {
FD_SET(fd, &writeSet);
}
for (const auto &fd: fds.except) {
FD_SET(fd, &exceptSet);
}
timeval timeout{.tv_sec=timeoutSec, .tv_usec=timeoutUsec};
int res = select(FD_SETSIZE, &readSet, &writeSet, &exceptSet, &timeout);
if (res == -1) {
throw std::runtime_error{fmt::format("Error during select: {}", strerror(errno))};
}
if (res == 0) {
continue;
}
FileDescriptorSets ret;
for (int i = 0; i < FD_SETSIZE; i++) {
if (FD_ISSET(i, &readSet)) {
ret.read.push_back(i);
}
if (FD_ISSET(i, &writeSet)) {
ret.write.push_back(i);
}
if (FD_ISSET(i, &exceptSet)) {
ret.except.push_back(i);
}
}
return ret;
}
}