-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdset.cc
98 lines (83 loc) · 1.71 KB
/
fdset.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
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
#include "fdset.h"
#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
/* An internal data structure used by the select function. */
fd_set readSet;
FDSet::FDSet(int maxSize)
{
MaxSize = maxSize;
ElementList = new int[MaxSize];
Count = 0;
ActiveSet = new int[MaxSize];
}
FDSet::~FDSet()
{
delete ElementList;
delete ActiveSet;
}
bool
FDSet::addFD(int fd)
{
/* Make sure there is room in the set for the fd. */
if( Count == MaxSize )
return false;
/* Make sure the fd is not already in the set. */
for( int i = 0; i < Count; i++ )
if(fd == ElementList[i])
return false;
/* Add the fd. */
ElementList[Count] = fd;
Count++;
return true;
}
bool
FDSet::removeFD(int fd)
{
for(int i = 0; i < Count; i++)
if(fd == ElementList[i]) {
ElementList[i] = ElementList[Count - 1];
Count--;
return true;
}
return false;
}
void
FDSet::clear()
{
Count = 0;
}
int *
FDSet::select()
{
timeval tv;
int result;
while(1) {
tv.tv_sec = 25;
tv.tv_usec = 0;
/* Add the fd's to the internal fd_set. */
FD_ZERO(&readSet);
int maxFD = 0;
for(int i = 0; i < Count; i++) {
FD_SET(ElementList[i], &readSet);
if(ElementList[i] > maxFD)
maxFD = ElementList[i];
}
result = ::select(maxFD + 1, &readSet, 0, 0, &tv);
if(result == -1)
perror("FDSet::select(): invalid fd set.");
else if(result) {
ActiveSet[0] = -1;
int pos = 0;
for(int i = 0; i < Count; i++) {
int fd = ElementList[i];
if(FD_ISSET(fd, &readSet)) {
ActiveSet[pos] = fd;
pos++;
}
}
ActiveSet[pos] = -1;
return ActiveSet;
}
}
}