-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathselectserver.cpp
232 lines (195 loc) · 6.6 KB
/
selectserver.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "base/selectserver.h"
#include "base/log.h"
#include "base/socket.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
namespace base {
SelectServer::SelectServer() {
notifying_ = 0;
wait_ = false;
pdispFirst_ = NULL;
pdispNotifyNext_ = NULL;
eventer_ = new Eventer(this, &wait_);
}
SelectServer::~SelectServer() {
delete eventer_;
}
void SelectServer::Add(SelectDispatcher *pdisp) {
CritScope cs(&crit_);
pdisp->pdispNext_ = pdispFirst_;
pdispFirst_ = pdisp;
}
void SelectServer::Remove(SelectDispatcher *pdisp) {
CritScope cs(&crit_);
SelectDispatcher **ppdispT = &pdispFirst_;
while ((*ppdispT) != NULL) {
if (*ppdispT == pdisp) {
*ppdispT = pdisp->pdispNext_;
if (pdispNotifyNext_ == pdisp) {
pdispNotifyNext_ = pdisp->pdispNext_;
}
break;
}
ppdispT = &(*ppdispT)->pdispNext_;
}
}
void SelectServer::WakeUp() {
eventer_->Signal();
}
bool SelectServer::Wait(long64 ctWaitBig, bool fProcessIO) {
// Allow recursion when processing input.
if (notifying_ != 0) {
LOG() << "Error - re-entering!";
fProcessIO = false;
}
// Calculate timing information
int ctWait = (int)ctWaitBig;
struct timeval *ptvWait = NULL;
struct timeval tvWait;
struct timeval tvStop;
if (ctWait != kctForever) {
// Calculate wait timeval
tvWait.tv_sec = ctWait / 100;
tvWait.tv_usec = (ctWait % 100) * 10000;
ptvWait = &tvWait;
// Calculate when to return in a timeval
gettimeofday(&tvStop, NULL);
tvStop.tv_sec += tvWait.tv_sec;
tvStop.tv_usec += tvWait.tv_usec;
if (tvStop.tv_usec >= 1000000) {
tvStop.tv_usec -= 1000000;
tvStop.tv_sec += 1;
}
}
// Zero all fd_sets. Don't need to do this inside the loop since
// select() zeros the descriptors not signaled
fd_set fdsRead;
FD_ZERO(&fdsRead);
fd_set fdsWrite;
FD_ZERO(&fdsWrite);
wait_ = true;
while (wait_) {
int fdMax = -1;
{
CritScope cr(&crit_);
SelectDispatcher *pdispT = pdispFirst_;
for (; pdispT != NULL; pdispT = pdispT->pdispNext_) {
// Query dispatchers for read and write wait state
if (!fProcessIO && (pdispT != eventer_->dispatcher())) {
continue;
}
int fd = pdispT->descriptor();
if (fd > fdMax) {
fdMax = fd;
}
dword ff = pdispT->GetEvents();
if (ff & (Dispatcher::kfRead | Dispatcher::kfRemoteClose)) {
FD_SET(fd, &fdsRead);
}
if (ff & (Dispatcher::kfWrite | Dispatcher::kfConnect)) {
FD_SET(fd, &fdsWrite);
}
}
}
// Wait then call handlers as appropriate
// < 0 means error
// 0 means timeout
// > 0 means count of descriptors ready
int n = select(fdMax + 1, &fdsRead, &fdsWrite, NULL, ptvWait);
// If error, return error
if (n < 0) {
LOG() << "select returns < 0, errno: " << errno << ", "
<< Socket::GetErrorString(errno);
return false;
}
// If timeout, return success
if (n == 0) {
return true;
}
// Find a dispatcher to notify, then notify outside of the loop
// and outside of the critical section.
notifying_++;
SelectDispatcher *pdispNotify = pdispFirst_;
do {
dword ff = 0;
{
CritScope cs(&crit_);
while (pdispNotify != NULL) {
if (!fProcessIO && (pdispNotify != eventer_->dispatcher())) {
pdispNotify = pdispNotify->pdispNext_;
continue;
}
int fd = pdispNotify->descriptor();
if (FD_ISSET(fd, &fdsRead)) {
FD_CLR(fd, &fdsRead);
if (pdispNotify->GetEvents() & Dispatcher::kfRemoteClose) {
ff |= Dispatcher::kfClose;
} else {
ff |= Dispatcher::kfRead;
}
}
if (FD_ISSET(fd, &fdsWrite)) {
FD_CLR(fd, &fdsWrite);
if (pdispNotify->GetEvents() & Dispatcher::kfConnect) {
ff |= Dispatcher::kfConnect;
} else {
ff |= Dispatcher::kfWrite;
}
}
if (ff != 0) {
break;
}
pdispNotify = pdispNotify->pdispNext_;
}
}
// Notify outside of the critical section and the loop.
if (pdispNotify != NULL) {
// Remember pdispNext so that it can be updated in case
// of deletion during callback
if (fProcessIO) {
pdispNotifyNext_ = pdispNotify->pdispNext_;
}
pdispNotify->OnEvent(ff);
pdispNotify = pdispNotifyNext_;
if (fProcessIO) {
pdispNotifyNext_ = NULL;
}
}
} while (pdispNotify != NULL);
notifying_--;
// Recalc the time remaining to wait. Doing it here means it doesn't
// get calced twice the first time through the loop
if (ctWait != kctForever) {
ptvWait->tv_sec = 0;
ptvWait->tv_usec = 0;
struct timeval tvT;
gettimeofday(&tvT, NULL);
if (tvStop.tv_sec < tvT.tv_sec) {
continue;
}
if (tvStop.tv_sec == tvT.tv_sec) {
if (tvStop.tv_usec > tvT.tv_usec) {
ptvWait->tv_usec = tvStop.tv_usec - tvT.tv_usec;
}
continue;
}
ptvWait->tv_sec = tvStop.tv_sec - tvT.tv_sec;
ptvWait->tv_usec = tvStop.tv_usec - tvT.tv_usec;
if (ptvWait->tv_usec < 0) {
ptvWait->tv_usec += 1000000;
ptvWait->tv_sec -= 1;
}
}
}
return true;
}
Dispatcher *SelectServer::CreateDispatcher() {
return new SelectDispatcher(this);
}
SocketServer *SocketServer::Create() {
return new SelectServer;
}
} // namespace base