/src/PROJ/curl/lib/multi_ev.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include <curl/curl.h> |
28 | | |
29 | | #include "urldata.h" |
30 | | #include "url.h" |
31 | | #include "cfilters.h" |
32 | | #include "curl_trc.h" |
33 | | #include "multiif.h" |
34 | | #include "curlx/timeval.h" |
35 | | #include "multi_ev.h" |
36 | | #include "select.h" |
37 | | #include "uint-bset.h" |
38 | | #include "uint-spbset.h" |
39 | | #include "uint-table.h" |
40 | | #include "curlx/warnless.h" |
41 | | #include "multihandle.h" |
42 | | #include "socks.h" |
43 | | /* The last 3 #include files should be in this order */ |
44 | | #include "curl_printf.h" |
45 | | #include "curl_memory.h" |
46 | | #include "memdebug.h" |
47 | | |
48 | | |
49 | | static void mev_in_callback(struct Curl_multi *multi, bool value) |
50 | 0 | { |
51 | 0 | multi->in_callback = value; |
52 | 0 | } |
53 | | |
54 | | #define CURL_MEV_CONN_HASH_SIZE 3 |
55 | | |
56 | | /* Information about a socket for which we inform the libcurl application |
57 | | * what to supervise (CURL_POLL_IN/CURL_POLL_OUT/CURL_POLL_REMOVE) |
58 | | */ |
59 | | struct mev_sh_entry { |
60 | | struct uint_spbset xfers; /* bitset of transfers `mid`s on this socket */ |
61 | | struct connectdata *conn; /* connection using this socket or NULL */ |
62 | | void *user_data; /* libcurl app data via curl_multi_assign() */ |
63 | | unsigned int action; /* CURL_POLL_IN/CURL_POLL_OUT we last told the |
64 | | * libcurl application to watch out for */ |
65 | | unsigned int readers; /* this many transfers want to read */ |
66 | | unsigned int writers; /* this many transfers want to write */ |
67 | | }; |
68 | | |
69 | | static size_t mev_sh_entry_hash(void *key, size_t key_length, size_t slots_num) |
70 | 0 | { |
71 | 0 | curl_socket_t fd = *((curl_socket_t *) key); |
72 | 0 | (void) key_length; |
73 | 0 | return (fd % (curl_socket_t)slots_num); |
74 | 0 | } |
75 | | |
76 | | static size_t mev_sh_entry_compare(void *k1, size_t k1_len, |
77 | | void *k2, size_t k2_len) |
78 | 0 | { |
79 | 0 | (void) k1_len; (void) k2_len; |
80 | 0 | return (*((curl_socket_t *) k1)) == (*((curl_socket_t *) k2)); |
81 | 0 | } |
82 | | |
83 | | /* sockhash entry destructor callback */ |
84 | | static void mev_sh_entry_dtor(void *freethis) |
85 | 0 | { |
86 | 0 | struct mev_sh_entry *entry = (struct mev_sh_entry *)freethis; |
87 | 0 | Curl_uint_spbset_destroy(&entry->xfers); |
88 | 0 | free(entry); |
89 | 0 | } |
90 | | |
91 | | /* look up a given socket in the socket hash, skip invalid sockets */ |
92 | | static struct mev_sh_entry * |
93 | | mev_sh_entry_get(struct Curl_hash *sh, curl_socket_t s) |
94 | 0 | { |
95 | 0 | if(s != CURL_SOCKET_BAD) { |
96 | | /* only look for proper sockets */ |
97 | 0 | return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t)); |
98 | 0 | } |
99 | 0 | return NULL; |
100 | 0 | } |
101 | | |
102 | | /* make sure this socket is present in the hash for this handle */ |
103 | | static struct mev_sh_entry * |
104 | | mev_sh_entry_add(struct Curl_hash *sh, curl_socket_t s) |
105 | 0 | { |
106 | 0 | struct mev_sh_entry *there = mev_sh_entry_get(sh, s); |
107 | 0 | struct mev_sh_entry *check; |
108 | |
|
109 | 0 | if(there) { |
110 | | /* it is present, return fine */ |
111 | 0 | return there; |
112 | 0 | } |
113 | | |
114 | | /* not present, add it */ |
115 | 0 | check = calloc(1, sizeof(struct mev_sh_entry)); |
116 | 0 | if(!check) |
117 | 0 | return NULL; /* major failure */ |
118 | | |
119 | 0 | Curl_uint_spbset_init(&check->xfers); |
120 | | |
121 | | /* make/add new hash entry */ |
122 | 0 | if(!Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) { |
123 | 0 | mev_sh_entry_dtor(check); |
124 | 0 | return NULL; /* major failure */ |
125 | 0 | } |
126 | | |
127 | 0 | return check; /* things are good in sockhash land */ |
128 | 0 | } |
129 | | |
130 | | /* delete the given socket entry from the hash */ |
131 | | static void mev_sh_entry_kill(struct Curl_multi *multi, curl_socket_t s) |
132 | 0 | { |
133 | 0 | Curl_hash_delete(&multi->ev.sh_entries, (char *)&s, sizeof(curl_socket_t)); |
134 | 0 | } |
135 | | |
136 | | static size_t mev_sh_entry_user_count(struct mev_sh_entry *e) |
137 | 0 | { |
138 | 0 | return Curl_uint_spbset_count(&e->xfers) + (e->conn ? 1 : 0); |
139 | 0 | } |
140 | | |
141 | | static bool mev_sh_entry_xfer_known(struct mev_sh_entry *e, |
142 | | struct Curl_easy *data) |
143 | 0 | { |
144 | 0 | return Curl_uint_spbset_contains(&e->xfers, data->mid); |
145 | 0 | } |
146 | | |
147 | | static bool mev_sh_entry_conn_known(struct mev_sh_entry *e, |
148 | | struct connectdata *conn) |
149 | 0 | { |
150 | 0 | return (e->conn == conn); |
151 | 0 | } |
152 | | |
153 | | static bool mev_sh_entry_xfer_add(struct mev_sh_entry *e, |
154 | | struct Curl_easy *data) |
155 | 0 | { |
156 | | /* detect weird values */ |
157 | 0 | DEBUGASSERT(mev_sh_entry_user_count(e) < 100000); |
158 | 0 | return Curl_uint_spbset_add(&e->xfers, data->mid); |
159 | 0 | } |
160 | | |
161 | | static bool mev_sh_entry_conn_add(struct mev_sh_entry *e, |
162 | | struct connectdata *conn) |
163 | 0 | { |
164 | | /* detect weird values */ |
165 | 0 | DEBUGASSERT(mev_sh_entry_user_count(e) < 100000); |
166 | 0 | DEBUGASSERT(!e->conn); |
167 | 0 | if(e->conn) |
168 | 0 | return FALSE; |
169 | 0 | e->conn = conn; |
170 | 0 | return TRUE; |
171 | 0 | } |
172 | | |
173 | | |
174 | | static bool mev_sh_entry_xfer_remove(struct mev_sh_entry *e, |
175 | | struct Curl_easy *data) |
176 | 0 | { |
177 | 0 | bool present = Curl_uint_spbset_contains(&e->xfers, data->mid); |
178 | 0 | if(present) |
179 | 0 | Curl_uint_spbset_remove(&e->xfers, data->mid); |
180 | 0 | return present; |
181 | 0 | } |
182 | | |
183 | | static bool mev_sh_entry_conn_remove(struct mev_sh_entry *e, |
184 | | struct connectdata *conn) |
185 | 0 | { |
186 | 0 | DEBUGASSERT(e->conn == conn); |
187 | 0 | if(e->conn == conn) { |
188 | 0 | e->conn = NULL; |
189 | 0 | return TRUE; |
190 | 0 | } |
191 | 0 | return FALSE; |
192 | 0 | } |
193 | | |
194 | | /* Purge any information about socket `s`. |
195 | | * Let the socket callback know as well when necessary */ |
196 | | static CURLMcode mev_forget_socket(struct Curl_multi *multi, |
197 | | struct Curl_easy *data, |
198 | | curl_socket_t s, |
199 | | const char *cause) |
200 | 0 | { |
201 | 0 | struct mev_sh_entry *entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
202 | 0 | int rc = 0; |
203 | |
|
204 | 0 | if(!entry) /* we never knew or already forgot about this socket */ |
205 | 0 | return CURLM_OK; |
206 | | |
207 | | /* We managed this socket before, tell the socket callback to forget it. */ |
208 | 0 | if(multi->socket_cb) { |
209 | 0 | CURL_TRC_M(data, "ev %s, call(fd=%" FMT_SOCKET_T ", ev=REMOVE)", |
210 | 0 | cause, s); |
211 | 0 | mev_in_callback(multi, TRUE); |
212 | 0 | rc = multi->socket_cb(data, s, CURL_POLL_REMOVE, |
213 | 0 | multi->socket_userp, entry->user_data); |
214 | 0 | mev_in_callback(multi, FALSE); |
215 | 0 | } |
216 | |
|
217 | 0 | mev_sh_entry_kill(multi, s); |
218 | 0 | if(rc == -1) { |
219 | 0 | multi->dead = TRUE; |
220 | 0 | return CURLM_ABORTED_BY_CALLBACK; |
221 | 0 | } |
222 | 0 | return CURLM_OK; |
223 | 0 | } |
224 | | |
225 | | static CURLMcode mev_sh_entry_update(struct Curl_multi *multi, |
226 | | struct Curl_easy *data, |
227 | | struct mev_sh_entry *entry, |
228 | | curl_socket_t s, |
229 | | unsigned char last_action, |
230 | | unsigned char cur_action) |
231 | 0 | { |
232 | 0 | int rc, comboaction; |
233 | | |
234 | | /* we should only be called when the callback exists */ |
235 | 0 | DEBUGASSERT(multi->socket_cb); |
236 | 0 | if(!multi->socket_cb) |
237 | 0 | return CURLM_OK; |
238 | | |
239 | | /* Transfer `data` goes from `last_action` to `cur_action` on socket `s` |
240 | | * with `multi->ev.sh_entries` entry `entry`. Update `entry` and trigger |
241 | | * `multi->socket_cb` on change, if the callback is set. */ |
242 | 0 | if(last_action == cur_action) /* nothing from `data` changed */ |
243 | 0 | return CURLM_OK; |
244 | | |
245 | 0 | if(last_action & CURL_POLL_IN) { |
246 | 0 | DEBUGASSERT(entry->readers); |
247 | 0 | if(!(cur_action & CURL_POLL_IN)) |
248 | 0 | entry->readers--; |
249 | 0 | } |
250 | 0 | else if(cur_action & CURL_POLL_IN) |
251 | 0 | entry->readers++; |
252 | |
|
253 | 0 | if(last_action & CURL_POLL_OUT) { |
254 | 0 | DEBUGASSERT(entry->writers); |
255 | 0 | if(!(cur_action & CURL_POLL_OUT)) |
256 | 0 | entry->writers--; |
257 | 0 | } |
258 | 0 | else if(cur_action & CURL_POLL_OUT) |
259 | 0 | entry->writers++; |
260 | |
|
261 | 0 | DEBUGASSERT(entry->readers <= mev_sh_entry_user_count(entry)); |
262 | 0 | DEBUGASSERT(entry->writers <= mev_sh_entry_user_count(entry)); |
263 | 0 | DEBUGASSERT(entry->writers + entry->readers); |
264 | |
|
265 | 0 | CURL_TRC_M(data, "ev update fd=%" FMT_SOCKET_T ", action '%s%s' -> '%s%s'" |
266 | 0 | " (%d/%d r/w)", s, |
267 | 0 | (last_action & CURL_POLL_IN) ? "IN" : "", |
268 | 0 | (last_action & CURL_POLL_OUT) ? "OUT" : "", |
269 | 0 | (cur_action & CURL_POLL_IN) ? "IN" : "", |
270 | 0 | (cur_action & CURL_POLL_OUT) ? "OUT" : "", |
271 | 0 | entry->readers, entry->writers); |
272 | |
|
273 | 0 | comboaction = (entry->writers ? CURL_POLL_OUT : 0) | |
274 | 0 | (entry->readers ? CURL_POLL_IN : 0); |
275 | 0 | if(((int)entry->action == comboaction)) /* nothing for socket changed */ |
276 | 0 | return CURLM_OK; |
277 | | |
278 | 0 | CURL_TRC_M(data, "ev update call(fd=%" FMT_SOCKET_T ", ev=%s%s)", |
279 | 0 | s, (comboaction & CURL_POLL_IN) ? "IN" : "", |
280 | 0 | (comboaction & CURL_POLL_OUT) ? "OUT" : ""); |
281 | 0 | mev_in_callback(multi, TRUE); |
282 | 0 | rc = multi->socket_cb(data, s, comboaction, multi->socket_userp, |
283 | 0 | entry->user_data); |
284 | |
|
285 | 0 | mev_in_callback(multi, FALSE); |
286 | 0 | if(rc == -1) { |
287 | 0 | multi->dead = TRUE; |
288 | 0 | return CURLM_ABORTED_BY_CALLBACK; |
289 | 0 | } |
290 | 0 | entry->action = (unsigned int)comboaction; |
291 | 0 | return CURLM_OK; |
292 | 0 | } |
293 | | |
294 | | static CURLMcode mev_pollset_diff(struct Curl_multi *multi, |
295 | | struct Curl_easy *data, |
296 | | struct connectdata *conn, |
297 | | struct easy_pollset *ps, |
298 | | struct easy_pollset *prev_ps) |
299 | 0 | { |
300 | 0 | struct mev_sh_entry *entry; |
301 | 0 | curl_socket_t s; |
302 | 0 | unsigned int i, j; |
303 | 0 | CURLMcode mresult; |
304 | | |
305 | | /* The transfer `data` reports in `ps` the sockets it is interested |
306 | | * in and which combination of CURL_POLL_IN/CURL_POLL_OUT it wants |
307 | | * to have monitored for events. |
308 | | * There can be more than 1 transfer interested in the same socket |
309 | | * and 1 transfer might be interested in more than 1 socket. |
310 | | * `prev_ps` is the pollset copy from the previous call here. On |
311 | | * the 1st call it will be empty. |
312 | | */ |
313 | 0 | DEBUGASSERT(ps); |
314 | 0 | DEBUGASSERT(prev_ps); |
315 | | |
316 | | /* Handle changes to sockets the transfer is interested in. */ |
317 | 0 | for(i = 0; i < ps->num; i++) { |
318 | 0 | unsigned char last_action; |
319 | 0 | bool first_time = FALSE; /* data/conn appears first time on socket */ |
320 | |
|
321 | 0 | s = ps->sockets[i]; |
322 | | /* Have we handled this socket before? */ |
323 | 0 | entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
324 | 0 | if(!entry) { |
325 | | /* new socket, add new entry */ |
326 | 0 | first_time = TRUE; |
327 | 0 | entry = mev_sh_entry_add(&multi->ev.sh_entries, s); |
328 | 0 | if(!entry) /* fatal */ |
329 | 0 | return CURLM_OUT_OF_MEMORY; |
330 | 0 | CURL_TRC_M(data, "ev new entry fd=%" FMT_SOCKET_T, s); |
331 | 0 | } |
332 | 0 | else if(conn) { |
333 | 0 | first_time = !mev_sh_entry_conn_known(entry, conn); |
334 | 0 | } |
335 | 0 | else { |
336 | 0 | first_time = !mev_sh_entry_xfer_known(entry, data); |
337 | 0 | } |
338 | | |
339 | | /* What was the previous action the transfer had regarding this socket? |
340 | | * If the transfer is new to the socket, disregard the information |
341 | | * in `last_poll`, because the socket might have been destroyed and |
342 | | * reopened. We'd have cleared the sh_entry for that, but the socket |
343 | | * might still be mentioned in the hashed pollsets. */ |
344 | 0 | last_action = 0; |
345 | 0 | if(first_time) { |
346 | 0 | if(conn) { |
347 | 0 | if(!mev_sh_entry_conn_add(entry, conn)) |
348 | 0 | return CURLM_OUT_OF_MEMORY; |
349 | 0 | } |
350 | 0 | else { |
351 | 0 | if(!mev_sh_entry_xfer_add(entry, data)) |
352 | 0 | return CURLM_OUT_OF_MEMORY; |
353 | 0 | } |
354 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", added %s #%" FMT_OFF_T |
355 | 0 | ", total=%u/%d (xfer/conn)", s, |
356 | 0 | conn ? "connection" : "transfer", |
357 | 0 | conn ? conn->connection_id : data->mid, |
358 | 0 | Curl_uint_spbset_count(&entry->xfers), |
359 | 0 | entry->conn ? 1 : 0); |
360 | 0 | } |
361 | 0 | else { |
362 | 0 | for(j = 0; j < prev_ps->num; j++) { |
363 | 0 | if(s == prev_ps->sockets[j]) { |
364 | 0 | last_action = prev_ps->actions[j]; |
365 | 0 | break; |
366 | 0 | } |
367 | 0 | } |
368 | 0 | } |
369 | | /* track readers/writers changes and report to socket callback */ |
370 | 0 | mresult = mev_sh_entry_update(multi, data, entry, s, |
371 | 0 | last_action, ps->actions[i]); |
372 | 0 | if(mresult) |
373 | 0 | return mresult; |
374 | 0 | } |
375 | | |
376 | | /* Handle changes to sockets the transfer is NO LONGER interested in. */ |
377 | 0 | for(i = 0; i < prev_ps->num; i++) { |
378 | 0 | bool stillused = FALSE; |
379 | |
|
380 | 0 | s = prev_ps->sockets[i]; |
381 | 0 | for(j = 0; j < ps->num; j++) { |
382 | 0 | if(s == ps->sockets[j]) { |
383 | | /* socket is still supervised */ |
384 | 0 | stillused = TRUE; |
385 | 0 | break; |
386 | 0 | } |
387 | 0 | } |
388 | 0 | if(stillused) |
389 | 0 | continue; |
390 | | |
391 | 0 | entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
392 | | /* if entry does not exist, we were either never told about it or |
393 | | * have already cleaned up this socket via Curl_multi_ev_socket_done(). |
394 | | * In other words: this is perfectly normal */ |
395 | 0 | if(!entry) |
396 | 0 | continue; |
397 | | |
398 | 0 | if(conn && !mev_sh_entry_conn_remove(entry, conn)) { |
399 | | /* `conn` says in `prev_ps` that it had been using a socket, |
400 | | * but `conn` has not been registered for it. |
401 | | * This should not happen if our book-keeping is correct? */ |
402 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", conn lost " |
403 | 0 | "interest but is not registered", s); |
404 | 0 | DEBUGASSERT(NULL); |
405 | 0 | continue; |
406 | 0 | } |
407 | | |
408 | 0 | if(!conn && !mev_sh_entry_xfer_remove(entry, data)) { |
409 | | /* `data` says in `prev_ps` that it had been using a socket, |
410 | | * but `data` has not been registered for it. |
411 | | * This should not happen if our book-keeping is correct? */ |
412 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", transfer lost " |
413 | 0 | "interest but is not registered", s); |
414 | 0 | DEBUGASSERT(NULL); |
415 | 0 | continue; |
416 | 0 | } |
417 | | |
418 | 0 | if(mev_sh_entry_user_count(entry)) { |
419 | | /* track readers/writers changes and report to socket callback */ |
420 | 0 | mresult = mev_sh_entry_update(multi, data, entry, s, |
421 | 0 | prev_ps->actions[i], 0); |
422 | 0 | if(mresult) |
423 | 0 | return mresult; |
424 | 0 | CURL_TRC_M(data, "ev entry fd=%" FMT_SOCKET_T ", removed transfer, " |
425 | 0 | "total=%u/%d (xfer/conn)", s, |
426 | 0 | Curl_uint_spbset_count(&entry->xfers), |
427 | 0 | entry->conn ? 1 : 0); |
428 | 0 | } |
429 | 0 | else { |
430 | 0 | mresult = mev_forget_socket(multi, data, s, "last user gone"); |
431 | 0 | if(mresult) |
432 | 0 | return mresult; |
433 | 0 | } |
434 | 0 | } /* for loop over num */ |
435 | | |
436 | | /* Remember for next time */ |
437 | 0 | memcpy(prev_ps, ps, sizeof(*prev_ps)); |
438 | 0 | return CURLM_OK; |
439 | 0 | } |
440 | | |
441 | | static void mev_pollset_dtor(void *key, size_t klen, void *entry) |
442 | 0 | { |
443 | 0 | (void)key; |
444 | 0 | (void)klen; |
445 | 0 | free(entry); |
446 | 0 | } |
447 | | |
448 | | static struct easy_pollset* |
449 | | mev_add_new_conn_pollset(struct connectdata *conn) |
450 | 0 | { |
451 | 0 | struct easy_pollset *ps; |
452 | |
|
453 | 0 | ps = calloc(1, sizeof(*ps)); |
454 | 0 | if(!ps) |
455 | 0 | return NULL; |
456 | 0 | if(Curl_conn_meta_set(conn, CURL_META_MEV_POLLSET, ps, mev_pollset_dtor)) |
457 | 0 | return NULL; |
458 | 0 | return ps; |
459 | 0 | } |
460 | | |
461 | | static struct easy_pollset* |
462 | | mev_add_new_xfer_pollset(struct Curl_easy *data) |
463 | 0 | { |
464 | 0 | struct easy_pollset *ps; |
465 | |
|
466 | 0 | ps = calloc(1, sizeof(*ps)); |
467 | 0 | if(!ps) |
468 | 0 | return NULL; |
469 | 0 | if(Curl_meta_set(data, CURL_META_MEV_POLLSET, ps, mev_pollset_dtor)) |
470 | 0 | return NULL; |
471 | 0 | return ps; |
472 | 0 | } |
473 | | |
474 | | static struct easy_pollset * |
475 | | mev_get_last_pollset(struct Curl_easy *data, |
476 | | struct connectdata *conn) |
477 | 0 | { |
478 | 0 | if(data) { |
479 | 0 | if(conn) |
480 | 0 | return Curl_conn_meta_get(conn, CURL_META_MEV_POLLSET); |
481 | 0 | return Curl_meta_get(data, CURL_META_MEV_POLLSET); |
482 | 0 | } |
483 | 0 | return NULL; |
484 | 0 | } |
485 | | |
486 | | static void mev_init_cur_pollset(struct easy_pollset *ps, |
487 | | struct Curl_easy *data, |
488 | | struct connectdata *conn) |
489 | 0 | { |
490 | 0 | memset(ps, 0, sizeof(*ps)); |
491 | 0 | if(conn) |
492 | 0 | Curl_conn_adjust_pollset(data, conn, ps); |
493 | 0 | else if(data) |
494 | 0 | Curl_multi_getsock(data, ps, "ev assess"); |
495 | 0 | } |
496 | | |
497 | | static CURLMcode mev_assess(struct Curl_multi *multi, |
498 | | struct Curl_easy *data, |
499 | | struct connectdata *conn) |
500 | 0 | { |
501 | 0 | if(multi && multi->socket_cb) { |
502 | 0 | struct easy_pollset ps, *last_ps; |
503 | |
|
504 | 0 | mev_init_cur_pollset(&ps, data, conn); |
505 | 0 | last_ps = mev_get_last_pollset(data, conn); |
506 | |
|
507 | 0 | if(!last_ps && ps.num) { |
508 | 0 | if(conn) |
509 | 0 | last_ps = mev_add_new_conn_pollset(conn); |
510 | 0 | else |
511 | 0 | last_ps = mev_add_new_xfer_pollset(data); |
512 | 0 | if(!last_ps) |
513 | 0 | return CURLM_OUT_OF_MEMORY; |
514 | 0 | } |
515 | | |
516 | 0 | if(last_ps) |
517 | 0 | return mev_pollset_diff(multi, data, conn, &ps, last_ps); |
518 | 0 | else |
519 | 0 | DEBUGASSERT(!ps.num); |
520 | 0 | } |
521 | 0 | return CURLM_OK; |
522 | 0 | } |
523 | | |
524 | | CURLMcode Curl_multi_ev_assess_xfer(struct Curl_multi *multi, |
525 | | struct Curl_easy *data) |
526 | 0 | { |
527 | 0 | return mev_assess(multi, data, NULL); |
528 | 0 | } |
529 | | |
530 | | CURLMcode Curl_multi_ev_assess_conn(struct Curl_multi *multi, |
531 | | struct Curl_easy *data, |
532 | | struct connectdata *conn) |
533 | 0 | { |
534 | 0 | return mev_assess(multi, data, conn); |
535 | 0 | } |
536 | | |
537 | | CURLMcode Curl_multi_ev_assess_xfer_bset(struct Curl_multi *multi, |
538 | | struct uint_bset *set) |
539 | 0 | { |
540 | 0 | unsigned int mid; |
541 | 0 | CURLMcode result = CURLM_OK; |
542 | |
|
543 | 0 | if(multi && multi->socket_cb && Curl_uint_bset_first(set, &mid)) { |
544 | 0 | do { |
545 | 0 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
546 | 0 | if(data) |
547 | 0 | result = Curl_multi_ev_assess_xfer(multi, data); |
548 | 0 | } |
549 | 0 | while(!result && Curl_uint_bset_next(set, mid, &mid)); |
550 | 0 | } |
551 | 0 | return result; |
552 | 0 | } |
553 | | |
554 | | |
555 | | CURLMcode Curl_multi_ev_assign(struct Curl_multi *multi, |
556 | | curl_socket_t s, |
557 | | void *user_data) |
558 | 0 | { |
559 | 0 | struct mev_sh_entry *e = mev_sh_entry_get(&multi->ev.sh_entries, s); |
560 | 0 | if(!e) |
561 | 0 | return CURLM_BAD_SOCKET; |
562 | 0 | e->user_data = user_data; |
563 | 0 | return CURLM_OK; |
564 | 0 | } |
565 | | |
566 | | void Curl_multi_ev_expire_xfers(struct Curl_multi *multi, |
567 | | curl_socket_t s, |
568 | | const struct curltime *nowp, |
569 | | bool *run_cpool) |
570 | 0 | { |
571 | 0 | struct mev_sh_entry *entry; |
572 | |
|
573 | 0 | DEBUGASSERT(s != CURL_SOCKET_TIMEOUT); |
574 | 0 | entry = mev_sh_entry_get(&multi->ev.sh_entries, s); |
575 | | |
576 | | /* Unmatched socket, we cannot act on it but we ignore this fact. In |
577 | | real-world tests it has been proved that libevent can in fact give |
578 | | the application actions even though the socket was just previously |
579 | | asked to get removed, so thus we better survive stray socket actions |
580 | | and just move on. */ |
581 | 0 | if(entry) { |
582 | 0 | struct Curl_easy *data; |
583 | 0 | unsigned int mid; |
584 | |
|
585 | 0 | if(Curl_uint_spbset_first(&entry->xfers, &mid)) { |
586 | 0 | do { |
587 | 0 | data = Curl_multi_get_easy(multi, mid); |
588 | 0 | if(data) { |
589 | | /* Expire with out current now, so we will get it below when |
590 | | * asking the splaytree for expired transfers. */ |
591 | 0 | Curl_expire_ex(data, nowp, 0, EXPIRE_RUN_NOW); |
592 | 0 | } |
593 | 0 | } |
594 | 0 | while(Curl_uint_spbset_next(&entry->xfers, mid, &mid)); |
595 | 0 | } |
596 | |
|
597 | 0 | if(entry->conn) |
598 | 0 | *run_cpool = TRUE; |
599 | 0 | } |
600 | 0 | } |
601 | | |
602 | | void Curl_multi_ev_socket_done(struct Curl_multi *multi, |
603 | | struct Curl_easy *data, curl_socket_t s) |
604 | 0 | { |
605 | 0 | mev_forget_socket(multi, data, s, "socket done"); |
606 | 0 | } |
607 | | |
608 | | void Curl_multi_ev_xfer_done(struct Curl_multi *multi, |
609 | | struct Curl_easy *data) |
610 | 0 | { |
611 | 0 | DEBUGASSERT(!data->conn); /* transfer should have been detached */ |
612 | 0 | if(data != multi->admin) { |
613 | 0 | (void)mev_assess(multi, data, NULL); |
614 | 0 | Curl_meta_remove(data, CURL_META_MEV_POLLSET); |
615 | 0 | } |
616 | 0 | } |
617 | | |
618 | | void Curl_multi_ev_conn_done(struct Curl_multi *multi, |
619 | | struct Curl_easy *data, |
620 | | struct connectdata *conn) |
621 | 0 | { |
622 | 0 | (void)mev_assess(multi, data, conn); |
623 | 0 | Curl_conn_meta_remove(conn, CURL_META_MEV_POLLSET); |
624 | 0 | } |
625 | | |
626 | | #define CURL_MEV_PS_HASH_SLOTS (991) /* nice prime */ |
627 | | |
628 | | void Curl_multi_ev_init(struct Curl_multi *multi, size_t hashsize) |
629 | 0 | { |
630 | 0 | Curl_hash_init(&multi->ev.sh_entries, hashsize, mev_sh_entry_hash, |
631 | 0 | mev_sh_entry_compare, mev_sh_entry_dtor); |
632 | 0 | } |
633 | | |
634 | | void Curl_multi_ev_cleanup(struct Curl_multi *multi) |
635 | 0 | { |
636 | 0 | Curl_hash_destroy(&multi->ev.sh_entries); |
637 | 0 | } |