[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); |
---|
| 135 | if (d->pfunc) { |
---|
| 136 | owl_perlconfig_dispatch_free(d); |
---|
| 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); |
---|
| 154 | if (d->pfunc == NULL) { |
---|
| 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; |
---|
| 162 | d->cfunc = NULL; |
---|
| 163 | d->pfunc = cb; |
---|
| 164 | owl_select_add_dispatch(d); |
---|
| 165 | return 0; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | int owl_select_remove_perl_dispatch(int fd) |
---|
| 169 | { |
---|
| 170 | int elt; |
---|
| 171 | owl_dispatch *d; |
---|
| 172 | |
---|
| 173 | elt = owl_select_find_dispatch(fd); |
---|
| 174 | if (elt != -1) { |
---|
| 175 | d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); |
---|
| 176 | if (d->pfunc != NULL) { |
---|
| 177 | owl_select_remove_dispatch(fd); |
---|
| 178 | return 0; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | return 1; |
---|
| 182 | } |
---|
| 183 | |
---|
[2f69081] | 184 | int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e) |
---|
[9c7a701] | 185 | { |
---|
| 186 | int i, len, max_fd; |
---|
| 187 | owl_dispatch *d; |
---|
| 188 | owl_list *dl; |
---|
| 189 | |
---|
| 190 | dl = owl_global_get_dispatchlist(&g); |
---|
| 191 | FD_ZERO(r); |
---|
| 192 | FD_ZERO(e); |
---|
| 193 | max_fd = 0; |
---|
| 194 | len = owl_select_dispatch_count(g); |
---|
| 195 | for(i = 0; i < len; i++) { |
---|
| 196 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
| 197 | FD_SET(d->fd, r); |
---|
| 198 | FD_SET(d->fd, e); |
---|
| 199 | if (max_fd < d->fd) max_fd = d->fd; |
---|
| 200 | } |
---|
| 201 | return max_fd + 1; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | void owl_select_dispatch(fd_set *fds, int max_fd) |
---|
| 205 | { |
---|
| 206 | int i, len; |
---|
| 207 | owl_dispatch *d; |
---|
| 208 | owl_list *dl; |
---|
| 209 | |
---|
| 210 | dl = owl_global_get_dispatchlist(&g); |
---|
| 211 | len = owl_select_dispatch_count(); |
---|
| 212 | for(i = 0; i < len; i++) { |
---|
| 213 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
[18a54ee] | 214 | /* While d shouldn't normally be null, the list may be altered by |
---|
| 215 | * functions we dispatch to. */ |
---|
| 216 | if (d != NULL && FD_ISSET(d->fd, fds)) { |
---|
[9c7a701] | 217 | if (d->cfunc != NULL) { |
---|
| 218 | (d->cfunc)(); |
---|
| 219 | } |
---|
| 220 | else if (d->pfunc != NULL) { |
---|
| 221 | owl_perlconfig_do_dispatch(d); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[2f69081] | 227 | int owl_select_aim_hack(fd_set *rfds, fd_set *wfds) |
---|
[9c7a701] | 228 | { |
---|
| 229 | aim_conn_t *cur; |
---|
| 230 | aim_session_t *sess; |
---|
| 231 | int max_fd; |
---|
| 232 | |
---|
[2f69081] | 233 | FD_ZERO(rfds); |
---|
| 234 | FD_ZERO(wfds); |
---|
[9c7a701] | 235 | max_fd = 0; |
---|
| 236 | sess = owl_global_get_aimsess(&g); |
---|
| 237 | for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) { |
---|
| 238 | if (cur->fd != -1) { |
---|
[2f69081] | 239 | FD_SET(cur->fd, rfds); |
---|
| 240 | if (cur->status & AIM_CONN_STATUS_INPROGRESS) { |
---|
| 241 | /* Yes, we're checking writable sockets here. Without it, AIM |
---|
| 242 | login is really slow. */ |
---|
| 243 | FD_SET(cur->fd, wfds); |
---|
| 244 | } |
---|
| 245 | |
---|
[9c7a701] | 246 | if (cur->fd > max_fd) |
---|
| 247 | max_fd = cur->fd; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | return max_fd; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | void owl_select() |
---|
| 254 | { |
---|
| 255 | int i, max_fd, aim_max_fd, aim_done; |
---|
| 256 | fd_set r; |
---|
| 257 | fd_set e; |
---|
[2f69081] | 258 | fd_set aim_rfds, aim_wfds; |
---|
[9c7a701] | 259 | struct timeval timeout; |
---|
| 260 | |
---|
[b7bb454] | 261 | owl_select_process_timers(&timeout); |
---|
[9c7a701] | 262 | |
---|
[2f69081] | 263 | max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e); |
---|
[9c7a701] | 264 | |
---|
| 265 | /* AIM HACK: |
---|
| 266 | * |
---|
| 267 | * The problem - I'm not sure where to hook into the owl/faim |
---|
| 268 | * interface to keep track of when the AIM socket(s) open and |
---|
| 269 | * close. In particular, the bosconn thing throws me off. So, |
---|
| 270 | * rather than register particular dispatchers for AIM, I look up |
---|
| 271 | * the relevant FDs and add them to select's watch lists, then |
---|
| 272 | * check for them individually before moving on to the other |
---|
| 273 | * dispatchers. --asedeno |
---|
| 274 | */ |
---|
| 275 | aim_done = 1; |
---|
[2f69081] | 276 | FD_ZERO(&aim_rfds); |
---|
| 277 | FD_ZERO(&aim_wfds); |
---|
[9c7a701] | 278 | if (owl_global_is_doaimevents(&g)) { |
---|
| 279 | aim_done = 0; |
---|
[2f69081] | 280 | aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds); |
---|
[9c7a701] | 281 | if (max_fd < aim_max_fd) max_fd = aim_max_fd; |
---|
| 282 | for(i = 0; i <= aim_max_fd; i++) { |
---|
[2f69081] | 283 | if (FD_ISSET(i, &aim_rfds)) { |
---|
[9c7a701] | 284 | FD_SET(i, &r); |
---|
| 285 | FD_SET(i, &e); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | /* END AIM HACK */ |
---|
[2f69081] | 290 | |
---|
| 291 | if ( select(max_fd+1, &r, &aim_wfds, &e, &timeout) ) { |
---|
[9c7a701] | 292 | /* Merge fd_sets and clear AIM FDs. */ |
---|
| 293 | for(i = 0; i <= max_fd; i++) { |
---|
| 294 | /* Merge all interesting FDs into one set, since we have a |
---|
| 295 | single dispatch per FD. */ |
---|
[2f69081] | 296 | if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) { |
---|
[9c7a701] | 297 | /* AIM HACK: no separate dispatch, just process here if |
---|
| 298 | needed, and only once per run through. */ |
---|
[2f69081] | 299 | if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) { |
---|
[9c7a701] | 300 | owl_process_aim(); |
---|
| 301 | aim_done = 1; |
---|
| 302 | } |
---|
| 303 | else { |
---|
| 304 | FD_SET(i, &r); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | /* NOTE: the same dispatch function is called for both exceptional |
---|
| 309 | and read ready FDs. */ |
---|
| 310 | owl_select_dispatch(&r, max_fd); |
---|
| 311 | } |
---|
| 312 | } |
---|