1 | #include "owl.h" |
---|
2 | |
---|
3 | static 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. */ |
---|
6 | int 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. */ |
---|
22 | void 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. */ |
---|
46 | void 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 | |
---|
65 | int owl_select_dispatch_count() |
---|
66 | { |
---|
67 | return owl_list_get_size(owl_global_get_dispatchlist(&g)); |
---|
68 | } |
---|
69 | |
---|
70 | int 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 | |
---|
91 | int 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 | |
---|
107 | int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *w, 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, w); |
---|
122 | FD_SET(d->fd, e); |
---|
123 | if (max_fd < d->fd) max_fd = d->fd; |
---|
124 | } |
---|
125 | return max_fd + 1; |
---|
126 | } |
---|
127 | |
---|
128 | void owl_select_dispatch(fd_set *fds, int max_fd) |
---|
129 | { |
---|
130 | int i, len; |
---|
131 | owl_dispatch *d; |
---|
132 | owl_list *dl; |
---|
133 | |
---|
134 | dl = owl_global_get_dispatchlist(&g); |
---|
135 | len = owl_select_dispatch_count(); |
---|
136 | for(i = 0; i < len; i++) { |
---|
137 | d = (owl_dispatch*)owl_list_get_element(dl, i); |
---|
138 | /* While d shouldn't normally be null, the list may be altered by |
---|
139 | * functions we dispatch to. */ |
---|
140 | if (d != NULL && FD_ISSET(d->fd, fds)) { |
---|
141 | if (d->cfunc != NULL) { |
---|
142 | (d->cfunc)(); |
---|
143 | } |
---|
144 | else if (d->pfunc != NULL) { |
---|
145 | owl_perlconfig_do_dispatch(d); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | int owl_select_aim_hack(fd_set *fds) |
---|
152 | { |
---|
153 | aim_conn_t *cur; |
---|
154 | aim_session_t *sess; |
---|
155 | int max_fd; |
---|
156 | |
---|
157 | FD_ZERO(fds); |
---|
158 | max_fd = 0; |
---|
159 | sess = owl_global_get_aimsess(&g); |
---|
160 | for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) { |
---|
161 | if (cur->fd != -1) { |
---|
162 | FD_SET(cur->fd, fds); |
---|
163 | if (cur->fd > max_fd) |
---|
164 | max_fd = cur->fd; |
---|
165 | } |
---|
166 | } |
---|
167 | cur = owl_global_get_bosconn(&g); |
---|
168 | if (cur->fd != -1) { |
---|
169 | FD_SET(cur->fd, fds); |
---|
170 | if (cur->fd > max_fd) |
---|
171 | max_fd = cur->fd; |
---|
172 | } |
---|
173 | |
---|
174 | return max_fd; |
---|
175 | } |
---|
176 | |
---|
177 | void owl_select() |
---|
178 | { |
---|
179 | int i, max_fd, aim_max_fd, aim_done; |
---|
180 | fd_set r; |
---|
181 | fd_set w; |
---|
182 | fd_set e; |
---|
183 | fd_set aim_fds; |
---|
184 | struct timeval timeout; |
---|
185 | |
---|
186 | timeout.tv_sec = 1; |
---|
187 | timeout.tv_usec = 0; |
---|
188 | |
---|
189 | max_fd = owl_select_dispatch_prepare_fd_sets(&r, &w, &e); |
---|
190 | |
---|
191 | /* AIM HACK: |
---|
192 | * |
---|
193 | * The problem - I'm not sure where to hook into the owl/faim |
---|
194 | * interface to keep track of when the AIM socket(s) open and |
---|
195 | * close. In particular, the bosconn thing throws me off. So, |
---|
196 | * rather than register particular dispatchers for AIM, I look up |
---|
197 | * the relevant FDs and add them to select's watch lists, then |
---|
198 | * check for them individually before moving on to the other |
---|
199 | * dispatchers. --asedeno |
---|
200 | */ |
---|
201 | aim_done = 1; |
---|
202 | FD_ZERO(&aim_fds); |
---|
203 | FD_ZERO(&w); |
---|
204 | if (owl_global_is_doaimevents(&g)) { |
---|
205 | aim_done = 0; |
---|
206 | aim_max_fd = owl_select_aim_hack(&aim_fds); |
---|
207 | if (max_fd < aim_max_fd) max_fd = aim_max_fd; |
---|
208 | for(i = 0; i <= aim_max_fd; i++) { |
---|
209 | if (FD_ISSET(i, &aim_fds)) { |
---|
210 | FD_SET(i, &r); |
---|
211 | FD_SET(i, &w); /* Yes, we're checking writable sockets |
---|
212 | here. Without it, AIM login is really |
---|
213 | slow. */ |
---|
214 | FD_SET(i, &e); |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | /* END AIM HACK */ |
---|
219 | |
---|
220 | if ( select(max_fd, &r, &w, &e, &timeout) ) { |
---|
221 | /* Merge fd_sets and clear AIM FDs. */ |
---|
222 | for(i = 0; i <= max_fd; i++) { |
---|
223 | /* Merge all interesting FDs into one set, since we have a |
---|
224 | single dispatch per FD. */ |
---|
225 | if (FD_ISSET(i, &r) || FD_ISSET(i, &w) || FD_ISSET(i, &e)) { |
---|
226 | /* AIM HACK: no separate dispatch, just process here if |
---|
227 | needed, and only once per run through. */ |
---|
228 | if (!aim_done && FD_ISSET(i, &aim_fds)) { |
---|
229 | owl_process_aim(); |
---|
230 | aim_done = 1; |
---|
231 | } |
---|
232 | else { |
---|
233 | FD_SET(i, &r); |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | /* NOTE: the same dispatch function is called for both exceptional |
---|
239 | and read ready FDs. */ |
---|
240 | owl_select_dispatch(&r, max_fd); |
---|
241 | } |
---|
242 | } |
---|