… | |
… | |
124 | set_now (); |
124 | set_now (); |
125 | #endif |
125 | #endif |
126 | |
126 | |
127 | for (;;) |
127 | for (;;) |
128 | { |
128 | { |
|
|
129 | struct timeval *to = 0; |
|
|
130 | |
|
|
131 | #if IOM_TIME |
|
|
132 | struct timeval tval; |
|
|
133 | time_watcher *w; |
|
|
134 | |
|
|
135 | for (;tw.size ();) |
|
|
136 | { |
|
|
137 | w = tw[0]; |
|
|
138 | |
|
|
139 | for (time_watcher **i = tw.begin (); i < tw.end (); ++i) |
|
|
140 | if ((*i)->at < w->at) |
|
|
141 | w = *i; |
|
|
142 | |
|
|
143 | if (w->at > NOW) |
|
|
144 | { |
|
|
145 | double diff = w->at - NOW; |
|
|
146 | tval.tv_sec = (int)diff; |
|
|
147 | tval.tv_usec = (int)((diff - tval.tv_sec) * 1000000); |
|
|
148 | to = &tval; |
|
|
149 | break; |
|
|
150 | } |
|
|
151 | else if (w->at >= 0) |
|
|
152 | w->call (*w); |
|
|
153 | else |
|
|
154 | unreg (w); |
|
|
155 | } |
|
|
156 | #endif |
|
|
157 | |
129 | #if IOM_CHECK |
158 | #if IOM_CHECK |
130 | for (int i = 0; i < cw.size (); ++i) |
159 | for (int i = 0; i < cw.size (); ++i) |
131 | cw[i]->call (*cw[i]); |
160 | cw[i]->call (*cw[i]); |
132 | #endif |
|
|
133 | |
|
|
134 | #if IOM_TIME |
|
|
135 | time_watcher *w; |
|
|
136 | |
|
|
137 | for (;;) |
|
|
138 | { |
|
|
139 | w = tw[0]; |
|
|
140 | |
|
|
141 | for (time_watcher **i = tw.begin (); i < tw.end (); ++i) |
|
|
142 | if ((*i)->at < w->at) |
|
|
143 | w = *i; |
|
|
144 | |
|
|
145 | if (w->at > NOW) |
|
|
146 | break; |
|
|
147 | |
|
|
148 | // call it |
|
|
149 | w->call (*w); |
|
|
150 | |
|
|
151 | if (w->at < 0) |
|
|
152 | unreg (w); |
|
|
153 | } |
|
|
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; |