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