-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepoll.cpp
161 lines (147 loc) · 3.92 KB
/
epoll.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifdef __linux__
#include "epoll.hpp"
namespace NNet
{
TEPoll::TEPoll() : Fd_(epoll_create1(EPOLL_CLOEXEC))
{
if (Fd_ < 0)
{
throw std::system_error(errno, std::generic_category(), "epoll_create1");
}
}
TEPoll::~TEPoll()
{
if (Fd_ >= 0)
{
close(Fd_);
}
}
void TEPoll::Poll()
{
auto ts = GetTimeout();
if (static_cast<int>(InEvents_.size()) <= MaxFd_)
{
InEvents_.resize(MaxFd_ + 1);
}
for (const auto& ch : Changes_)
{
int fd = ch.Fd;
auto& ev = InEvents_[fd];
epoll_event eev = {};
eev.data.fd = fd;
bool change = false;
bool newEv = false;
if (ch.Handle)
{
newEv = !!ev.Read && !!ev.Write;
if (ch.Type & TEvent::READ)
{
eev.events |= EPOLLIN;
change |= ev.Read != ch.Handle;
ev.Read = ch.Handle;
}
if (ch.Type & TEvent::WRITE)
{
eev.events |= EPOLLOUT;
change |= ev.Write != ch.Handle;
ev.Write = ch.Handle;
}
}
else
{
if (ch.Type & TEvent::READ)
{
change |= !!ev.Read;
ev.Read = {};
}
if (ch.Type & TEvent::WRITE)
{
change |= !!ev.Write;
ev.Write = {};
}
if (ev.Read)
{
eev.events |= EPOLLIN;
}
if (ev.Write)
{
eev.events |= EPOLLOUT;
}
}
if (newEv)
{
if (epoll_ctl(Fd_, EPOLL_CTL_ADD, fd, &eev) < 0)
{
throw std::system_error(errno, std::generic_category(), "epoll_ctl");
}
}
else if (!eev.events)
{
if (epoll_ctl(Fd_, EPOLL_CTL_DEL, fd, nullptr) < 0)
{
if (!(errno == EBADF || errno == ENOENT))
{ // closed descriptor after TSocket -> close
throw std::system_error(errno, std::generic_category(), "epoll_ctl");
}
}
}
else if (change)
{
if (epoll_ctl(Fd_, EPOLL_CTL_MOD, fd, &eev) < 0)
{
if (errno == ENOENT)
{
if (epoll_ctl(Fd_, EPOLL_CTL_ADD, fd, &eev) < 0)
{
throw std::system_error(errno, std::generic_category(), "epoll_ctl");
}
}
else
{
throw std::system_error(errno, std::generic_category(), "epoll_ctl");
}
}
}
}
Reset();
OutEvents_.resize(std::max<size_t>(1, InEvents_.size()));
int nfds;
if ((nfds = epoll_pwait2(
Fd_, &OutEvents_[0], static_cast<int>(OutEvents_.size()), &ts, nullptr)) < 0)
{
if (errno == EINTR)
{
return;
}
throw std::system_error(errno, std::generic_category(), "epoll_wait");
}
for (int i = 0; i < nfds; ++i)
{
int fd = OutEvents_[i].data.fd;
auto ev = InEvents_[fd];
if (OutEvents_[i].events & EPOLLIN)
{
ReadyEvents_.emplace_back(TEvent{fd, TEvent::READ, ev.Read});
ev.Read = {};
}
if (OutEvents_[i].events & EPOLLOUT)
{
ReadyEvents_.emplace_back(TEvent{fd, TEvent::WRITE, ev.Write});
ev.Write = {};
}
if (OutEvents_[i].events & EPOLLHUP)
{
if (ev.Read)
{
ReadyEvents_.emplace_back(TEvent{fd, TEvent::READ, ev.Read});
}
if (ev.Write)
{
ReadyEvents_.emplace_back(TEvent{fd, TEvent::WRITE, ev.Write});
}
}
}
ProcessTimers();
}
} // namespace NNet
#endif // __linux__