[9c7a701] | 1 | #include "owl.h" |
---|
| 2 | |
---|
| 3 | static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $"; |
---|
| 4 | |
---|
[58d1f8a] | 5 | int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2) { |
---|
[ca1fc33a] | 6 | return t1->time - t2->time; |
---|
[b7bb454] | 7 | } |
---|
| 8 | |
---|
[58d1f8a] | 9 | int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2) { |
---|
[ca1fc33a] | 10 | return t1 == t2; |
---|
[b7bb454] | 11 | } |
---|
| 12 | |
---|
[c675b39] | 13 | owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data) |
---|
[b7bb454] | 14 | { |
---|
[ca1fc33a] | 15 | owl_timer *t = owl_malloc(sizeof(owl_timer)); |
---|
[58d1f8a] | 16 | GList **timers = owl_global_get_timerlist(&g); |
---|
[b7bb454] | 17 | |
---|
[ca1fc33a] | 18 | t->time = time(NULL) + after; |
---|
| 19 | t->interval = interval; |
---|
| 20 | t->callback = cb; |
---|
[c675b39] | 21 | t->destroy = destroy; |
---|
[ca1fc33a] | 22 | t->data = data; |
---|
[b7bb454] | 23 | |
---|
[58d1f8a] | 24 | *timers = g_list_insert_sorted(*timers, t, |
---|
| 25 | (GCompareFunc)_owl_select_timer_cmp); |
---|
[ca1fc33a] | 26 | return t; |
---|
[b7bb454] | 27 | } |
---|
| 28 | |
---|
| 29 | void owl_select_remove_timer(owl_timer *t) |
---|
| 30 | { |
---|
[58d1f8a] | 31 | GList **timers = owl_global_get_timerlist(&g); |
---|
| 32 | if (t && g_list_find(*timers, t)) { |
---|
| 33 | *timers = g_list_remove(*timers, t); |
---|
[c675b39] | 34 | if(t->destroy) { |
---|
| 35 | t->destroy(t); |
---|
| 36 | } |
---|
[ca1fc33a] | 37 | owl_free(t); |
---|
| 38 | } |
---|
[b7bb454] | 39 | } |
---|
| 40 | |
---|
| 41 | void owl_select_process_timers(struct timeval *timeout) |
---|
| 42 | { |
---|
[ca1fc33a] | 43 | time_t now = time(NULL); |
---|
[58d1f8a] | 44 | GList **timers = owl_global_get_timerlist(&g); |
---|
[b7bb454] | 45 | |
---|
[58d1f8a] | 46 | while(*timers) { |
---|
| 47 | owl_timer *t = (*timers)->data; |
---|
[c675b39] | 48 | int remove = 0; |
---|
[b7bb454] | 49 | |
---|
[ca1fc33a] | 50 | if(t->time > now) |
---|
| 51 | break; |
---|
[b7bb454] | 52 | |
---|
[ca1fc33a] | 53 | /* Reschedule if appropriate */ |
---|
| 54 | if(t->interval > 0) { |
---|
| 55 | t->time = now + t->interval; |
---|
[58d1f8a] | 56 | *timers = g_list_remove(*timers, t); |
---|
| 57 | *timers = g_list_insert_sorted(*timers, t, |
---|
| 58 | (GCompareFunc)_owl_select_timer_cmp); |
---|
[ca1fc33a] | 59 | } else { |
---|
[c675b39] | 60 | remove = 1; |
---|
[ca1fc33a] | 61 | } |
---|
[b7bb454] | 62 | |
---|
[ca1fc33a] | 63 | /* Do the callback */ |
---|
[c675b39] | 64 | t->callback(t, t->data); |
---|
| 65 | if(remove) { |
---|
| 66 | owl_select_remove_timer(t); |
---|
| 67 | } |
---|
[ca1fc33a] | 68 | } |
---|
[b7bb454] | 69 | |
---|
[58d1f8a] | 70 | if(*timers) { |
---|
| 71 | owl_timer *t = (*timers)->data; |
---|
[ca1fc33a] | 72 | timeout->tv_sec = t->time - now; |
---|
[58d1f8a] | 73 | if (timeout->tv_sec > 60) |
---|
| 74 | timeout->tv_sec = 60; |
---|
| 75 | } else { |
---|
| 76 | timeout->tv_sec = 60; |
---|
[ca1fc33a] | 77 | } |
---|
[b7bb454] | 78 | |
---|
[ca1fc33a] | 79 | timeout->tv_usec = 0; |
---|
[b7bb454] | 80 | } |
---|
| 81 | |
---|
[9c7a701] | 82 | /* Returns the index of the dispatch for the file descriptor. */ |
---|
| 83 | int owl_select_find_dispatch(int fd) |
---|
| 84 | { |
---|
| 85 | int i, len; |
---|
| 86 | owl_list *dl; |
---|
| 87 | owl_dispatch *d; |
---|
| 88 | |
---|
| 89 | dl = owl_global_get_dispatchlist(&g); |
---|
| 90 | len = owl_list_get_size(dl); |
---|
| 91 | for(i = 0; i < len; i++) { |
---|
| 92 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
| 93 | if (d->fd == fd) return i; |
---|
| 94 | } |
---|
| 95 | return -1; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /* Adds a new owl_dispatch to the list, replacing existing ones if needed. */ |
---|
| 99 | void owl_select_add_dispatch(owl_dispatch *d) |
---|
| 100 | { |
---|
| 101 | int elt; |
---|
| 102 | owl_list *dl; |
---|
| 103 | |
---|
| 104 | elt = owl_select_find_dispatch(d->fd); |
---|
| 105 | dl = owl_global_get_dispatchlist(&g); |
---|
| 106 | |
---|
| 107 | if (elt != -1) { /* If we have a dispatch for this FD */ |
---|
| 108 | owl_dispatch *d_old; |
---|
| 109 | d_old = (owl_dispatch*)owl_list_get_element(dl, elt); |
---|
| 110 | /* Ignore if we're adding the same dispatch again. Otherwise |
---|
| 111 | replace the old dispatch. */ |
---|
| 112 | if (d_old != d) { |
---|
| 113 | owl_list_replace_element(dl, elt, d); |
---|
| 114 | owl_free(d_old); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | else { |
---|
| 118 | owl_list_append_element(dl, d); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /* Removes an owl_dispatch to the list, based on it's file descriptor. */ |
---|
| 123 | void owl_select_remove_dispatch(int fd) |
---|
| 124 | { |
---|
| 125 | int elt; |
---|
| 126 | owl_list *dl; |
---|
| 127 | |
---|
| 128 | elt = owl_select_find_dispatch(fd); |
---|
| 129 | dl = owl_global_get_dispatchlist(&g); |
---|
| 130 | |
---|
| 131 | if (elt != -1) { |
---|
| 132 | owl_dispatch *d; |
---|
| 133 | d = (owl_dispatch*)owl_list_get_element(dl, elt); |
---|
| 134 | owl_list_remove_element(dl, elt); |
---|
[f36cd97] | 135 | if (d->destroy) { |
---|
| 136 | d->destroy(d); |
---|
[9c7a701] | 137 | } |
---|
| 138 | owl_free(d); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | int owl_select_dispatch_count() |
---|
| 143 | { |
---|
| 144 | return owl_list_get_size(owl_global_get_dispatchlist(&g)); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | int owl_select_add_perl_dispatch(int fd, SV *cb) |
---|
| 148 | { |
---|
| 149 | int elt; |
---|
| 150 | owl_dispatch *d; |
---|
| 151 | elt = owl_select_find_dispatch(fd); |
---|
| 152 | if (elt != -1) { |
---|
| 153 | d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); |
---|
[f36cd97] | 154 | if (d->cfunc != owl_perlconfig_dispatch) { |
---|
[9c7a701] | 155 | /* don't mess with non-perl dispatch functions from here. */ |
---|
| 156 | return 1; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | d = malloc(sizeof(owl_dispatch)); |
---|
| 161 | d->fd = fd; |
---|
[f36cd97] | 162 | d->cfunc = owl_perlconfig_dispatch; |
---|
| 163 | d->destroy = owl_perlconfig_dispatch_free; |
---|
| 164 | d->data = cb; |
---|
[9c7a701] | 165 | owl_select_add_dispatch(d); |
---|
| 166 | return 0; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | int owl_select_remove_perl_dispatch(int fd) |
---|
| 170 | { |
---|
| 171 | int elt; |
---|
| 172 | owl_dispatch *d; |
---|
| 173 | |
---|
| 174 | elt = owl_select_find_dispatch(fd); |
---|
| 175 | if (elt != -1) { |
---|
| 176 | d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); |
---|
[f36cd97] | 177 | if (d->cfunc == owl_perlconfig_dispatch) { |
---|
[9c7a701] | 178 | owl_select_remove_dispatch(fd); |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | return 1; |
---|
| 183 | } |
---|
| 184 | |
---|
[2f69081] | 185 | int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e) |
---|
[9c7a701] | 186 | { |
---|
| 187 | int i, len, max_fd; |
---|
| 188 | owl_dispatch *d; |
---|
| 189 | owl_list *dl; |
---|
| 190 | |
---|
| 191 | dl = owl_global_get_dispatchlist(&g); |
---|
| 192 | FD_ZERO(r); |
---|
| 193 | FD_ZERO(e); |
---|
| 194 | max_fd = 0; |
---|
| 195 | len = owl_select_dispatch_count(g); |
---|
| 196 | for(i = 0; i < len; i++) { |
---|
| 197 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
| 198 | FD_SET(d->fd, r); |
---|
| 199 | FD_SET(d->fd, e); |
---|
| 200 | if (max_fd < d->fd) max_fd = d->fd; |
---|
| 201 | } |
---|
| 202 | return max_fd + 1; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void owl_select_dispatch(fd_set *fds, int max_fd) |
---|
| 206 | { |
---|
| 207 | int i, len; |
---|
| 208 | owl_dispatch *d; |
---|
| 209 | owl_list *dl; |
---|
| 210 | |
---|
| 211 | dl = owl_global_get_dispatchlist(&g); |
---|
| 212 | len = owl_select_dispatch_count(); |
---|
| 213 | for(i = 0; i < len; i++) { |
---|
| 214 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
[18a54ee] | 215 | /* While d shouldn't normally be null, the list may be altered by |
---|
| 216 | * functions we dispatch to. */ |
---|
| 217 | if (d != NULL && FD_ISSET(d->fd, fds)) { |
---|
[9c7a701] | 218 | if (d->cfunc != NULL) { |
---|
[f36cd97] | 219 | d->cfunc(d); |
---|
[9c7a701] | 220 | } |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
[2f69081] | 225 | int owl_select_aim_hack(fd_set *rfds, fd_set *wfds) |
---|
[9c7a701] | 226 | { |
---|
| 227 | aim_conn_t *cur; |
---|
| 228 | aim_session_t *sess; |
---|
| 229 | int max_fd; |
---|
| 230 | |
---|
[2f69081] | 231 | FD_ZERO(rfds); |
---|
| 232 | FD_ZERO(wfds); |
---|
[9c7a701] | 233 | max_fd = 0; |
---|
| 234 | sess = owl_global_get_aimsess(&g); |
---|
| 235 | for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) { |
---|
| 236 | if (cur->fd != -1) { |
---|
[2f69081] | 237 | FD_SET(cur->fd, rfds); |
---|
| 238 | if (cur->status & AIM_CONN_STATUS_INPROGRESS) { |
---|
| 239 | /* Yes, we're checking writable sockets here. Without it, AIM |
---|
| 240 | login is really slow. */ |
---|
| 241 | FD_SET(cur->fd, wfds); |
---|
| 242 | } |
---|
| 243 | |
---|
[9c7a701] | 244 | if (cur->fd > max_fd) |
---|
| 245 | max_fd = cur->fd; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | return max_fd; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void owl_select() |
---|
| 252 | { |
---|
| 253 | int i, max_fd, aim_max_fd, aim_done; |
---|
| 254 | fd_set r; |
---|
| 255 | fd_set e; |
---|
[2f69081] | 256 | fd_set aim_rfds, aim_wfds; |
---|
[9c7a701] | 257 | struct timeval timeout; |
---|
| 258 | |
---|
[b7bb454] | 259 | owl_select_process_timers(&timeout); |
---|
[9c7a701] | 260 | |
---|
[2f69081] | 261 | max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e); |
---|
[9c7a701] | 262 | |
---|
| 263 | /* AIM HACK: |
---|
| 264 | * |
---|
| 265 | * The problem - I'm not sure where to hook into the owl/faim |
---|
| 266 | * interface to keep track of when the AIM socket(s) open and |
---|
| 267 | * close. In particular, the bosconn thing throws me off. So, |
---|
| 268 | * rather than register particular dispatchers for AIM, I look up |
---|
| 269 | * the relevant FDs and add them to select's watch lists, then |
---|
| 270 | * check for them individually before moving on to the other |
---|
| 271 | * dispatchers. --asedeno |
---|
| 272 | */ |
---|
| 273 | aim_done = 1; |
---|
[2f69081] | 274 | FD_ZERO(&aim_rfds); |
---|
| 275 | FD_ZERO(&aim_wfds); |
---|
[9c7a701] | 276 | if (owl_global_is_doaimevents(&g)) { |
---|
| 277 | aim_done = 0; |
---|
[2f69081] | 278 | aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds); |
---|
[9c7a701] | 279 | if (max_fd < aim_max_fd) max_fd = aim_max_fd; |
---|
| 280 | for(i = 0; i <= aim_max_fd; i++) { |
---|
[2f69081] | 281 | if (FD_ISSET(i, &aim_rfds)) { |
---|
[9c7a701] | 282 | FD_SET(i, &r); |
---|
| 283 | FD_SET(i, &e); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | /* END AIM HACK */ |
---|
[2f69081] | 288 | |
---|
| 289 | if ( select(max_fd+1, &r, &aim_wfds, &e, &timeout) ) { |
---|
[9c7a701] | 290 | /* Merge fd_sets and clear AIM FDs. */ |
---|
| 291 | for(i = 0; i <= max_fd; i++) { |
---|
| 292 | /* Merge all interesting FDs into one set, since we have a |
---|
| 293 | single dispatch per FD. */ |
---|
[2f69081] | 294 | if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) { |
---|
[9c7a701] | 295 | /* AIM HACK: no separate dispatch, just process here if |
---|
| 296 | needed, and only once per run through. */ |
---|
[2f69081] | 297 | if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) { |
---|
[9c7a701] | 298 | owl_process_aim(); |
---|
| 299 | aim_done = 1; |
---|
| 300 | } |
---|
| 301 | else { |
---|
| 302 | FD_SET(i, &r); |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | /* NOTE: the same dispatch function is called for both exceptional |
---|
| 307 | and read ready FDs. */ |
---|
| 308 | owl_select_dispatch(&r, max_fd); |
---|
| 309 | } |
---|
| 310 | } |
---|