1 | /* |
1 | /* |
2 | iom.C -- generic I/O multiplexor |
2 | iom.C -- generic I/O multiplexor |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
15 | You should have received a copy of the GNU General Public License |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include "../config.h" |
|
|
21 | |
|
|
22 | #include <cstdio> |
20 | #include <cstdio> |
23 | #include <cstdlib> |
21 | #include <cstdlib> |
24 | #include <cerrno> |
22 | #include <cerrno> |
25 | |
23 | |
26 | #include <sys/select.h> |
24 | #include <sys/select.h> |
… | |
… | |
34 | tstamp NOW; |
32 | tstamp NOW; |
35 | static bool iom_valid; |
33 | static bool iom_valid; |
36 | io_manager iom; |
34 | io_manager iom; |
37 | |
35 | |
38 | template<class watcher> |
36 | template<class watcher> |
39 | void io_manager::reg (watcher *w, simplevec<watcher *> &queue) |
37 | void io_manager::reg (watcher *w, io_manager_vec<watcher> &queue) |
40 | { |
38 | { |
41 | if (!iom_valid) |
39 | if (!iom_valid) |
42 | abort (); |
40 | abort (); |
43 | |
41 | |
44 | if (!w->active) |
42 | if (!w->active) |
… | |
… | |
47 | w->active = queue.size (); |
45 | w->active = queue.size (); |
48 | } |
46 | } |
49 | } |
47 | } |
50 | |
48 | |
51 | template<class watcher> |
49 | template<class watcher> |
52 | void io_manager::unreg (watcher *w, simplevec<watcher *> &queue) |
50 | void io_manager::unreg (watcher *w, io_manager_vec<watcher> &queue) |
53 | { |
51 | { |
54 | if (!iom_valid) |
52 | if (!iom_valid) |
55 | return; |
53 | return; |
56 | |
54 | |
57 | if (w->active) |
55 | if (w->active) |
… | |
… | |
142 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
140 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
143 | to = &tval; |
141 | to = &tval; |
144 | } |
142 | } |
145 | break; |
143 | break; |
146 | } |
144 | } |
147 | else if (next->at >= 0) |
145 | else |
148 | { |
146 | { |
149 | unreg (next); |
147 | unreg (next); |
150 | next->call (*next); |
148 | next->call (*next); |
151 | } |
149 | } |
152 | } |
150 | } |
… | |
… | |
167 | FD_ZERO (&rfd); |
165 | FD_ZERO (&rfd); |
168 | FD_ZERO (&wfd); |
166 | FD_ZERO (&wfd); |
169 | |
167 | |
170 | int fds = 0; |
168 | int fds = 0; |
171 | |
169 | |
172 | for (io_watcher **i = iow.end (); i-- > iow.begin (); ) |
170 | for (io_manager_vec<io_watcher>::iterator i = iow.end (); i-- > iow.begin (); ) |
173 | if (*i) |
171 | if (*i) |
174 | { |
172 | { |
175 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
173 | if ((*i)->events & EVENT_READ ) FD_SET ((*i)->fd, &rfd); |
176 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
174 | if ((*i)->events & EVENT_WRITE) FD_SET ((*i)->fd, &wfd); |
177 | |
175 | |