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