… | |
… | |
129 | #if IOM_CHECK |
129 | #if IOM_CHECK |
130 | for (int i = 0; i < cw.size (); ++i) |
130 | for (int i = 0; i < cw.size (); ++i) |
131 | cw[i]->call (*cw[i]); |
131 | cw[i]->call (*cw[i]); |
132 | #endif |
132 | #endif |
133 | |
133 | |
|
|
134 | struct timeval *to = 0; |
|
|
135 | |
134 | #if IOM_TIME |
136 | #if IOM_TIME |
|
|
137 | struct timeval tval; |
135 | time_watcher *w; |
138 | time_watcher *w; |
136 | |
139 | |
137 | for (;;) |
140 | for (;tw.size ();) |
138 | { |
141 | { |
139 | w = tw[0]; |
142 | w = tw[0]; |
140 | |
143 | |
141 | for (time_watcher **i = tw.begin (); i < tw.end (); ++i) |
144 | for (time_watcher **i = tw.begin (); i < tw.end (); ++i) |
142 | if ((*i)->at < w->at) |
145 | if ((*i)->at < w->at) |
143 | w = *i; |
146 | w = *i; |
144 | |
147 | |
145 | if (w->at > NOW) |
148 | if (w->at > NOW) |
|
|
149 | { |
|
|
150 | double diff = w->at - NOW; |
|
|
151 | tval.tv_sec = (int)diff; |
|
|
152 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
|
|
153 | to = &tval; |
146 | break; |
154 | break; |
147 | |
155 | } |
148 | // call it |
156 | else if (w->at >= 0) |
149 | w->call (*w); |
157 | w->call (*w); |
150 | |
158 | else |
151 | if (w->at < 0) |
|
|
152 | unreg (w); |
159 | unreg (w); |
153 | } |
160 | } |
154 | |
|
|
155 | double diff = w->at - NOW; |
|
|
156 | struct timeval to; |
|
|
157 | to.tv_sec = (int)diff; |
|
|
158 | to.tv_usec = (int)((diff - to.tv_sec) * 1000000); |
|
|
159 | #endif |
161 | #endif |
160 | |
162 | |
161 | #if IOM_IO |
163 | #if IOM_IO |
162 | fd_set rfd, wfd; |
164 | fd_set rfd, wfd; |
163 | |
165 | |
… | |
… | |
169 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
171 | for (io_watcher **w = iow.begin (); w < iow.end (); ++w) |
170 | { |
172 | { |
171 | if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); |
173 | if ((*w)->events & EVENT_READ ) FD_SET ((*w)->fd, &rfd); |
172 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
174 | if ((*w)->events & EVENT_WRITE) FD_SET ((*w)->fd, &wfd); |
173 | |
175 | |
174 | if ((*w)->fd > fds) fds = (*w)->fd; |
176 | if ((*w)->fd >= fds) fds = (*w)->fd + 1; |
175 | } |
177 | } |
176 | |
178 | |
|
|
179 | if (!to && !fds) |
|
|
180 | break; // no events |
|
|
181 | |
|
|
182 | fds = select (fds, &rfd, &wfd, 0, to); |
177 | # if IOM_TIME |
183 | # if IOM_TIME |
178 | fds = select (fds + 1, &rfd, &wfd, 0, &to); |
|
|
179 | set_now (); |
184 | set_now (); |
180 | # else |
|
|
181 | fds = select (fds + 1, &rfd, &wfd, 0, 0); |
|
|
182 | # endif |
185 | # endif |
183 | |
186 | |
184 | if (fds > 0) |
187 | if (fds > 0) |
185 | for (int i = 0; i < iow.size (); ++i) |
188 | for (int i = 0; i < iow.size (); ++i) |
186 | { |
189 | { |
… | |
… | |
192 | if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; |
195 | if (!FD_ISSET (w->fd, &wfd)) revents &= ~EVENT_WRITE; |
193 | |
196 | |
194 | if (revents) |
197 | if (revents) |
195 | w->call (*w, revents); |
198 | w->call (*w, revents); |
196 | } |
199 | } |
197 | } |
|
|
198 | #elif IOM_TIME |
200 | #elif IOM_TIME |
|
|
201 | if (!to) |
|
|
202 | break; |
|
|
203 | |
199 | select (0, 0, 0, 0, &to); |
204 | select (0, 0, 0, 0, &to); |
200 | set_now (); |
205 | set_now (); |
|
|
206 | #else |
|
|
207 | break; |
201 | #endif |
208 | #endif |
202 | } |
209 | } |
203 | |
|
|
204 | void io_manager::idle_cb (time_watcher &w) |
|
|
205 | { |
|
|
206 | w.at = NOW + 1000000000; |
|
|
207 | } |
210 | } |
208 | |
211 | |
209 | io_manager::io_manager () |
212 | io_manager::io_manager () |
210 | { |
213 | { |
|
|
214 | #if IOM_TIME |
211 | set_now (); |
215 | set_now (); |
|
|
216 | #endif |
212 | |
217 | |
213 | iom_valid = true; |
218 | iom_valid = true; |
214 | |
|
|
215 | #if IOM_TIME |
|
|
216 | idle = new time_watcher (this, &io_manager::idle_cb); |
|
|
217 | idle->start (0); |
|
|
218 | #endif |
|
|
219 | } |
219 | } |
220 | |
220 | |
221 | io_manager::~io_manager () |
221 | io_manager::~io_manager () |
222 | { |
222 | { |
223 | iom_valid = false; |
223 | iom_valid = false; |