-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selectable.cc
47 lines (33 loc) · 993 Bytes
/
Selectable.cc
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
#include "mcl.h"
#include <sys/time.h>
#include <unistd.h>
List<Selectable*> Selectable::ioList;
// Do a select on all registered Selectable objects
void Selectable::select(int sec, int usec)
{
fd_set in_set, out_set;
Selectable *i;
int max_fd = 0;
struct timeval tv;
tv.tv_usec = usec;
tv.tv_sec = sec;
FD_ZERO(&in_set);
FD_ZERO(&out_set);
for (i = ioList.rewind(); i; i = ioList.next())
max_fd = max(max_fd, i->init_fdset(&in_set, &out_set));
while (::select(max_fd+1, &in_set, &out_set, NULL, &tv) < 0)
if (errno != EAGAIN && errno != EINTR)
{
perror ("select");
exit (1);
}
// @@ Async connections are ready when ready to be written to, not read from
for (i = ioList.rewind(); i; i = ioList.next())
i->check_fdset(&in_set, &out_set);
}
Selectable::Selectable() {
ioList.append(this);
}
Selectable::~Selectable() {
ioList.remove(this);
}