Last change
on this file since 2961 was
2889,
checked in by adrfried, 16 years ago
|
FDWatcher and GGZClient Objects introduced
|
File size:
1.3 KB
|
Line | |
---|
1 | #include "FDWatcher.h" |
---|
2 | |
---|
3 | namespace orxonox |
---|
4 | { |
---|
5 | FDWatcher::FDWatcher() |
---|
6 | { |
---|
7 | pollfds = 0; |
---|
8 | npollfds = 0; |
---|
9 | } |
---|
10 | |
---|
11 | FDWatcher::~FDWatcher() |
---|
12 | { |
---|
13 | if (pollfds) { |
---|
14 | delete [] pollfds; |
---|
15 | } |
---|
16 | } |
---|
17 | |
---|
18 | void FDWatcher::tick(const float dt) |
---|
19 | { |
---|
20 | int ret = poll(pollfds, npollfds, 0); |
---|
21 | if (ret < 0) { |
---|
22 | // TODO error |
---|
23 | } |
---|
24 | for (int i=0; ret>0; i++, ret--) { |
---|
25 | if (pollfds[i].revents & POLLIN) { |
---|
26 | (*watches[pollfds[i].fd])(pollfds[i].fd); |
---|
27 | } |
---|
28 | if (pollfds[i].revents & !POLLIN) { |
---|
29 | // TODO error |
---|
30 | } |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | void FDWatcher::add(const int fd, intfunction cb) |
---|
35 | { |
---|
36 | watches[fd] = cb; |
---|
37 | npollfds++; |
---|
38 | rebuild(); |
---|
39 | } |
---|
40 | |
---|
41 | void FDWatcher::remove(const int fd) |
---|
42 | { |
---|
43 | watches.erase(fd); |
---|
44 | npollfds--; |
---|
45 | rebuild(); |
---|
46 | } |
---|
47 | |
---|
48 | void FDWatcher::rebuild() |
---|
49 | { |
---|
50 | if (pollfds) { |
---|
51 | delete [] pollfds; |
---|
52 | } |
---|
53 | if (npollfds) { |
---|
54 | pollfds = new struct pollfd[npollfds]; |
---|
55 | } |
---|
56 | else { |
---|
57 | pollfds = 0; |
---|
58 | } |
---|
59 | int i=0; |
---|
60 | for (std::map<int, intfunction>::iterator it=watches.begin(); |
---|
61 | it!=watches.end(); it++, i++) |
---|
62 | { |
---|
63 | pollfds[i].fd = it->first; |
---|
64 | pollfds[i].events = POLLIN; |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.