-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect.cpp
66 lines (55 loc) · 1.84 KB
/
select.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
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "select.hpp"
namespace NNet {
void TSelect::Poll() {
auto ts = GetTimeout();
constexpr int bits = sizeof(fd_mask)*8;
if (static_cast<int>(InEvents_.size()) <= MaxFd_) {
InEvents_.resize(MaxFd_+1);
}
if (MaxFd_ >= static_cast<int>(ReadFds_.size())*bits) {
ReadFds_.resize((MaxFd_+bits)/bits);
WriteFds_.resize((MaxFd_+bits)/bits);
}
for (const auto& ch : Changes_) {
int fd = ch.Fd;
auto& ev = InEvents_[fd];
if (ch.Handle) {
if (ch.Type & TEvent::READ) {
FD_SET(fd, ReadFds()); ev.Read = ch.Handle;
}
if (ch.Type & TEvent::WRITE) {
FD_SET(fd, WriteFds()); ev.Write = ch.Handle;
}
} else {
if (ch.Type & TEvent::READ) {
FD_CLR(fd, ReadFds()); ev.Read = {};
}
if (ch.Type & TEvent::WRITE) {
FD_CLR(fd, WriteFds()); ev.Write = {};
}
}
}
Reset();
if (pselect(static_cast<int>(InEvents_.size()), ReadFds(), WriteFds(), nullptr, &ts, nullptr) < 0) {
throw std::system_error(errno, std::generic_category(), "select");
}
for (int k=0; k < static_cast<int>(InEvents_.size()); ++k) {
auto ev = InEvents_[k];
if (FD_ISSET(k, WriteFds())) {
assert(ev.Write);
ReadyEvents_.emplace_back(TEvent{k, TEvent::WRITE, ev.Write});
} else if (ev.Write) {
// fd was cleared by select, set it
FD_SET(k, WriteFds());
}
if (FD_ISSET(k, ReadFds())) {
assert(ev.Read);
ReadyEvents_.emplace_back(TEvent{k, TEvent::READ, ev.Read});
} else if (ev.Read) {
// fd was cleared by select, set it
FD_SET(k, ReadFds());
}
}
ProcessTimers();
}
} // namespace NNet