source: select.c @ 18a54ee

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 18a54ee was 18a54ee, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 16 years ago
eliminate a segfault.
  • Property mode set to 100644
File size: 5.8 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
5/* Returns the index of the dispatch for the file descriptor. */
6int owl_select_find_dispatch(int fd)
7{
8  int i, len;
9  owl_list *dl;
10  owl_dispatch *d;
11 
12  dl = owl_global_get_dispatchlist(&g);
13  len = owl_list_get_size(dl);
14  for(i = 0; i < len; i++) {
15    d = (owl_dispatch*)owl_list_get_element(dl, i);
16    if (d->fd == fd) return i;
17  }
18  return -1;
19}
20
21/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
22void owl_select_add_dispatch(owl_dispatch *d)
23{
24  int elt;
25  owl_list *dl;
26
27  elt = owl_select_find_dispatch(d->fd);
28  dl = owl_global_get_dispatchlist(&g);
29 
30  if (elt != -1) {  /* If we have a dispatch for this FD */
31    owl_dispatch *d_old;
32    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
33    /* Ignore if we're adding the same dispatch again.  Otherwise
34       replace the old dispatch. */
35    if (d_old != d) {
36      owl_list_replace_element(dl, elt, d);
37      owl_free(d_old);
38    }
39  }
40  else {
41    owl_list_append_element(dl, d);
42  }
43}
44
45/* Removes an owl_dispatch to the list, based on it's file descriptor. */
46void owl_select_remove_dispatch(int fd)
47{
48  int elt;
49  owl_list *dl;
50
51  elt = owl_select_find_dispatch(fd);
52  dl = owl_global_get_dispatchlist(&g);
53 
54  if (elt != -1) {
55    owl_dispatch *d;
56    d = (owl_dispatch*)owl_list_get_element(dl, elt);
57    owl_list_remove_element(dl, elt);
58    if (d->pfunc) {
59      owl_perlconfig_dispatch_free(d);
60    }
61    owl_free(d);
62  }
63}
64
65int owl_select_dispatch_count()
66{
67  return owl_list_get_size(owl_global_get_dispatchlist(&g));
68}
69
70int owl_select_add_perl_dispatch(int fd, SV *cb)
71{
72  int elt;
73  owl_dispatch *d;
74  elt = owl_select_find_dispatch(fd);
75  if (elt != -1) {
76    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
77    if (d->pfunc == NULL) {
78      /* don't mess with non-perl dispatch functions from here. */
79      return 1;
80    }
81  }
82
83  d = malloc(sizeof(owl_dispatch));
84  d->fd = fd;
85  d->cfunc = NULL;
86  d->pfunc = cb;
87  owl_select_add_dispatch(d);
88  return 0;
89}
90
91int owl_select_remove_perl_dispatch(int fd)
92{
93  int elt;
94  owl_dispatch *d;
95 
96  elt = owl_select_find_dispatch(fd);
97  if (elt != -1) {
98    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
99    if (d->pfunc != NULL) {
100      owl_select_remove_dispatch(fd);
101      return 0;
102    }
103  }
104  return 1;
105}
106
107int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
108{
109  int i, len, max_fd;
110  owl_dispatch *d;
111  owl_list *dl;
112
113  dl = owl_global_get_dispatchlist(&g);
114  FD_ZERO(r);
115  FD_ZERO(e);
116  max_fd = 0;
117  len = owl_select_dispatch_count(g);
118  for(i = 0; i < len; i++) {
119    d = (owl_dispatch*)owl_list_get_element(dl, i);
120    FD_SET(d->fd, r);
121    FD_SET(d->fd, e);
122    if (max_fd < d->fd) max_fd = d->fd;
123  }
124  return max_fd + 1;
125}
126
127void owl_select_dispatch(fd_set *fds, int max_fd)
128{
129  int i, len;
130  owl_dispatch *d;
131  owl_list *dl;
132
133  dl = owl_global_get_dispatchlist(&g);
134  len = owl_select_dispatch_count();
135  for(i = 0; i < len; i++) {
136    d = (owl_dispatch*)owl_list_get_element(dl, i);
137    /* While d shouldn't normally be null, the list may be altered by
138     * functions we dispatch to. */
139    if (d != NULL && FD_ISSET(d->fd, fds)) {
140      if (d->cfunc != NULL) {
141        (d->cfunc)();
142      }
143      else if (d->pfunc != NULL) {
144        owl_perlconfig_do_dispatch(d);
145      }
146    }
147  }
148}
149
150int owl_select_aim_hack(fd_set *fds)
151{
152  aim_conn_t *cur;
153  aim_session_t *sess;
154  int max_fd;
155
156  FD_ZERO(fds);
157  max_fd = 0;
158  sess = owl_global_get_aimsess(&g);
159  for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) {
160    if (cur->fd != -1) {
161      FD_SET(cur->fd, fds);
162      if (cur->fd > max_fd)
163        max_fd = cur->fd;
164    }
165  }
166  cur = owl_global_get_bosconn(&g);
167  if (cur->fd != -1) {
168    FD_SET(cur->fd, fds);
169    if (cur->fd > max_fd)
170      max_fd = cur->fd;
171  }
172 
173  return max_fd;
174}
175
176void owl_select()
177{
178  int i, max_fd, aim_max_fd, aim_done;
179  fd_set r;
180  fd_set w;
181  fd_set e;
182  fd_set aim_fds;
183  struct timeval timeout;
184
185  timeout.tv_sec = 1;
186  timeout.tv_usec = 0;
187
188  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
189
190  /* AIM HACK:
191   *
192   *  The problem - I'm not sure where to hook into the owl/faim
193   *  interface to keep track of when the AIM socket(s) open and
194   *  close. In particular, the bosconn thing throws me off. So,
195   *  rather than register particular dispatchers for AIM, I look up
196   *  the relevant FDs and add them to select's watch lists, then
197   *  check for them individually before moving on to the other
198   *  dispatchers. --asedeno
199   */
200  aim_done = 1;
201  FD_ZERO(&aim_fds);
202  FD_ZERO(&w);
203  if (owl_global_is_doaimevents(&g)) {
204    aim_done = 0;
205    aim_max_fd = owl_select_aim_hack(&aim_fds);
206    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
207    for(i = 0; i <= aim_max_fd; i++) {
208      if (FD_ISSET(i, &aim_fds)) {
209        FD_SET(i, &r);
210        FD_SET(i, &w); /* Yes, we're checking writable sockets
211                          here. Without it, AIM login is really
212                          slow. */
213        FD_SET(i, &e);
214      }
215    }
216  }
217  /* END AIM HACK */
218 
219  if ( select(max_fd, &r, &w, &e, &timeout) ) {
220    /* Merge fd_sets and clear AIM FDs. */
221    for(i = 0; i <= max_fd; i++) {
222      /* Merge all interesting FDs into one set, since we have a
223         single dispatch per FD. */
224      if (FD_ISSET(i, &r) || FD_ISSET(i, &w) || FD_ISSET(i, &e)) {
225        /* AIM HACK: no separate dispatch, just process here if
226           needed, and only once per run through. */
227        if (!aim_done && FD_ISSET(i, &aim_fds)) {
228          owl_process_aim();
229          aim_done = 1;
230        }
231        else {
232          FD_SET(i, &r);
233        }
234      }
235    }
236
237    /* NOTE: the same dispatch function is called for both exceptional
238       and read ready FDs. */
239    owl_select_dispatch(&r, max_fd);
240  }
241}
Note: See TracBrowser for help on using the repository browser.