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 *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 | |
---|
127 | void 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 | |
---|
150 | int owl_select_aim_hack(fd_set *rfds, fd_set *wfds) |
---|
151 | { |
---|
152 | aim_conn_t *cur; |
---|
153 | aim_session_t *sess; |
---|
154 | int max_fd; |
---|
155 | |
---|
156 | FD_ZERO(rfds); |
---|
157 | FD_ZERO(wfds); |
---|
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, rfds); |
---|
163 | if (cur->status & AIM_CONN_STATUS_INPROGRESS) { |
---|
164 | /* Yes, we're checking writable sockets here. Without it, AIM |
---|
165 | login is really slow. */ |
---|
166 | FD_SET(cur->fd, wfds); |
---|
167 | } |
---|
168 | |
---|
169 | if (cur->fd > max_fd) |
---|
170 | max_fd = cur->fd; |
---|
171 | } |
---|
172 | } |
---|
173 | return max_fd; |
---|
174 | } |
---|
175 | |
---|
176 | void owl_select() |
---|
177 | { |
---|
178 | int i, max_fd, aim_max_fd, aim_done; |
---|
179 | fd_set r; |
---|
180 | fd_set e; |
---|
181 | fd_set aim_rfds, aim_wfds; |
---|
182 | struct timeval timeout; |
---|
183 | |
---|
184 | timeout.tv_sec = 1; |
---|
185 | timeout.tv_usec = 0; |
---|
186 | |
---|
187 | max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e); |
---|
188 | |
---|
189 | /* AIM HACK: |
---|
190 | * |
---|
191 | * The problem - I'm not sure where to hook into the owl/faim |
---|
192 | * interface to keep track of when the AIM socket(s) open and |
---|
193 | * close. In particular, the bosconn thing throws me off. So, |
---|
194 | * rather than register particular dispatchers for AIM, I look up |
---|
195 | * the relevant FDs and add them to select's watch lists, then |
---|
196 | * check for them individually before moving on to the other |
---|
197 | * dispatchers. --asedeno |
---|
198 | */ |
---|
199 | aim_done = 1; |
---|
200 | FD_ZERO(&aim_rfds); |
---|
201 | FD_ZERO(&aim_wfds); |
---|
202 | if (owl_global_is_doaimevents(&g)) { |
---|
203 | aim_done = 0; |
---|
204 | aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds); |
---|
205 | if (max_fd < aim_max_fd) max_fd = aim_max_fd; |
---|
206 | for(i = 0; i <= aim_max_fd; i++) { |
---|
207 | if (FD_ISSET(i, &aim_rfds)) { |
---|
208 | FD_SET(i, &r); |
---|
209 | FD_SET(i, &e); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | /* END AIM HACK */ |
---|
214 | |
---|
215 | if ( select(max_fd+1, &r, &aim_wfds, &e, &timeout) ) { |
---|
216 | /* Merge fd_sets and clear AIM FDs. */ |
---|
217 | for(i = 0; i <= max_fd; i++) { |
---|
218 | /* Merge all interesting FDs into one set, since we have a |
---|
219 | single dispatch per FD. */ |
---|
220 | if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) { |
---|
221 | /* AIM HACK: no separate dispatch, just process here if |
---|
222 | needed, and only once per run through. */ |
---|
223 | if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) { |
---|
224 | owl_process_aim(); |
---|
225 | aim_done = 1; |
---|
226 | } |
---|
227 | else { |
---|
228 | FD_SET(i, &r); |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | /* NOTE: the same dispatch function is called for both exceptional |
---|
233 | and read ready FDs. */ |
---|
234 | owl_select_dispatch(&r, max_fd); |
---|
235 | } |
---|
236 | } |
---|