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